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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# HiMoE β Hierarchical Mixture of Experts
|
| 2 |
+
|
| 3 |
+
> *A Matryoshka-inspired two-level routing architecture for efficient large-scale language modelling.*
|
| 4 |
+
|
| 5 |
+
**Author:** AG Β· **Year:** 2026
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## Overview
|
| 10 |
+
|
| 11 |
+
HiMoE replaces the standard feed-forward network (FFN) in each Transformer block with a hierarchical routing system. A **Level-1 router** selects one of N MoE blocks; that block's own **Level-2 router** selects one of M local experts. Only a single expert is ever activated per token β regardless of total model size.
|
| 12 |
+
|
| 13 |
+
```
|
| 14 |
+
Token
|
| 15 |
+
βββΊ Level-1 Router (1 of 6 MoE blocks)
|
| 16 |
+
βββΊ Level-2 Router (1 of 8 experts)
|
| 17 |
+
βββΊ Expert FFN βββΊ output
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
With the default config (N=6, M=8, 2 layers) the model holds **~52M parameters** but activates only **~3.3% per token** β the compute footprint of a ~1.7M dense model.
|
| 21 |
+
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
## Repository Structure
|
| 25 |
+
|
| 26 |
+
```
|
| 27 |
+
.
|
| 28 |
+
βββ train_himoe.py # Full training script (self-contained)
|
| 29 |
+
βββ hamlet.txt # Training corpus (place here before running)
|
| 30 |
+
βββ README.md
|
| 31 |
+
βββ model/ # Created automatically on first save
|
| 32 |
+
βββ config.json # Hyperparameters + vocab snapshot
|
| 33 |
+
βββ backbone.pt # Embeddings, attention, LN, LM head
|
| 34 |
+
βββ main_router.pt # Level-1 gate (or layer_01_main_router.pt for n_layer > 1)
|
| 35 |
+
βββ moe_expert_001/
|
| 36 |
+
β βββ router.pt # Level-2 gate for this MoE block
|
| 37 |
+
β βββ model_001.pt
|
| 38 |
+
β βββ model_002.pt
|
| 39 |
+
β βββ ... (model_008.pt)
|
| 40 |
+
βββ moe_expert_002/
|
| 41 |
+
β βββ ...
|
| 42 |
+
βββ ...
|
| 43 |
+
βββ moe_expert_006/
|
| 44 |
+
βββ sample.txt # Generated text after training
|
| 45 |
+
βββ routing_log.json # Expert attribution for first 50 tokens
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
Each learnable component lives in its own file β making it straightforward to hot-swap, quantise, or fine-tune individual experts without touching the rest of the model.
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## Quickstart
|
| 53 |
+
|
| 54 |
+
### 1. Install dependencies
|
| 55 |
+
|
| 56 |
+
```bash
|
| 57 |
+
pip install torch
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
No other dependencies. Everything else is standard library.
|
| 61 |
+
|
| 62 |
+
### 2. Add your data
|
| 63 |
+
|
| 64 |
+
Place `hamlet.txt` (or any plain-text corpus) in the same directory as `train_himoe.py`.
|
| 65 |
+
|
| 66 |
+
### 3. Train
|
| 67 |
+
|
| 68 |
+
```bash
|
| 69 |
+
python train_himoe.py
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
Checkpoints are saved to `model/` every `eval_interval` steps and at the end of training. A sample generation and routing log are written automatically.
|
| 73 |
+
|
| 74 |
+
### 4. Resume training
|
| 75 |
+
|
| 76 |
+
```bash
|
| 77 |
+
python train_himoe.py --resume
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### 5. Custom config
|
| 81 |
+
|
| 82 |
+
All hyperparameters are overridable from the command line:
|
| 83 |
+
|
| 84 |
+
```bash
|
| 85 |
+
python train_himoe.py \
|
| 86 |
+
--num_moes 8 \
|
| 87 |
+
--num_experts 16 \
|
| 88 |
+
--n_embd 512 \
|
| 89 |
+
--n_layer 4 \
|
| 90 |
+
--max_iters 10000 \
|
| 91 |
+
--lr 2e-4 \
|
| 92 |
+
--data_file my_corpus.txt \
|
| 93 |
+
--model_dir checkpoints/run_01
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
|
| 98 |
+
## Architecture
|
| 99 |
+
|
| 100 |
+
### HiMoEConfig defaults
|
| 101 |
+
|
| 102 |
+
| Parameter | Default | Description |
|
| 103 |
+
|---|---|---|
|
| 104 |
+
| `n_embd` | 256 | Embedding / hidden dimension |
|
| 105 |
+
| `n_layer` | 2 | Number of Transformer layers |
|
| 106 |
+
| `n_head` | 4 | Attention heads |
|
| 107 |
+
| `block_size` | 128 | Context window (tokens) |
|
| 108 |
+
| `num_moes` | 6 | Level-1 choices (MoE blocks) |
|
| 109 |
+
| `num_experts` | 8 | Level-2 choices per MoE block |
|
| 110 |
+
| `dropout` | 0.1 | Dropout rate |
|
| 111 |
+
| `batch_size` | 32 | Training batch size |
|
| 112 |
+
| `max_iters` | 3000 | Training steps |
|
| 113 |
+
| `lr` | 3e-4 | Peak learning rate |
|
| 114 |
+
|
| 115 |
+
### Sparsity
|
| 116 |
+
|
| 117 |
+
| Routing Level | Active | Total | % Active |
|
| 118 |
+
|---|---|---|---|
|
| 119 |
+
| Level-1 (MoE blocks) | 1 | 6 | 16.7% |
|
| 120 |
+
| Level-2 (experts) | 1 | 48 | 2.1% |
|
| 121 |
+
| **Full model (params)** | **~1.7M** | **~52M** | **~3.3%** |
|
| 122 |
+
|
| 123 |
+
### Checkpoint layout for multi-layer models
|
| 124 |
+
|
| 125 |
+
When `n_layer > 1`, routers and expert directories are prefixed by layer:
|
| 126 |
+
|
| 127 |
+
```
|
| 128 |
+
model/
|
| 129 |
+
layer_01_main_router.pt
|
| 130 |
+
layer_01_moe_expert_001/
|
| 131 |
+
layer_01_moe_expert_002/
|
| 132 |
+
...
|
| 133 |
+
layer_02_main_router.pt
|
| 134 |
+
layer_02_moe_expert_001/
|
| 135 |
+
...
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
---
|
| 139 |
+
|
| 140 |
+
## Training Details
|
| 141 |
+
|
| 142 |
+
- **Optimiser:** AdamW with weight decay 0.1 on matrix parameters, 0.0 on biases and norms
|
| 143 |
+
- **LR schedule:** Cosine decay with 100-step linear warmup, minimum LR = 10% of peak
|
| 144 |
+
- **Gradient clipping:** 1.0
|
| 145 |
+
- **Weight tying:** Token embedding matrix and LM head share weights
|
| 146 |
+
- **Routing:** Hard top-1 at both levels (no auxiliary load-balancing loss required)
|
| 147 |
+
|
| 148 |
+
---
|
| 149 |
+
|
| 150 |
+
## Modular Deployment
|
| 151 |
+
|
| 152 |
+
Because every component is a separate file, you can:
|
| 153 |
+
|
| 154 |
+
**Load only what you need:**
|
| 155 |
+
```python
|
| 156 |
+
import torch
|
| 157 |
+
# Load just one expert for inspection or fine-tuning
|
| 158 |
+
expert_weights = torch.load("model/moe_expert_003/model_005.pt")
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
**Swap a router:**
|
| 162 |
+
```python
|
| 163 |
+
torch.save(new_router.state_dict(), "model/moe_expert_003/router.pt")
|
| 164 |
+
```
|
| 165 |
+
|
| 166 |
+
**Fine-tune a single MoE block** without touching the backbone or other experts.
|
| 167 |
+
|
| 168 |
+
**Add a new expert** by saving a new `model_009.pt` and retraining only the corresponding router.
|
| 169 |
+
|
| 170 |
+
---
|
| 171 |
+
|
| 172 |
+
## Output Files
|
| 173 |
+
|
| 174 |
+
After training completes:
|
| 175 |
+
|
| 176 |
+
| File | Contents |
|
| 177 |
+
|---|---|
|
| 178 |
+
| `model/sample.txt` | 400-token generation from a blank context |
|
| 179 |
+
| `model/routing_log.json` | Per-token (MoE, expert) routing decisions for the first 50 generated tokens |
|
| 180 |
+
| `model/config.json` | Full config + vocabulary + last saved step |
|
| 181 |
+
|
| 182 |
+
The training loop also prints an **expert utilisation summary** β a bar chart in the terminal showing how evenly tokens are distributed across MoE blocks and experts.
|
| 183 |
+
|
| 184 |
+
---
|
| 185 |
+
|
| 186 |
+
## Paper
|
| 187 |
+
|
| 188 |
+
A full write-up of the architecture, sparsity analysis, and experiments is included as `himoe_paper.pdf`.
|
| 189 |
+
|
| 190 |
+
---
|
| 191 |
+
|
| 192 |
+
## Citation
|
| 193 |
+
|
| 194 |
+
```
|
| 195 |
+
@misc{himoe2026,
|
| 196 |
+
title = {HiMoE: Hierarchical Mixture of Experts for Efficient Large-Scale Language Modelling},
|
| 197 |
+
author = {AG},
|
| 198 |
+
year = {2026}
|
| 199 |
+
}
|
| 200 |
+
```
|