Upload train.py
Browse files
train.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
from trl import SFTConfig, SFTTrainer
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
|
| 5 |
+
from peft import LoraConfig
|
| 6 |
+
import trackio
|
| 7 |
+
|
| 8 |
+
# Configuration
|
| 9 |
+
MODEL_ID = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 10 |
+
DATASET_ID = "iamtarun/code_instructions_120k_alpaca"
|
| 11 |
+
OUTPUT_DIR = "./qwen-coder-multilingual-sft"
|
| 12 |
+
HUB_MODEL_ID = "moos124/qwen-coder-multilingual-sft"
|
| 13 |
+
|
| 14 |
+
def preprocess_function(example):
|
| 15 |
+
# Convert Alpaca format to ChatML format
|
| 16 |
+
# dataset columns: instruction, input, output
|
| 17 |
+
user_content = example["instruction"]
|
| 18 |
+
if example.get("input"):
|
| 19 |
+
user_content += f"\n\nInput: {example['input']}"
|
| 20 |
+
|
| 21 |
+
return {
|
| 22 |
+
"messages": [
|
| 23 |
+
{"role": "user", "content": user_content},
|
| 24 |
+
{"role": "assistant", "content": example["output"]}
|
| 25 |
+
]
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
def main():
|
| 29 |
+
# 1. Load Dataset
|
| 30 |
+
dataset = load_dataset(DATASET_ID, split="train")
|
| 31 |
+
dataset = dataset.map(preprocess_function, remove_columns=dataset.column_names)
|
| 32 |
+
|
| 33 |
+
# 2. Tokenizer & Model
|
| 34 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 35 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 36 |
+
|
| 37 |
+
# 3. PEFT Config (LoRA)
|
| 38 |
+
peft_config = LoraConfig(
|
| 39 |
+
r=16,
|
| 40 |
+
lora_alpha=32,
|
| 41 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 42 |
+
lora_dropout=0.05,
|
| 43 |
+
bias="none",
|
| 44 |
+
task_type="CAUSAL_LM",
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# 4. SFTConfig
|
| 48 |
+
sft_config = SFTConfig(
|
| 49 |
+
output_dir=OUTPUT_DIR,
|
| 50 |
+
max_seq_length=2048,
|
| 51 |
+
dataset_text_field="messages",
|
| 52 |
+
packing=False,
|
| 53 |
+
per_device_train_batch_size=4,
|
| 54 |
+
gradient_accumulation_steps=4,
|
| 55 |
+
learning_rate=2e-4,
|
| 56 |
+
num_train_epochs=1,
|
| 57 |
+
weight_decay=0.01,
|
| 58 |
+
lr_scheduler_type="cosine",
|
| 59 |
+
warmup_steps=100,
|
| 60 |
+
logging_steps=10,
|
| 61 |
+
logging_first_step=True,
|
| 62 |
+
disable_tqdm=True,
|
| 63 |
+
logging_strategy="steps",
|
| 64 |
+
bf16=True,
|
| 65 |
+
gradient_checkpointing=True,
|
| 66 |
+
push_to_hub=True,
|
| 67 |
+
hub_model_id=HUB_MODEL_ID,
|
| 68 |
+
save_strategy="steps",
|
| 69 |
+
save_steps=500,
|
| 70 |
+
report_to="trackio",
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# 5. Trainer
|
| 74 |
+
trainer = SFTTrainer(
|
| 75 |
+
model=MODEL_ID,
|
| 76 |
+
train_dataset=dataset,
|
| 77 |
+
args=sft_config,
|
| 78 |
+
peft_config=peft_config,
|
| 79 |
+
processing_class=tokenizer,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# 6. Train
|
| 83 |
+
trainer.train()
|
| 84 |
+
|
| 85 |
+
# 7. Save & Push
|
| 86 |
+
trainer.save_model(OUTPUT_DIR)
|
| 87 |
+
trainer.push_to_hub()
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
main()
|