Precision and quantization interview questions
7 questions with model answers
- junior 2
- mid 3
- senior 2
7 precision and quantization questions asked in AI engineer, ML engineer and applied AI engineer interviews, each with a model answer you can say out loud in a minute or two: 2 junior, 3 mid-level and 2 senior. The lesson Precision and Quantization: Fewer Bytes per Weight teaches the ideas behind them.Updated
Model answer
Parameters × bits per weight ÷ 8. GPT-2 small's 124,439,808 weights take 497.8 MB in FP32 and 62.2 MB in INT4, an eighth, plus small per-group scales. Decode reads every weight each step, so bytes per weight set both the memory and the bytes each step moves.
Model answer
They split the bits differently: FP16 has 5 exponent and 10 fraction bits, BF16 8 and 7. Exponent bits buy range, fraction bits precision, so FP16 is finer but tops out at 65,504, while BF16 keeps FP32's range. Llama 2 shipped in BF16; teams loading it in FP16 found quality much worse than advertised.
Model answer
Divide each value by the group's largest magnitude, multiply by 127, round; store the integers plus one scale. A value moves at most half a step.
Scroll sideways to see the whole diagram
Start with real weights: the first 12 of the 768 numbers in GPT-2's embedding row for the token " bank".
Text version
- Start with real weights: the first 12 of the 768 numbers in GPT-2's embedding row for the token " bank".
- Find the largest magnitude in the group. Here it is 0.344.
- Map it to the largest INT8 value, 127. The scale is the size of one integer step: 0.344 / 127, about 0.00271.
- Divide every value by the scale and round to the nearest integer. 0.137 becomes 51.
- Store the integers, one byte each, plus the one scale for the whole group.
- To use a weight, multiply back: 51 × 0.00271 is 0.1381, not 0.137. That 0.0011 is the quantization error.
- Rounding moves each value at most half a step, 0.00135 here. A bigger largest value means a bigger step and a bigger error for every value in the group.
Real values: 12 numbers from GPT-2's embedding row for " bank" (lib/labs/data/running-example.json, stored to 3 decimals). Method: absmax quantization, LLM.int8() (Dettmers et al., 2022), §2.1.
One outlier widens the step for its whole group: adding an invented value 20 times our largest GPT-2 weight took the step from 0.00271 to 0.0542, and the largest error on the real weights from 0.00126 to 0.027. Hence per-block scales.
Model answer
Post-training, weight-only quantization. I didn't train the model, PTQ is the common path frameworks offer in a few lines, and weight quantization affects quality more stably.
It buys less than the byte ratio suggests. For Llama 2 13B at 2,048 tokens with a 16-bit cache, INT4 weights take 6.5 GB instead of 26, so 43 conversations fit instead of 32: the cache takes most of the memory. Next lever: shrink the cache. Hands-On LLMs' rule of thumb: stay at 4 bits or above.
Model answer
It's a ceiling, not a forecast. Batch-1 decode is bandwidth-bound, so time should track bytes read, but the KV cache doesn't shrink. Scaling our measured 9.1 ms CPU step by bytes gives about 1.3 ms at INT4: an estimate, not a measurement, with the cache now 13% of the bytes.
And the format needs hardware and kernel support: format conversion costs compute, and LLM.int8() claims only modest matrix-multiply speedups. I'd measure step time on our setup.
Model answer
import numpy as np
def quantize(x, qmin=-128, qmax=127):
lo, hi = min(x.min(), 0.0), max(x.max(), 0.0)
scale = (hi - lo) / (qmax - qmin) or 1.0
zp = int(np.clip(round(qmin - lo / scale),
qmin, qmax))
q = np.round(x / scale) + zp
return np.clip(q, qmin, qmax).astype(np.int8), scale, zp
def dequantize(q, scale, zp):
return (q.astype(np.float32) - zp) * scale
Asymmetric quantization maps the observed range onto -128 to 127 with a scale and a zero point, keeping 0.0 exactly representable. Interviewers check rounding before adding the zero point, clipping, the all-zero case, and outliers: one huge value stretches the scale and crushes the rest, so clip to a percentile or use per-channel scales.
Model answer
Quantization-aware training simulates low-precision rounding during fine-tuning, so accuracy holds better than post-training quantization. Distillation trains a small student on a teacher's soft outputs. Eval mode disables dropout and makes batch norm use running statistics; forgetting it corrupts metrics. Contrastive learning pulls matching pairs together in embedding space and pushes others apart. Verify each on a fixed held-out set and the target hardware: accuracy per slice, latency and memory versus the baseline.