Datasets:
Download train_lora_unsloth.py from emgena/emgena_sentinel_coder_weights_teaser: direct link, hf CLI and curl.
- Browser
- Download file 3.47 kB
-
https://huggingface.co/datasets/emgena/emgena_sentinel_coder_weights_teaser/resolve/main/train_lora_unsloth.py
- Command line
-
hf download hf://datasets/emgena/emgena_sentinel_coder_weights_teaser/train_lora_unsloth.py
-
curl -L -o train_lora_unsloth.py https://huggingface.co/datasets/emgena/emgena_sentinel_coder_weights_teaser/resolve/main/train_lora_unsloth.py
3.47 kB
| #!/usr/bin/env python3 | |
| """ | |
| EMGENA AI LABS v3.0 - UNSLOTH QLoRA 4-BIT FINE-TUNING ENGINE | |
| Works cleanly with local `chatml_format.jsonl` (NVIDIA RTX / A100 / Google Colab). | |
| Usage: | |
| python train_lora_unsloth.py --dry-run # Validate dataset resolution & ChatML records offline | |
| python train_lora_unsloth.py # Run full 4-bit QLoRA training + GGUF export | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parent | |
| def resolve_dataset_file() -> Path: | |
| for name in ("chatml_format.jsonl", "full_training_dataset.jsonl"): | |
| p = ROOT / name | |
| if p.exists() and p.stat().st_size > 0: | |
| return p | |
| for p in sorted(ROOT.glob("*.jsonl")): | |
| if p.stat().st_size > 0: | |
| return p | |
| raise FileNotFoundError("Missing ChatML JSONL file in package directory.") | |
| def dry_run(): | |
| ds_file = resolve_dataset_file() | |
| count = 0 | |
| with ds_file.open("r", encoding="utf-8") as f: | |
| for line in f: | |
| if line.strip(): | |
| obj = json.loads(line) | |
| assert isinstance(obj.get("messages"), list) and len(obj["messages"]) >= 2 | |
| count += 1 | |
| print(f"✅ Unsloth v3.0 Dry-Run Passed: {ds_file.name} ({count} valid ChatML trajectories)") | |
| def main(): | |
| if "--dry-run" in sys.argv: | |
| dry_run() | |
| return | |
| import torch | |
| from datasets import load_dataset | |
| from transformers import TrainingArguments | |
| from trl import SFTTrainer | |
| from unsloth import FastLanguageModel | |
| ds_file = resolve_dataset_file() | |
| max_seq_length = 2048 | |
| model, tokenizer = FastLanguageModel.from_pretrained( | |
| model_name="unsloth/Qwen2.5-Coder-7B-Instruct-bnb-4bit", | |
| max_seq_length=max_seq_length, | |
| dtype=None, | |
| 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", | |
| ) | |
| dataset = load_dataset("json", data_files={"train": str(ds_file)}, split="train") | |
| dataset = dataset.map( | |
| lambda batch: { | |
| "text": [ | |
| tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=False) | |
| for msgs in batch["messages"] | |
| ] | |
| }, | |
| batched=True, | |
| ) | |
| trainer = SFTTrainer( | |
| model=model, | |
| tokenizer=tokenizer, | |
| train_dataset=dataset, | |
| dataset_text_field="text", | |
| max_seq_length=max_seq_length, | |
| dataset_num_proc=2, | |
| packing=False, | |
| args=TrainingArguments( | |
| per_device_train_batch_size=2, | |
| gradient_accumulation_steps=4, | |
| warmup_steps=5, | |
| num_train_epochs=1, | |
| learning_rate=2e-4, | |
| fp16=not torch.cuda.is_bf16_supported(), | |
| bf16=torch.cuda.is_bf16_supported(), | |
| logging_steps=10, | |
| output_dir=str(ROOT / "lora_output"), | |
| ), | |
| ) | |
| trainer.train() | |
| model.save_pretrained_merged(str(ROOT / "checkpoint_merged"), tokenizer, save_method="merged_16bit") | |
| print("🎉 Fine-tuning complete! Export to GGUF via: model.save_pretrained_gguf('gguf_model', tokenizer, quantization_method='q4_k_m')") | |
| if __name__ == "__main__": | |
| main() | |