#6593 [Engine] eliminate sm_86 register and stack spills in strata-inkling CUDA decode kernels

open · @nmqx · 0 comentarios · En GitHub

BenchmarksServer & APIMulti-GPUAMD / HIPNVIDIA / CUDAWindowsLinux

Descripción

## Problem and result

On Ampere `sm_86` (RTX 3090, CUDA 13.3), `cuobjdump --dump-resource-usage` on `strata-inkling` (`v0.2.19` / `0.1.65`) revealed heavy register pressure and local-memory stack spills across the single-GPU CUDA decode hot path:

1. **`ink_kernels.cuh` (trunk dense GEMV & AttnRes, affects both `SQ-20` and `SQ-15`):**
   - `k_ink_gemv_multi_q8row*` and `k_ink_gemv_multi_bf16` indexed `InkMultiProj` (128 B) by runtime `pid`, forcing `nvcc` to copy the struct to local stack (`STACK:128–136 B`).
   - `k_ink_attn_res_mix`, `k_ink_attn_res_3x_cat_rms`, and `k_ink_mlp_res_cat_rms` indexed `DepthStatePtrs::p[s]` dynamically and kept `sc_local[4][8]` / `s_inv[8]` on thread-local stack (`STACK:144–240 B`), running 48–97× per decode step.
2. **`ink_moe_cuda.cuh` (`S_K` MoE decode/verify kernels, affects `SQ-15`):**
   - `k_ink_moe_gu_sk<1,4,16>` and `k_ink_moe_dn_sk<1,4,16>` inlined a 7-way runtime `switch (ed.gu_code / ed.dn_code)` while holding both `wg[16]` and `wu[16]` (32 weight registers) live simultaneously and assigning `xp[m]` / `hp[m]` in runtime loops, hitting `REG:255` (`16.7%` occupancy) and `STACK:24–72 B`.

With both fixes (`3 files, +298 / -128`, exact greedy token parity):
- **Same-toolchain (`CUDA 13.3.73`) matched A/B on RTX 3090 (`sm_86`, `--gpus 1`):**
  - `SQ-20` (`SF4`/`MX8` experts — exercises `ink_kernels.cuh` alone): **`p=1 off` `+15.8%`** (`67.3 -> 77.9 tps`), **`p=8` `0.0%`** (`248.3 -> 248.3 tps`, prefill `+1.4%`).
  - `SQ-15` (`S3_K`/`S4_K`/`S5_K` + `SF4` — exercises both fixes): **`p=1 off` `+8.3%`** (`59.0 -> 63.9 tps`), **`p=8` `+3.8%`** (`227.5 -> 236.2 tps`, prefill `+1.2%`).
- **Interleaved A/B vs official CI `strata-inkling 0.1.65` (`p=1` across 8/8 cases):** `SQ-15` **`+5.3% to +6.7%`** (`147.4 -> 157.3 tps` @ 512p DSpark, `98.5 -> 104.0 tps` @ 4096p DSpark); `SQ-20` **`+10.3% to +10.9%`** plain (`68.9 -> 76.4 tps`) and **`+4.0% to +4.3%`** DSpark (`92.6 -> 96.3 tps`).

## Related discussion

none (self-contained `sm_86` kernel register/stack spill fix, bit-identical arithmetic).

## Approach

- **`engine/src/inkling2/cuda/ink_kernels.cuh`:**
  - Adds `ink_resolve_multi_proj()` with `#pragma unroll` over `p = 0..4` so every `InkMultiProj` field access uses a compile-time constant offset in `CONSTANT[0]` (applied to `k_ink_gemv_multi_bf16`, `k_ink_gemv_multi_q8row_m1`, `k_ink_gemv_multi_q8row_preq`, `k_ink_gemv_multi_q8row`).
  - Unrolls `s = 0..7` over `DepthStatePtrs` and moves the tiny `tid == 0` softmax reduction buffers (`s_sc_local`, `s_inv_buf`, `s_sc`, `s_sinv`) to `__shared__` in `k_ink_attn_res_mix`, `k_ink_attn_res_3x_cat_rms`, and `k_ink_mlp_res_cat_rms` (barrier-safe; no extra `__syncthreads()` needed).
- **`engine/src/inkling2/cuda/ink_moe_cuda.cuh` & `ink_runner.cuh`:**
  - Streams one `uint4` at a time in `dp64_i8_smem()` and reuses a single `uint32_t w[16]` sequentially for gate then up in `sk_gu_fused_4lane()` (halving live weight registers `32 -> 16`, identical `dp4a` + `fmaf` order) with `#pragma unroll 1` on the outer `s = 0..8` loop.
  - Adds per-format specialized `k_ink_moe_gu_sk_fmt<FMT, MAX_M>` and `k_ink_moe_dn_sk_fmt<FMT, MAX_M>` with static `#pragma unroll` shared-memory pointer init (`xp[m] = s_xq[m]`, `hp[m] = s_hq[m]`) and occupancy-tuned `__launch_bounds__`.
  - Records per-layer `sk_gu_mask` / `sk_dn_mask` bitmasks in `InkLayerMoeInfo` (`ink_runner.cuh`) and dispatches only present `S_K` formats via `launch_ink_moe_*_sk_mask` (keeping the original `k_ink_moe_*_sk` fallback whenever `mask == 0`).

## Validation

**Passed:**
- **Build & `cuobjdump --dump-resource-usage` (`sm_86`, Windows 11, CUDA 13.3.73, MSVC 19.50, Ninja, `v0.2.19` `c6f6427d9` + commit):**

| Kernel (`sm_86`) | Before (`v0.2.19`) | After (`perf/inkling-sm86-spills`) |
| --- | --- | --- |
| `k_ink_moe_gu_sk<1>` → `*_sk_fmt<S2..S8, 1>` | `REG:255 STACK:0` | **`REG:76–96 STACK:0`** |
| `k_ink_moe_dn_sk<1>` → `*_sk_fmt<S2..S8, 1>` | `REG:168 STACK:0` | **`REG:78–86 STACK:0`** |
| `k_ink_moe_gu_sk<4>` → `*_sk_fmt<S2..S8, 4>` | `REG:255 STACK:32` | **`REG:83–90 STACK:0`** |
| `k_ink_moe_dn_sk<4>` → `*_sk_fmt<S2..S8, 4>` | `REG:255 STACK:32` | **`REG:88 STACK:0`** |
| `k_ink_moe_gu_sk<16>` / `dn_sk<16>` → `*_sk_fmt<S2..S8, 16>` | `REG:255 STACK:72 / 24` | **`REG:156–166 / 136–139 STACK:0`** |
| `k_ink_gemv_multi_q8row_m1` / `_preq<4,8,16>` / `<4,8,16>` | `STACK:128` | **`STACK:0`** |
| `k_ink_gemv_multi_bf16<8,16>` / `<1,4,FP16>` (`<1,4,BF16>`) | `STACK:128–136` | **`STACK:0`** (`STACK:8–16`) |
| `k_ink_attn_res_mix` / `3x_cat_rms` / `mlp_res_cat_rms` | `STACK:240 / 208 / 144` | **`STACK:0 / 0 / 0`** (`REG:40`) |

- **Correctness & parity (RTX 3090 GPU 1, `CUDA_VISIBLE_DEVICES=1`):**
  - `--selfcheck` PASS (`rc=0`) on both `Inkling-2-Mini-SQ-15.strata` and `Inkling-2-Mini-SQ-20.strata`.
  - `--serve` greedy parity (`GEN 48 temperature=0 100,200,300,400,500,600,700,800`, patched vs stock `0.1.65`): **`48/48` identical token IDs (`0` diffs)** on all 4 combinations (`SQ-15` off/dspark, `SQ-20` off/dspark).
- **Same-toolchain matched A/B (`CUDA 13.3.73`, RTX 3090 `--gpus 1`, 512 prompt / 256 gen):**

| Case | Local `v0.2.19` Ctrl (tps) | Patched (tps) | Decode Δ | PP Δ |
| --- | ---: | ---: | ---: | ---: |
| `SQ-20` `512/256 p=1 --draft off` | `67.3` | **`77.9`** | **+15.8%** | +18.3% |
| `SQ-20` `512/256 p=8` | `248.3` | **`248.3`** | **0.0%** | +1.4% |
| `SQ-15` `512/256 p=1 --draft off` | `59.0` | **`63.9`** | **+8.3%** | — |
| `SQ-15` `512/256 p=8` | `227.5` | **`236.2`** | **+3.8%** | +1.2% |

- **Interleaved A/B vs official CI `strata-inkling 0.1.65` (40 runs, 2 interleaved reps/case, `rc=0`, RTX 3090 `--gpus 1`):**
  - `SQ-15`: `512p off` `60.7 -> 64.0` (**+5.4%**), `512p dspark` `147.4 -> 157.3` (**+6.7%**), `4096p off` `57.4 -> 60.5` (**+5.3%**), `4096p dspark` `98.5 -> 104.0` (**+5.6%**).
  - `SQ-20`: `512p off` `68.9 -> 76.4` (**+10.9%**), `512p dspark` `92.6 -> 96.3` (**+4.0%**), `4096p off` `64.7 -> 71.3` (**+10.3%**), `4096p dspark` `90.8 -> 94.7` (**+4.3%**).
  - *(Note: comparing local CUDA 13.3.73 vs CI `0.1.65` showed a toolchain-only codegen difference on `p=8` across 6 unmodified kernels; the same-toolchain table above isolates the patch itself at `0.0%` on `SQ-20 p=8` and `+3.8%` on `SQ-15 p=8`.)*

**Failed:**
- none observed.

**Not run:**
- Linux, HIP, SYCL, and Metal builds/runs (no AMD/Intel/macOS hardware on this host).
- Non-`sm_86` NVIDIA architectures (`sm_75`/`sm_89`/`sm_120`).
- `SQ-10` (`FMT_S1_5`) runtime benchmark.

## Limits

- `FMT_S1_5` (used by `SQ-10`, not present in `SQ-15` or `SQ-20` and not benchmarked locally) still compiles to `REG:255 STACK:8 / 232 / 208` (`M=1 / 4 / 16`) due to ternary LUT register pressure; its launch bounds were kept at `(128, 2)` to avoid extra spilling.
- `k_ink_gemv_multi_bf16<1,false>` (`STACK:8`), `k_ink_gemv_multi_bf16<4,false>` (`STACK:16`), and `k_ink_gemv_multi_q8row<1>` (`STACK:16`) retain small residual frames down from `128–136 B`; all other targeted kernels reached `STACK:0`.

En el sitio

Enlaces a install, modelos, releases.