| """ |
| Two-stage LoRA training: pretrain on synthetic, then fine-tune on the real |
| 688-pair mumble-cleanup dataset. |
| |
| Stage 1: LoRA pretraining on the 50k synthetic corpus at lr=2e-4. |
| Stage 2: Resume the stage-1 adapter, fine-tune on the 688 real pairs at |
| a much lower lr (2e-5) with fewer iters to avoid overwriting the |
| pretraining signal. |
| |
| Usage: |
| python scripts/train_2stage.py |
| """ |
|
|
| import argparse |
| import subprocess |
| import sys |
| from pathlib import Path |
|
|
|
|
| STAGE1_CONFIG = { |
| "data": "data/mlx_dataset", |
| "adapter_path": "data/models/mumble-cleanup-2stage/stage1_adapters", |
| "iters": 2000, |
| "batch_size": 4, |
| "grad_accumulation": 4, |
| "learning_rate": 2e-4, |
| "max_seq_length": 1024, |
| "steps_per_report": 20, |
| "steps_per_eval": 100, |
| "save_every": 500, |
| "lora_layers": 16, |
| "lora_rank": 16, |
| "num_lora_train_tokens": 0, |
| } |
|
|
| STAGE2_CONFIG = { |
| "data": "data/mlx_dataset_688", |
| "adapter_path": "data/models/mumble-cleanup-2stage/stage2_adapters", |
| "fused_path": "data/models/mumble-cleanup-2stage/fused", |
| "iters": 600, |
| "batch_size": 2, |
| "grad_accumulation": 8, |
| "learning_rate": 2e-5, |
| "max_seq_length": 512, |
| "steps_per_report": 10, |
| "steps_per_eval": 50, |
| "save_every": 200, |
| "lora_layers": 16, |
| "lora_rank": 16, |
| } |
|
|
|
|
| def run(cmd: list[str], cwd: Path): |
| print(f"\n$ {' '.join(cmd)}") |
| proc = subprocess.run(cmd, cwd=cwd) |
| if proc.returncode != 0: |
| sys.exit(proc.returncode) |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="2-stage LoRA training") |
| parser.add_argument("--model", type=str, default="Qwen/Qwen2.5-0.5B-Instruct") |
| parser.add_argument("--skip-stage1", action="store_true", |
| help="Skip stage 1 if adapter already exists") |
| args = parser.parse_args() |
|
|
| here = Path(__file__).resolve().parent.parent |
|
|
| |
| stage1_adapter = here / STAGE1_CONFIG["adapter_path"] |
| if args.skip_stage1 and stage1_adapter.exists(): |
| print(f"Skipping stage 1 (--skip-stage1); using existing adapter at {stage1_adapter}") |
| else: |
| print("=" * 60) |
| print("STAGE 1: pretrain on 50k synthetic") |
| print("=" * 60) |
| run([ |
| sys.executable, "-m", "mlx_lm", "lora", |
| "--model", args.model, |
| "--train", |
| "--data", str(here / STAGE1_CONFIG["data"]), |
| "--fine-tune-type", "lora", |
| "--batch-size", str(STAGE1_CONFIG["batch_size"]), |
| "--grad-accumulation-steps", str(STAGE1_CONFIG["grad_accumulation"]), |
| "--iters", str(STAGE1_CONFIG["iters"]), |
| "--learning-rate", str(STAGE1_CONFIG["learning_rate"]), |
| "--steps-per-report", str(STAGE1_CONFIG["steps_per_report"]), |
| "--steps-per-eval", str(STAGE1_CONFIG["steps_per_eval"]), |
| "--save-every", str(STAGE1_CONFIG["save_every"]), |
| "--adapter-path", str(stage1_adapter), |
| "--max-seq-length", str(STAGE1_CONFIG["max_seq_length"]), |
| "--num-layers", str(STAGE1_CONFIG["lora_layers"]), |
| "--seed", "42", |
| "--mask-prompt", |
| ], cwd=here) |
|
|
| |
| print("=" * 60) |
| print("STAGE 2: fine-tune on 688 real pairs") |
| print("=" * 60) |
| stage2_adapter = here / STAGE2_CONFIG["adapter_path"] |
| stage2_adapter.mkdir(parents=True, exist_ok=True) |
| resume_from = stage1_adapter / "adapters.safetensors" |
|
|
| run([ |
| sys.executable, "-m", "mlx_lm", "lora", |
| "--model", args.model, |
| "--train", |
| "--data", str(here / STAGE2_CONFIG["data"]), |
| "--fine-tune-type", "lora", |
| "--batch-size", str(STAGE2_CONFIG["batch_size"]), |
| "--grad-accumulation-steps", str(STAGE2_CONFIG["grad_accumulation"]), |
| "--iters", str(STAGE2_CONFIG["iters"]), |
| "--learning-rate", str(STAGE2_CONFIG["learning_rate"]), |
| "--steps-per-report", str(STAGE2_CONFIG["steps_per_eval"]), |
| "--steps-per-eval", str(STAGE2_CONFIG["steps_per_eval"]), |
| "--save-every", str(STAGE2_CONFIG["save_every"]), |
| "--adapter-path", str(stage2_adapter), |
| "--resume-adapter-file", str(resume_from), |
| "--max-seq-length", str(STAGE2_CONFIG["max_seq_length"]), |
| "--num-layers", str(STAGE2_CONFIG["lora_layers"]), |
| "--seed", "43", |
| "--mask-prompt", |
| ], cwd=here) |
|
|
| |
| fused = here / STAGE2_CONFIG["fused_path"] |
| run([ |
| sys.executable, "-m", "mlx_lm", "fuse", |
| "--model", args.model, |
| "--adapter-path", str(stage2_adapter), |
| "--save-path", str(fused), |
| "--dequantize", |
| ], cwd=here) |
|
|
| print("\n" + "=" * 60) |
| print("2-stage training complete") |
| print("=" * 60) |
| print(f"Stage 1 adapter: {stage1_adapter}") |
| print(f"Stage 2 adapter: {stage2_adapter}") |
| print(f"Fused model: {fused}") |
| print("\nNext steps:") |
| print(" 1. Convert to GGUF:") |
| print(f" python ../EchoFlow/Vendor/llama.cpp/convert_hf_to_gguf.py {fused} \\") |
| print(f" --outfile data/models/mumble-cleanup-2stage-f16.gguf") |
| print(f" ../EchoFlow/Vendor/llama.cpp/build-tools/bin/llama-quantize \\") |
| print(f" data/models/mumble-cleanup-2stage-f16.gguf \\") |
| print(f" data/models/mumble-cleanup-2stage-q4km.gguf Q4_K_M") |
| print(" 2. Evaluate against golden corpus") |
| print(" 3. Update BonsaiSize.swift with new SHA-256") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|