KV memory and quantization · Easy

Per-Channel INT8 Dequantization

Problem 33 of 50 · Python and NumPy

Problem

Dequantize an R × C integer weight matrix using one affine scale and zero point per output column: weight[r,c] = (quantized[r,c]−zero_points[c])*scales[c]. Return a floating matrix of identical shape. Subtract in a wide type so INT8 extremes cannot wrap.

def dequantize_int8(quantized, scales, zero_points):

Constraints

  • 1 ≤ R,C ≤ 16; quantized and zero_points contain integers in [-128,127].
  • scales has C finite positive values; zero_points has C integers.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    [
      1,
      2
    ],
    [
      3,
      4
    ]
  ],
  [
    0.5,
    2
  ],
  [
    0,
    0
  ]
]
Expected result: [
  [
    0.5,
    4
  ],
  [
    1.5,
    8
  ]
]

Scale column 0 by 0.5 and column 1 by 2, giving [[0.5,4],[1.5,8]].

Example 2

Arguments: [
  [
    [
      -128,
      127
    ]
  ],
  [
    0.25,
    0.5
  ],
  [
    -128,
    127
  ]
]
Expected result: [
  [
    0,
    0
  ]
]

Each integer equals its column zero point, so both dequantized values are zero.

Sign in to solve this problem

Browse all coding problems · Learn the foundations