KV memory and quantization · Hard

Packed Groupwise INT4 Multiplication

Problem 40 of 50 · Python and NumPy

Problem

Multiply floating activations x (M × K) by a packed signed INT4 weight matrix (K × N). Flatten weights in row-major order before packing: the first weight is the low nibble, the next the high nibble. Decode nibbles ≥8 by subtracting 16. If K*N is odd, ignore the final unused high nibble.

Groups run along the K dimension. Dequantized weight[k,n] = signed_int4[k,n] * scales[k//group_size][n]. scales has ceil(K/group_size) rows and N=output_features columns. Return the M × N floating product. There are no zero points.

def packed_int4_matmul(x, packed_weights, scales, group_size, output_features):

Constraints

  • 1 ≤ M,K,N ≤ 16; 1 ≤ group_size ≤ 16.
  • packed_weights contains exactly ceil(K*N/2) integers in [0,255].
  • scales are finite positive numbers with the stated shape; x is finite.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [
    [
      2
    ]
  ],
  [
    13
  ],
  [
    [
      0.5
    ]
  ],
  1,
  1
]
Expected result: [
  [
    -3
  ]
]

Nibble 13 represents -3; scale 0.5 gives -1.5, multiplied by activation 2 to produce -3.

Example 2

Arguments: [
  [
    [
      1,
      2
    ]
  ],
  [
    33,
    67
  ],
  [
    [
      1,
      0.5
    ]
  ],
  2,
  2
]
Expected result: [
  [
    7,
    5
  ]
]

The dequantized rows are [1,1] and [3,2], so [1,2] projects to [7,5].

Sign in to solve this problem

Browse all coding problems · Learn the foundations