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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Spider-FLEXITOKENS FP8 Training
|
| 2 |
+
|
| 3 |
+
FP8 training pipeline for [Spider-FLEXITOKENS](https://huggingface.co/CLIWorks/Spider-FLEXITOKENS) on NVIDIA Blackwell GPUs (sm_120) using **torchao Float8Linear** and optional **TileKernels** fused MoE routing.
|
| 4 |
+
|
| 5 |
+
## Architecture
|
| 6 |
+
|
| 7 |
+
Spider is a Recurrent-Depth Transformer (RDT) with:
|
| 8 |
+
- **1B parameters** (996M), hidden_size=2048
|
| 9 |
+
- **Byte-level vocab**: 272 tokens (256 UTF-8 bytes + 16 specials: BOS=257, EOS=258, PAD=256)
|
| 10 |
+
- **6 recurrent layers** with MoE (32 experts, top-2 routing) + MLA attention
|
| 11 |
+
- **2 prelude + 2 coda** dense layers
|
| 12 |
+
- **Engram conditional memory** at recurrent layers 1 and 4
|
| 13 |
+
- **FlexiTokens** via BoundaryPredictor + downsample/upsample
|
| 14 |
+
- **ACT halting** with LTI injection + LoRA adapter per loop iteration
|
| 15 |
+
- Gradient checkpointing on recurrent layers
|
| 16 |
+
|
| 17 |
+
## Training Scripts
|
| 18 |
+
|
| 19 |
+
| File | Description |
|
| 20 |
+
|------|-------------|
|
| 21 |
+
| `fp8_train.py` | Pure torchao FP8 training (Python MoE loop) |
|
| 22 |
+
| `tk-train.py` | torchao FP8 + TileKernels fused MoE routing |
|
| 23 |
+
| `fp8-ready-spider.py` | Model definition (Python MoE loop) — used by `fp8_train.py` |
|
| 24 |
+
| `tk-spider.py` | Model definition (TileKernels MoE) — used by `tk-train.py` |
|
| 25 |
+
|
| 26 |
+
### fp8_train.py
|
| 27 |
+
|
| 28 |
+
Standard torchao FP8 training. Uses Python loops for MoE expert routing (top-2 gating, expert dispatch, weighted reduction). All linear layers (except embeddings, norms, routers, predictors) are converted to `Float8Linear` with the **tensorwise** recipe.
|
| 29 |
+
|
| 30 |
+
### tk-train.py
|
| 31 |
+
|
| 32 |
+
Same as `fp8_train.py` but replaces the Python MoE loop with **TileKernels** fused CUDA kernels:
|
| 33 |
+
- `topk_gate` — fused top-k gating
|
| 34 |
+
- `normalize_weight` — gate weight normalization
|
| 35 |
+
- `get_fused_mapping` — expert-token mapping computation
|
| 36 |
+
- `expand_to_fused` — token-to-expert expansion
|
| 37 |
+
- `reduce_fused` — expert output reduction
|
| 38 |
+
- `aux_fi` — auxiliary load-balancing loss
|
| 39 |
+
|
| 40 |
+
Falls back to Python MoE loop if TileKernels is unavailable.
|
| 41 |
+
|
| 42 |
+
## Requirements
|
| 43 |
+
|
| 44 |
+
- **GPU**: NVIDIA Blackwell (sm_120) or Hopper (sm_90)
|
| 45 |
+
- **CUDA**: 13.2+
|
| 46 |
+
- **PyTorch**: 2.11.0+ (with CUDA 13.x support)
|
| 47 |
+
- **torchao**: `pip install torchao` (for Float8Linear FP8 training)
|
| 48 |
+
- **TileKernels** (optional, for tk-train.py): included in `tile_kernels/` directory
|
| 49 |
+
- **bitsandbytes** (optional): for 8-bit AdamW optimizer
|
| 50 |
+
- **loguru** (optional): logging; falls back to stdlib logging
|
| 51 |
+
- **numpy**: for memory-mapped dataset loading
|
| 52 |
+
|
| 53 |
+
## Data
|
| 54 |
+
|
| 55 |
+
### FineWeb-Edu Byte-Level Dataset
|
| 56 |
+
|
| 57 |
+
Pre-tokenized FineWeb-Edu at byte level (~17B tokens, 14 shards):
|
| 58 |
+
- Format: uint16 `.bin` files, no header
|
| 59 |
+
- Vocab: BOS=257, EOS=258, PAD=256, raw bytes 0-255
|
| 60 |
+
- Each shard: ~2.5GB, ~1.2B tokens
|
| 61 |
+
- `metadata.json` with shard info
|
| 62 |
+
|
| 63 |
+
```python
|
| 64 |
+
from torch.utils.data import IterableDataset, DataLoader, get_worker_info
|
| 65 |
+
import numpy as np, os, torch
|
| 66 |
+
|
| 67 |
+
class LocalByteLevelDataset(IterableDataset):
|
| 68 |
+
def __init__(self, data_dir, seq_len=2048, rank=0, world_size=1):
|
| 69 |
+
self.seq_len = seq_len
|
| 70 |
+
self.data_dir = data_dir
|
| 71 |
+
self.rank = rank
|
| 72 |
+
self.world_size = world_size
|
| 73 |
+
self._files = self._discover_files()
|
| 74 |
+
|
| 75 |
+
def _discover_files(self):
|
| 76 |
+
import glob as _glob
|
| 77 |
+
files = sorted(_glob.glob(os.path.join(self.data_dir, "**/*.bin"), recursive=True))
|
| 78 |
+
return [f for i, f in enumerate(files) if i % self.world_size == self.rank]
|
| 79 |
+
|
| 80 |
+
def __iter__(self):
|
| 81 |
+
worker = get_worker_info()
|
| 82 |
+
num_workers = worker.num_workers if worker else 1
|
| 83 |
+
worker_id = worker.id if worker else 0
|
| 84 |
+
files = [f for i, f in enumerate(self._files) if i % num_workers == worker_id]
|
| 85 |
+
for filepath in files:
|
| 86 |
+
arr = np.memmap(filepath, dtype=np.uint16, mode='r')
|
| 87 |
+
pos = 0
|
| 88 |
+
while pos + self.seq_len + 1 <= len(arr):
|
| 89 |
+
chunk = arr[pos:pos + self.seq_len + 1]
|
| 90 |
+
pos += self.seq_len + 1
|
| 91 |
+
x = torch.tensor(chunk[:-1], dtype=torch.long)
|
| 92 |
+
y = torch.tensor(chunk[1:], dtype=torch.long)
|
| 93 |
+
y[y == 256] = -100 # PAD_ID = -100 for loss
|
| 94 |
+
yield x, y
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
## Usage
|
| 98 |
+
|
| 99 |
+
### FP8 Training (torchao only)
|
| 100 |
+
|
| 101 |
+
```bash
|
| 102 |
+
python3 fp8_train.py \
|
| 103 |
+
--seq_len 256 --micro_batch 112 --grad_accum 4 \
|
| 104 |
+
--precision fp8 --n_loops 6
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
### FP8 + TileKernels Training
|
| 108 |
+
|
| 109 |
+
```bash
|
| 110 |
+
# Make TileKernels importable
|
| 111 |
+
export PYTHONPATH="$HOME/TileKernels:$PYTHONPATH"
|
| 112 |
+
|
| 113 |
+
python3 tk-train.py \
|
| 114 |
+
--seq_len 256 --micro_batch 112 --grad_accum 4 \
|
| 115 |
+
--precision fp8 --n_loops 6
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
### Resume from Checkpoint
|
| 119 |
+
|
| 120 |
+
```bash
|
| 121 |
+
python3 fp8_train.py --resume checkpoints-fp8/spider-step2650.pt
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
### Fresh Start (Load Weights, Reset Optimizer)
|
| 125 |
+
|
| 126 |
+
```bash
|
| 127 |
+
python3 fp8_train.py --resume checkpoints-fp8/spider-step2650.pt --reset_steps
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
### With torch.compile
|
| 131 |
+
|
| 132 |
+
```bash
|
| 133 |
+
python3 fp8_train.py --compile --precision fp8
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
### Mock Data (Quick Test)
|
| 137 |
+
|
| 138 |
+
```bash
|
| 139 |
+
python3 fp8_train.py --mock_data --max_steps 20 --seq_len 128 --micro_batch 2
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
## CLI Flags
|
| 143 |
+
|
| 144 |
+
| Flag | Default | Description |
|
| 145 |
+
|------|---------|-------------|
|
| 146 |
+
| `--seq_len` | 2048 | Sequence length |
|
| 147 |
+
| `--micro_batch` | 64 | Micro batch size per GPU |
|
| 148 |
+
| `--grad_accum` | 1 (env: `GRAD_ACCUM`) | Gradient accumulation steps |
|
| 149 |
+
| `--n_loops` | 6 (env: `N_LOOPS`) | RDT loop iterations |
|
| 150 |
+
| `--lr` | 3e-4 (env: `LR`) | Peak learning rate |
|
| 151 |
+
| `--precision` | auto | `auto`, `fp8`, or `bf16` |
|
| 152 |
+
| `--compile` | off | Enable `torch.compile(mode="default")` |
|
| 153 |
+
| `--resume` | auto-detect | Path to checkpoint |
|
| 154 |
+
| `--reset_steps` | off | Load weights, zero optimizer, reset step=0 |
|
| 155 |
+
| `--ckpt_dir` | `checkpoints-fp8` / `checkpoints-tk` | Checkpoint directory |
|
| 156 |
+
| `--data_dir` | `/home/lamcodealong/fineweb_bytelevel` | Local .bin data directory |
|
| 157 |
+
| `--mock_data` | off | Use synthetic data for testing |
|
| 158 |
+
| `--max_steps` | 0 (unlimited) | Max training steps |
|
| 159 |
+
| `--no_gradient_checkpointing` | off | Disable grad checkpointing |
|
| 160 |
+
|
| 161 |
+
## Environment Variables
|
| 162 |
+
|
| 163 |
+
| Variable | Default | Description |
|
| 164 |
+
|----------|---------|-------------|
|
| 165 |
+
| `SEQ_LEN` | 2048 | Override `--seq_len` |
|
| 166 |
+
| `MICRO_BATCH` | 64 | Override `--micro_batch` |
|
| 167 |
+
| `GRAD_ACCUM` | 1 | Override `--grad_accum` |
|
| 168 |
+
| `N_LOOPS` | 6 | Override `--n_loops` |
|
| 169 |
+
| `LR` | 3e-4 | Override `--lr` |
|
| 170 |
+
| `TARGET_TOKENS` | 10B | Total training tokens |
|
| 171 |
+
| `CKPT_EVERY` | 50 | Steps between checkpoints |
|
| 172 |
+
|
| 173 |
+
## FP8 Configuration
|
| 174 |
+
|
| 175 |
+
- **Recipe**: `tensorwise` (axiswise incompatible with grad checkpointing + reshape on sm_120)
|
| 176 |
+
- **Module filter**: Skips `boundary_predictor`, `loop_embedding`, `engram`, `layernorm`, `norm`, `embed_tokens`, `lm_head`, `halt_predictor`, `router`
|
| 177 |
+
- **pad_inner_dim=True**: Pads linear layer inner dims for FP8 alignment
|
| 178 |
+
- **Warmup**: 2 fwd+bwd passes with dummy data before training starts (kernel compilation)
|
| 179 |
+
|
| 180 |
+
## Performance
|
| 181 |
+
|
| 182 |
+
Benchmarked on RTX PRO 6000 (Blackwell sm_120, 96GB VRAM):
|
| 183 |
+
|
| 184 |
+
| Config | Throughput | Peak VRAM |
|
| 185 |
+
|--------|-----------|-----------|
|
| 186 |
+
| FP8, mb=112, seq=256, ga=4 | ~11.8k tok/s | 25.3 GB |
|
| 187 |
+
| FP8, mb=64, seq=2048, ga=1 | ~2.2k tok/s | ~42 GB |
|
| 188 |
+
|
| 189 |
+
## Checkpoints
|
| 190 |
+
|
| 191 |
+
Latest checkpoint: `spider-step2650.pt` (3.8GB)
|
| 192 |
+
- Contains: `model_state_dict`, `optimizer_state_dict`, `step`, `epoch`, `cfg`, `best_loss`
|
| 193 |
+
- Step 2650, loss ~1.9
|
| 194 |
+
|
| 195 |
+
## TileKernels
|
| 196 |
+
|
| 197 |
+
TileKernels provides fused CUDA/Triton kernels for MoE operations. Only the MoE routing kernels are used here (no weight dtype changes):
|
| 198 |
+
|
| 199 |
+
- `tile_kernels.moe.topk_gate` — fused top-k gating
|
| 200 |
+
- `tile_kernels.moe.normalize_weight` — gate weight L2 normalization
|
| 201 |
+
- `tile_kernels.moe.get_fused_mapping` — compute expert-token dispatch mapping
|
| 202 |
+
- `tile_kernels.moe.expand_to_fused` — expand tokens to expert slots
|
| 203 |
+
- `tile_kernels.moe.reduce_fused` — reduce expert outputs back to tokens
|
| 204 |
+
- `tile_kernels.moe.aux_fi` — auxiliary load-balancing loss
|
| 205 |
+
|
| 206 |
+
Install from the included `tile_kernels/` directory:
|
| 207 |
+
```bash
|
| 208 |
+
cd tile_kernels && pip install -e .
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
## Tokenizer
|
| 212 |
+
|
| 213 |
+
Byte-level encoding (no learned tokenizer needed):
|
| 214 |
+
|
| 215 |
+
```python
|
| 216 |
+
def encode(text: str) -> list[int]:
|
| 217 |
+
return [257] + list(text.encode('utf-8')) + [258] # BOS + bytes + EOS
|
| 218 |
+
|
| 219 |
+
def decode(ids: list[int]) -> str:
|
| 220 |
+
return bytes(i for i in ids if 0 <= i <= 255).decode('utf-8', errors='replace')
|
| 221 |
+
```
|
| 222 |
+
|
| 223 |
+
Special tokens: `PAD=256`, `BOS=257`, `EOS=258`, `IMG_START=0`, `IMG_END=1`, `IMG_PATCH=2`, `IMG_NEWLINE=3`, plus boundary and loop sentinels.
|
| 224 |
+
|
| 225 |
+
## Known Limitations
|
| 226 |
+
|
| 227 |
+
- **DeepGEMM**: Blocked on sm_120 (`Unknown recipe arch_major: 12`)
|
| 228 |
+
- **FlashMLA**: sm_100f kernels produce CUTLASS errors on sm_120
|
| 229 |
+
- **TransformerEngine**: `cublasLtGroupedMatrixLayoutInit_internal` symbol error
|
| 230 |
+
- **torchao axiswise recipe**: Incompatible with gradient checkpointing + reshape on sm_120
|
| 231 |
+
- **torch.compile reduce-overhead mode**: Has issues; use `mode="default"` only
|
| 232 |
+
|
| 233 |
+
## License
|
| 234 |
+
|
| 235 |
+
Same as Spider-FLEXITOKENS.
|