AlgoViz

Quantization, file formats and runtimes

hard 6 min

Quantization maps weight values to a smaller set of representable levels, typically with scales and sometimes other metadata.

Video lesson · Chapter 1 of 4

A billion weights in half a gigabyte

0:00 / 3:54

Theory

Read or listen first. About 2 min.

What it is

Quantization maps weight values to a smaller set of representable levels, typically with scales and sometimes other metadata. The file format records how to interpret tensors; the execution kernel determines how they are used. The artifact, runtime and hardware must agree.

Why it exists

Estimate payload size first, then measure total device memory, accuracy on sensitive cases and actual token throughput. A compressed file can occupy less disk space without delivering lower latency on a device that lacks a suitable kernel.

Fit weights into a memory budget

Weight tensors → Packing (calibrate and pack). Packing → Quality check (load compatible tensors)
Weight tensors → Packing (calibrate and pack). Packing → Quality check (load compatible tensors)

Inside this chapter

Chapter sources
Float16 vs Int8 vs Int4
Quantization represents some values using fewer bits, often with scales per group. For a billion weights, four bits each require roughly half a billion bytes before scales and other overhead. Storage precision, arithmetic precision and total serving memory are distinct quantities.

Integer quantisation stores each weight as a small integer plus a shared scale (and sometimes a zero point) for a group of weights. The scale maps the integer grid back to real values, so dequantised weight = scale x (integer - zero point). Smaller groups follow the local range more closely and lose less accuracy but store more scales. Storage precision is not arithmetic precision: many kernels dequantise to 16-bit before multiplying.

Example. Seven billion values at 16 bits have a payload of 14 decimal GB. At four bits the payload is 3.5 GB, before scales or other memory.

Another example. A 7B chat model that needs 14 GB at 16-bit drops to about 3.6 GB of weights at 4-bit with group scales, which lets it share a 16 GB GPU with a reasonable KV cache.

Weight payload for seven billion parameters

StorageBits per weightPayloadPlus group scales
Float161614 GBnone needed
Int887 GBsmall per-channel scales
Int4, group 12843.5 GBabout 0.125 extra bits per weight for a 16-bit scale
Decimal gigabytes, weights only. KV cache, activations and runtime buffers come on top.
GGUF Format
GGUF is a file format for model tensors and metadata used by compatible inference software. The extension does not specify one quantization level. Check the model architecture, tensor encodings and supported operators against the chosen runtime before treating a file as deployable.

GGUF bundles tensors with key-value metadata such as the architecture, tokenizer and chat template, so one file can carry everything a compatible runtime needs. Each tensor records its own encoding, so a single file may mix higher-precision embeddings with low-bit feed-forward weights. Two files from the same base can differ in quality and size because they chose different encodings. Inspect the metadata and tensor types instead of trusting the filename.

Example. Inspect two GGUF files made from one base. Compare their tensor encodings and metadata instead of assuming the extension implies four-bit weights.

What a GGUF file declares

The extension names the container. The tensor types inside decide the precision, and they can differ per tensor.
AWQ & GPTQ
AWQ uses activation information to protect important weights during low-bit quantization. GPTQ uses approximate second-order information to compensate for quantization error. Calibration data should resemble deployment inputs. Measure quality and real kernel throughput on the intended hardware for each exported model.

Plain rounding treats every weight the same. AWQ notices that a small share of input channels see large activations and matter most, so it rescales them before quantisation to reduce their relative error. GPTQ quantises a layer one column at a time and, using an approximation of how the layer's output error responds to each weight, nudges the not-yet-quantised weights to compensate. Both use a modest calibration set, so choose prompts that look like real traffic, including digits and code if you serve them.

Example. Calibrate one quantizer on conversational prose and test on invoice digits. Evaluate the actual downstream error even if average reconstruction error is small.

Another example. An invoice extractor quantised with chat-only calibration keeps prose quality but starts swapping digits in totals. Recalibrating with 200 illustrative invoice prompts restores the field accuracy.

Two ways to spend calibration data

Both rely on calibration inputs. If those inputs differ from production, the protected channels may be the wrong ones.
ExLlamaV2
ExLlamaV2 is an inference implementation with specific hardware and quantization support. Kernel compatibility determines whether a theoretical memory saving becomes a usable speedup. Record runtime version, device, batch size, prompt length and decode length when comparing it with another serving stack.

A runtime like ExLlamaV2 ships hand-written GPU kernels for particular quantised encodings, and those kernels are what turn small weights into speed. If the kernel for your encoding or GPU is missing, you may get a fallback path that is slower than 16-bit. Prompt processing is compute-heavy and benefits from large matrix kernels; decoding one token at a time is usually limited by memory bandwidth. Time them separately or a comparison can hide which phase changed.

Example. Time prompt processing and decoding separately on the intended GPU. Repeat with identical weights, context length and batch size before comparing runtimes.

Benchmark record for a runtime comparison

FieldRun ARun BWhy it matters
Runtime and versionpinnedpinnedkernels change between releases
GPU modelsamesamekernel support differs by device
Weights and encodingidenticalidenticalotherwise you compare models
Batch, prompt, decode length1, 2k, 2561, 2k, 256prefill and decode scale differently
Prefill and decode tokens per secondmeasuredmeasuredreport them separately
A template, not results. Fill both columns from your own hardware.
Quantization
Quantizing a generator or vector index can reduce memory in a RAG pipeline. Track where precision is reduced: index quantization may alter candidate recall, while model quantization may alter generation. Test both stages separately on the same questions before attributing a regression.

A RAG system can lose precision in two places: the vector index, where compressed vectors can change which passages are retrieved, and the generator, where quantised weights can change how evidence is used. If both change at once, a drop in answer accuracy cannot be attributed. Measure recall against an uncompressed index on the same questions, then hold retrieval fixed and compare generators.

Example. Run exact retrieval against an uncompressed index, then change only vector precision. Hold the generator fixed when attributing the change in recall.

Another example. A policy search bot moves its index to 8-bit vectors and its generator to 4-bit weights in one release. Answer accuracy falls an illustrative 6 points; the stage-by-stage rerun shows recall at 10 barely moved, so the generator change is the one to revisit.

Change one stage at a time

Compare recall first. Only then swap the generator's precision, with retrieval held fixed.

How it works

Step through the idea, or press Play.

Step 1 of 3

Weight tensors. 1 billion weights

Understand it

Worked example

Fit weights into a memory budget

  1. 1

    1 billion weights

    This estimate counts weight payload only. Scales, metadata, nonquantized layers, cache and activations require additional space.

  2. 2

    4 bits per weight

    Eight bits form one byte. One billion values at four bits occupy 500 million bytes before overhead, compared with two billion bytes at 16 bits.

  3. 3

    Measure held-out error and decode speed

    A smaller artifact needs a compatible kernel to deliver throughput. Check the intended runtime and compare output quality on the deployment workload.

Weight payload before overhead

Bweights=Pq8B_{\rm weights}=\frac{Pq}{8}
For P parameters stored at q bits each, divide by eight to obtain bytes. One billion four-bit parameters occupy 500 million bytes before scales, metadata, unquantized tensors or runtime state.

Follow the handoffs

Estimate payload size first, then measure total device memory, accuracy on sensitive cases and actual token throughput. A compressed file can occupy less disk space without delivering lower latency on a device that lacks a suitable kernel.

Scale and zero point for an 8-bit grid

s=xmax⁡−xmin⁡qmax⁡−qmin⁡,z=round ⁣(qmin⁡−xmin⁡s),q=clamp ⁣(round ⁣(xs)+z),x^=s (q−z)s=\frac{x_{\max}-x_{\min}}{q_{\max}-q_{\min}},\quad z=\mathrm{round}\!\left(q_{\min}-\frac{x_{\min}}{s}\right),\quad q=\mathrm{clamp}\!\left(\mathrm{round}\!\left(\frac{x}{s}\right)+z\right),\quad \hat x=s\,(q-z)
For values in [-1, 3] on 0 to 255: s = 4/255, about 0.0157, and z = 64. The weight 1.0 becomes q = 128 and dequantises to about 1.004.

From weights to the full service budget

ItemSizeCounted in P x q / 8?
1 billion weights at 4 bits500 MByes
Group scales and metadatatens of MBno
Layers kept at 16-bitdepends on modelno
KV cache for live requestsgrows with context and usersno
Activations and runtime buffersdepends on batchno
Illustrative. A 0.5 GB payload fitting in 0.6 GB free memory leaves almost nothing for the rest.

Summary

The whole topic in one minute.

The calculation covers only the encoded weights. Runtime memory also depends on the sequence lengths and number of active requests.

Estimate payload size first, then measure total device memory, accuracy on sensitive cases and actual token throughput. A compressed file can occupy less disk space without delivering lower latency on a device that lacks a suitable kernel.

Sources