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.

true

vLLM Neuron selects the megakernel. If a requirement fails, startup stops with an error that gives the reasons.

false

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

quantization: "mxfp4", on Trn3, at LNC 1.

Role

The decode server of a disaggregated deployment (kv_role: "kv_consumer").

Execution

A Neuron device. CPU mode, CPU compile and VLLM_NEURON_DISABLE_NKI_KERNELS are not supported.

Sharding

TP 8, DP 16, and EP 128 (--enable-expert-parallel). attention_dp_size and mlp_dp_size are 1.

Batch

A global decode batch of 128: each num_seqs_buckets entry times the DP size is 128, so the buckets are [8]. The default buckets do not meet this.

KV cache

The hybrid KV cache manager is on (--no-disable-hybrid-kv-cache-manager with a DI connector). The KV cache is packed FP8 e4m3 (--kv-cache-dtype fp8 and fp8_packed_kv) or BF16 (bfloat16, or auto with a BF16 model).

Context

decode_context_length_buckets is not set. The decode context, ceil(max_model_len / block_size) * block_size, is at least 1024 tokens. Its last 8192-token tile is a multiple of 512 tokens with packed FP8, or 256 tokens with BF16. The block size is even with packed FP8. For example, 10240 passes and 8448 fails with packed FP8.

Features

Speculative decoding with EAGLE3 only: the c128spec form of the kernel verifies the drafts. EAGLE3 also needs the validated setup: num_speculative_tokens 3 and the packed FP8 e4m3 KV cache. EAGLE3 does not support the mega-decode DMA-order schedule (VLLM_NEURON_MEGA_DMA_ORDER_CONSTRAINTS_FILE or VLLM_NEURON_MEGA_DMA_ORDER_DERIVE), which matches only the decode graph without drafts. EAGLE3 runs only on Model Runner V2 and needs VLLM_USE_V2_MODEL_RUNNER=1. Without the variable, this server keeps the per-layer decode on its default runner. No jump-forward decoding, debug logits, pooling, or prompt embeds.

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 c512_trn3_gen1

Batch

A global decode batch of 512: each num_seqs_buckets entry times the DP size is 512, so the buckets are [32].

KV cache

The KV cache is packed FP8 e4m3 (--kv-cache-dtype fp8 and fp8_packed_kv). The c512 kernel was validated only with this cache.

Features

No speculative decoding. c128spec is a form of the c128 kernel, and c512 has no speculative-decoding form, so EAGLE3 does not qualify.

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 c1024_trn3_gen1

Batch

A global decode batch of 1024: each num_seqs_buckets entry times the DP size is 1024, so the buckets are [64].

Component map#

Component

File

Responsibility

Selection

vllm/platform.py, model/gpt_oss/gk_decode_constraints.py

resolves neuron_config.gk_decode from the requirements

Model factory gate

model/gpt_oss/factory.py

selects the megakernel perf model when neuron_config.gk_decode

Megakernel decode model

model/gpt_oss/model_mxfp4_perf.py

decode-only weight holders; get_kv_spec, bind_kv_cache, set_kv_cache_buffers, use_stacked_kv_cache

Kernel assembly

model/gpt_oss/mega_decode.py

stacks per-layer weights once; builds attention-control tensors; passes the per-buffer KV; calls the kernel

Kernel entry

functional/gpt_oss_mega_decode.py

wraps gpt_oss_decode_mega_kernel of the selected variant (c128, c512 or c1024), and gpt_oss_spec_decode_mega_kernel (c128spec) for an EAGLE3 verify step

KV allocation

vllm/worker/neuron_model_runner.py, vllm/worker/neuron_model_runner_v2.py

megakernel-gated _initialize_stacked_kv_cache (per-buffer for c128, stacked for c512 and c1024, and stacked for c128 with an EAGLE3 draft on V2) in both model runners; attn-metadata build

Stacked KV cap

vllm/worker/neuron_worker.py

caps the KV budget of the stacked layout of c512 and c1024, and of c128 with an EAGLE3 draft (uint32 DMA offset)

NIXL connector

vllm/kv_connector/neuron_nixl_connector.py

per-buffer registration; DCP read logic; SWA DI truncation

Greedy sampler

functional/argmax.py, functional/sampling.py

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 as W[i] (see the layout list in mega_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 traced decode(); restacking in-graph captured an ~810 MB concatenate per step.

  • KV cache: group_size per-group base-0 buffers, passed as separate K and V args (see “KV-cache layout”). The kernel also accepts the stacked K_cache/V_cache and 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 ReplicaGroup args).

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”).

c128

c512

c1024

nkilib package

models.gpt_oss.c128_giga_kernel

models.gpt_oss.c512_giga_kernel

models.gpt_oss.c1024_giga_kernel

MoE

static, all experts

dynamic (DLoC): skips expert blocks with no routed tokens

dynamic (DLoC), as c512

max_num_seqs per DP rank

8

32

64

Global decode batch (DP16)

128

512

1024

KV cache input

one base-0 buffer for each HMA group

one stacked [group_size, ...] K and V

as c512

Fused embedding and RoPE

supported

not supported (the model raises)

not supported (the model raises)

Layer-0 input (x_first_full)

pre-gathered

layer-0 AllGather in the kernel

as c512

Extra compiler argument

none

--enable-ocp-compliant-scale-computation

as c512

V2 model runner

supported

supported

supported

Speculative decoding

EAGLE3 on the V2 model runner: c128spec verifies the drafts

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=1 when 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