Upload train2.py with huggingface_hub
Browse files
train2.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, BitsAndBytesConfig
|
| 3 |
+
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
|
| 4 |
+
from trl import SFTTrainer
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
+
import torch
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Login first
|
| 10 |
+
login(token=os.environ.get("HF_TOKEN", ""))
|
| 11 |
+
|
| 12 |
+
dataset = load_dataset("Achilles1089/achilles-web3-training")
|
| 13 |
+
print("Loaded", len(dataset["train"]), "examples")
|
| 14 |
+
|
| 15 |
+
bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16)
|
| 16 |
+
tok = AutoTokenizer.from_pretrained("Qwen/Qwen3-30B-A3B-Instruct-2507", trust_remote_code=True)
|
| 17 |
+
tok.pad_token = tok.eos_token
|
| 18 |
+
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-30B-A3B-Instruct-2507", quantization_config=bnb, device_map="auto", trust_remote_code=True)
|
| 20 |
+
model = prepare_model_for_kbit_training(model)
|
| 21 |
+
model = get_peft_model(model, LoraConfig(r=64, lora_alpha=128, target_modules=["q_proj","k_proj","v_proj","o_proj","gate_proj","up_proj","down_proj"], lora_dropout=0.05, task_type="CAUSAL_LM"))
|
| 22 |
+
|
| 23 |
+
def fmt(ex):
|
| 24 |
+
t=""
|
| 25 |
+
for m in ex.get("messages",[]):
|
| 26 |
+
r,c=m.get("role",""),m.get("content","")
|
| 27 |
+
if r=="system": t+="<|im_start|>system\n"+c+"<|im_end|>\n"
|
| 28 |
+
elif r=="user": t+="<|im_start|>user\n"+c+"<|im_end|>\n"
|
| 29 |
+
elif r=="assistant": t+="<|im_start|>assistant\n"+c+"<|im_end|>\n"
|
| 30 |
+
return {"text":t}
|
| 31 |
+
|
| 32 |
+
trainer = SFTTrainer(model=model, args=TrainingArguments(output_dir="./achilles", num_train_epochs=2, per_device_train_batch_size=2, gradient_accumulation_steps=4, learning_rate=2e-4, bf16=True, gradient_checkpointing=True, logging_steps=10, save_steps=500, report_to="none"), train_dataset=dataset["train"].map(fmt), eval_dataset=dataset["valid"].map(fmt), tokenizer=tok, dataset_text_field="text", max_seq_length=2048, packing=True)
|
| 33 |
+
trainer.train()
|
| 34 |
+
trainer.push_to_hub("Achilles1089/achilles-web3-30b")
|
| 35 |
+
print("DONE!")
|