Quantization Hardware: Tensor Cores & Custom Kernels
How quantization maps to hardware: NVIDIA tensor core data types, CUTLASS INT8 kernels, CPU SIMD for quantized inference, Apple Neural Engine, and the hardware-software co-design for efficient quantized computation.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- NVIDIA Tensor Core Architecture
- INT8 Tensor Cores (Turing/Ampere)
- FP8 Tensor Cores (Hopper)
- 2:4 Structured Sparsity (Ampere)
- CPU Quantized Inference (SIMD)
- Custom Kernels for Mixed Precision
- Apple & Mobile Accelerators
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Explain how tensor cores execute quantized matrix multiply.
- Describe the data layout requirements for INT8/FP8 tensor cores.
- Explain 2:4 structured sparsity hardware support.
- Describe CPU SIMD optimizations for quantized models (AVX2, ARM NEON).
- Choose quantization format based on target hardware.
Notation
- MMA — matrix multiply-accumulate (tensor core operation)
- TOPS — tera operations per second
- SIMD — single instruction, multiple data
Core Intuition
Quantization only provides speedup if the hardware can exploit the reduced precision. A GPU that can only do FP16 matmul sees no benefit from INT4 weights stored in memory (still needs to dequantize). Real speedup requires: (1) native low-precision compute units (tensor cores for INT8/FP8), or (2) SIMD parallelism where more operations fit per instruction (CPU vectorization). Hardware-software co-design determines which quantization methods are practical.
Tensor Core Computation
NVIDIA Tensor Core Architecture
Tensor cores perform small matrix multiply-accumulate operations in a single clock cycle:
where are in low precision and are in higher precision (accumulator).
Supported configurations (H100):
| Input A | Input B | Accumulator | TFLOPS/TOPS |
|---|---|---|---|
| FP16 | FP16 | FP32 | 989 |
| BF16 | BF16 | FP32 | 989 |
| FP8 E4M3 | FP8 E4M3 | FP32 | 1979 |
| FP8 E5M2 | FP8 E4M3 | FP32 | 1979 |
| INT8 | INT8 | INT32 | 1979 |
Key observation: INT8 and FP8 deliver exactly 2x the throughput of FP16/BF16.
INT8 Tensor Cores (Turing/Ampere)
Operation: .
Tile size (A100): (M=16, N=8, K=32 per tensor core MMA).
Workflow for W8A8:
- Quantize weights offline (INT8 per-channel).
- Quantize activations online (INT8 per-token).
- INT8 matmul on tensor cores → INT32 accumulator.
- Rescale INT32 output by → FP16/BF16 output.
Data layout requirement: Column-major for B, row-major for A. Incorrect layout → falls back to CUDA cores (10x slower).
Frameworks: CUTLASS, cuBLAS INT8 GEMM, TensorRT-LLM.
FP8 Tensor Cores (Hopper)
Advantage over INT8:
- No explicit quantize/dequantize step — just scale and cast.
- Same throughput (1979 TOPS on H100).
- Simpler kernel implementation.
- Native support for mixed E4M3/E5M2 inputs.
NVIDIA Transformer Engine: Automatic FP8 management:
- Tracks per-tensor scale history.
- Applies delayed scaling.
- Handles overflow detection and scale correction.
- Transparent to the user (just set
fp8_autocast).
2:4 Structured Sparsity (Ampere)
Hardware support (A100+): Specialized sparse tensor core format.
Pattern: Exactly 2 zeros per group of 4 elements (50% sparsity):
possible patterns per group.
Storage: Compressed format stores only the 2 non-zero values + a 2-bit index encoding which positions are non-zero.
Speedup: 2x over dense at same precision (tensor core skips zero multiplications).
Combined with quantization: INT8 + 2:4 sparsity = 4x effective speedup over FP16 dense.
CPU Quantized Inference (SIMD)
AVX2 (x86-64): 256-bit registers → 32 INT8 operations per instruction.
AVX-512 VNNI: Dedicated dot-product instruction for INT8:
vpdpbusd: Multiply 64 pairs of INT8 values, accumulate to INT32.- 4x throughput over scalar.
ARM NEON (Apple M-series, mobile): 128-bit registers → 16 INT8 per instruction.
sdot/udot: Dot product of 4xINT8 → INT32.
llama.cpp optimization: Hand-tuned SIMD kernels for each quantization type:
- Q4_K: Dequantize 32 weights at once, multiply with FP32 activations.
- Q8_0: Direct INT8×INT8 with VNNI.
- Achieves 60-90% of theoretical peak throughput.
Custom Kernels for Mixed Precision
W4A16 dequantization kernel (vLLM/Marlin):
- Load INT4 weights (packed: 2 per byte).
- Dequantize to FP16 in registers.
- FP16 tensor core matmul.
- Repeat for next tile.
Marlin kernel (optimized W4A16): Achieves near-optimal throughput by:
- Overlapping dequantization with matmul (software pipelining).
- Optimal shared memory layout.
- 3.6x speedup over naive W4A16 on A100.
Key insight: Even without native INT4 tensor cores, W4A16 provides speedup through reduced memory bandwidth (weight loading is the bottleneck at small batch sizes).
Apple & Mobile Accelerators
Apple Neural Engine (ANE):
- Native INT8 support with per-channel quantization.
- CoreML framework handles quantization automatically.
- 15.8 TOPS (M1), 38 TOPS (M4).
Qualcomm Hexagon DSP:
- INT8 and INT4 support.
- Used for on-device LLM inference (Snapdragon 8 Gen 3).
Google TPU:
- INT8 matrix units.
- BF16 native support (Google invented BF16).
Common Pitfalls
Pitfall 1. Assuming INT4 weights provide automatic 2x speedup on GPU. Without native INT4 tensor cores (available on Blackwell, not Hopper), W4A16 relies on dequantization + FP16 matmul. Speedup is from memory bandwidth, not compute.
Pitfall 2. Incorrect data layout for tensor cores. If weight matrix isn't in the required column-major/interleaved format, CUDA falls back to non-tensor-core path. Always verify with profiler.
Pitfall 3. Ignoring CPU quantization for small models. For models under 7B, CPU inference with optimized SIMD kernels (llama.cpp) can be faster than GPU with kernel launch overhead.
Summary
- GPU tensor cores: INT8/FP8 at 2x throughput over FP16 (A100/H100).
- 2:4 sparsity: Additional 2x with structured pruning on Ampere+.
- CPU SIMD: AVX2/VNNI/NEON for efficient quantized inference without GPU.
- Custom kernels (Marlin, CUTLASS) bridge the gap for mixed-precision formats.
- Hardware determines viable formats: Match quantization to target hardware.
- W4A16: Memory-bound speedup (bandwidth). W8A8: Compute-bound speedup (tensor cores).
Exercises
Exercise 1. Compute the peak INT8 throughput of an A100 in TOPS. Compare to FP16 TFLOPS. What's the theoretical speedup for a compute-bound workload?
Exercise 2. For 2:4 sparsity on a matrix: compute the compressed size (non-zeros + indices) vs dense storage.
Exercise 3. Design an AVX-512 loop for INT8 matmul of a 1024x1024 matrix. Estimate throughput on a modern CPU (assume 2GHz, 2 VNNI units).
Exercise 4. For the Marlin W4A16 kernel: estimate the arithmetic intensity (FLOPs/byte) and determine if it's compute-bound or memory-bound on an A100.
Exercise 5. An edge device has: 8 TOPS INT8, 2 TFLOPS FP16, 50 GB/s bandwidth. For a 3B model: should you use W8A8, W4A16, or W4A8? Justify with roofline analysis.