Upload train_qwen3_hf.py with huggingface_hub
Browse files- train_qwen3_hf.py +56 -0
train_qwen3_hf.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# dependencies = ["trl>=0.12.0", "peft>=0.7.0", "transformers>=4.45.0", "datasets", "accelerate", "torch"]
|
| 3 |
+
# ///
|
| 4 |
+
"""Fine-tune Qwen3-0.6B on CodeForces-CoTS (100 examples)"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 8 |
+
|
| 9 |
+
from datasets import load_dataset
|
| 10 |
+
from peft import LoraConfig
|
| 11 |
+
from trl import SFTTrainer, SFTConfig
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
print(f"CUDA available: {torch.cuda.is_available()}")
|
| 15 |
+
if torch.cuda.is_available():
|
| 16 |
+
print(f"GPU: {torch.cuda.get_device_name(0)}")
|
| 17 |
+
|
| 18 |
+
dataset = load_dataset("open-r1/codeforces-cots", "solutions", split="train").select(range(100))
|
| 19 |
+
print(f"Dataset: {len(dataset)} examples")
|
| 20 |
+
|
| 21 |
+
peft_config = LoraConfig(
|
| 22 |
+
r=16, lora_alpha=32, lora_dropout=0.05,
|
| 23 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
|
| 24 |
+
bias="none", task_type="CAUSAL_LM"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
training_args = SFTConfig(
|
| 28 |
+
output_dir="./qwen3-0.6b-codeforces-cots",
|
| 29 |
+
num_train_epochs=1,
|
| 30 |
+
per_device_train_batch_size=1,
|
| 31 |
+
gradient_accumulation_steps=8,
|
| 32 |
+
learning_rate=2e-4,
|
| 33 |
+
warmup_ratio=0.1,
|
| 34 |
+
logging_steps=5,
|
| 35 |
+
save_strategy="no",
|
| 36 |
+
eval_strategy="no",
|
| 37 |
+
max_length=2048,
|
| 38 |
+
push_to_hub=True,
|
| 39 |
+
hub_model_id="gilbaes/qwen3-0.6b-codeforces-cots",
|
| 40 |
+
report_to="none",
|
| 41 |
+
bf16=True,
|
| 42 |
+
gradient_checkpointing=True,
|
| 43 |
+
optim="adamw_torch_fused",
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
trainer = SFTTrainer(
|
| 47 |
+
model="Qwen/Qwen3-0.6B",
|
| 48 |
+
train_dataset=dataset,
|
| 49 |
+
peft_config=peft_config,
|
| 50 |
+
args=training_args,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
print(f"Trainable params: {trainer.model.num_parameters(only_trainable=True):,}")
|
| 54 |
+
trainer.train()
|
| 55 |
+
trainer.push_to_hub()
|
| 56 |
+
print("Done! Model at gilbaes/qwen3-0.6b-codeforces-cots")
|