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) 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.
Classes:
-
RDNAHybridW4A16LinearKernel–Hybrid W4A16 kernel: HIP skinny for decode, Triton for prefill.
Functions:
-
pack_int4_exllama_shuffle–Pack uint4 values into ExLlama shuffle format: [N, K] -> [N, K//8] int32.
-
pack_skinny_int4–Pack [N, K] uint4 into the skinny weight layout the kernels consume.
-
triton_w4a16_skinny_fmt_gemm–Fused W4A16 GEMM reading from skinny weight format [N, K//8].
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.
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 | |
_act_pad_bytes(row_bytes)
¶
Bytes to add to an activation row stride to move it off the cliff.
_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.
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
_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.
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
_rdna_hybrid_w4a16_apply_impl(x_2d, w_q, w_s, w_zp, bias, cu_count, group_size)
¶
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.
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
_select_skinny_gfx1151_config(M, N, K, group_size, dtype, has_zp=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 always uses the scalar-tuned ladder.
num_stages None means "leave Triton's default pipeline depth alone". Re-run benchmarks/kernels/benchmark_rdna_hybrid_w4a16_gemm.py after edits.
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
_triton_w4a16_skinny_fmt_kernel(a_ptr, b_ptr, scales_ptr, zp_ptr, c_ptr, M, N, K, K8, num_groups, stride_sn, stride_bn, stride_am, group_size, ZP_BIAS, HAS_ZP, PACKED_DEQUANT, 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 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.
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
_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.
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
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].
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
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).
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
triton_w4a16_skinny_fmt_gemm(a, b_q, scales, group_size, zp_bias=8, zp=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.
Returns:
-
Tensor–Output matrix [M, N], same dtype as a.
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |