Upload train_glm_qlora.py with huggingface_hub
Browse files- train_glm_qlora.py +29 -24
train_glm_qlora.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
# /// script
|
| 2 |
# requires-python = ">=3.10"
|
| 3 |
# dependencies = [
|
| 4 |
-
# "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# "trackio",
|
| 6 |
# "datasets",
|
| 7 |
# ]
|
|
@@ -9,13 +13,16 @@
|
|
| 9 |
|
| 10 |
"""
|
| 11 |
Agent Zero SFT: zai-org/GLM-4.7-Flash (30B MoE)
|
| 12 |
-
QLoRA (4-bit) fine-tuning
|
| 13 |
Router layers frozen - only attention layers trained.
|
| 14 |
"""
|
| 15 |
|
|
|
|
| 16 |
import trackio
|
| 17 |
from datasets import load_dataset
|
| 18 |
-
from
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Load dataset
|
| 21 |
print("Loading dataset...")
|
|
@@ -23,28 +30,14 @@ train_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/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 |
-
#
|
| 27 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 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,
|
|
@@ -57,6 +50,7 @@ config = SFTConfig(
|
|
| 57 |
gradient_accumulation_steps=16,
|
| 58 |
learning_rate=1e-4,
|
| 59 |
bf16=True,
|
|
|
|
| 60 |
|
| 61 |
logging_steps=10,
|
| 62 |
save_strategy="steps",
|
|
@@ -74,13 +68,24 @@ config = SFTConfig(
|
|
| 74 |
run_name="glm-4.7-flash-qlora-v1",
|
| 75 |
)
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
print("Initializing trainer...")
|
| 78 |
trainer = SFTTrainer(
|
| 79 |
-
model=
|
| 80 |
-
tokenizer=tokenizer,
|
| 81 |
train_dataset=train_ds,
|
| 82 |
eval_dataset=val_ds,
|
| 83 |
args=config,
|
|
|
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
print("Starting training...")
|
|
|
|
| 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>=0.24.0",
|
| 8 |
+
# "bitsandbytes>=0.41.0",
|
| 9 |
# "trackio",
|
| 10 |
# "datasets",
|
| 11 |
# ]
|
|
|
|
| 13 |
|
| 14 |
"""
|
| 15 |
Agent Zero SFT: zai-org/GLM-4.7-Flash (30B MoE)
|
| 16 |
+
QLoRA (4-bit) fine-tuning on agent-zero-sft-v1 dataset.
|
| 17 |
Router layers frozen - only attention layers trained.
|
| 18 |
"""
|
| 19 |
|
| 20 |
+
import torch
|
| 21 |
import trackio
|
| 22 |
from datasets import load_dataset
|
| 23 |
+
from peft import LoraConfig
|
| 24 |
+
from transformers import BitsAndBytesConfig
|
| 25 |
+
from trl import SFTTrainer, SFTConfig
|
| 26 |
|
| 27 |
# Load dataset
|
| 28 |
print("Loading dataset...")
|
|
|
|
| 30 |
val_ds = load_dataset("wheattoast11/agent-zero-sft-v1", data_files="data/validation.jsonl", split="train")
|
| 31 |
print(f"Train: {len(train_ds)}, Val: {len(val_ds)}")
|
| 32 |
|
| 33 |
+
# 4-bit quantization config
|
| 34 |
+
bnb_config = BitsAndBytesConfig(
|
|
|
|
|
|
|
|
|
|
| 35 |
load_in_4bit=True,
|
| 36 |
+
bnb_4bit_quant_type="nf4",
|
| 37 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 38 |
+
bnb_4bit_use_double_quant=True,
|
| 39 |
)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
config = SFTConfig(
|
| 42 |
output_dir="agent-zero-glm-4.7-v1",
|
| 43 |
push_to_hub=True,
|
|
|
|
| 50 |
gradient_accumulation_steps=16,
|
| 51 |
learning_rate=1e-4,
|
| 52 |
bf16=True,
|
| 53 |
+
gradient_checkpointing=True,
|
| 54 |
|
| 55 |
logging_steps=10,
|
| 56 |
save_strategy="steps",
|
|
|
|
| 68 |
run_name="glm-4.7-flash-qlora-v1",
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# LoRA targeting attention layers only (router layers frozen)
|
| 72 |
+
peft_config = LoraConfig(
|
| 73 |
+
r=16,
|
| 74 |
+
lora_alpha=32,
|
| 75 |
+
lora_dropout=0.05,
|
| 76 |
+
bias="none",
|
| 77 |
+
task_type="CAUSAL_LM",
|
| 78 |
+
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
print("Initializing trainer...")
|
| 82 |
trainer = SFTTrainer(
|
| 83 |
+
model="zai-org/GLM-4.7-Flash",
|
|
|
|
| 84 |
train_dataset=train_ds,
|
| 85 |
eval_dataset=val_ds,
|
| 86 |
args=config,
|
| 87 |
+
peft_config=peft_config,
|
| 88 |
+
model_init_kwargs={"quantization_config": bnb_config},
|
| 89 |
)
|
| 90 |
|
| 91 |
print("Starting training...")
|