Implement INT8 Quantization

Medium
MLE Interview Prep

Implement a function that performs symmetric INT8 quantization on floating-point values. INT8 quantization is a technique used in deep learning to reduce model size and speed up inference by converting 32-bit floating-point numbers to 8-bit integers.

Your function should:

  1. Calculate the appropriate scale factor for symmetric quantization
  2. Quantize the input values to signed 8-bit integers in the range [-127, 127]
  3. Dequantize the values back to floating-point to demonstrate the reconstruction

For symmetric quantization:

  • The scale factor maps the maximum absolute value to 127
  • The zero point is always 0 (symmetric around origin)
  • Quantized values are clipped to the valid INT8 range

Return a dictionary containing:

  • 'quantized': List of quantized integer values
  • 'scale': The scale factor used (rounded to 6 decimal places)
  • 'dequantized': List of reconstructed floating-point values (rounded to 4 decimal places)

Handle the edge case where all input values are zero by using a scale of 1.0.

Examples

Example 1:
Input: x = [1.0, -1.0, 0.5, -0.5]
Output: {'quantized': [127, -127, 64, -64], 'scale': 0.007874, 'dequantized': [1.0, -1.0, 0.5039, -0.5039]}
Explanation: 1) Find abs_max = max(|1.0|, |-1.0|, |0.5|, |-0.5|) = 1.0 2) Calculate scale = 1.0 / 127 = 0.007874 3) Quantize each value: 1.0/0.007874 = 127, -1.0/0.007874 = -127, 0.5/0.007874 = 63.5 -> round to 64, -0.5/0.007874 = -63.5 -> round to -64 4) Dequantize: 127 * 0.007874 = 1.0, -127 * 0.007874 = -1.0, 64 * 0.007874 = 0.5039, -64 * 0.007874 = -0.5039 Note the quantization error for 0.5 and -0.5 due to the discrete nature of INT8 representation.

Starter Code

import numpy as np

def int8_quantize(x: list[float]) -> dict:
	"""
	Perform symmetric INT8 quantization on a floating-point array.
	
	Args:
		x: Input list of floating-point values
		
	Returns:
		Dictionary with 'quantized', 'scale', and 'dequantized' keys
	"""
	# Your code here
	pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews