This document is relevant for: Trn2, Trn3
GPT-OSS-120B decode megakernel#
The decode megakernel fuses the entire GPT-OSS-120B decoder stack (all 36
layers: attention + MoE) into a single decode NEFF, replacing the per-layer
decode loop. It is decode-only, runs at logical-NC config 1 (lnc1), and
uses a packed-FP8 KV cache. The target deployment is disaggregated inference
(DI): a native per-layer prefill engine produces KV, which is transferred
over NIXL to a megakernel decode engine (TP8 × DP16 × EP128).
This document describes the integration as it stands: the model, runner, and kernel seams, the per-buffer KV-cache layout, and the DI wiring.
Why decode-only + DI#
The kernel is a single lnc1 NEFF (grid[1]). A per-layer prefill graph uses
lnc2 and lowers TP/EP SendRecv collectives that do not lower at lnc1
(NCC_IBTN243). A fused single-engine server (prefill + decode in one vllm serve, lnc1) therefore cannot compile. The megakernel is architecturally a
DI decode component:
Prefill role — native per-layer
model_mxfp4(TP4/EP2/lnc2),kv_role=kv_producer.Decode role — megakernel
model_mxfp4_perf(TP8/DP16/EP128/lnc1, packed-FP8),kv_role=kv_consumer.
The two roles run as independent single-node engines wired by IP over NIXL/LIBFABRIC.
Selection#
This document describes the c128_trn3_gen1 megakernel: 128 concurrent
decode sequences on the gen1 Trn3 UltraServer. It also describes the
c512_trn3_gen1 and c1024_trn3_gen1 variants: 512 and 1024 concurrent decode
sequences on the same hardware. Each supports only the configuration that it was
validated for.
neuron_config.gk_decode has three values:
Value |
Result |
|---|---|
Not set |
vLLM Neuron selects the megakernel when the server meets every requirement below. Otherwise the server uses the per-layer decode. For an MXFP4 decode server on Trn3, an INFO line gives the reasons. |
|
vLLM Neuron selects the megakernel. If a requirement fails, startup stops with an error that gives the reasons. |
|
The server uses the per-layer decode, also when it meets every requirement. |
NeuronPlatform._resolve_gk_decode makes the decision in
check_and_update_config. model/gpt_oss/gk_decode_constraints.py holds the
requirements, so the two decisions use the same rules. The requirements of
c128_trn3_gen1 are:
Area |
Requirement |
|---|---|
Model |
GPT-OSS 120B: hidden and intermediate size 2880, 128 experts with 4 for each token, 36 layers, 64 attention heads, 8 KV heads, and head dimension 64. The check reads the model config, not the model ID, so a renamed or fine-tuned 120B checkpoint also qualifies. GPT-OSS 20B (32 experts, 24 layers) does not. |
Quantization and platform |
|
Role |
The decode server of a disaggregated deployment ( |
Execution |
A Neuron device. CPU mode, CPU compile and |
Sharding |
TP 8, DP 16, and EP 128 ( |
Batch |
A global decode batch of 128: each |
KV cache |
The hybrid KV cache manager is on ( |
Context |
|
Features |
Speculative decoding with EAGLE3 only: the |
The kernel also needs the same k_scale and v_scale on every layer. The
model checks this at weight load.
neuron_config.gk_variant: "c512" selects the c512_trn3_gen1 megakernel (see
“Kernel variants”). c512_trn3_gen1_violations checks its requirements. They
are the requirements above, with three changes:
Area |
Requirement of |
|---|---|
Batch |
A global decode batch of 512: each |
KV cache |
The KV cache is packed FP8 e4m3 ( |
Features |
No speculative decoding. |
neuron_config.gk_variant: "c1024" selects the c1024_trn3_gen1 megakernel:
1024 concurrent decode sequences on the same hardware.
c1024_trn3_gen1_violations checks its requirements. They are the
c512_trn3_gen1 requirements, with one change:
Area |
Requirement of |
|---|---|
Batch |
A global decode batch of 1024: each |
Component map#
Component |
File |
Responsibility |
|---|---|---|
Selection |
|
resolves |
Model factory gate |
|
selects the megakernel perf model when |
Megakernel decode model |
|
decode-only weight holders; |
Kernel assembly |
|
stacks per-layer weights once; builds attention-control tensors; passes the per-buffer KV; calls the kernel |
Kernel entry |
|
wraps |
KV allocation |
|
megakernel-gated |
Stacked KV cap |
|
caps the KV budget of the stacked layout of c512 and c1024, and of c128 with an EAGLE3 draft (uint32 DMA offset) |
NIXL connector |
|
per-buffer registration; DCP read logic; SWA DI truncation |
Greedy sampler |
|
lnc-matched distributed argmax |
The megakernel path is confined to the perf model + its runner overrides; other models are untouched by the model/kernel changes.
Kernel contract#
The kernel (gpt_oss_decode_mega_kernel, an nkilib c128_giga_kernel fork)
consumes, per decode step:
Weights, stacked once onto a leading
[num_layers, …]axis in the exact per-layer shapes the kernel indexes asW[i](see the layout list inmega_decode.py). Each stack is built once on the host and loaded to the device directly by its own weight loader (build_stacked_weight_specs), so the per-layer copies never reach the device. Stacking is not inside the traceddecode(); restacking in-graph captured an ~810 MBconcatenateper step.KV cache:
group_sizeper-group base-0 buffers, passed as separate K and V args (see “KV-cache layout”). The kernel also accepts the stackedK_cache/V_cacheand asserts exactly one form is present; it reads the packed-FP8 path from the K buffer rank.Attention control (layer-invariant, built from per-layer
attn_metadata): RoPE cos/sin, a full block table + a trimmed-SWA block table, active mask, absolute + window-frame positions, and two KV-update indices (kv_cache_update_idx+kv_cache_update_idx_swa).TP and EP rank meshes as tuples-of-tuples (two
ReplicaGroupargs).
GPT-OSS alternates even = sliding-window (SWA, window 128) and odd = full
attention layers. The kernel bakes this parity: it selects the block table and
write index per layer by i % 2, and indexes the KV cache buffer by i // 2
(paired layers 2j, 2j+1 share buffer j). Weights stay indexed by i; only
the KV-cache axis is group_size.
Kernel variants: c128, c512 and c1024#
neuron_config.gk_variant selects the kernel package (default "c128"). The
customer sets it, because it selects the throughput operating point. The
variant also selects the requirements that gk_decode is resolved against (see
“Selection”).
|
|
|
|
|---|---|---|---|
nkilib package |
|
|
|
MoE |
static, all experts |
dynamic (DLoC): skips expert blocks with no routed tokens |
dynamic (DLoC), as c512 |
|
8 |
32 |
64 |
Global decode batch ( |
128 |
512 |
1024 |
KV cache input |
one base-0 buffer for each HMA group |
one stacked |
as c512 |
Fused embedding and RoPE |
supported |
not supported (the model raises) |
not supported (the model raises) |
Layer-0 input ( |
pre-gathered |
layer-0 AllGather in the kernel |
as c512 |
Extra compiler argument |
none |
|
as c512 |
V2 model runner |
supported |
supported |
supported |
Speculative decoding |
EAGLE3 on the V2 model runner: |
not supported |
not supported |
GK_DLOC_VARIANTS (model/neuron_config.py) names the two DLoC variants, c512
and c1024. The code keys the stacked KV cache, the KV cap, the compiler
argument, the T_ep check, the server marker and the fusion checks on this
tuple. In vllm-neuron, the two variants run the same code. Only the kernel
package and the marker value differ.
The c512 kernel takes the first 46 parameters of the c128 kernel, in the same
order. The wrapper passes them positionally, so a c512 package that changes these
46 parameters fails with a TypeError at the call. A newer c512 kernel can add
parameters after them if each added parameter has a default.
The c1024 kernel is a copy of the c512 package with more perf flags, which are
off by default. It has the same signature as the c512 kernel, and the wrapper
gives it the same 46 positional arguments. Two static Python bools of the NKI
wrapper, is_c512 and is_c1024, select the kernel at trace time. At most one
of them is True.
c1024 and VLLM_NEURON_MEGA_VACTIVE_ONCHIP. The c1024 package reads all
the VLLM_NEURON_MEGA_* flags of the c512 package. At more than 32 sequences
for each DP rank (c1024 runs 64), VLLM_NEURON_MEGA_VACTIVE_ONCHIP=1 puts a
batch position above 31 in a stream-shuffle mask, and the trace fails with
nc_stream_shuffle shuffle_mask[31]=32 must be 0-31 or 255. For this reason,
a c1024 server needs VLLM_NEURON_MEGA_VACTIVE_ONCHIP="" (off) and
VLLM_NEURON_MEGA_VACTIVE_DMA_ENGINE="scalar+sync". The kernel tests the value
for truth, so 0 also turns the on-chip path on. Unset the variable, or set it
to the empty string. A c512 server (32 sequences for each DP rank) can keep
VLLM_NEURON_MEGA_VACTIVE_ONCHIP=1.
T_ep constraint. The DLoC MoE uses a token block of 128 (this is not the
vLLM KV block size, which stays 128 for every variant). moe_tkg accepts the
block only when it divides T_ep and is at most T_ep / 2:
T_shard = max_num_seqs * S_decode / tp_size
T_ep = T_shard * ep_degree # must be a multiple of 128 and at least 256
At tp_size=8 and ep_degree=128, max_num_seqs=32 gives T_ep=512 (c512),
and max_num_seqs=64 gives T_ep=1024 (c1024).
mega_decode.assert_dloc_t_ep checks this before the trace.
Server marker. A c512 or c1024 decode server sets VLLM_NEURON_MEGA_GIGA
to its variant (c512 or c1024) on every process. vllm_neuron/__init__.py
reads it before the first kernel import:
It does not seed the c128 DMA-order kernel flags, because the c512 and c1024 packages read some of the same names.
It sets
VLLM_NEURON_MEGA_KV_UPDATE=1when the variable is unset or empty. The c512 and c1024 kernels write the KV cache in place only when the value is not empty. An empty value skips the write with no error, and decode then reads a KV cache without the current token.
MegaDecodeRunner raises when the marker and gk_variant do not agree. The
check is exact. The c512 marker gives the same seeds as c1024, but a c1024
server with the c512 marker also raises. The other VLLM_NEURON_MEGA_*
flags of the c512 and c1024 kernels are perf settings, which the launcher sets.
Gate/up MX scale layout. Each variant reads the layout from its own
package. A c512 or c1024 package without its own mega_flag_default_on has no
PACK64 path, so the host keeps the unpacked layout. A packed scale in a kernel
that reads the unpacked layout gives wrong MoE results with no error.
KV-cache layout (per-buffer, HMA-preserving)#
vLLM’s Hybrid KV-cache Manager (HMA) reports GPT-OSS as 2 groups (SWA + full)
and packs the 36 per-layer specs into group_size = 18 shared buffers (each
buffer shared by one SWA + one full layer, shared_by = [2i, 2i+1]). num_blocks
is a single global scalar; all buffers are identical size.
get_kv_spec stays honest and per-layer (SWA layers report their window;
full layers report full attention) so HMA stays enabled and SWA keeps its reduced
per-request block-ID consumption. The megakernel-gated initialize_kv_cache
branch allocates one separate tensor per HMA group:
group_bufs[i] : [2, num_blocks, kv, block, d] # K half, then V half, per group i
K_i = group_bufs[i][0] # kernel per-buffer K arg (packed-FP8: a trailing .view)
V_i = group_bufs[i][1] # kernel per-buffer V arg
Each group is its own allocation the kernel indexes base-0, so the block-KV DMA
offset spans one group and stays under uint32 with no cap. Packed-FP8 K is a
trailing .view([…, block//2, d, 2]) on group_bufs[i][0], same bytes and same
num_blocks. V is never packed.
The buffers are handed to the kernel via a megakernel-only
set_kv_cache_buffers, which stores each as an individual model attribute
(K_cache_buf_i / V_cache_buf_i) rather than one list. A list would not
survive the fork-child meta-swap, which walks tensor attributes but does not
recurse into a list, so the buffers would stay on the real device and fail the
meta-only compile. Per-layer bind_kv_cache({layer: [k, v]}) keeps the generic
signature every model shares; packed-ness is inferred from
k.dim() == v.dim() + 1.
Each group buffer is [2, num_blocks, …] in its own storage, so the per-layer DI
region is that whole buffer and the NIXL connector registers each group’s K and V
directly.
Stacked layout (c512 and c1024). The c512 and c1024 kernels take one
stacked K and one stacked V. For gk_variant="c512" and gk_variant="c1024",
the model reports gk_kv_layout == "stacked", and initialize_kv_cache makes
ONE allocation:
kv_stack : [2, group_size, num_blocks, kv, block, d] # the K half, then the V half
K_stack = kv_stack[0] # [group_size, nb, kv, block, d] (packed FP8: [.., block//2, d, 2])
V_stack = kv_stack[1] # [group_size, nb, kv, block, d]
Buffer i is the slice [:, i], in the same layer order as the per-buffer
layout. Its K slab and its V slab are each contiguous, so the NIXL connector
registers them the same way. set_kv_cache_stack stores K_stack and
V_stack as two tensor attributes of the model.
Stacked layout with EAGLE3 (c128). c128spec also takes one stacked K and
one stacked V. The V2 model runner sets the aux layers of the target when it
loads the model. The model with aux layers then reports
gk_kv_layout == "stacked" for c128 too, and the runner makes the stacked
layout. The stack holds the target buffers only, because c128spec reads
buffer i // 2 for layer i. Each draft layer gets its own
[2, num_blocks, kv, block, d] buffer outside the stack, which the runner binds
to the speculator and registers with the KV connector.
DI wiring (NIXL)#
The problem. The transfer must land each prefill KV block on the group buffer
block the kernel reads, matching what the native per-layer prefill registers.
vLLM’s stock _register_packed_kv_cache assumes a block-major,
all-layers-interleaved (“DSv4 packed”) layout and would address the wrong bytes.
The fix. For the megakernel decode (gk_decode set,
kv_role != kv_producer),
NeuronNixlConnectorWorker._register_packed_kv_cache registers per-buffer K/V
regions, one per group buffer’s K and one per group buffer’s V, each
block_len = kv·block·d (one layer-pair block), de-duplicating buffers shared by
paired layers via base address. This reproduces what a native per-layer prefill
registers, so the handshake sees head_ratio = 1 (match path) and a NIXL
descriptor base_region + b·block_len lands on that buffer’s block b. A cache
without the megakernel defers to super().
The prefill role runs the native per-layer model (gk_decode=False), which
registers the same per-buffer region set natively — so P and D region counts and
block-lens match with no producer-side change.
The connector also carries a sliding-window DI truncation fix
(NeuronNixlConnectorScheduler): on a hybrid SWA model the decode node admits
the prompt at length N but recomputes the last token at N-1, so the SWA skip
evicts the window’s oldest block one step early at the N % block_size == block_size-1 boundary and that block is never transferred. Both nodes are made
to operate at N-1. This is independent of the megakernel KV layout.
KV budget (no cap)#
Each group buffer is indexed base-0, so the block-KV DMA HWAPOffset spans one
group’s num_blocks · kv_heads · block · head_size, which stays under uint32
(NCC_IBTN127) at any pool size the HBM holds. No megakernel-specific
KV-budget cap is needed; num_blocks is the standard profiled budget.
neuron_worker.py no longer adjusts it.
For the 120B megakernel config (group_size=18, kv_heads=1, block=128,
head_size=64) the profiled budget is num_blocks ≈ 36,175 per engine
(≈9.94 GiB KV, ~2.54M KV tokens), about 2x the count the earlier stacked uint32
cap allowed. The KV budget is now bounded by gpu_memory_utilization, not by any
megakernel offset limit.
The stacked layout of c512 and c1024 is the exception. Buffer i starts at a
static offset of i * num_blocks * page_bytes / 2, so the deepest buffer
(group_size - 1) sets the largest block-KV DMA offset.
NeuronWorker.determine_available_memory caps the budget for this layout so
that the offset stays below 0.85 * 2**32 (else the compile fails with
NCC_IBTN127). For the 120B config, the cap is 26,214 blocks per buffer
(7.2 GiB). The cap reads gk_kv_layout, so it also applies to the stack of c128
with EAGLE3, which c128spec reads at the same static buffer offsets. The draft
buffers are outside the stack, so the cap stays at 26,214 blocks per buffer. The
capped budget also holds these blocks in each draft buffer. The validated EAGLE3
decode server has 17,346 blocks per buffer, below the cap.
This document is relevant for: Trn2, Trn3