Python / PyTorch 工业标准参考实现答案 (含测试验证与边界断言)
import torch
# Exact 16 quantile points of NormalFloat4 (NF4) codebook
NF4_CODEBOOK = torch.tensor([
-1.0, -0.6961928009986877, -0.5250730514526367, -0.39491748809814453,
-0.28444138169288635, -0.18477343022823334, -0.09105003625154495, 0.0,
0.07958029955625534, 0.16093020141124725, 0.24611230194568634, 0.33791524171829224,
0.44070982933044434, 0.5626170039176941, 0.7229568362236023, 1.0
])
def quantize_nf4(weights: torch.Tensor, block_size: int = 64) -> tuple[torch.Tensor, torch.Tensor]:
"""Quantizes weight tensor into 4-bit NF4 indices."""
orig_shape = weights.shape
flat_w = weights.flatten()
N = flat_w.numel()
codebook = NF4_CODEBOOK.to(weights.device)
# Pad to multiple of block_size
pad_len = (block_size - (N % block_size)) % block_size
if pad_len > 0:
flat_w = torch.cat([flat_w, torch.zeros(pad_len, device=weights.device)])
blocks = flat_w.view(-1, block_size)
# Compute per-block absolute maximum scale
scales = torch.max(torch.abs(blocks), dim=-1, keepdim=True).values.clamp(min=1e-8)
norm_blocks = blocks / scales
# Find closest centroid in NF4 codebook: (num_blocks, block_size, 16)
diff = torch.abs(norm_blocks.unsqueeze(-1) - codebook.view(1, 1, 16))
quantized_indices = torch.argmin(diff, dim=-1)
return quantized_indices.view(-1)[:N].reshape(orig_shape), scales.squeeze(-1)
# --- Verification & Test Case ---
if __name__ == "__main__":
w = torch.randn(128)
q_idx, scales = quantize_nf4(w, block_size=64)
assert q_idx.max() <= 15 and q_idx.min() >= 0
print("NF4 quantization executed successfully! Sample indices:", q_idx[:6].tolist())