bitnet-tc

Tensor-core ternary x INT8 GEMM for BitNet b1.58 (W1.58 A8) models on NVIDIA Ampere and newer, loadable through kernels. The reference baseline is cuBLAS bf16 on the same shapes, and end-to-end the stock transformers forward, which it doubles.

Ternary-weight language models store each weight as {-1, 0, +1} in 2 bits, 8x smaller than bf16, but without a kernel that multiplies packed ternary weights directly, inference dequantizes them back to floating point and the size advantage never becomes speed. This kernel runs the product on the INT8 tensor cores against the packed weights themselves: autoregressive decode, where every token reads every weight, gets several times faster, and the LM head, the largest single matrix in the model, gets the most.

One decode step's linear layers as stacked bars: the bf16 tower compresses to a fraction of its height under the ternary kernel

The linear layers of one LLaMA-7B decode step at M = 1, each measured live: 0.76 ms of cuBLAS bf16 collapses to 0.16 ms (4.8x), the LM head alone 9.4x, with the weight footprint down from 636 MB to 80 MB.

Usage

import torch
from kernels import get_kernel

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

N, K = 4096, 4096
W = torch.randint(-1, 2, (N, K), dtype=torch.int8, device="cuda")
w_packed = bitnet.pack_weights(W)                       # [N, K//4] uint8
scale_wt = torch.ones(N, dtype=torch.bfloat16, device="cuda")

x = torch.randn(1, K, dtype=torch.bfloat16, device="cuda")
y = bitnet.bitnet_linear(x, w_packed, scale_wt)         # [1, N] bf16

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark. BitLinear is the drop-in nn.Module; BitLinearKernel is the kernelize layer for the transformers BitNet modules.

API

Symbol Purpose
pack_weights(W) ternary {-1,0,+1} int8 [N,K] -> packed uint8 [N,K//4]
quantize_activation(x) bf16 [..,K] -> (int8, per-row bf16 scale)
bitnet_gemm(x_int8, w_packed, scale_act, scale_wt) INT8 activations x packed ternary weights -> bf16
bitnet_gemv_fused(x_bf16, w_packed, scale_wt) fused quantize + GEMV, M < 16, K <= 16384
bitnet_linear(x_bf16, w_packed, scale_wt) one-shot forward, auto-dispatch
BitLinear(in, out) nn.Module wrapper
BitLinearKernel kernelize layer for transformers BitNet BitLinear / AutoBitLinear

Weight encoding: 2-bit codes {1, 2, 3} packed four per byte (decode is byte - 2), the packing used by Microsoft's bitnet.cpp.

Method

Compute paths dispatch by batch size:

M path tensor cores
1 fused quantize + GEMV (BN=32) dp4a
2-15 fused multi-M GEMV (shared B reads) dp4a
16-383 BM=32 split-K IMMA, INT32 atomic reduction (non-split when N fills the grid) m16n8k32
>= 384 IMMA, 128x128 tile, 3-stage cp.async m16n8k32

Measured

Against cuBLAS bf16 on real LLaMA-2-7B and BitNet b1.58 2B-4T layer shapes:

Shape (M, N, K) Layer speedup vs cuBLAS bf16
1, 32000, 4096 LLaMA-7B LM head ~10x
1, 12288, 4096 LLaMA-7B QKV proj ~6-7x
1, 11008, 4096 LLaMA-7B FFN gate ~6-7x
1, 4096, 4096 LLaMA-7B attn O proj ~2-3x
128, 11008, 4096 mid-batch, large N ~1.1x
512, 11008, 4096 LLaMA-7B FFN gate (prefill) ~1.5x
512, 4096, 4096 square prefill parity (~1.0x)

Top measured throughput is ~150 TFLOPS at M=512 on the large-N shapes, on top of the 8x weight-memory reduction. End-to-end through transformers (microsoft/bitnet-b1.58-2B-4T, all 210 BitLinear layers routed to the kernel): decode 7.5 -> 15.3 tok/s (2.05x) versus the stock bf16 online-quant path, ~2.3x on an L4; wikitext-2 perplexity +0.086% vs stock.

Small-N mid-batch (M=64-256, N=4096) is the remaining weak corner at ~0.6-0.75x of cuBLAS, where the problem is too small to fill the GPU at INT8 throughput.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+ (Ampere, Ada, Hopper).
  • K divisible by 32; fused GEMV path requires K <= 16384.
  • bf16 activations and output; int8 quantized activations; uint8 packed weights.

References

Ma et al., "The Era of 1-bit LLMs" (BitNet b1.58, 2024); Microsoft bitnet.cpp (the shared packing); NVIDIA IMMA tensor-core instructions.

License

Apache-2.0.

Downloads last month
9
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