File size: 2,729 Bytes
69c41e0
907dd77
69c41e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4cfc9b
 
 
 
69c41e0
 
 
 
 
 
 
f4cfc9b
 
 
 
69c41e0
 
 
 
 
 
f4cfc9b
69c41e0
 
 
 
 
f4cfc9b
69c41e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# /// script
# dependencies = ["trl>=0.12.0", "peft>=0.7.0", "trackio", "torch", "transformers"]
# ///

from datasets import load_dataset
from peft import LoraConfig
from trl import SFTTrainer, SFTConfig
import trackio

print("πŸš€ Starting ComfyUI Specialist Training (Production)")
print("=" * 60)

# Load our custom ComfyUI dataset
dataset = load_dataset("lokegud/comfyui-workflows-dataset", split="train")
print(f"πŸ“Š Dataset loaded: {len(dataset)} examples")

# Split for evaluation
dataset_split = dataset.train_test_split(test_size=0.15, seed=42)
train_dataset = dataset_split["train"]
eval_dataset = dataset_split["test"]

print(f"πŸ“ˆ Train: {len(train_dataset)} | Eval: {len(eval_dataset)}")

# LoRA configuration - optimized for 1.5B model
peft_config = LoraConfig(
    r=32,  # Higher rank for better learning
    lora_alpha=64,
    lora_dropout=0.05,
    target_modules=["q_proj", "v_proj", "k_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
    task_type="CAUSAL_LM"
)

# Training configuration - Fixed for 702 examples
# With 702 examples: 597 train, 105 eval
# Steps per epoch: 597 / (2 * 8) = ~37 steps/epoch
# Total steps: 37 * 3 epochs = ~111 steps
training_args = SFTConfig(
    output_dir="comfyui-specialist-v1",
    num_train_epochs=3,
    per_device_train_batch_size=2,
    per_device_eval_batch_size=2,
    gradient_accumulation_steps=8,  # Effective batch size: 16
    learning_rate=2e-4,
    warmup_ratio=0.1,  # Warm up for 10% of training (~11 steps)
    logging_steps=1,  # Log every step
    eval_strategy="epoch",  # Evaluate after each epoch
    save_strategy="epoch",  # Save after each epoch
    save_total_limit=3,
    load_best_model_at_end=True,
    metric_for_best_model="eval_loss",
    greater_is_better=False,
    push_to_hub=True,
    hub_model_id="lokegud/comfyui-specialist-v1",
    hub_strategy="end",  # Only push final model
    hub_private_repo=False,
    report_to="trackio",
    project="comfyui-specialist",
    run_name="production-v1",
    gradient_checkpointing=True,
    bf16=True,  # Faster training with bf16
    max_length=2048,  # Longer context for full workflows
    dataset_text_field="messages",  # Chat format
)

print("πŸ”§ Initializing trainer with Qwen2.5-1.5B-Instruct...")

# Initialize trainer
trainer = SFTTrainer(
    model="Qwen/Qwen2.5-1.5B-Instruct",
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    peft_config=peft_config,
    args=training_args,
)

print("πŸ‹οΈ Training ComfyUI Specialist...")
trainer.train()

print("πŸ“€ Pushing final model to Hub...")
trainer.push_to_hub()

print("βœ… Training complete!")
print(f"πŸ“¦ Model: lokegud/comfyui-specialist-v1")
print(f"πŸ“Š Trackio: https://lokegud-trackio.hf.space/")