Skip to content

Four-bit weights: a layout of hk's own, a dequantize that was the bottleneck, and AWQ in vLLM

Archived: a dated record

This chapter is the state of the work when it was written: its API, numbers and baselines are of that time. Current documentation starts at the docs home; the numbers the project quotes are on the scorecard.

Part of the development record, in the order the work happened. Paths, API names and numbers are those of the time.

A 4-bit model reads a quarter of the bytes a bf16 one does, and at decode those bytes are the whole cost. This chapter covers four pieces: - hk.load_w4, which dequantizes 4-bit codes into a register tile; - the layout it reads, written once from an AWQ or GPTQ checkpoint; - a W4A16 decode GEMM, hk.ops.w4a16; - HK_VLLM_INT4=1, which routes vLLM's AWQ layers to it.

The layout, and the repack

AWQ packs eight output columns to an int32, in its own nibble order, along N. GPTQ packs eight input rows, along K. Neither puts a row of W where a lane of a row-layout tile wants it. So hk writes its own layout once, at load (repack_awq, repack_gptq): - codes eight to an int32 in K order; - per group, a scale s and the zero point times the scale, zs, so a code dequantizes as q * s - zs, one FMA.

The first version kept a row's words together, row-major. A lane's 16 columns of a base tile were then two words, one 8-byte load, and one load instruction touched 16 rows 2 KB apart, 8 bytes of each.

Where the time went

The decode GEMM is chapter 9's, with load_w4 in place of the bf16 load: - one 16-row block of W per workgroup; - K split across 16 warps; - partial sums meeting in LDS.

Each change below was measured at Qwen3-8B's four linear shapes and one token, bf16, all on the same GPU, in GB/s of codes streamed:

version qkv o gate_up down
row-major codes, round-to-nearest-even bf16 271 231 328 270
codes interleaved [N/16][K/32][16][4] 310 265 339 295
plus: one add and one v_perm per pair, each half converting its own 8 columns 374 325 433 401
two 16-row blocks a workgroup on 8 warps instead 385 322 462 380

The last row is a marginal call. On this GPU the two-block schedule is within ±3% at qkv and o, +7% at gate_up and -5% at down. In the fp16 A/B below, run in another pod on another GPU, it was 5-22% faster at the 4096-deep layers. It is used at K ≤ 4096 (SHALLOW_K), and down keeps one block.

The interleave puts a block's 16 rows of one 32-column chunk side by side. One 16-byte load per lane then reads 256 contiguous bytes. It helped less than expected, so access was not the main cost.

The dequantize was. In the first version each code cost a mask, a conversion, an FMA, and a four-instruction round-to-nearest-even into bf16. A 16-bit row tile holds each row twice, in lanes l and l+16, so all of that was done twice. Now: - the codes are spread one to a byte by two masks and converted a byte at a time (v_cvt_f32_ubyte*); - bf16 is rounded half up, one add, and a pair is packed by one v_perm; fp16 takes one v_cvt_pkrtz per pair; - each half of the wave converts its own eight columns and takes the other eight from its partner with v_permlanex16.

Still not memory-bound. At one token the WMMA computes 16 token rows, 15 of them padding. The next lever is a dot-product path for M ≤ 4, which is what the skinny GEMMs below are.

Against what vLLM runs

tools/hk-bench/w4a16_ab.py measures every arm against the dequantized product before timing it, with weights rotated past the Infinity Cache. The arms: - awq is ops.awq_gemm, what vLLM's AWQ method calls below 256 tokens. On ROCm that is the Triton kernel: the C++ one is not built (_C has no awq_gemm), so VLLM_USE_TRITON_AWQ=1. The Triton kernel takes fp16 only; given bf16 it fails with out_dtype=bfloat16 is unsupported. An AWQ model is therefore served in fp16, and the table is in fp16. - rdna3 is gptq_gemm_rdna3, vLLM's HIP W4A16 kernel for gfx1100, which its GPTQ path selects.

hk against the Triton AWQ kernel, fp16, µs per call:

shape 1 token 4 16 32 64
qkv 6144 x 4096 70.4 / 31.0 (2.27x) 70.9 / 31.2 (2.27x) 82.6 / 31.7 (2.61x) 75.2 / 50.7 (1.48x) 137.8 / 74.9 (1.84x)
o 4096 x 4096 53.8 / 24.4 (2.20x) 53.9 / 24.2 (2.23x) 54.8 / 24.0 (2.28x) 56.6 / 38.3 (1.48x) 95.6 / 53.3 (1.79x)
gate_up 24576 x 4096 234.0 / 97.9 (2.39x) 243.9 / 102.0 (2.39x) 248.1 / 104.7 (2.37x) 264.1 / 184.9 (1.43x) 474.5 / 261.6 (1.81x)
down 4096 x 12288 131.3 / 58.8 (2.23x) 132.1 / 59.4 (2.22x) 135.3 / 60.8 (2.23x) 138.3 / 98.5 (1.40x) 243.4 / 163.9 (1.49x)

Against gptq_gemm_rdna3 the result splits: - Up to 4 tokens it wins, a skinny GEMV without WMMA's 15 padding rows. bf16: hk 0.69-0.84x at 1-4 tokens before the two-block schedule. fp16: 0.81x at gate_up's one token, 0.90x at down's four. - From 8 tokens hk wins: 1.13-2.25x bf16, 1.30-1.82x fp16 at 64. - Its fp16 output misses a 3e-2 check by a hair (0.03-0.04 of the output's magnitude), so most fp16 rows have no number for it.

hk routes only AWQ layers, whose kernel in vLLM is the Triton one. For a GPTQ model, the same table says to leave 1-4 tokens to gptq_gemm_rdna3.

In vLLM

HK_VLLM_INT4=1 wraps AutoAWQLinearMethod: - after vLLM's own weight processing, the layer also gets hk's layout, a second copy of the codes; - apply becomes one call of the opaque op hk_vllm::w4a16. Inside it, batches of up to 64 tokens take hk's kernel, and the rest take vLLM's own path on vLLM's own tensors.

It changes what the compiled graphs call, so it uses a compile-cache root of its own, hk-int4.

Qwen/Qwen3-8B-AWQ, fp16, vLLM 0.31.0 under its defaults, 72 tokens, best of two rounds, VLLM_USE_TRITON_AWQ=1 in both arms. Greedy tokens are identical to stock's at every shape. The captured graphs called hk's kernel 4464 times and vLLM's 0 times; the uncaptured fallbacks are prefill.

ctx x batch stock ms int4 step: stock ms int4
1024 x 1 4965 1305 (3.80x) 66.37 14.88 (4.46x)
1024 x 4 5710 2085 (2.74x) 66.49 15.34 (4.33x)
1024 x 16 9252 5572 (1.66x) 70.08 18.93 (3.70x)
1024 x 32 14421 11063 (1.30x) 82.48 36.37 (2.27x)
4096 x 1 6706 3043 (2.20x) 78.34 26.83 (2.92x)
4096 x 4 10408 6683 (1.56x) 79.72 27.76 (2.87x)
4096 x 16 28885 25198 (1.15x) 91.58 39.11 (2.34x)
4096 x 32 54765 51325 (1.07x) 136.04 86.39 (1.57x)

Prefill is unchanged (1.00x) because it stays on vLLM's path. A stock AWQ decode step of 66 ms at one request, against 24.5 ms for the bf16 model in chapter 20, is the Triton AWQ kernel, which runs this card's 4-bit model slower than its 16-bit one.

Qwen/Qwen3-14B-AWQ, the same setup. Its layers are 5120 and 17408 deep, and every one is taken. Greedy tokens are identical at every shape; the captured graphs called hk's kernel 3360 times and vLLM's 0 times.

ctx x batch stock ms int4 step: stock ms int4
1024 x 1 8857 2001 (4.43x) 118.06 21.40 (5.52x)
1024 x 16 16853 9953 (1.69x) 122.00 25.88 (4.71x)
4096 x 1 11497 4597 (2.50x) 131.95 34.76 (3.80x)
4096 x 16 50210 43465 (1.16x) 145.80 50.82 (2.87x)

The larger model gains more: its layers are wider, so more of each step is the 4-bit GEMM.

What is left

  • A dot-product path for 1-4 tokens. It is where gptq_gemm_rdna3 still wins, and where the WMMA computes 15 rows of padding.
  • The GEMM. The plan's second kernel dequantizes into LDS and runs bf16 WMMA. It is not written: above 64 tokens the layer stays on vLLM's dequantize-and-matmul.
  • GPTQ models. repack_gptq is written and tested, but no vLLM method is wrapped for it.