Skip to content

vllm.model_executor.kernels.linear.mixed_precision.rdna_hybrid_w4a16

Hybrid W4A16 kernel: Triton for prefill, HIP skinny for decode.

Routes based on the activation shape

M <= _max_skinny_batch_size() and K*M <= MEDIUM_SKINNY_LIMIT_ELEMENTS: HIP skinny GEMM (wvSplitK_int4_g) M >= MIN_DENSE_M: dense GEMM on a cached dequantized copy, when KernelConfig.rdna_hybrid_w4a16_prefill_dequant is "enable" otherwise: Triton W4A16 fused dequant GEMM

Stores the weights ONCE as int8 [N, K//2] (ExLlama shuffle packed). Both paths read this single buffer: the HIP skinny kernel uses it directly, and the triton kernel reinterprets it as int32 [N, K//8] via a view (and transposes tiles in-register). No dual weight storage unless rdna_hybrid_w4a16_prefill_dequant caches the dense prefill copy.

Classes:

Functions:

RDNAHybridW4A16LinearKernel

Bases: MPLinearKernel

Hybrid W4A16 kernel: HIP skinny for decode, Triton for prefill.

Stores the weights once as int8 [N, K//2] (ExLlama shuffle packed). The HIP skinny kernel reads it directly; the triton kernel reinterprets the same buffer as int32 [N, K//8] via a view, so there is no dual weight storage unless rdna_hybrid_w4a16_prefill_dequant caches a dense prefill copy.

_act_pad_bytes(row_bytes)

Bytes to add to an activation row stride to move it off the cliff.

_gfx1151_bf16_decode_tile(M, N, K, group_size)

bf16 (BLOCK_M, BLOCK_N, BLOCK_K, num_warps) for 9 <= M <= 128 (decode batches, speculative-decoding verify) where it beats the ladder below; None keeps the ladder.

Swept on gfx1151 over 25 Qwen3, Qwen3.8 and SmolLM2 shapes at group sizes 32, 64 and 128. At 33..64 the ladder's (64, 64, 32, 4) falls to about 30 GB/s on wide-N layers.

_int4_pair_to_fp16x2(x)

Unpack two packed int4 nibbles into a uint32 holding two fp16 lanes, each equal to 1024 + nibble, with one v_and_or_b32 ((x & 0x000F000F) | 0x64006400).

OR-ing a 4-bit nibble into the low mantissa of fp16 1024.0 (0x6400) bitcasts to exactly 1024+n. Doing it on a full 32-bit lane dequants two nibbles per instruction, vs the scalar v_and_b16 + v_or_b16 pair Triton emits from the elementwise form.

_pad_activation_rows(x_2d)

Copy x_2d into a row-padded buffer when its row stride is on the cliff.

The packed weight is padded once at load time, but activations are produced fresh every step, so this materialises a padded copy.

_rdna_hybrid_w4a16_apply_impl(x_2d, w_q, w_s, w_zp, bias, cu_count, group_size, w_dequant=None, w_scale_zp_carrier=None)

Dispatch between skinny GEMM and Triton based on batch size M.

w_zp is [N//8, K//G] int32 for asymmetric layers, None for symmetric. w_dequant is the optional dense [N, K] copy used for prefill. w_scale_zp_carrier is the optional pack_scale_zp_carrier output for the packed fp16 Triton path.

_select_skinny_gfx1151_config(M, N, K, group_size, dtype, has_zp=False, has_scale_zp_carrier=False)

Return (BLOCK_M, BLOCK_N, BLOCK_K, num_warps, num_stages, packed_dequant) for gfx1151.

The packed fp16 dequant and the scalar bf16 dequant want different tiles, so selection is dtype-aware. bf16 uses the scalar-tuned ladder except where gfx1151 decode tiles provide a better configuration.

num_stages None means "leave Triton's default pipeline depth alone". Re-run benchmarks/kernels/benchmark_rdna_hybrid_w4a16_gemm.py after edits.

_triton_w4a16_skinny_fmt_kernel(a_ptr, b_ptr, scales_ptr, zp_ptr, scale_zp_carrier_ptr, c_ptr, M, N, K, K8, num_groups, stride_sn, stride_bn, stride_am, group_size, ZP_BIAS, HAS_ZP, PACKED_DEQUANT, HAS_SCALE_ZP_CARRIER, BLOCK_M, BLOCK_N, BLOCK_K)

Fused W4A16 GEMM reading weights from skinny format [N, K//8].

B is stored as [N, K//8] int32 using ExLlama shuffle packing: each int32 packs 8 K-values with interleave [0,2,4,6,1,3,5,7]: packed = val[0] | (val[2]<<4) | (val[4]<<8) | (val[6]<<12) | (val[1]<<16) | (val[3]<<20) | (val[5]<<24) | (val[7]<<28)

Scales are [N, K//G] (skinny layout, NOT transposed). When HAS_ZP=True, zp_ptr holds [N//8, K//G] int32 with row n's raw zero-point at word[n//8] bits 4*(n%8), and dequant is (nibble - zp_raw) * scale. When HAS_ZP=False, only the constant ZP_BIAS is subtracted (symmetric). With HAS_SCALE_ZP_CARRIER (asymmetric, PACKED_DEQUANT only), scale and bias_eff = -zp * scale come from one int32 per (n, group) in scale_zp_carrier_ptr instead (see pack_scale_zp_carrier), and scales_ptr and zp_ptr are not read.

With PACKED_DEQUANT the nibble arrives as b_raw = 1024 + nibble (the magic-constant unpack), so the subtrahend absorbs the 1024: the arithmetic is unchanged and every intermediate stays exact, since fp16 represents every integer below 2048.

_weight_pad_bytes(row_bytes)

Bytes to add to a packed weight row stride to move it off the cliff.

Only strides that are a multiple of _WEIGHT_CLIFF_BYTES are moved, and only by one cache line; padding a stride that is already off the cliff costs memory and measures slower.

dequantize_w4a16(unpacked, scales, zp, group_size, zp_bias, dtype)

Dequantize [N, K] int32 uint4 values (natural K order) to [N, K] dtype.

scales and zp are [N, K//G]; without zp the constant zp_bias is subtracted.

pack_int4_exllama_shuffle(w_uint4)

Pack uint4 values into ExLlama shuffle format: [N, K] -> [N, K//8] int32.

Each int32 packs 8 K-values with interleave order [0,2,4,6,1,3,5,7].

pack_scale_zp_carrier(scales, zp)

Fold fp16 per-group scales and packed zero-points into one int32 each.

Lets the packed fp16 Triton prefill dequant replace its scale and zero-point loads with one. The low 16 bits hold the scale and the high 16 bits bias_eff = -zp * scale, so dequant is a single FMA on the magic-constant unpack.

Parameters:

  • scales

    (Tensor) –

    [N, K//G] fp16 per-group scales.

  • zp

    (Tensor) –

    [N//8, K//G] int32 zero-points, row n at word[n//8] bits 4*(n%8).

Returns:

  • Tensor –

    [N, K//G] int32 carrier.

pack_skinny_int4(unpacked)

Pack [N, K] uint4 into the skinny weight layout the kernels consume.

ExLlama shuffle to [N, K//8] int32, viewed as int8 [N, K//2]. On gfx1151 the row stride is nudged off the cliff (see _weight_pad_bytes).

triton_w4a16_skinny_fmt_gemm(a, b_q, scales, group_size, zp_bias=8, zp=None, scale_zp_carrier=None)

Fused W4A16 GEMM reading from skinny weight format [N, K//8].

Parameters:

  • a

    (Tensor) –

    Activation matrix [M, K], float16 or bfloat16.

  • b_q

    (Tensor) –

    Packed weight matrix [N, K//8], int32 (ExLlama shuffle).

  • scales

    (Tensor) –

    Per-group scales [N, K//G], same dtype as a.

  • group_size

    (int) –

    Quantization group size (resolved from -1 to K by caller).

  • zp_bias

    (int, default: 8 ) –

    Constant zero bias (default 8 for unsigned int4).

  • zp

    (Tensor | None, default: None ) –

    Raw per-group zero-points [N//8, K//G] int32, row n at word[n//8] bits 4*(n%8) (asymmetric). When provided, dequant is (nibble - zp_raw) * scale.

  • scale_zp_carrier

    (Tensor | None, default: None ) –

    fp16 scale/bias carrier from pack_scale_zp_carrier (asymmetric only). Read by the packed fp16 dequant in place of scales and zp, and lets it be selected at a smaller M. Ignored otherwise.

Returns:

  • Tensor –

    Output matrix [M, N], same dtype as a.