wheattoast11 commited on
Commit
e10de62
·
verified ·
1 Parent(s): dcf9113

Upload train_glm_qlora_v8.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_glm_qlora_v8.py +112 -0
train_glm_qlora_v8.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "trl>=0.12.0",
5
+ # "peft>=0.7.0",
6
+ # "transformers @ git+https://github.com/huggingface/transformers.git",
7
+ # "accelerate @ git+https://github.com/huggingface/accelerate.git",
8
+ # "bitsandbytes @ git+https://github.com/bitsandbytes-foundation/bitsandbytes.git",
9
+ # "trackio",
10
+ # "datasets",
11
+ # ]
12
+ # ///
13
+
14
+ """
15
+ Agent Zero SFT: zai-org/GLM-4.7-Flash (30B MoE)
16
+ QLoRA (4-bit) with CPU offload — all three libs from source for compat.
17
+ """
18
+
19
+ import os
20
+ import torch
21
+ import trackio
22
+ from huggingface_hub import login
23
+ login(token=os.environ["HF_TOKEN"])
24
+
25
+ from datasets import load_dataset
26
+ from peft import LoraConfig
27
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
28
+ from trl import SFTTrainer, SFTConfig
29
+
30
+ print("Loading dataset...")
31
+ train_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/train.jsonl", split="train")
32
+ val_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/validation.jsonl", split="train")
33
+ print(f"Train: {len(train_ds)}, Val: {len(val_ds)}")
34
+
35
+ bnb_config = BitsAndBytesConfig(
36
+ load_in_4bit=True,
37
+ bnb_4bit_quant_type="nf4",
38
+ bnb_4bit_compute_dtype=torch.bfloat16,
39
+ bnb_4bit_use_double_quant=True,
40
+ llm_int8_enable_fp32_cpu_offload=True,
41
+ )
42
+
43
+ offload_dir = "/tmp/offload"
44
+ os.makedirs(offload_dir, exist_ok=True)
45
+
46
+ print("Loading model in 4-bit with CPU offload...")
47
+ model = AutoModelForCausalLM.from_pretrained(
48
+ "zai-org/GLM-4.7-Flash",
49
+ quantization_config=bnb_config,
50
+ trust_remote_code=True,
51
+ device_map="auto",
52
+ max_memory={0: "21GiB", "cpu": "40GiB"},
53
+ offload_folder=offload_dir,
54
+ torch_dtype=torch.bfloat16,
55
+ )
56
+ tokenizer = AutoTokenizer.from_pretrained("zai-org/GLM-4.7-Flash", trust_remote_code=True)
57
+ print("Model loaded.")
58
+
59
+ if hasattr(model, 'hf_device_map'):
60
+ devices = {}
61
+ for v in model.hf_device_map.values():
62
+ devices[str(v)] = devices.get(str(v), 0) + 1
63
+ print(f"Device distribution: {devices}")
64
+
65
+ config = SFTConfig(
66
+ output_dir="agent-zero-glm-4.7-v1",
67
+ push_to_hub=True,
68
+ hub_model_id="wheattoast11/agent-zero-glm-4.7-v1",
69
+ hub_strategy="every_save",
70
+ hub_private_repo=True,
71
+ num_train_epochs=2,
72
+ per_device_train_batch_size=1,
73
+ gradient_accumulation_steps=16,
74
+ learning_rate=1e-4,
75
+ bf16=True,
76
+ gradient_checkpointing=True,
77
+ logging_steps=10,
78
+ save_strategy="steps",
79
+ save_steps=50,
80
+ save_total_limit=2,
81
+ eval_strategy="steps",
82
+ eval_steps=50,
83
+ warmup_ratio=0.1,
84
+ lr_scheduler_type="cosine",
85
+ report_to="trackio",
86
+ project="agent-zero-finetune",
87
+ run_name="glm-4.7-flash-qlora-v1",
88
+ )
89
+
90
+ peft_config = LoraConfig(
91
+ r=16, lora_alpha=32, lora_dropout=0.05,
92
+ bias="none", task_type="CAUSAL_LM",
93
+ target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
94
+ )
95
+
96
+ print("Initializing trainer...")
97
+ trainer = SFTTrainer(
98
+ model=model,
99
+ tokenizer=tokenizer,
100
+ train_dataset=train_ds,
101
+ eval_dataset=val_ds,
102
+ args=config,
103
+ peft_config=peft_config,
104
+ )
105
+
106
+ print("Starting training...")
107
+ trainer.train()
108
+
109
+ print("Pushing to Hub...")
110
+ trainer.push_to_hub()
111
+ trackio.finish()
112
+ print("Done!")