""" MLX-LM LoRA training wrapper for the Echo Flow transcript-cleanup model. Wraps `mlx_lm.lora` with sensible defaults for an M-series Mac and the Qwen2.5-0.5B base model. After training, fuses the adapter into a deployable model directory. Usage: # Smoke test python scripts/train_mlx.py --smoke # Full run on the synthetic corpus python scripts/train_mlx.py --full """ import argparse import os import shutil import subprocess import sys from pathlib import Path SMOKE_CONFIG = { "data": "data/mlx_dataset_smoke", "adapter_path": "data/models/mlx-smoke/adapters", "fused_path": "data/models/mlx-smoke/fused", "iters": 30, "batch_size": 1, "grad_accumulation": 4, "learning_rate": 2e-4, "max_seq_length": 1024, "steps_per_report": 5, "steps_per_eval": 10, "save_every": 50, "lora_layers": 8, "lora_rank": 8, } FULL_CONFIG = { "data": "data/mlx_dataset", "adapter_path": "data/models/mumble-cleanup-v2-mlx/adapters", "fused_path": "data/models/mumble-cleanup-v2-mlx/fused", "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": 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="MLX-LM training wrapper") parser.add_argument("--smoke", action="store_true", help="Use smoke-test config") parser.add_argument("--full", action="store_true", help="Use full training config") parser.add_argument("--model", type=str, default="Qwen/Qwen2.5-0.5B-Instruct") parser.add_argument("--resume-adapter-file", type=str, default=None) args = parser.parse_args() if not args.smoke and not args.full: print("Choose --smoke or --full") sys.exit(1) cfg = SMOKE_CONFIG if args.smoke else FULL_CONFIG here = Path(__file__).resolve().parent.parent data_dir = here / cfg["data"] adapter_path = here / cfg["adapter_path"] fused_path = here / cfg["fused_path"] if not data_dir.exists(): print(f"Data dir not found: {data_dir}") print("Run scripts/prepare_mlx_data.py first.") sys.exit(1) adapter_path.mkdir(parents=True, exist_ok=True) lora_cmd = [ sys.executable, "-m", "mlx_lm", "lora", "--model", args.model, "--train", "--data", str(data_dir), "--fine-tune-type", "lora", "--batch-size", str(cfg["batch_size"]), "--grad-accumulation-steps", str(cfg["grad_accumulation"]), "--iters", str(cfg["iters"]), "--learning-rate", str(cfg["learning_rate"]), "--steps-per-report", str(cfg["steps_per_report"]), "--steps-per-eval", str(cfg["steps_per_eval"]), "--save-every", str(cfg["save_every"]), "--adapter-path", str(adapter_path), "--max-seq-length", str(cfg["max_seq_length"]), "--num-layers", str(cfg["lora_layers"]), "--seed", "42", "--mask-prompt", ] if args.resume_adapter_file: lora_cmd.extend(["--resume-adapter-file", args.resume_adapter_file]) run(lora_cmd, cwd=here) fuse_cmd = [ sys.executable, "-m", "mlx_lm", "fuse", "--model", args.model, "--adapter-path", str(adapter_path), "--save-path", str(fused_path), "--dequantize", ] run(fuse_cmd, cwd=here) print("\n=== Training complete ===") print(f"Adapter: {adapter_path}") print(f"Fused: {fused_path}") print("\nNext steps:") print(f" python scripts/convert_to_gguf.py --model {fused_path} \\") print(f" --outfile data/models/mumble-cleanup-v2-q4km.gguf --quant Q4_K_M") if __name__ == "__main__": main()