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.
KV memory and quantization · Hard
Problem 40 of 50 · Python and NumPy
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):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.
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].