Upload train_glm_qlora.py with huggingface_hub
Browse files- train_glm_qlora.py +16 -20
train_glm_qlora.py
CHANGED
|
@@ -13,25 +13,21 @@
|
|
| 13 |
|
| 14 |
"""
|
| 15 |
Agent Zero SFT: zai-org/GLM-4.7-Flash (30B MoE)
|
| 16 |
-
QLoRA (4-bit) fine-tuning with bitsandbytes
|
| 17 |
-
No Unsloth — transformers from source for glm4_moe_lite support.
|
| 18 |
-
Router layers frozen - only attention layers trained.
|
| 19 |
"""
|
| 20 |
|
| 21 |
import torch
|
| 22 |
import trackio
|
| 23 |
from datasets import load_dataset
|
| 24 |
from peft import LoraConfig
|
| 25 |
-
from transformers import BitsAndBytesConfig
|
| 26 |
from trl import SFTTrainer, SFTConfig
|
| 27 |
|
| 28 |
-
# Load dataset
|
| 29 |
print("Loading dataset...")
|
| 30 |
train_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/train.jsonl", split="train")
|
| 31 |
val_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/validation.jsonl", split="train")
|
| 32 |
print(f"Train: {len(train_ds)}, Val: {len(val_ds)}")
|
| 33 |
|
| 34 |
-
# 4-bit quantization config
|
| 35 |
bnb_config = BitsAndBytesConfig(
|
| 36 |
load_in_4bit=True,
|
| 37 |
bnb_4bit_quant_type="nf4",
|
|
@@ -39,54 +35,55 @@ bnb_config = BitsAndBytesConfig(
|
|
| 39 |
bnb_4bit_use_double_quant=True,
|
| 40 |
)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
config = SFTConfig(
|
| 43 |
output_dir="agent-zero-glm-4.7-v1",
|
| 44 |
push_to_hub=True,
|
| 45 |
hub_model_id="wheattoast11/agent-zero-glm-4.7-v1",
|
| 46 |
hub_strategy="every_save",
|
| 47 |
hub_private_repo=True,
|
| 48 |
-
|
| 49 |
num_train_epochs=2,
|
| 50 |
per_device_train_batch_size=1,
|
| 51 |
gradient_accumulation_steps=16,
|
| 52 |
learning_rate=1e-4,
|
| 53 |
bf16=True,
|
| 54 |
gradient_checkpointing=True,
|
| 55 |
-
|
| 56 |
logging_steps=10,
|
| 57 |
save_strategy="steps",
|
| 58 |
save_steps=50,
|
| 59 |
save_total_limit=2,
|
| 60 |
-
|
| 61 |
eval_strategy="steps",
|
| 62 |
eval_steps=50,
|
| 63 |
-
|
| 64 |
warmup_ratio=0.1,
|
| 65 |
lr_scheduler_type="cosine",
|
| 66 |
-
|
| 67 |
report_to="trackio",
|
| 68 |
project="agent-zero-finetune",
|
| 69 |
run_name="glm-4.7-flash-qlora-v1",
|
| 70 |
)
|
| 71 |
|
| 72 |
-
# LoRA targeting attention layers only (router layers frozen)
|
| 73 |
peft_config = LoraConfig(
|
| 74 |
-
r=16,
|
| 75 |
-
|
| 76 |
-
lora_dropout=0.05,
|
| 77 |
-
bias="none",
|
| 78 |
-
task_type="CAUSAL_LM",
|
| 79 |
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
|
| 80 |
)
|
| 81 |
|
| 82 |
print("Initializing trainer...")
|
| 83 |
trainer = SFTTrainer(
|
| 84 |
-
model=
|
|
|
|
| 85 |
train_dataset=train_ds,
|
| 86 |
eval_dataset=val_ds,
|
| 87 |
args=config,
|
| 88 |
peft_config=peft_config,
|
| 89 |
-
model_init_kwargs={"quantization_config": bnb_config, "trust_remote_code": True},
|
| 90 |
)
|
| 91 |
|
| 92 |
print("Starting training...")
|
|
@@ -94,6 +91,5 @@ trainer.train()
|
|
| 94 |
|
| 95 |
print("Pushing to Hub...")
|
| 96 |
trainer.push_to_hub()
|
| 97 |
-
|
| 98 |
trackio.finish()
|
| 99 |
print("Done! Model at: https://huggingface.co/wheattoast11/agent-zero-glm-4.7-v1")
|
|
|
|
| 13 |
|
| 14 |
"""
|
| 15 |
Agent Zero SFT: zai-org/GLM-4.7-Flash (30B MoE)
|
| 16 |
+
QLoRA (4-bit) fine-tuning with bitsandbytes.
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
|
| 19 |
import torch
|
| 20 |
import trackio
|
| 21 |
from datasets import load_dataset
|
| 22 |
from peft import LoraConfig
|
| 23 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 24 |
from trl import SFTTrainer, SFTConfig
|
| 25 |
|
|
|
|
| 26 |
print("Loading dataset...")
|
| 27 |
train_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/train.jsonl", split="train")
|
| 28 |
val_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/validation.jsonl", split="train")
|
| 29 |
print(f"Train: {len(train_ds)}, Val: {len(val_ds)}")
|
| 30 |
|
|
|
|
| 31 |
bnb_config = BitsAndBytesConfig(
|
| 32 |
load_in_4bit=True,
|
| 33 |
bnb_4bit_quant_type="nf4",
|
|
|
|
| 35 |
bnb_4bit_use_double_quant=True,
|
| 36 |
)
|
| 37 |
|
| 38 |
+
print("Loading model in 4-bit...")
|
| 39 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 40 |
+
"zai-org/GLM-4.7-Flash",
|
| 41 |
+
quantization_config=bnb_config,
|
| 42 |
+
trust_remote_code=True,
|
| 43 |
+
device_map="auto",
|
| 44 |
+
)
|
| 45 |
+
tokenizer = AutoTokenizer.from_pretrained("zai-org/GLM-4.7-Flash", trust_remote_code=True)
|
| 46 |
+
print("Model loaded.")
|
| 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 |
num_train_epochs=2,
|
| 55 |
per_device_train_batch_size=1,
|
| 56 |
gradient_accumulation_steps=16,
|
| 57 |
learning_rate=1e-4,
|
| 58 |
bf16=True,
|
| 59 |
gradient_checkpointing=True,
|
|
|
|
| 60 |
logging_steps=10,
|
| 61 |
save_strategy="steps",
|
| 62 |
save_steps=50,
|
| 63 |
save_total_limit=2,
|
|
|
|
| 64 |
eval_strategy="steps",
|
| 65 |
eval_steps=50,
|
|
|
|
| 66 |
warmup_ratio=0.1,
|
| 67 |
lr_scheduler_type="cosine",
|
|
|
|
| 68 |
report_to="trackio",
|
| 69 |
project="agent-zero-finetune",
|
| 70 |
run_name="glm-4.7-flash-qlora-v1",
|
| 71 |
)
|
| 72 |
|
|
|
|
| 73 |
peft_config = LoraConfig(
|
| 74 |
+
r=16, lora_alpha=32, lora_dropout=0.05,
|
| 75 |
+
bias="none", task_type="CAUSAL_LM",
|
|
|
|
|
|
|
|
|
|
| 76 |
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
|
| 77 |
)
|
| 78 |
|
| 79 |
print("Initializing trainer...")
|
| 80 |
trainer = SFTTrainer(
|
| 81 |
+
model=model,
|
| 82 |
+
tokenizer=tokenizer,
|
| 83 |
train_dataset=train_ds,
|
| 84 |
eval_dataset=val_ds,
|
| 85 |
args=config,
|
| 86 |
peft_config=peft_config,
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
print("Starting training...")
|
|
|
|
| 91 |
|
| 92 |
print("Pushing to Hub...")
|
| 93 |
trainer.push_to_hub()
|
|
|
|
| 94 |
trackio.finish()
|
| 95 |
print("Done! Model at: https://huggingface.co/wheattoast11/agent-zero-glm-4.7-v1")
|