aien-yardsticks
aien-dev/aien-yardsticks
Brought by drakestapleton
aien-yardsticks / yardsticks / modular-nemotron-h-kvexp / README.md
| 1 | > **YARDSTICK ONLY. Never part of AIEN. Nothing here ships in AIEN.** |
| 2 | > |
| 3 | > This is the Nemotron-H KV-head-expansion comparison adapter for Modular MAX, kept so AIEN can be measured against MAX. |
| 4 | > Modular MAX was removed from AIEN on 2026-10-15. This adapter is a frozen record only and is no longer run; the install, serve and test steps below are historical, so do not follow them. |
| 5 | > It was moved here, with its git history, from `aien-dev/aien-sovereign-core` (commit `5fdab70`, path `modular/nemotron_h_kvexp`) because AIEN repos may contain no Python. |
| 6 | > The Python in this folder is scheduled for replacement by Rust (see the "Replace the Python yardstick with Rust" issue in this repo). |
| 7 | |
| 8 | # KV-Head Expanded Nemotron-H Architecture for Modular MAX |
| 9 | |
| 10 | Custom architecture adapter enabling high-throughput serving of NVIDIA Nemotron-H models (such as `nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16`) on Modular MAX. |
| 11 | |
| 12 | ## Architecture Rationale |
| 13 | |
| 14 | Modular MAX employs optimized FlashAttention kernels for Grouped-Query Attention (GQA). In current MAX versions, the maximum supported GQA group size is 8 (`group = num_attention_heads / num_key_value_heads <= 8`). |
| 15 | |
| 16 | Nemotron-3.5-Lightning-30B utilizes a GQA configuration where: |
| 17 | - Query heads (`n_heads`): 32 |
| 18 | - Key-Value heads (`n_kv`): 2 |
| 19 | - GQA group ratio: 32 / 2 = 16 |
| 20 | |
| 21 | Because 16 > 8, loading the standard checkpoint triggers an unsupported group size error in the MAX attention kernel. |
| 22 | |
| 23 | This adapter resolves the kernel limitation by calculating the minimum integer expansion factor: |
| 24 | |
| 25 | ```python |
| 26 | expansion = math.ceil((n_heads / n_kv) / MAX_NVIDIA_GQA_GROUP) |
| 27 | ``` |
| 28 | |
| 29 | For Nemotron-H (group 16), `expansion = 2`. Each KV head is replicated across the channel dimension during state dictionary weight loading: |
| 30 | - Replicated KV heads: 2 * 2 = 4 |
| 31 | - Effective GQA group ratio: 32 / 4 = 8 |
| 32 | |
| 33 | This satisfies the FlashAttention kernel constraint without altering model activations, attention math, or output distributions. |
| 34 | |
| 35 | ## Key Features |
| 36 | |
| 37 | 1. **Native Dynamic Expansion**: Replicates KV heads on the fly during safe tensor loading and state dict conversion. |
| 38 | 2. **Offline PEFT LoRA Folding**: Set `ATLAS_LORA_DIR` to fold LoRA adapters (A and B matrices) directly into base weights prior to QKV fusion, eliminating runtime LoRA overhead. |
| 39 | 3. **Lazy Registration Override**: Overrides default `NemotronHForCausalLM` registration within the MAX pipeline registry to ensure transparent resolution from standard model checkpoints. |
| 40 | |
| 41 | ## Installation & Usage |
| 42 | |
| 43 | Install as an editable package: |
| 44 | |
| 45 | ```bash |
| 46 | pip install -e . |
| 47 | ``` |
| 48 | |
| 49 | Serve using the MAX CLI: |
| 50 | |
| 51 | ```bash |
| 52 | max serve \ |
| 53 | --model nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16 \ |
| 54 | --served-model-name nemotron-lightning \ |
| 55 | --custom-architectures $(pwd) \ |
| 56 | --port 18006 \ |
| 57 | --max-batch-size 8 \ |
| 58 | --max-length 32768 \ |
| 59 | --kv-cache-format bfloat16 |
| 60 | ``` |
| 61 | |
| 62 | ## Testing |
| 63 | |
| 64 | Run unit tests via standard Python unittest: |
| 65 | |
| 66 | ```bash |
| 67 | python3 -m unittest discover -s tests -v |
| 68 | ``` |
| 69 | |
| 70 | ## License |
| 71 | |
| 72 | Licensed under the Apache License, Version 2.0. |
| 73 | |
| 74 | ## Benchmarks (NVIDIA DGX Spark / Grace Blackwell GB10) |
| 75 | |
| 76 | Benchmarked on ARM64 Grace Neoverse V2 cores using realistic Nemotron-30B layer dimensions (4608x4096 expanded to 5120x4096): |
| 77 | |
| 78 | | Metric | Measured Value | |
| 79 | |---|---| |
| 80 | | Latency per Layer | 5.49 ms | |
| 81 | | Full Model Expansion (52 layers) | 285.63 ms | |
| 82 | | Memory Copy Throughput | 12.80 GB/s | |
| 83 | |
| 84 | Load-time expansion overhead is under 300 ms for the entire 30B model, introducing negligible startup delay while enabling high-throughput serving on Modular MAX. |