Upload scripts/train_7b_local.py with huggingface_hub
Browse files- scripts/train_7b_local.py +55 -0
scripts/train_7b_local.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Fine-tune Qwen 2.5 7B Instruct Q4 for command adapter."""
|
| 2 |
+
import json, torch
|
| 3 |
+
from datasets import Dataset
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, BitsAndBytesConfig
|
| 5 |
+
from peft import LoraConfig, get_peft_model
|
| 6 |
+
from trl import SFTTrainer
|
| 7 |
+
|
| 8 |
+
MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
|
| 9 |
+
OUTPUT_DIR = "./adapter-model-7b"
|
| 10 |
+
|
| 11 |
+
print("Loading dataset...")
|
| 12 |
+
examples = []
|
| 13 |
+
with open("dataset_v3.jsonl") as f:
|
| 14 |
+
for line in f:
|
| 15 |
+
d = json.loads(line)
|
| 16 |
+
text = f"<|im_start|>system\nYou are a command adapter. Output ONLY valid JSON. No explanation.<|im_end|>\n<|im_start|>user\n{d['input']}<|im_end|>\n<|im_start|>assistant\n{d['output']}<|im_end|>"
|
| 17 |
+
examples.append({"text": text})
|
| 18 |
+
examples = examples * 4
|
| 19 |
+
dataset = Dataset.from_list(examples)
|
| 20 |
+
print(f"Dataset: {len(examples)} examples")
|
| 21 |
+
|
| 22 |
+
print("Loading model (Q4)...")
|
| 23 |
+
bnb_config = BitsAndBytesConfig(
|
| 24 |
+
load_in_4bit=True,
|
| 25 |
+
bnb_4bit_quant_type="nf4",
|
| 26 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 27 |
+
)
|
| 28 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 29 |
+
if tokenizer.pad_token is None:
|
| 30 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 31 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, quantization_config=bnb_config, device_map="auto", trust_remote_code=True)
|
| 32 |
+
|
| 33 |
+
lora_config = LoraConfig(
|
| 34 |
+
r=32, lora_alpha=64,
|
| 35 |
+
target_modules=["q_proj","v_proj","k_proj","o_proj","gate_proj","up_proj","down_proj"],
|
| 36 |
+
lora_dropout=0.05, bias="none", task_type="CAUSAL_LM",
|
| 37 |
+
)
|
| 38 |
+
model = get_peft_model(model, lora_config)
|
| 39 |
+
model.print_trainable_parameters()
|
| 40 |
+
|
| 41 |
+
print("Training...")
|
| 42 |
+
args = TrainingArguments(
|
| 43 |
+
output_dir=OUTPUT_DIR, num_train_epochs=5,
|
| 44 |
+
per_device_train_batch_size=2, gradient_accumulation_steps=4,
|
| 45 |
+
learning_rate=2e-4, fp16=True, logging_steps=20,
|
| 46 |
+
save_strategy="epoch", warmup_ratio=0.1,
|
| 47 |
+
lr_scheduler_type="cosine", report_to="none",
|
| 48 |
+
)
|
| 49 |
+
trainer = SFTTrainer(model=model, train_dataset=dataset, args=args, processing_class=tokenizer)
|
| 50 |
+
trainer.train()
|
| 51 |
+
|
| 52 |
+
print("Saving...")
|
| 53 |
+
model.save_pretrained(OUTPUT_DIR)
|
| 54 |
+
tokenizer.save_pretrained(OUTPUT_DIR)
|
| 55 |
+
print(f"Done! Saved to {OUTPUT_DIR}")
|