Skip to content
data

QDtype

QDtype(name: str, bits: int, signed: bool, quant_min: int, quant_max: int, storage: str)

Descriptor for a quantized integer grid — bits, signedness, range, storage.

Where a QScheme fixes the geometry of the affine map, a QDtype pins the target grid itself: how many bits the codes use, whether they are signed, the inclusive [quant_min, quant_max] range they span, and the ordinary Lucid dtype that physically stores them. Every observer and fake-quant reads quant_min / quant_max off a QDtype to clamp its codes.

Unlike the reference framework, Lucid does not add quantized dtypes to the C++ engine's dtype enum; a QDtype is a pure-Python descriptor and the integer codes live in an ordinary storage dtype. This keeps the whole quantization subsystem off the engine's critical path until a real low-precision GEMM is wired in, and lets 4-bit codes ride inside int8 until bit-packing lands.

Four instances are exported as module-level constants, spanning the grids:

qint8: [−128, 127] (8-bit signed),quint8: [0, 255] (8-bit unsigned)\mathtt{qint8}:\ [-128,\ 127]\ (\text{8-bit signed}),\qquad \mathtt{quint8}:\ [0,\ 255]\ (\text{8-bit unsigned}) qint4: [−8, 7] (4-bit signed),qint32: [−231, 231−1] (32-bit signed)\mathtt{qint4}:\ [-8,\ 7]\ (\text{4-bit signed}),\qquad \mathtt{qint32}:\ [-2^{31},\ 2^{31}-1]\ (\text{32-bit signed})
  • qint8 — 8-bit signed ([-128, 127]), stored in int8. The default weight grid: symmetric weights centre on 0, so a signed grid uses its full range. The workhorse of int8 inference.
  • quint8 — 8-bit unsigned ([0, 255]), stored in int16 (the engine has no uint8, so codes ride in the next wider signed type). The default activation grid: post-ReLU activations are ≥0\ge 0, so an unsigned grid spends all 256 codes on the range that actually occurs.
  • qint4 — 4-bit signed ([-8, 7]), stored (unpacked) in int8 until 4-bit packing lands. The aggressive weight grid for the real MLX GEMM: about 2x smaller than int8 for a modest accuracy cost, used for memory-bound decode.
  • qint32 — 32-bit signed ([-2**31, 2**31 - 1]), stored in int32. The accumulator grid: int8 × int8 partial sums accumulate in int32 before requantization, so it rarely appears as a weight / activation dtype.

Parameters

namestr
Canonical name of the grid ("qint8", "quint8", …); also drives __repr__ and appears in module extra_repr strings.
bitsint
Bit width of the grid (4 / 8 / 32).
signedbool
Whether codes are signed. Governs where a symmetric zero_point is pinned (0 when signed, mid-range when unsigned).
quant_minint
Inclusive lower bound of the representable integer range.
quant_maxint
Inclusive upper bound of the representable integer range.
storagestr
Name of the ordinary Lucid dtype that physically holds the codes. quint8 maps to int16 (no engine uint8); qint4 maps to int8 (unpacked) until 4-bit packing lands.

Attributes

name, bits, signed, quant_min, quant_max, storage
The frozen fields above; a QDtype is an immutable value object (@dataclass(frozen=True)), so instances can be shared and compared by value.

Notes

  • Grid width. quant_max - quant_min + 1 == 2 ** bits for every instance (256 for the 8-bit grids, 16 for qint4).
  • Signed / unsigned pairing with schemes. A symmetric scheme + signed dtype pins zero_point to 0; a symmetric scheme + unsigned dtype pins it to the grid midpoint. Affine schemes solve for zero_point regardless.
  • The MLX low-precision GEMM only consumes bits in {4, 8}; qint32 is an accumulator descriptor, not a kernel input.
  • repr(qint8) is "lucid.quantization.qint8" — the canonical import path, not the field dump — so the descriptor round-trips visually.

Examples

>>> import lucid.quantization as Q
>>> Q.qint8.bits, Q.qint8.signed, (Q.qint8.quant_min, Q.qint8.quant_max)
(8, True, (-128, 127))
>>> Q.quint8.signed, (Q.quint8.quant_min, Q.quint8.quant_max)
(False, (0, 255))
>>> Q.qint4.quant_max - Q.qint4.quant_min + 1     # 2 ** 4
16
>>> repr(Q.qint8)
'lucid.quantization.qint8'

See Also

Used by 13

… 1 more

Dunder methods

1
dunder

__repr__

→str
__repr__()

Return "lucid.quantization.<name>".