PIXELBANKv9.1.0
Menu

Simulate INT8 quantization and dequantization of model weights.

INT8 quantization maps float32 values to [-128, 127]:

  1. Find scale: scale = max(|weights|) / 127
  2. Quantize: q = round(weights / scale), clamped to [-128, 127]
  3. Dequantize: weights_approx = q * scale

Compute the quantization error (MSE between original and dequantized).

Input: Space-separated float weights

Output:

  • Line 1: Dequantized weights, rounded to 4 decimal places
  • Line 2: Scale factor, rounded to 6 decimal places
  • Line 3: MSE error, rounded to 6 decimal places

Example:

Input:
1.0 -0.5 0.3 -0.8
Output:
0.9921 -0.4961 0.2953 -0.7953
0.007874
0.000013
Reasoning:
  • First, we find the scale factor: scale=max⁡(∣weights∣)/127=max⁡(∣1.0∣,∣−0.5∣,∣0.3∣,∣−0.8∣)/127=1.0/127=0.007874scale = \max(|weights|) / 127 = \max(|1.0|, |-0.5|, |0.3|, |-0.8|) / 127 = 1.0 / 127 = 0.007874
  • Then, we quantize the weights: q=\round(weights/scale)q = \round(weights / scale), which results in q=\round(1.0/0.007874)=127q = \round(1.0 / 0.007874) = 127, q=\round(−0.5/0.007874)=−63q = \round(-0.5 / 0.007874) = -63, q=\round(0.3/0.007874)=38q = \round(0.3 / 0.007874) = 38, q=\round(−0.8/0.007874)=−101q = \round(-0.8 / 0.007874) = -101
  • Next, we dequantize the weights: weights_approx=q\*scaleweights\_approx = q \* scale, resulting in 1.0\*0.007874=0.99211.0 \* 0.007874 = 0.9921 (after rounding), −0.5\*0.007874=−0.4961-0.5 \* 0.007874 = -0.4961 (after rounding), 0.3\*0.007874=0.29530.3 \* 0.007874 = 0.2953 (after rounding), −0.8\*0.007874=−0.7953-0.8 \* 0.007874 = -0.7953 (after rounding)
  • Finally, we calculate the MSE error between the original and dequantized weights: MSE=1n∑(original−dequantized)2MSE = \frac{1}{n} \sum (original - dequantized)^2, which equals 0.0000130.000013 after calculation and rounding

Constraints:

  • Clamp quantized values to [-128, 127]
  • Use round() for rounding to nearest integer
  • Round outputs as specified
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.