aien-sovereign-core
Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark
Brought by drakestapleton
aien-sovereign-core / modular / nemotron_h_kvexp / README.md
| 1 | # KV-Head Expanded Nemotron-H Architecture for Modular MAX |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | ## Architecture Rationale |
| 6 | |
| 7 | 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`). |
| 8 | |
| 9 | Nemotron-3.5-Lightning-30B utilizes a GQA configuration where: |
| 10 | - Query heads (`n_heads`): 32 |
| 11 | - Key-Value heads (`n_kv`): 2 |
| 12 | - GQA group ratio: 32 / 2 = 16 |
| 13 | |
| 14 | Because 16 > 8, loading the standard checkpoint triggers an unsupported group size error in the MAX attention kernel. |
| 15 | |
| 16 | This adapter resolves the kernel limitation by calculating the minimum integer expansion factor: |
| 17 | |
| 18 | ```python |
| 19 | expansion = math.ceil((n_heads / n_kv) / MAX_NVIDIA_GQA_GROUP) |
| 20 | ``` |
| 21 | |
| 22 | For Nemotron-H (group 16), `expansion = 2`. Each KV head is replicated across the channel dimension during state dictionary weight loading: |
| 23 | - Replicated KV heads: 2 * 2 = 4 |
| 24 | - Effective GQA group ratio: 32 / 4 = 8 |
| 25 | |
| 26 | This satisfies the FlashAttention kernel constraint without altering model activations, attention math, or output distributions. |
| 27 | |
| 28 | ## Key Features |
| 29 | |
| 30 | 1. **Native Dynamic Expansion**: Replicates KV heads on the fly during safe tensor loading and state dict conversion. |
| 31 | 2. **Offline PEFT LoRA Folding**: Set `MAX_LORA_DIR` to fold LoRA adapters (A and B matrices) directly into base weights prior to QKV fusion, eliminating runtime LoRA overhead. |
| 32 | 3. **Lazy Registration Override**: Overrides default `NemotronHForCausalLM` registration within the MAX pipeline registry to ensure transparent resolution from standard model checkpoints. |
| 33 | |
| 34 | ## Installation & Usage |
| 35 | |
| 36 | Install as an editable package: |
| 37 | |
| 38 | ```bash |
| 39 | pip install -e . |
| 40 | ``` |
| 41 | |
| 42 | Serve using the MAX CLI: |
| 43 | |
| 44 | ```bash |
| 45 | max serve \ |
| 46 | --model nvidia/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-BF16 \ |
| 47 | --served-model-name nemotron-lightning \ |
| 48 | --custom-architectures $(pwd) \ |
| 49 | --port 18006 \ |
| 50 | --max-batch-size 8 \ |
| 51 | --max-length 32768 \ |
| 52 | --kv-cache-format bfloat16 |
| 53 | ``` |
| 54 | |
| 55 | ## Testing |
| 56 | |
| 57 | Run unit tests via standard Python unittest: |
| 58 | |
| 59 | ```bash |
| 60 | python3 -m unittest discover -s tests -v |
| 61 | ``` |
| 62 | |
| 63 | ## License |
| 64 | |
| 65 | Licensed under the Apache License, Version 2.0. |
| 66 | |
| 67 | ## Benchmarks (NVIDIA DGX Spark / Grace Blackwell GB10) |
| 68 | |
| 69 | Benchmarked on ARM64 Grace Neoverse V2 cores using realistic Nemotron-30B layer dimensions (4608x4096 expanded to 5120x4096): |
| 70 | |
| 71 | | Metric | Measured Value | |
| 72 | |---|---| |
| 73 | | Latency per Layer | 5.49 ms | |
| 74 | | Full Model Expansion (52 layers) | 285.63 ms | |
| 75 | | Memory Copy Throughput | 12.80 GB/s | |
| 76 | |
| 77 | 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. |