feat: QLoRA fine-tuning (int4/int8) + DPO + PPO for small LLMs - #1
Open
feiyuehchen wants to merge 1 commit into
Open
feat: QLoRA fine-tuning (int4/int8) + DPO + PPO for small LLMs#1feiyuehchen wants to merge 1 commit into
feiyuehchen wants to merge 1 commit into
Conversation
Add metalgrad.finetune subpackage for end-to-end LLM training on Apple Silicon. Targets Qwen2.5-0.5B/1.5B, Llama-3.2-1B, and DeepSeek-Coder-1.3B with int4/int8 quantization via QLoRA. New ops: - cross_entropy_per_token: fused Metal kernel, unreduced (N,) output for DPO/PPO per-token log-prob computation - log_sigmoid: numerically stable log(sigmoid(x)) = -softplus(-x) Finetune modules: - lora.py: LoRALinear, apply_lora (QVGO default), save/load adapters - models/: generic CausalLM using metalgrad ops (rms_norm, attention, swiglu, rope_*), config for 4 model families, HF weight loader - memory.py: OOM protection — pre-flight estimation, gradient checkpointing, mx.eval forcing, periodic cache clearing - sft.py: SFT trainer with cosine LR, grad accumulation, adamw_step - dpo.py: DPO trainer with cached ref log-probs, fused CE per-token - ppo.py: PPO trainer with value head, GAE, clipped objective, heuristic reward, rollout buffer with memory cap Also consolidates version to v0.1.0 (pyproject.toml + __init__.py), adds CHANGELOG.md, and updates docs/scope.md to reflect shipped state. All 40+ gradcheck tests pass. SFT/DPO/PPO smoke-tested with synthetic data on Qwen2.5-0.5B int4 (loss converges, memory stable at ~780MB). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
cross_entropy_per_token(fused Metal kernel, unreduced per-row output for DPO/PPO log-prob) andlog_sigmoid(numerically stable,mx.compile-fused)metalgrad.finetunesubpackage: LoRA/QLoRA adapters, model definitions (Qwen2.5, Llama 3.2, DeepSeek-Coder) using metalgrad ops, SFT/DPO/PPO trainers with OOM protectionmx.eval(), dynamic batch truncation, rollout buffer cap, memory logging__version__andpyproject.tomlboth at0.1.0, addedCHANGELOG.md, updateddocs/scope.mdSupported models (int4/int8 QLoRA)
Training pipelines
Test plan
cross_entropy_per_tokenandlog_sigmoid)pip install datasets)🤖 Generated with Claude Code