wheattoast11 commited on
Commit
6c09be3
·
verified ·
1 Parent(s): 18c51a5

Upload train_glm_qlora_v6.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_glm_qlora_v6.py +111 -0
train_glm_qlora_v6.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>=0.45.0",
9
+ # "trackio",
10
+ # "datasets",
11
+ # ]
12
+ # ///
13
+
14
+ """
15
+ Agent Zero SFT: zai-org/GLM-4.7-Flash (30B MoE)
16
+ QLoRA (4-bit) — transformers+accelerate both 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
+ )
41
+
42
+ offload_dir = "/tmp/offload"
43
+ os.makedirs(offload_dir, exist_ok=True)
44
+
45
+ print("Loading model in 4-bit...")
46
+ model = AutoModelForCausalLM.from_pretrained(
47
+ "zai-org/GLM-4.7-Flash",
48
+ quantization_config=bnb_config,
49
+ trust_remote_code=True,
50
+ device_map="auto",
51
+ max_memory={0: "21GiB", "cpu": "40GiB"},
52
+ offload_folder=offload_dir,
53
+ torch_dtype=torch.bfloat16,
54
+ )
55
+ tokenizer = AutoTokenizer.from_pretrained("zai-org/GLM-4.7-Flash", trust_remote_code=True)
56
+ print("Model loaded.")
57
+
58
+ if hasattr(model, 'hf_device_map'):
59
+ devices = {}
60
+ for v in model.hf_device_map.values():
61
+ devices[str(v)] = devices.get(str(v), 0) + 1
62
+ print(f"Device distribution: {devices}")
63
+
64
+ config = SFTConfig(
65
+ output_dir="agent-zero-glm-4.7-v1",
66
+ push_to_hub=True,
67
+ hub_model_id="wheattoast11/agent-zero-glm-4.7-v1",
68
+ hub_strategy="every_save",
69
+ hub_private_repo=True,
70
+ num_train_epochs=2,
71
+ per_device_train_batch_size=1,
72
+ gradient_accumulation_steps=16,
73
+ learning_rate=1e-4,
74
+ bf16=True,
75
+ gradient_checkpointing=True,
76
+ logging_steps=10,
77
+ save_strategy="steps",
78
+ save_steps=50,
79
+ save_total_limit=2,
80
+ eval_strategy="steps",
81
+ eval_steps=50,
82
+ warmup_ratio=0.1,
83
+ lr_scheduler_type="cosine",
84
+ report_to="trackio",
85
+ project="agent-zero-finetune",
86
+ run_name="glm-4.7-flash-qlora-v1",
87
+ )
88
+
89
+ peft_config = LoraConfig(
90
+ r=16, lora_alpha=32, lora_dropout=0.05,
91
+ bias="none", task_type="CAUSAL_LM",
92
+ target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
93
+ )
94
+
95
+ print("Initializing trainer...")
96
+ trainer = SFTTrainer(
97
+ model=model,
98
+ tokenizer=tokenizer,
99
+ train_dataset=train_ds,
100
+ eval_dataset=val_ds,
101
+ args=config,
102
+ peft_config=peft_config,
103
+ )
104
+
105
+ print("Starting training...")
106
+ trainer.train()
107
+
108
+ print("Pushing to Hub...")
109
+ trainer.push_to_hub()
110
+ trackio.finish()
111
+ print("Done!")