Guided Group Relative Policy Optimization (GRPO) training for MLX on Apple Silicon
Project description
๐ง MLX Guided GRPO
Train reasoning models on your Mac. No cloud needed.
The first production-ready GRPO training framework for Apple Silicon.
Fine-tune LLMs to think step-by-step using your M1/M2/M3/M4 Mac.
Quick Start โข Features โข Why Guided GRPO โข Installation โข Examples โข Docs
๐ฏ Train Your Own Reasoning Model in 5 Minutes
# Install
pip install mlx-guided-grpo
# Train (yes, it's this simple)
mlx-grpo --model mlx-community/Qwen2.5-3B-Instruct-4bit \
--data ./your_data.jsonl \
--train --train-type lora \
--curriculum-enabled
That's it. Your Mac is now training a reasoning model with curriculum learning.
๐ค Why Guided GRPO?
The ProblemTraining reasoning models (like DeepSeek-R1, o1) requires:
Most developers can't train reasoning models. |
The SolutionMLX Guided GRPO gives you:
Train reasoning models on consumer hardware. |
โจ Features
๐ Curriculum LearningGradually reduce scaffolding so models learn to think independently. Start with 100% guidance, end with 0%. |
๐ Two-Phase GenerationAutomatic recovery for incomplete |
๐ฏ Smart Token MaskingOnly train on tokens the model generated. Scaffolded tokens are properly masked from loss. |
โก Apple Silicon NativeBuilt on MLX for maximum Metal GPU utilization. 2-3x faster than PyTorch on Mac. |
๐ง Conditional Gradient ScalingTrain different layers for thinking vs answering. Fine-grained control over what the model learns. |
๐พ Crash RecoveryAutomatic checkpointing and resume. Metal GPU crashes? Training continues. |
Full Feature List
- Training: GRPO, DR-GRPO, BNPO loss variants
- Adapters: LoRA, DoRA, Full fine-tuning
- Memory: Gradient checkpointing, cache management
- Rewards: Hierarchical rewards, custom reward functions
- Logging: WandB integration, rollout logging
- Monitoring: Threshold-based early stopping
๐ Benchmarks
| Model | Hardware | Tokens/sec | Memory |
|---|---|---|---|
| Qwen2.5-3B-4bit | M3 Max 64GB | ~150 | 12GB |
| Qwen2.5-7B-4bit | M3 Max 64GB | ~80 | 24GB |
| Llama-3.2-3B-4bit | M2 Pro 32GB | ~120 | 10GB |
GRPO training with group_size=4, batch_size=2
๐ Installation
From PyPI (Recommended)
pip install mlx-guided-grpo
From Source
git clone https://github.com/adeelahmad/mlx-guided-grpo.git
cd mlx-guided-grpo
pip install -e ".[all]"
Requirements
- macOS 13.5+ with Apple Silicon (M1/M2/M3/M4)
- Python 3.10+
- 16GB+ RAM recommended
๐ Quick Start
1. Prepare Your Data
Create a JSONL file with prompts and reasoning traces:
{"prompt": "What is 15 * 7?", "answer": "<think>\nI need to multiply 15 by 7.\n15 * 7 = 105\n</think>\n\n\\boxed{105}"}
{"prompt": "Solve: 2x + 5 = 13", "answer": "<think>\nSubtract 5 from both sides:\n2x = 8\nDivide by 2:\nx = 4\n</think>\n\n\\boxed{4}"}
2. Train Your Model
mlx-grpo \
--model mlx-community/Qwen2.5-3B-Instruct-4bit \
--data ./math_data.jsonl \
--train \
--train-type lora \
--iters 1000 \
--batch-size 2 \
--group-size 4 \
--curriculum-enabled \
--adapter-path ./my-reasoning-model
3. Use Your Model
from mlx_lm import load, generate
model, tokenizer = load("mlx-community/Qwen2.5-3B-Instruct-4bit",
adapter_path="./my-reasoning-model")
prompt = "What is 23 * 17?"
response = generate(model, tokenizer, prompt=prompt, max_tokens=500)
print(response)
# <think>
# I need to multiply 23 by 17...
# </think>
# \boxed{391}
๐ Examples
Basic GRPO Training
mlx-grpo \
--model mlx-community/Qwen2.5-0.5B-Instruct-4bit \
--data ./data \
--train --train-type lora \
--group-size 4 \
--learning-rate 1e-5
Curriculum Learning (Recommended for Reasoning)
mlx-grpo \
--model mlx-community/Qwen2.5-3B-Instruct-4bit \
--data ./reasoning_data \
--train --train-type lora \
--curriculum-enabled \
--curriculum-start-ratio 1.0 \
--curriculum-end-ratio 0.0 \
--curriculum-warmup-iters 100 \
--curriculum-taper-iters 500 \
--enforce-thinking
With WandB Logging
mlx-grpo \
--model mlx-community/Qwen2.5-3B-Instruct-4bit \
--data ./data \
--train --train-type lora \
--wandb my-experiment \
--log-rollouts \
--log-rollouts-to-wandb
Advanced: Dual-Gradient Mode (CGS)
mlx-grpo \
--model mlx-community/Qwen2.5-7B-Instruct-4bit \
--data ./data \
--train --train-type lora \
--thinking-layers "0-15" \
--answer-layers "16-31" \
--thinking-gradient-weight 0.5 \
--answer-gradient-weight 1.0
๐ง Key Concepts
Curriculum Learning
Progressive scaffolding teaches models to reason independently:
Iteration 0-100: [โโโโโโโโโโโโ] 100% scaffolding (model learns format)
Iteration 100-400: [โโโโโโโโโโโโ] 66% scaffolding (gradual reduction)
Iteration 400-700: [โโโโโโโโโโโโ] 33% scaffolding (increasing independence)
Iteration 700+: [โโโโโโโโโโโโ] 0% scaffolding (full independence)
Smart Token Masking
Only train on what the model actually generated:
[PROMPT] [SCAFFOLD PREFIX] [MODEL GENERATION]
โ โ โ
masked masked LOSS COMPUTED
This prevents the model from getting "free credit" for scaffolded tokens.
Two-Phase Generation
Automatic recovery for incomplete structured outputs:
Phase 1: Model generates โ "<think>Let me solve this... 2+2="
(Incomplete! Missing </think>)
Phase 2: Inject "</think>\n\boxed{" โ Continue generation โ "4}"
(Complete! Injected tokens masked from loss)
๐ Documentation
| Topic | Link |
|---|---|
| Full CLI Reference | docs/cli.md |
| Training Arguments | docs/arguments.md |
| Custom Rewards | docs/rewards.md |
| Architecture | docs/architecture.md |
| API Reference | docs/api.md |
๐ Comparison
| Feature | MLX Guided GRPO | TRL (HuggingFace) | OpenRLHF |
|---|---|---|---|
| Apple Silicon Native | โ | โ | โ |
| Curriculum Learning | โ | โ | โ |
| Scaffold Token Masking | โ | โ | โ |
| Two-Phase Generation | โ | โ | โ |
| Single GPU Training | โ | โ | โ ๏ธ |
| Consumer Hardware | โ | โ ๏ธ | โ |
| One-Command Training | โ | โ | โ |
๐ ๏ธ Troubleshooting
Out of Memory?
# Reduce memory usage
mlx-grpo ... \
--grad-checkpoint \
--batch-size 1 \
--group-size 2 \
--max-completion-length 256
Metal GPU Crash?
Training auto-saves checkpoints. Just resume:
mlx-grpo ... --resume
Slow Training?
# Use quantized model
--model mlx-community/Qwen2.5-3B-Instruct-4bit
# Reduce group size
--group-size 2
๐ค Contributing
Contributions are welcome! See CONTRIBUTING.md for guidelines.
# Setup development environment
git clone https://github.com/adeelahmad/mlx-guided-grpo.git
cd mlx-guided-grpo
pip install -e ".[dev]"
# Run formatting
black mlx_grpo/
isort mlx_grpo/
๐ Citation
If you use MLX Guided GRPO in your research, please cite:
@software{mlx_guided_grpo,
author = {Ahmad, Adeel},
title = {MLX Guided GRPO: Reasoning Model Training for Apple Silicon},
year = {2024},
url = {https://github.com/adeelahmad/mlx-guided-grpo}
}
๐ License
MIT License - see LICENSE for details.
๐ Acknowledgments
- MLX - Apple's ML framework
- mlx-lm - MLX language model utilities
- DeepSeek - GRPO algorithm
- Qwen - Excellent base models
Built with โค๏ธ for the Mac ML community
LinkedIn โข GitHub โข Contact
If this project helps you, please โญ star the repo!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mlx_guided_grpo-2.1.1.tar.gz.
File metadata
- Download URL: mlx_guided_grpo-2.1.1.tar.gz
- Upload date:
- Size: 182.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fc122fdbeffd2c8b6ba7cc0d9a3332a9d779ec2091e61a35bfbd351b1fa7cf8
|
|
| MD5 |
7bf43c3f2950fd851eb54494628085d5
|
|
| BLAKE2b-256 |
59794d9fad5d1571f9fdbea6817d1859a63fa572f62102990111074fcbed6752
|
Provenance
The following attestation bundles were made for mlx_guided_grpo-2.1.1.tar.gz:
Publisher:
publish.yml on adeelahmad/mlx-guided-grpo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mlx_guided_grpo-2.1.1.tar.gz -
Subject digest:
5fc122fdbeffd2c8b6ba7cc0d9a3332a9d779ec2091e61a35bfbd351b1fa7cf8 - Sigstore transparency entry: 919429056
- Sigstore integration time:
-
Permalink:
adeelahmad/mlx-guided-grpo@611bdd85deb01700647e8e1bc4e29398b735e141 -
Branch / Tag:
refs/tags/v2.1.1 - Owner: https://github.com/adeelahmad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@611bdd85deb01700647e8e1bc4e29398b735e141 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mlx_guided_grpo-2.1.1-py3-none-any.whl.
File metadata
- Download URL: mlx_guided_grpo-2.1.1-py3-none-any.whl
- Upload date:
- Size: 201.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba2207e791817cc93c9a46d7d1d7bed279cc3ad35e021d83b4d0893b7954ca36
|
|
| MD5 |
9682db47142267699d9f727a9ba5d2c5
|
|
| BLAKE2b-256 |
1dbb7e76637ba228d46a3f1b399485a421838967b06739dcb0e03f3a0632cfea
|
Provenance
The following attestation bundles were made for mlx_guided_grpo-2.1.1-py3-none-any.whl:
Publisher:
publish.yml on adeelahmad/mlx-guided-grpo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mlx_guided_grpo-2.1.1-py3-none-any.whl -
Subject digest:
ba2207e791817cc93c9a46d7d1d7bed279cc3ad35e021d83b4d0893b7954ca36 - Sigstore transparency entry: 919429060
- Sigstore integration time:
-
Permalink:
adeelahmad/mlx-guided-grpo@611bdd85deb01700647e8e1bc4e29398b735e141 -
Branch / Tag:
refs/tags/v2.1.1 - Owner: https://github.com/adeelahmad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@611bdd85deb01700647e8e1bc4e29398b735e141 -
Trigger Event:
push
-
Statement type: