liveBook · Manning

Quantization

As explained in Deep Learning with R, Third Edition

Model quantization is the conversion of model computations or parameters to lower-precision numerical representations to reduce resource requirements.

Quantization lowers the numerical precision used for inference. A trained model normally has float32 weights; converting them to int8 can make an inference-only model four times smaller while keeping accuracy close to the original. That matters when memory, power, or latency is limited. section 6.3.2 section 18.3.6

Quantize a trained Keras model

With an existing model, use `model |> quantize_weights("int8")`, then obtain predictions with `model |> predict(...)`. This is the practical route; implementing the conversion by hand is usually unnecessary. section 18.3.6 section 6.3.2

Why not simply cast every value to int8? Small values could round to zero. Instead, an abs-max scheme scales each input tensor to use the available −127 to 127 range, rounds and casts it, and performs the matrix multiplication in int8. Casting the result back to float32 and dividing by the input scales approximately restores the original result. The remaining difference comes from rounding. section 18.3.6

abs_max_quantize <- function(value) {
  abs_max <- op_max(op_abs(value), keepdims = TRUE)  #1
  scale <- op_divide(127, abs_max + 1e-7)  #2
  scaled_value <- value * scale  #3
  scaled_value <- op_clip(op_round(scaled_value), -127, 127)  #4
  scaled_value <- op_cast(scaled_value, dtype = "int8")  #5
  list(scaled_value, scale)
}

.[int_x, x_scale] <- abs_max_quantize(x)
.[int_kernel, kernel_scale] <- abs_max_quantize(kernel)

The R function calculates an absolute maximum, scales and clips values, then casts them to int8. section 18.3.6

int_y <- op_matmul(int_x, int_kernel)
y <- op_cast(int_y, dtype = "float32") / (x_scale * kernel_scale)

The quantized matrix multiplication is cast back to float32 and divided by the two scales. section 18.3.6

Check the numerical difference

For a small matrix multiplication, the quantized output begins `[0.9843737, 0.39332393]`; the float32 output begins `[0.98, 0.40999997]`. The results are close, not identical. section 18.3.6

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through scaling, int8 matrix multiplication, unscaling, and numerical comparison, then favors a built-in API over hand implementation.
Examples
Compares quantized and float32 results for small matrices and shows R code for scaling, multiplication, and Keras predictions.
What it assumes
Assumes an already-trained model while illustrating scaling and casting with small matrix inputs.
What you can do after
Apply it via Keras quantize_weights("int8") to prepare an inference-only model and run predictions.
How it connects
Connects quantization to weight pruning, mixed precision, matrix multiplication, and model deployment.

Also explained in

Compare how 13 books explain quantization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.