Write triton dequant kernel

From wenyi-li/awesome-agent-kernel-skills · View interactive version

Write triton dequant kernel is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. Guide the agent through implementing a Triton kernel that unpacks and dequantizes a quantized weight tensor (int4 or int8) into fp16 or bf16. This is the standalone building block underneath W4A16 / W

How to use this skill

Skill file

Skill: Write a Triton Dequant Kernel (int4 / int8 → fp16 / bf16)

Purpose

Guide the agent through implementing a Triton kernel that unpacks and dequantizes a quantized weight tensor (int4 or int8) into fp16 or bf16. This is the standalone building block underneath W4A16 / W8A16 schemes (AWQ, GPTQ, SqueezeLLM, bitsandbytes NF4, int8 per-channel). Covers bit-unpacking, per-group scale/zero arithmetic, NF4 codebook lookup, and — critically — when not to write a standalone dequant kernel because the operation should be fused into the matmul instead.


Use this when


Do not use this when


Inputs the agent should gather first

  1. Quantization scheme — AWQ, GPTQ, bitsandbytes NF4, SqueezeLLM, plain int8 per-channel, or a custom variant. Each has a different packing, scale/zero layout, and dequant formula.
  2. Bit width — 3, 4, or 8. Determines packing density (8 int4 / uint32, ~10 int3 / uint32, 4 int8 / uint32) and unpacking arithmetic.
  3. Symmetric vs asymmetric — symmetric (NF4, plain int8): w = scale * q. Asymmetric (AWQ, GPTQ): w = scale * (q - zero).
  4. Group size — typical 128 along K. Scales and zeros are stored at this granularity; the kernel must compute the correct scale/zero index per weight position.
  5. Scale and zero dtypes — scales usually fp16/bf16. Zeros may be stored quantized (AWQ packs zeros as int4) or as fp16. Verify.
  6. Packing convention — the order low-bit values are packed. AWQ: interleaved [0,4,1,5,2,6,3,7] (SIMT throughput). GPTQ default: sequential [0,1,2,3,4,5,6,7]. NF4: 2 nibbles per uint8, high-vs-low-nibble-first depends on library.
  7. GPTQ act-order (desc_act) — if true, the K dimension is permuted; kernel must apply g_idx[k] to look up the group. Missing this produces silently wrong outputs.
  8. NF4 codebook — the 16 fp16 levels. bitsandbytes, HuggingFace, and forks ship slightly different codebooks. Confirm exact values.
  9. Output dtype — fp16 or bf16. Affects overflow clamping.
  10. Output target — full tensor materialization (rare, justify), per-tile-on-demand (common for fused-matmul testing), or streaming.
  11. Storage layout — AWQ [K, N//8] (pack along N) vs GPTQ [K//8, N] (pack along K). Unpacking arithmetic differs.

Required reasoning process

  1. Decide whether the kernel should exist. If the consumer is a fp16 GEMM, propose a fused dequant-GEMM instead and state the bandwidth penalty. Only proceed for a valid use case (debugging baseline, MoE hot-swap, runtime modification, scheme without a fused kernel).
  1. Write out the dequant formula as kernel comments.
  1. Plan the bit-unpacking. For int4 packed 8/uint32: q[i] = (packed >> (4 * unpack_order[i])) & 0xF. unpack_order is scheme-specific — AWQ: [0,4,1,5,2,6,3,7], GPTQ default: [0,1,2,3,4,5,6,7]. Wrong order produces plausible-looking but incorrect output; the model degrades silently rather than crashing.
  1. Plan scale/zero indexing. With group_size G along K, scales have shape [K // G, N]. For weight (k, n), scale is scales[k // G, n]:

```

group_idx = k_offsets // GROUP_SIZE

scale = tl.load(scales_ptr + group_idx[:, None] * N + n_offsets[None, :])

```

For GPTQ act-order, replace k_offsets // GROUP_SIZE with tl.load(g_idx_ptr + k_offsets).

  1. Grid. One program per [BLOCK_K, BLOCK_N] tile. Grid = (cdiv(K, BLOCK_K), cdiv(N, BLOCK_N)). Each program loads packed ints, matching scales and zeros, dequantizes, writes fp16.
  1. Packed tile load. BLOCK_K int4 weights along K = BLOCK_K // 8 uint32 loads. Each uint32 unpacks into 8 fp16 values via shifts and masks on the vector.
  1. Arithmetic precision. For typical scales (1e-3 to 1e-1), fp16 dequant is fine. For outlier blocks or |q - zero| * scale approaching 65504, multiply in fp32 and cast back at store.
  1. NF4 codebook. Pass as a tl.constexpr 16-entry tuple of fp16 values. Triton lacks direct constexpr-table gather; use an inline tl.where chain or constexpr indexing if your Triton version supports it. Confirm values match the source library exactly.
  1. Output layout. Write to [K, N] fp16 (full materialization) or a tile buffer indexed by program coordinates. Do not transpose during dequant — adds smem traffic.
  1. Validate by roundtrip. Quantize an fp16 reference, dequant with the kernel, compare against the reference Python dequant (bitsandbytes.functional.dequantize_4bit, auto_gptq, awq). Mismatches point to packing-order or scale-indexing bugs.

Kernel design rules


Correctness requirements


Performance requirements


Output format

The agent should produce:

  1. Scheme specification. Explicit statement of the scheme (AWQ/GPTQ/NF4/int8), bit width, group size, packing order, and dequant formula as math comments.
  2. Storage layout note. A short comment showing how BLOCK_K * BLOCK_N weights map to packed storage words, including the unpack permutation if non-sequential.
  3. The Triton kernel with @triton.jit: packed weight ptr, scales ptr, zeros ptr (if asymmetric), output ptr, K, N, GROUP_SIZE, strides, and BLOCK_K, BLOCK_N, BIT_WIDTH as tl.constexpr. NF4 LUT as a constexpr tuple when applicable.
  4. A Python launcher computing the grid, validating shapes and dtypes, asserting K % GROUP_SIZE == 0, and dispatching the correct scheme variant.
  5. A correctness test comparing against the reference Python dequant for the scheme, with torch.allclose(atol=scale.max() / 2).
  6. A "when not to use this" note in the launcher docstring or header, pointing to fused-matmul alternatives (Marlin, AWQ-fused-GEMM, GPTQ kernels, ExLlama).

Common failure modes


Review checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.