NanoMoE — Mixture-of-Experts in JAX
A lightweight, educational Mixture-of-Experts (MoE) GPT-style language model built from scratch in JAX / Flax.
Inspired by nanoGPT, NanoMoE replaces the standard FFN in each transformer block with a sparse MoE layer — only top-k experts activate per token, giving increased model capacity with reduced compute per forward pass.
Architecture
Input Tokens
↓
Token Embedding + Positional Embedding
↓
┌─────────────────────────────────────┐
│ Transformer Block ×N │
│ │
│ LayerNorm → Causal Multi-Head Attn │
│ ↓ + Residual │
│ LayerNorm → MoE Layer │
│ ↓ + Residual │
│ │
│ ┌─── MoE Layer ─────────────────┐ │
│ │ Router (Top-K Gating) │ │
│ │ ├─ Expert 1 (FFN) │ │
│ │ ├─ Expert 2 (FFN) │ │
│ │ ├─ ... │ │
│ │ └─ Expert N (FFN) │ │
│ │ → Weighted Sum of Top-K │ │
│ └───────────────────────────────┘ │
└─────────────────────────────────────┘
↓
LayerNorm → Linear Head → Logits
Key Features
- Sparse MoE Routing — Top-K gating with softmax; only a subset of experts runs per token
- Load-Balancing Loss — Switch Transformer-style auxiliary loss for uniform expert utilisation
- Pure JAX/Flax — No custom CUDA kernels; portable across CPU, GPU, and TPU
- Autoregressive Generation — Temperature + top-k sampling for text generation
- Fully JIT-compiled training and evaluation steps
Quick Start
Install
git clone https://github.com/carrycooldude/MoE-JAX.git
cd MoE-JAX
pip install -r requirements.txt
Note: For GPU support, install the appropriate
jaxlibCUDA wheel — see JAX installation.
Train on Tiny Shakespeare
python examples/train_shakespeare.py
This downloads Tiny Shakespeare (~1 MB), trains a character-level NanoMoE, and generates sample text.
Run Tests
python -m pytest tests/ -v
Project Structure
MoE-JAX/
├── nano_moe/
│ ├── __init__.py # Public API
│ ├── config.py # Hyperparameter dataclass
│ ├── layers.py # ExpertFFN, Router, MoELayer, Attention, TransformerBlock
│ ├── model.py # NanoMoE model + generate()
│ ├── train.py # Training loop, JIT-compiled steps
│ └── utils.py # Param counting, batching, data loading
├── examples/
│ └── train_shakespeare.py
├── tests/
│ ├── test_layers.py
│ └── test_model.py
├── requirements.txt
└── README.md
Default Hyperparameters
| Parameter | Value | Description |
|---|---|---|
d_model |
128 | Hidden dimension |
n_layers |
4 | Transformer blocks |
n_heads |
4 | Attention heads |
d_ff |
512 | Expert FFN inner dim |
n_experts |
4 | Experts per MoE layer |
top_k |
2 | Active experts per token |
block_size |
128 | Max context length |
aux_loss_coeff |
0.01 | Load-balancing loss weight |
How It Works
- Router projects each token to
n_expertslogits and selects the top-k experts - Experts are independent 2-layer FFNs (d_model → d_ff → d_model, GELU activation)
- Weighted Sum combines the top-k expert outputs using normalised softmax gates
- Auxiliary Loss penalises uneven routing to prevent expert collapse
License
MIT
Metadata
Release files for nano-moe-jax 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| nano_moe_jax-0.1.0.tar.gz | 13.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nano_moe_jax-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.9 kB
Release files / nano_moe_jax-0.1.0.tar.gz
| Download URL | nano_moe_jax-0.1.0.tar.gz |
|---|---|
| Size | 13.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
75f5044f265b0b144bad0aec56bbf123e357d318c3e885d66f62a86973512274
|
|
BLAKE2b-256 checksum How to use checksums |
17d8c9dc0808d2e839e5cf5cb21654cc7deeb43e2d39241f2bd9cf8ef8e7e817
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 24, 2026.
Transparency logRelease files / nano_moe_jax-0.1.0-py3-none-any.whl
| Download URL | nano_moe_jax-0.1.0-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
77ae7fd51f363b3c8178e03959da02a5d2c7271694738dad0ebf468b1edb2b51
|
|
BLAKE2b-256 checksum How to use checksums |
e72caf191fb26744ab0c54368457c847cdce82ce5716e771cf1a4ed056523033
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 24, 2026.
Transparency log