bitnet-qat-train

Fused quantization-aware training for BitNet b1.58 (W1.58 A8) on NVIDIA Ampere and newer, loadable through kernels. Training counterpart of bitnet-tc (CUDA inference) and bitnet-cpu (CPU inference); all three share the same 2-bit packing. The reference baseline is the eager fake-quant recipe, tracked to |Δloss| ≤ 0.0019 over 300 identical steps.

Quantization-aware training for ternary models is normally simulated: the quantized weight is materialized as a full bf16 tensor every step, the matmul runs in bf16, and autograd saves the full-precision operands. This kernel runs the real thing: ternarization, scaling, and 2-bit packing are one fused pass, the forward runs on INT8 tensor cores, and autograd saves INT8 activations and 2-bit weights, so the training loop is faster and lighter while following the same recipe to the same loss.

Two training curves track each other while step-time and memory bars show the fused kernel at a fraction of the eager recipe

The fused kernel and the eager fake-quant recipe trained on identical data and seeds: maximum loss gap 0.0078 over 240 steps, while the fused step runs 4.8x faster (7 vs 32 ms) at 2.2x less peak memory (660 vs 1,475 MB) on a 2560-to-6912 stack.

Usage

import torch
from kernels import get_kernel

qat = get_kernel("phanerozoic/bitnet-qat-train", version=1, trust_remote_code=True)

layer = qat.QATBitLinear(2560, 6912, device="cuda")   # master weights, bf16
x = torch.randn(8, 512, 2560, dtype=torch.bfloat16, device="cuda")
y = layer(x)                                          # [8, 512, 6912] bf16
y.sum().backward()                                    # STE gradients on layer.weight

layer = qat.QATBitLinear.from_linear(dense_linear)
w_packed, scale_wt = layer.export_inference()         # -> bitnet-tc / bitnet-cpu

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark.

API

Symbol Purpose
QATBitLinear(in, out) QAT linear layer, full-precision master weights
QATBitLinear.from_linear(lin) wrap an existing nn.Linear
QATBitLinear.export_inference() -> (w_packed, scale_wt) for the inference kernels
qat_bitnet_linear(x, W) functional autograd forward
ternarize_pack(W) fused absmean + RoundClip + 2-bit pack -> (w_packed, gamma)
quantize_activation(x) per-token absmax INT8
bitnet_linear_inference(x, w_packed, scale_wt) no-autograd inference forward on packed weights

Method

Forward computes quant(x) @ ternarize(W)^T * s_x * gamma with INT32 accumulation, which is exact integer arithmetic; the eager recipe's bf16 matmul of the same quantized values is the less precise side. Backward is the straight-through estimator: dX = dY @ W_hat, dW = dY^T @ x_hat, with the operands reconstructed transiently from the saved 2-bit and INT8 state, so saved weight state is 8x smaller and saved activation state 2x smaller than the eager recipe. gamma's dependence on W is treated as constant, matching the standard recipe.

Measured

On an L4 (sm_89), torch 2.12, against the eager fake-quant recipe:

  • Gradients: with references built from the kernel's own quantized operands, dX and dW match bitwise across 12 shape/dtype cases; the forward residual is bf16 output rounding (< 7e-3 maximum relative).
  • Training: a 4-layer transformer LM trained 300 steps on identical batches and seeds tracks the eager recipe to |Δloss| ≤ 0.0019 over the first 50 steps and 0.0006 at termination, at 1.43x the eager wall-clock.
  • Memory: peak allocation through an 8-layer stack (2560 ↔ 6912, batch 4x512, forward+backward) is 1.64x lower (1887 → 1151 MB).
  • Step time: forward+backward on (2560 → 6912), batch 8x512, is 1.76x faster (15.48 → 8.80 ms median of 20).
  • Ternarization is bitwise deterministic across repeated invocations.
  • Exported weights produce bitwise-identical outputs through the phanerozoic/bitnet-tc inference kernel loaded in the same process.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+ (Ampere, Ada, Hopper).
  • K divisible by 32; bf16 activations; bf16 or f32 master weights.

References

Ma et al., "The Era of 1-bit LLMs" (BitNet b1.58, 2024); straight-through estimation (Bengio et al., 2013); the shared 2-bit packing of phanerozoic/bitnet-tc and Microsoft's bitnet.cpp.

License

Apache-2.0.

Downloads last month
1
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64
Kernel Builder
2c516fc