Datasets:
Tasks:
Text Generation
Languages:
English
Size:
n<1K
Tags:
code
notebooks
training-scripts
dataset:Nanthasit/sakthai-kaggle-notebooks
license-mit
dataset-card
License:
Upload scripts/train-sakthai-cpu.py with huggingface_hub
Browse files- scripts/train-sakthai-cpu.py +88 -0
scripts/train-sakthai-cpu.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
SakThai CPU training — Qwen2.5-0.5B + LoRA + completion-only loss.
|
| 4 |
+
Auto-detects CPU/GPU. Designed to work on this local machine (no CUDA).
|
| 5 |
+
"""
|
| 6 |
+
import os, torch
|
| 7 |
+
from datasets import load_dataset, concatenate_datasets
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
+
from peft import LoraConfig, get_peft_model
|
| 10 |
+
from trl import SFTConfig, SFTTrainer
|
| 11 |
+
|
| 12 |
+
HF_USER = "Nanthasit"
|
| 13 |
+
BASE_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 14 |
+
MAX_SEQ_LEN = 1024
|
| 15 |
+
LIMIT = 20 # small subset for CPU testing; set to None for full data
|
| 16 |
+
EPOCHS = 1
|
| 17 |
+
|
| 18 |
+
is_cpu = not torch.cuda.is_available()
|
| 19 |
+
print(f"Device: {'CPU' if is_cpu else 'GPU'}")
|
| 20 |
+
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 22 |
+
if tokenizer.pad_token is None:
|
| 23 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 24 |
+
|
| 25 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
+
BASE_MODEL, torch_dtype=torch.float32,
|
| 27 |
+
)
|
| 28 |
+
model.config.use_cache = False
|
| 29 |
+
|
| 30 |
+
lora_config = LoraConfig(
|
| 31 |
+
r=16, lora_alpha=32, lora_dropout=0.05, bias="none", task_type="CAUSAL_LM",
|
| 32 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
|
| 33 |
+
"gate_proj", "up_proj", "down_proj"],
|
| 34 |
+
use_rslora=True,
|
| 35 |
+
)
|
| 36 |
+
model = get_peft_model(model, lora_config)
|
| 37 |
+
model.print_trainable_parameters()
|
| 38 |
+
|
| 39 |
+
def to_text(ex):
|
| 40 |
+
msgs = ex["messages"]
|
| 41 |
+
tools = ex.get("tools") or None
|
| 42 |
+
text = tokenizer.apply_chat_template(
|
| 43 |
+
msgs, tools=tools, tokenize=False, add_generation_prompt=False,
|
| 44 |
+
)
|
| 45 |
+
return {"text": text}
|
| 46 |
+
|
| 47 |
+
main = load_dataset(f"{HF_USER}/sakthai-combined-v7", split="train")
|
| 48 |
+
if LIMIT:
|
| 49 |
+
main = main.select(range(min(LIMIT, len(main))))
|
| 50 |
+
train_data = main.map(to_text, remove_columns=main.column_names)
|
| 51 |
+
try:
|
| 52 |
+
supp = load_dataset(f"{HF_USER}/sakthai-irrelevance-supplement", split="train")
|
| 53 |
+
if LIMIT:
|
| 54 |
+
supp = supp.select(range(min(LIMIT, len(supp))))
|
| 55 |
+
supp_text = supp.map(to_text, remove_columns=supp.column_names)
|
| 56 |
+
train_data = concatenate_datasets([train_data, supp_text])
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print("irrelevance-supplement skipped:", e)
|
| 59 |
+
|
| 60 |
+
eval_raw = load_dataset(f"{HF_USER}/sakthai-combined-v7", split="test")
|
| 61 |
+
if LIMIT:
|
| 62 |
+
eval_raw = eval_raw.select(range(min(max(5, LIMIT // 4), len(eval_raw))))
|
| 63 |
+
eval_data = eval_raw.map(to_text, remove_columns=eval_raw.column_names)
|
| 64 |
+
print(f"train={len(train_data)} eval={len(eval_data)}")
|
| 65 |
+
|
| 66 |
+
args = SFTConfig(
|
| 67 |
+
output_dir="./sakthai-0.5b-cpu",
|
| 68 |
+
num_train_epochs=EPOCHS,
|
| 69 |
+
per_device_train_batch_size=1,
|
| 70 |
+
gradient_accumulation_steps=1,
|
| 71 |
+
learning_rate=2e-4, lr_scheduler_type="cosine", warmup_ratio=0.03,
|
| 72 |
+
logging_steps=1, eval_strategy="steps", eval_steps=5,
|
| 73 |
+
save_strategy="no",
|
| 74 |
+
load_best_model_at_end=False,
|
| 75 |
+
fp16=False, bf16=False,
|
| 76 |
+
report_to="none",
|
| 77 |
+
dataset_text_field="text",
|
| 78 |
+
max_seq_length=MAX_SEQ_LEN,
|
| 79 |
+
completion_only_loss=True,
|
| 80 |
+
)
|
| 81 |
+
trainer = SFTTrainer(
|
| 82 |
+
model=model, processing_class=tokenizer, args=args,
|
| 83 |
+
train_dataset=train_data, eval_dataset=eval_data,
|
| 84 |
+
)
|
| 85 |
+
trainer.train()
|
| 86 |
+
trainer.save_model("./sakthai-0.5b-cpu-final")
|
| 87 |
+
tokenizer.save_pretrained("./sakthai-0.5b-cpu-final")
|
| 88 |
+
print(f"\nDone. Model saved to ./sakthai-0.5b-cpu-final")
|