wheattoast11 commited on
Commit
cf0e6d9
·
verified ·
1 Parent(s): 3520fb7

Upload train_glm_qlora.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_glm_qlora.py +93 -0
train_glm_qlora.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "unsloth[cu124-ampere]",
5
+ # "trackio",
6
+ # "datasets",
7
+ # ]
8
+ # ///
9
+
10
+ """
11
+ Agent Zero SFT: zai-org/GLM-4.7-Flash (30B MoE)
12
+ QLoRA (4-bit) fine-tuning with Unsloth optimizations.
13
+ Router layers frozen - only attention layers trained.
14
+ """
15
+
16
+ import trackio
17
+ from datasets import load_dataset
18
+ from unsloth import FastLanguageModel
19
+
20
+ # Load dataset
21
+ print("Loading dataset...")
22
+ train_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/train.jsonl", split="train")
23
+ val_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/validation.jsonl", split="train")
24
+ print(f"Train: {len(train_ds)}, Val: {len(val_ds)}")
25
+
26
+ # Load model in 4-bit with Unsloth
27
+ print("Loading model with Unsloth (4-bit QLoRA)...")
28
+ model, tokenizer = FastLanguageModel.from_pretrained(
29
+ model_name="zai-org/GLM-4.7-Flash",
30
+ max_seq_length=2048,
31
+ load_in_4bit=True,
32
+ dtype=None, # auto-detect
33
+ )
34
+
35
+ # Apply LoRA adapters via Unsloth
36
+ model = FastLanguageModel.get_peft_model(
37
+ model,
38
+ r=16,
39
+ lora_alpha=32,
40
+ lora_dropout=0.05,
41
+ target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
42
+ bias="none",
43
+ use_gradient_checkpointing="unsloth", # Unsloth optimized
44
+ )
45
+
46
+ from trl import SFTTrainer, SFTConfig
47
+
48
+ config = SFTConfig(
49
+ output_dir="agent-zero-glm-4.7-v1",
50
+ push_to_hub=True,
51
+ hub_model_id="wheattoast11/agent-zero-glm-4.7-v1",
52
+ hub_strategy="every_save",
53
+ hub_private_repo=True,
54
+
55
+ num_train_epochs=2,
56
+ per_device_train_batch_size=1,
57
+ gradient_accumulation_steps=16,
58
+ learning_rate=1e-4,
59
+ bf16=True,
60
+
61
+ logging_steps=10,
62
+ save_strategy="steps",
63
+ save_steps=50,
64
+ save_total_limit=2,
65
+
66
+ eval_strategy="steps",
67
+ eval_steps=50,
68
+
69
+ warmup_ratio=0.1,
70
+ lr_scheduler_type="cosine",
71
+
72
+ report_to="trackio",
73
+ project="agent-zero-finetune",
74
+ run_name="glm-4.7-flash-qlora-v1",
75
+ )
76
+
77
+ print("Initializing trainer...")
78
+ trainer = SFTTrainer(
79
+ model=model,
80
+ tokenizer=tokenizer,
81
+ train_dataset=train_ds,
82
+ eval_dataset=val_ds,
83
+ args=config,
84
+ )
85
+
86
+ print("Starting training...")
87
+ trainer.train()
88
+
89
+ print("Pushing to Hub...")
90
+ trainer.push_to_hub()
91
+
92
+ trackio.finish()
93
+ print("Done! Model at: https://huggingface.co/wheattoast11/agent-zero-glm-4.7-v1")