We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← JAX Conceptual Deep-Dives step 9 of 15
int8 Dequantization
Read an integer code as a real value
A quantized code needs its scale and zero point to have a meaning. Dequantization maps that code to a value on the quantization grid:
x_hat = (q - zero_point) * scale
Subtracting zero_point makes the code for real zero map back to zero.
Multiplying by scale converts the remaining number of grid steps into
real units. Use the same parameters that were used to quantize the data.
Worked example
import jax.numpy as jnp
q = jnp.array([1.0, 3.0, 5.0, 127.0], dtype=jnp.float32)
scale, zero_point = 0.5, 3.0
x_hat = (q - zero_point) * scale
# [-1.0, 0.0, 1.0, 62.0]
The preceding quantization exercise maps [-0.75, 0, 0.75, 100] to these
codes. Zero is recovered exactly. The first and third values were rounded
onto the grid, while 100 was clipped and now reconstructs as 62.
Problem
Implement affine_dequantize(q, scale, zero_point) by subtracting the
zero point and then multiplying by the scale.
qis a one-dimensional float32 array containing integer-valued codes in[-128, 127].scaleis a finite positive scalar.zero_pointis an integer-valued scalar in[-128, 127], supplied as a float. It may be negative.- Return a floating array with the same shape as
q.
Empty input returns an empty array. Do not round or clip the reconstructed values, and do not divide by the scale.
Why reconstruction is approximate
Quantization is many-to-one: different inputs can produce the same code. Dequantization selects that code's grid value; it is not an exact inverse of the original input mapping. Rounding and clipping caused the information loss before this step. Floating-point arithmetic may add small numerical errors when evaluating the reconstruction formula.
Without clipping, ideal nearest rounding gives reconstruction error at most
scale / 2. Clipping can produce much larger errors, as the example shows.
Do not apply that half-step bound to clipped values.
Keep the subtraction in floating point
The input codes are already floats in this exercise. If you later work
with actual int8 arrays, widen them before subtracting the zero point:
the difference can exceed the int8 range. For example, -128 - 127 = -255.
Do not cast this exercise's input to int8.
The formula does not require every inference engine to expand all weights to floating point before multiplication. Quantized implementations can use integer arithmetic internally. This task only implements the mapping from codes back to real values.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Read an integer code as a real value
A quantized code needs its scale and zero point to have a meaning. Dequantization maps that code to a value on the quantization grid:
x_hat = (q - zero_point) * scale
Subtracting zero_point makes the code for real zero map back to zero.
Multiplying by scale converts the remaining number of grid steps into
real units. Use the same parameters that were used to quantize the data.
Worked example
import jax.numpy as jnp
q = jnp.array([1.0, 3.0, 5.0, 127.0], dtype=jnp.float32)
scale, zero_point = 0.5, 3.0
x_hat = (q - zero_point) * scale
# [-1.0, 0.0, 1.0, 62.0]
The preceding quantization exercise maps [-0.75, 0, 0.75, 100] to these
codes. Zero is recovered exactly. The first and third values were rounded
onto the grid, while 100 was clipped and now reconstructs as 62.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.