Download scripts/train_unsloth.py from GlimmaryKarl/DualBlind: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/datasets/GlimmaryKarl/DualBlind/resolve/main/scripts/train_unsloth.py
- Command line
-
hf download hf://datasets/GlimmaryKarl/DualBlind/scripts/train_unsloth.py
-
curl -L -o train_unsloth.py https://huggingface.co/datasets/GlimmaryKarl/DualBlind/resolve/main/scripts/train_unsloth.py
2.25 kB
| #!/usr/bin/env python3 | |
| """ | |
| Free Fine-Tuning Script for DualBlind Arena Reasoning Dataset | |
| Runs on free Google Colab (T4 / L4 GPU) or local Linux/Mac with CUDA/MPS. | |
| """ | |
| import os | |
| from datasets import load_dataset | |
| from unsloth import FastLanguageModel | |
| from trl import SFTTrainer | |
| from transformers import TrainingArguments | |
| DATASET_NAME = "GlimmaryKarl/DualBlind" | |
| BASE_MODEL = "unsloth/Qwen2.5-7B-Instruct" # Or "unsloth/Llama-3.1-8B-Instruct" | |
| print(f"Loading base model: {BASE_MODEL}") | |
| model, tokenizer = FastLanguageModel.from_pretrained( | |
| model_name=BASE_MODEL, | |
| max_seq_length=4096, | |
| load_in_4bit=True, | |
| ) | |
| model = FastLanguageModel.get_peft_model( | |
| model, | |
| r=16, | |
| target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"], | |
| lora_alpha=16, | |
| lora_dropout=0, | |
| bias="none", | |
| use_gradient_checkpointing="unsloth", | |
| ) | |
| print(f"Loading reasoning SFT dataset from {DATASET_NAME}...") | |
| try: | |
| dataset = load_dataset(DATASET_NAME, "sft_reasoning", split="train") | |
| except Exception: | |
| print("Loading from local data/sft_reasoning_train.jsonl...") | |
| dataset = load_dataset("json", data_files="data/sft_reasoning_train.jsonl", split="train") | |
| def format_prompts(batch): | |
| texts = [] | |
| for msgs in batch["messages"]: | |
| texts.append(tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=False)) | |
| return {"text": texts} | |
| dataset = dataset.map(format_prompts, batched=True) | |
| trainer = SFTTrainer( | |
| model=model, | |
| tokenizer=tokenizer, | |
| train_dataset=dataset, | |
| dataset_text_field="text", | |
| max_seq_length=4096, | |
| dataset_num_proc=2, | |
| packing=False, | |
| args=TrainingArguments( | |
| per_device_train_batch_size=2, | |
| gradient_accumulation_steps=4, | |
| warmup_ratio=0.05, | |
| num_train_epochs=3, | |
| learning_rate=2e-4, | |
| fp16=True, | |
| logging_steps=5, | |
| optim="adamw_8bit", | |
| output_dir="reasoning_model_lora", | |
| save_strategy="epoch", | |
| ), | |
| ) | |
| print("Starting training...") | |
| trainer.train() | |
| print("Saving fine-tuned LoRA weights...") | |
| model.save_pretrained_merged("dualblind_reasoning_model", tokenizer, save_method="merged_16bit") | |
| print("Complete! Model saved to ./dualblind_reasoning_model") | |