Skip to content
module

quantization

31 members
lucid.quantization

lucid.quantization — post-training quantization, QAT, and quantized inference.

This sub-package brings weights and activations from float32 down to low-precision integer grids (int8 / int4), the top capability lever for inference and serving on Apple Silicon. It mirrors the reference framework's quantization surface while keeping a single canonical path per name (H8): the workflow API lives here (observers, QConfig, prepare / convert / quantize_dynamic / prepare_qat), and the quantized module layer lives under lucid.nn.quantized / lucid.nn.qat / lucid.nn.intrinsic.

Representation follows the sidecar design (B): a quantized weight is an ordinary integer lucid.Tensor plus float scale / zero_point buffers held by the owning module — no dedicated quantized tensor subtype and no new engine dtype.

Deploying what comes out of it. Every workflow here reaches lucid.coreml. A model from prepare_qat exports with its weights on the grid they were trained onto, so storing them there costs nothing — measured at 0.00e+00 against the eager model, where the same network quantized after training lands at 7e-04. A model from convert or quantize_dynamic holds a packed MLX linear whose Metal-only kernel a trace cannot follow, and exports through that form's own dequantize reference path — the same arithmetic to 5e-07, not an approximation of it.

Two things to know before reading a number from one. Observers keep recording in eval() mode, correctly, since post-training calibration runs there and is that recording; the export pauses them while it traces, but anything else that runs the model twice is comparing against something that moved. And prepare_qat prepares the model in place and hands it back, so a caller who wants to keep the float weights copies first.

Classes

Functions