Skip to content

vllm.model_executor.layers.mamba.gdn.qwen_gdn_linear_attn

Inference-only Qwen3-Next/Qwen3.5 model.

Classes:

Functions:

QwenGatedDeltaNetAttention

Bases: GatedDeltaNetAttention

Methods:

_forward_core(mixed_qkv, b, a, core_attn_out)

Core conv1d + recurrent attention (standard path).

Parameters:

  • mixed_qkv

    (Tensor) –

    packed [q, k, v] projection (num_tokens, qkv_dim)

  • b

    (Tensor) –

    beta gating vector (num_tokens, num_heads)

  • a

    (Tensor) –

    alpha gating vector (num_tokens, num_heads)

  • core_attn_out

    (Tensor) –

    Pre-allocated output buffer for attention results.

_forward_core_decode_aiter_fused(qkvz, ba, normed_out, attn_metadata)

Decode-only batch: conv1d + recurrence + gated RMSNorm in one kernel.

_forward_core_decode_non_spec(mixed_qkv, b, a, core_attn_out, attn_metadata)

Non-spec decode fast path: packed recurrent or ReplaySSM kernel.

_forward_core_rocm(qkvz, ba, z_out, core_attn_out)

ROCm AITER fast path: conv1d + recurrent attention from packed qkvz/ba layout.

For decode-only (no spec, no prefill) batches, dispatches directly to _forward_core_decode_aiter. Otherwise unpacks the packed layout and falls through to _forward_core.

Parameters:

  • qkvz

    (Tensor) –

    packed [q, k, v, z] projection (num_tokens, qkvz_dim)

  • ba

    (Tensor) –

    packed [b, a] gating vectors (num_tokens, 2*num_heads)

  • z_out

    (Tensor | None) –

    output buffer for z (num_tokens, num_heads, head_dim); mutated in-place. None if and only if the layer uses the fused AITER kernel (use_aiter_fused_gdn_decode): the gated RMSNorm is then applied too and core_attn_out receives the normed output.

  • core_attn_out

    (Tensor) –

    Pre-allocated output buffer for attention results.

_forward_flashinfer_prefill(q, k, v, g, beta, attn_metadata, *, ssm_state, conv_state, conv_input, output)

FlashInfer chunked prefill of the batch's prefill rows.

Writes the output into output when given and exports the batch's internal prefill checkpoints. Returns the output and the rows' final states.

_forward_triton_prefill_with_checkpoints(q, k, v, g, beta, attn_metadata, *, ssm_state, conv_state, conv_input)

Triton/FLA chunked prefill that exports the batch's internal prefill checkpoints.

The kernel stores each checkpoint's recurrent state in a per-sequence buffer, which the exporter moves into the checkpoint block together with the conv window. Returns the output and the rows' final states.

_output_projection(core_attn_out, z)

Part 3: RMSNormGated + output linear projection.

The RMSNormGated + quant sequence is eligible for fusion by the compilation pass when fuse_norm_quant is enabled.

_replayssm_decode(mixed_qkv, a, b, state_indices, out, attn_metadata)

One ReplaySSM decode step on post-conv mixed_qkv rows.

The per-step delta-rule inputs go to the d/k/g rings, and the checkpoint state is rewritten only on flush. The group's planner layer first fills this step's write_pos/is_flush rows.

_warmup_flashinfer_prefill_variants(q, k, v, g, beta, state)

Compile every FlashInfer GDN prefill kernel a batch can dispatch to.

FlashInfer builds separate kernels for its context-parallel route (few sequences) and its regular route, each with and without checkpoint output. A single sequence under use_cp="auto" takes the CP route wherever the heuristic can pick it; use_cp=False forces the other.

_warmup_prefill_kernels(qkv_or_qkvz, v_dim)

Warm up GDN prefill kernels during V1 profiling.

During V1 profile runs, _forward_core returns early because attn_metadata is None, so the autotuned kernels used by chunk_gated_delta_rule (e.g. solve_tril, chunk_scaled_dot_kkt) are never invoked. After profiling, vLLM allocates KV cache using most of the remaining GPU memory. When the first real inference triggers the autotuner it OOMs because there is not enough memory left for benchmarking.

This method runs minimal forward passes through chunk_gated_delta_rule with small dummy tensors to force autotuning while GPU memory is still plentiful. The autotuner results are cached globally, so only the first layer incurs actual benchmarking cost.

All kernels including chunk_fwd_kernel_o now use a fixed BT = chunk_size (64). A single warmup pass with T = 64 is sufficient to populate the autotuner cache.

The decode path uses gdn_aiter_fused_rearrange_sigmoid_gated_delta_rule which has fixed kernel parameters (no autotuning), so only the prefill (chunked) path needs warming up.

fix_query_key_value_ordering(mixed_qkvz, mixed_ba)

Derives query, key and value tensors from mixed_qkvzba.

forward_cuda(hidden_states, qkvz_input=None)

Forward pass with three parts: 1. Input projection 2. Core attention (custom op) 3. Output projection

qkvz_input, if given, is hidden_states pre-quantized for in_proj_qkvz; in_proj_ba still reads hidden_states.

forward_hip(hidden_states)

ROCm forward using AITER Triton fused projection+attention when available, otherwise falling back to the generic CUDA path.

forward_xpu(hidden_states)

Forward pass with three parts: 1. Input projection 2. Core attention (custom op, SYCL or Triton per --gdn-prefill-backend) 3. Output projection

maybe_disable_tp(quant_config)

Whether to replicate ba_proj instead of TP-sharding it.

Marlin requires output_size_per_partition >= MIN_THREAD_N=64, which the Qwen3.5 non-interleaved [num_v_heads]2 layout violates at TP>=2 (e.g. num_v_heads=64, TP=4 -> 16). Replicating the projection keeps each rank above the Marlin threshold; forward() then slices b/a to the local TP partition. Qwen3-Next's interleaved [num_v_heads2] layout is unaffected and stays TP-sharded.

See https://github.com/vllm-project/vllm/issues/35924

prepare_gdn_attention_core_inputs(mixed_qkvz, mixed_ba, num_tokens)

Derives mixed_qkv, z, b, a from projected qkvz/ba for the GDN custom op.

For gqa_interleaved_layout (Qwen3-Next): unpack the interleaved [ng, (hk + hk + np/nghv + np/nghv)] layout into contiguous qkv. For non-interleaved layout (Qwen3.5): simple split along last dim.

rearrange_mixed_qkv(mixed_qkv)

Split packed qkv into contiguous (1, seq, heads, dim) tensors.

The original code used rearrange(x, "l (h d) -> 1 l h d", d=...) followed by .contiguous() on each tensor. This version flattens all three splits into a single buffer via torch.cat so that torch.compile emits one Triton copy kernel instead of three separate contiguous() calls.

_aiter_flydsl_unsupported_reasons(vllm_config, head_k_dim, head_v_dim)

Every reason the AITER FlyDSL GDN prefill kernels cannot serve this model, or an empty list if they can.

_hip_gdn_unsupported_reasons(vllm_config, head_k_dim, head_v_dim)

Every reason the HIP GDN prefill kernel cannot serve this model, or an empty list if it can.

_log_gdn_backend_decision(vllm_config, requested_backend, active_backend)

Log the GDN prefill backend choice in the attention-selector style.

_resolve_gdn_prefill_backend(vllm_config)

Resolve GDN prefill backend.

The SYCL kernel is chosen when requested in ["sycl", "auto"], the platform is XPU and _xpu_C::gdn_attention is built. Unlike the other backends it is a fused whole-layer op, so selecting it also replaces the conv1d and decode kernels, not just prefill.

FlashInfer's GDN prefill kernel is chosen when: * requested in ["flashinfer", "auto"]; * platform == cuda; * head_k_dim == head_v_dim == 128; * one of the following: - Hopper (SM90); - Blackwell (SM10.x) with head_k_dim == 128, cuda_runtime >= 13; - Blackwell (SM12.x) with head_k_dim == 128, cuda_runtime >= 13.

In-tree CuteDSL GDN prefill kernel is chosen when: * "cutedsl" is requested; (opt-in only) * Blackwell (SM10.x or SM11.x) with head_k_dim == 128, cuda_runtime >= 13;

AITER FlyDSL GDN prefill kernels are chosen when: * "aiter_flydsl" is requested, or "auto" is requested on gfx942/gfx950; * ROCm AITER exposes the optimized VK prefill API and FlyDSL kernels; * the model uses BF16 activations with head_k_dim == head_v_dim == 128.

The HIP GDN prefill kernel is chosen when: * "hip" is requested on RDNA3.5 (gfx1150-gfx1153), or "auto" on gfx1151; * the build includes _rocm_C.rdna35_gdn_chunked; * the model uses BF16 or FP16 activations with head_k_dim == head_v_dim == 128.

qwen_gdn_attention_core(qkv_or_qkvz, b_or_ba, a_or_z_out, core_attn_out, layer_name, use_aiter=False)

Custom op dispatching to _forward_core or _forward_core_rocm.

Handles conv1d + recurrent attention only; input/output projections are performed by the caller.

When use_aiter=False (standard path): qkv_or_qkvz is [q, k, v], b_or_ba is b, a_or_z_out is a (read-only). When use_aiter=True (AITER Triton path, ROCm only): qkv_or_qkvz is [q, k, v, z], b_or_ba is [b, a], a_or_z_out is the z output buffer (mutated in-place).

core_attn_out is always mutated in-place.

qwen_gdn_attention_core_fused_norm_rocm(qkvz, ba, normed_out, layer_name)

conv1d + recurrence + gated RMSNorm into normed_out (mutated in-place).

supports_fused_mtp_decode()

ROCm runs the Triton port; elsewhere the CUDA op must be built.

xpu_gdn_kernel_available()

Whether vllm-xpu-kernels was built with the fused SYCL GDN op.