Command Palette

Search for a command to run...

Hectal
Phase 13: Choosing and Adapting Models
LLM Engineering · Exercise 3 of 6 PreviousNext
Advanced

Quantise to int8 and back

Write round_half_away(x) that rounds to the nearest whole number with exact halves going away from zero (2.5 gives 3, -2.5 gives -3). Python's built-in round rounds halves to even, so do not use it.

Write quantize(ws) returning (qs, scale): scale is the largest absolute weight divided by 127 (use 1.0 when every weight is 0), and each q is round_half_away(w / scale) limited to the range -127 to 127.

Write dequantize(qs, scale) returning each q * scale.

What Check tests

  1. Halves round away from zero
  2. The scale is the largest absolute weight divided by 127
  3. The weights become whole numbers on the grid
  4. Dequantising gives back values within half a step
  5. All-zero weights do not crash

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.