Work out effective bits per weight and model size for group quantization
Why it matters Picking a quantization format for a GPU or phone starts from this arithmetic: smaller groups are more accurate but add metadata overhead.
A "4-bit" model is never exactly 4 bits per weight. Each group of weights also stores a scale and maybe a zero point, and some layers such as embeddings are often kept in 16-bit.
Write quant_size(n_params, n_fp16, bits, group, scale_bits=16, zero_bits=0). n_params is the total parameter count and n_fp16 of them stay in 16-bit. The rest are quantized: effective bits per weight bpw = bits + (scale_bits + zero_bits) / group. Return (bpw, size_gb) where size_gb = ((n_params - n_fp16) * bpw / 8 + n_fp16 * 2) / 1e9.
def quant_size(n_params, n_fp16, bits, group, scale_bits=16, zero_bits=0)Example
7e9 params, 0.5e9 in fp16, 4 bits, group 128, fp16 scales, 4-bit zeros gives (4.15625, 4.376953125).
Understand the idea
Quantized weights carry small labels. Every group of, say, 128 weights shares one scale stored in 16 bits, so each weight pays 16 / 128 = 0.125 extra bits for its share of the label. A 4-bit zero point adds 4 / 128 = 0.03125 more. Worked example: 4-bit weights, group 128, fp16 scale and 4-bit zero: bpw = 4 + 20 / 128 = 4.15625. For 6.5 billion quantized params that is 6.5e9 x 4.15625 / 8 = 3.377 GB, plus 0.5e9 fp16 params at 2 bytes = 1 GB, total about 4.38 GB. Rule: bpw = bits + (scale_bits + zero_bits) / group; size = quantized x bpw / 8 + fp16 x 2 bytes. Group size 32 instead of 128 improves accuracy but raises the overhead to 0.5 bits per weight.