metal-rl/MetalRL
About
Native Apple Silicon full-parameter RLVR training framework built on MLX and Metal 4
AI & Machine LearningWhat you can do
- Unified Memory First (Colocated & Zero-Copy): RolloutEngine and Learner share the exact same mx.array weight tensors in unified memory with zero serialization and zero copy, running batched group rollouts and releasing KV/recurrent caches cleanly before backward.
- Custom Metal Kernels (mx.fast.metal_kernel): Shape-generic, single-pipeline Metal kernels for unbiased IEEE-754 BF16/FP16 Stochastic Rounding (inline PCG hash RNG, zero temporary random buffers) and single-pass 8-bit blockwise quantization/dequantization powering our hybrid Muon + AdamW optimizer.
- True $O(\text{chunk} \times V)$ Decoupled Head Backward & Gradient Checkpointing: chunked_rlvr_head_backward evaluates LM-head projections one chunk at a time outside the backbone autograd graph (supporting logit softcapping and unpadded vocab slicing), paired with layer-wise mx.checkpoint across deep/looped backbones.
- Zero-VRAM Sparse / PLE Table Offload: MmapShardedEmbedding streams frozen sharded embedding tables (such as Qwen4's ~41 GB N-gram PLE table) directly from safetensors via bit-exact BF16 mmap, consuming 0 GB of GPU wired memory.
Measured star growth
- in 24h
- Collecting history
- in 7 days
- Collecting history
- in 15 days
- Collecting history
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
Original
Native Apple Silicon RLVR Training Framework built on MLX & Metal
English | 简体中文
MetalRL is an open-source RLVR (Reinforcement Learning with Verifiable Rewards) training framework designed natively for Apple Silicon Unified Memory Architecture (UMA).
While most RL frameworks are architected around datacenter GPU topologies with separate rollout/training clusters and heavyweight weight serialization, MetalRL is built UMA-first — enabling full-parameter and mixed-precision post-training on a single desktop Mac and scaling cleanly to Thunderbolt RDMA (JACCL) multi-Mac stacks without rewriting training code.
Key Features
- Unified Memory First (Colocated & Zero-Copy):
RolloutEngineandLearnershare the exact samemx.arrayweight tensors in unified memory with zero serialization and zero copy, running batched group rollouts and releasing KV/recurrent caches cleanly before backward. - Custom Metal Kernels (
mx.fast.metal_kernel): Shape-generic, single-pipeline Metal kernels for unbiased IEEE-754 BF16/FP16 Stochastic Rounding (inline PCG hash RNG, zero temporary random buffers) and single-pass 8-bit blockwise quantization/dequantization powering our hybrid Muon + AdamW optimizer. - True $O(\text{chunk} \times V)$ Decoupled Head Backward & Gradient Checkpointing:
chunked_rlvr_head_backwardevaluates LM-head projections one chunk at a time outside the backbone autograd graph (supporting logit softcapping and unpadded vocab slicing), paired with layer-wisemx.checkpointacross deep/looped backbones. - Zero-VRAM Sparse / PLE Table Offload:
MmapShardedEmbeddingstreams frozen sharded embedding tables (such as Qwen4's ~41 GB N-gram PLE table) directly from safetensors via bit-exact BF16mmap, consuming 0 GB of GPU wired memory. - Production RLVR Loop: First-class GRPO, Dr.GRPO, and DAPO (asymmetric clipping + token-level reduction) with Truncated Importance Sampling (TIS), streaming zero-copy L2
grad_clip_norm,safetensorscheckpoint save/resume (including 8-bit optimizer states), JSONL observability (grad_norm,clip_frac,approx_kl, peak VRAM), and built-in Countdown, Math, and SVG verifiers.
Prompts ──▶ RolloutEngine ──▶ VerifierPool (CPU) ──▶ TrajectoryBuffer ──▶ Learner
▲ │
└────────────── Shared mx.array / JACCL sync ──────────────┘
Supported Flagship Models
MetalRL initially focuses on deep, first-class support for two next-generation sparse MoE architectures on Apple Silicon:
| Model | Architecture | Training Modes |
|---|---|---|
| Inkling-2-Mini | 26.4B Looped MoE (24 physical / 48 looped visits, 128 experts, native NVFP4 + MXFP8) | Native mixed-precision backbone RLVR (~41 GB peak, runs on 48 GB+ Macs) or --dequantize-experts full-parameter 26B RLVR (128 GB+ Macs) |
| Qwen4-35B-A3B | 35B-A3B Hybrid GDN + QSA + 256-expert MoE + PLE | Full-parameter 35B RLVR with 0-VRAM mmap N-gram offload & 8-bit Muon (256 GB Macs) |
Quickstart
Requires macOS on Apple Silicon, Python 3.11–3.13, and uv.
git clone https://github.com/metal-rl/MetalRL.git
cd MetalRL
uv sync --extra dev
uv run metalrl info
uv run pytest
Verified Training Curve (Open-Source Dataset)
Real 24-step DAPO RLVR run of Inkling-2-Mini (26.4B Looped MoE, native NVFP4 + MXFP8) on Jiayi-Pan/Countdown-Tasks-3to4 on a single Apple M5 Ultra Mac Studio (flat 41.3 GB peak VRAM, 0.00045 mean KL, reward climbing from 0.471 in Steps 1–12 to 0.709 in Steps 19–24 while completion length drops from 313 → 237 tokens):
Examples
Inkling-2-Mini RLVR (Countdown / Math)
uv sync --extra svg
uv run python scripts/train_inkling2_mini.py \
--model /path/to/Inkling-2-Mini-nvfp4 \
--task countdown \
--dataset Jiayi-Pan/Countdown-Tasks-3to4 \
--num-problems 128 \
--steps 24 \
--prompts-per-step 2 \
--group-size 4 \
--max-tokens 448 \
--muon-lr 1.5e-5 \
--adamw-lr 3e-6 \
--checkpoint-dir runs/inkling2_countdown \
--checkpoint-every 12 \
--log-jsonl runs/inkling2_countdown/metrics.jsonl
Add --dequantize-experts on 128–256 GB Macs for full-parameter 26B MoE training, or --resume to continue from the latest checkpoint.
Qwen4-35B-A3B Full-Parameter RLVR (Countdown / Math / SVG)
uv run python scripts/train_qwen4_35b.py \
--model /path/to/Qwen4-35B-A3B \
--task countdown \
--steps 10 \
--group-size 4 \
--max-tokens 384 \
--state-bits 8 \
--checkpoint-dir runs/qwen4_countdown \
--checkpoint-every 5 \
--log-jsonl runs/qwen4_countdown/metrics.jsonl
Plot Training Curves
uv run python scripts/plot_metrics.py runs/inkling2_countdown/metrics.jsonl -o runs/inkling2_countdown/curve.svg
Roadmap
- Single-Node UMA: Colocated zero-copy batched rollout/learner, custom Metal kernels (fused BF16/FP16 stochastic rounding & 8-bit block quant/dequant), true $O(\text{chunk} \times V)$ decoupled head backward, gradient checkpointing, checkpoint save/resume, zero-VRAM
mmapembedding offload, Countdown/Math/SVG verifiers, first-class Inkling-2-Mini & Qwen4-35B-A3B support. - Kernels & Rollout Acceleration: Fused Metal Chunked Cross-Entropy kernel, differentiable GDN / banded-mask Metal backward kernels, MXFP8 rollout precision, and sandboxed code execution verifiers.
- Multi-Mac RDMA (
JACCL): Data-parallel gradient aggregation and disaggregated Rollout/Learner roles over Thunderbolt RDMA. - Cluster Scale: Asynchronous Rollout/Learner pools with off-policy correction (TIS / CISPO).
Documentation
See docs/architecture.md for the system design specification and precision/memory model.
License
Apache-2.0 — see LICENSE.
Explore related projects
Loading…