quantization
31 memberslucid.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.
Phase 0 surface (this commit): the schemes / dtypes / parameter
container and the three arithmetic primitives (quantize,
dequantize, fake_quantize). Observers, QConfig, the
eager / dynamic / graph workflows, and the real low-precision GEMM land
in later phases.
Classes
QParams1 methodsImmutable bundle of everything that defines a quantized tensor's grid.
QDtype2 methodsDescriptor for a quantized integer grid — bits, signedness, range, storage.
QScheme2 methodsHow (scale, zero_point) map real values onto the integer grid.
FakeQuantize8 methodsObserver + straight-through fake-quantization, toggleable for QAT.
FixedQParamsObserver3 methodsObserver with a fixed (scale, zero_point) — no statistics collected.
HistogramObserver3 methodsPer-tensor observer that records a value histogram and picks a clip range.
MinMaxObserver3 methodsPer-tensor observer tracking the global running min / max of the input.
MovingAverageMinMaxObserver2 methodsPer-tensor observer with an exponential moving average of min / max.
MovingAveragePerChannelMinMaxObserver2 methodsPer-channel observer with an EMA of the per-channel min / max.
NoopObserverA no-op PlaceholderObserver — pure identity, collects no statistics.
ObserverBase3 methodsAbstract base for calibration observers that yield (scale, zero_point).
PerChannelMinMaxObserver3 methodsPer-channel observer tracking a running min / max along ch_axis.
PlaceholderObserver3 methodsCarries qdtype / qscheme metadata but collects no statistics.
QConfigThe atomic (activation, weight) observer / fake-quant recipe.
QConfigMapping5 methodsResolves which QConfig applies to each module in a model.
Functions
dequantize→ TensorDecode integer codes back to a float32 approximation of the original.
fake_quantize→ TensorSimulate quantization in the float domain with a straight-through gradient.
quantize→ TensorEncode a real tensor into integer codes on an affine quantization grid.
calculate_qparams→ (Tensor, Tensor)Derive (scale, zero_point) from an observed value range.
get_default_qat_qconfig→ QConfigReturn the standard quantization-aware-training recipe.
get_default_qat_qconfig_mapping→ QConfigMappingBuild the default QAT QConfigMapping.
get_default_qconfig→ QConfigReturn the standard static post-training-quantization recipe.
get_default_qconfig_mapping→ QConfigMappingBuild the default static-PTQ QConfigMapping.
convert→ nn.ModuleBake a calibrated model into quantized inference modules.
prepare→ nn.ModuleAttach activation observers to a float model ahead of calibration.
prepare_qat→ nn.ModuleInsert fake-quant modules for quantization-aware training.
quantize_dynamic→ nn.ModuleDynamically quantize a model — int8 weights, runtime-quantized activations.
convert_fx→ nn.ModuleGraph-mode convert — bake a calibrated model into quantized modules.
prepare_fx→ nn.ModuleGraph-mode prepare — auto-wrap the quant boundaries and insert observers.
fuse_modules→ nn.ModuleCollapse Conv/BN/ReLU (or Linear/ReLU) runs into single fused modules.
fuse_modules_qat→ nn.ModuleFuse for QAT — Conv+BN runs become trainable nn.intrinsic.qat modules.