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:
-
RDNAHybridW4A16LinearKernel–Hybrid W4A16 kernel: HIP skinny for decode, Triton for prefill.
Functions:
-
dequantize_w4a16–Dequantize [N, K] int32 uint4 values (natural K order) to [N, K] dtype.
-
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 unless rdna_hybrid_w4a16_prefill_dequant caches a dense prefill
copy.
Source code in vllm/model_executor/kernels/linear/mixed_precision/rdna_hybrid_w4a16.py
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 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 | |
_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, w_dequant=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.
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
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
_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
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.
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
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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |