Quantized projections¶
These helpers compose with jit and the existing execution/cache API.
INT4, INT8 and palette weights are captured constants; changing them requires
retracing and recompilation. NVFP4 byte tensors can change on every invocation.
Backend admission remains explicit. FP16 MATMUL follows TOSA’s FP16 accuracy
contract; the NumPy reference uses FP32 accumulation and is not a bitwise oracle
for arbitrary native FP16 dot products.
Explicit low-bit projections, composable inside virtio_accel.jit().
INT4, INT8 and palette weights are immutable captured constants. NVFP4 weights and block scales can be runtime INT8 byte carriers. None of these helpers quantizes floating weights or selects a device automatically.
- virtio_accel.quantized.int4_matmul(x, packed_weights, scales, *, group_size)¶
FP16 projection with captured signed INT4 weights, low nibble first.
Weights are row-major [N,K], two’s complement [-8,7].
scalesmust be a positive finite FP16 ndarray [N,K/group_size]. Output is [M,N] or [1,M,N]. Each decoded weight rounds to FP16 before the FP16 MATMUL. Core ML and OpenVINO may repack these constants at load time; placement is policy dependent.
- virtio_accel.quantized.int8_matmul(x, weights, scales, *, group_size)¶
FP16 projection with captured INT8 weights [N,K] and FP16 group scales.
Uses the same scale geometry and rounding as
int4_matmul(). For integer activations and INT32 output usenumpy.matmulinstead.
- virtio_accel.quantized.palette4_matmul(x, packed_indices, palettes, *, group_size)¶
FP16 projection with captured four-bit indices and arbitrary FP16 palettes.
Indices are row-major [N,K], low nibble first.
palettesis a finite FP16 ndarray [N,K/group_size,16]. Values include signed zero; no scale or clustering is applied. Core ML recognizes this constant lookup pattern.
- virtio_accel.quantized.nvfp4_matmul(x, packed_weights, block_scales, *, tensor_scale=1.0)¶
Project runtime NVFP4 weights with FP16 or FP32 activations/output.
K must be divisible by 16.
packed_weightsis an INT8 byte carrier [1,N*K/2], andblock_scalesan INT8 carrier [1,N*K/16] holding raw E4M3FN bits. Useuint8_array.view(np.int8)to preserve byte encodings. E2M1 weights are low nibble first in row-major [N,K]. Decode, apply block scales, MATMUL, then apply the separate positive finite tensor scale in the activation dtype. Byte inputs remain runtime tensors and can change per call.
A packed INT4 projection¶
>>> import numpy as np
>>> import virtio_accel as va
>>> from virtio_accel import quantized as q
>>> scales = np.ones((1, 1), dtype=np.float16)
>>> f = va.jit(lambda x: q.int4_matmul(x, bytes([0x21, 0x43]), scales, group_size=4), backend="reference")
>>> f(np.ones((1, 4), dtype=np.float16)).to_numpy().tolist()
[[10.0]]