Quantize
ModuleQuantize(scale: Tensor, zero_point: Tensor, qdtype: QDtype = quint8)Runtime entry into the quantized region — fake-quantizes the input.
The converted, inference-time form of a calibrated QuantStub,
installed by lucid.quantization.convert (or built directly via
from_float). Where the stub was a passive marker, Quantize is
active: every forward maps a real-valued activation onto the calibrated
(scale, zero_point) grid the observer recorded, which is the numerical
"entry" into the quantized region.
Under the sidecar representation (design B) the quantized activation is
carried as float — the result has int8 numerics (values snapped to the
grid) but remains an ordinary float32 tensor, so it flows through the
rest of the quantized graph without any packed-integer container. Integer
inputs such as token indices are recognised and passed through unchanged,
since only real-valued activations have a meaningful quantization grid.
where are the calibrated activation (scale, zero_point) and
the grid bounds (0, 255 for the default
quint8). The inner round-and-clamp is what quantizes; the outer
re-expresses the integer code as the nearest
representable float.
Parameters
Attributes
Notes
- Normally produced by
convert/from_float, which reads the calibratedQuantStub's observer viacalculate_qparams; construct directly only when you already hold the qparams. - The input's dtype decides the path: floating-point tensors are fake-quantized, integer tensors are returned verbatim.
- The exit-side
DeQuantizeis an identity, so a quantized region is bracketed by exactly one active boundary (this one).
Examples
>>> import lucid, lucid.nn as nn
>>> q = nn.quantized.Quantize(lucid.tensor(0.05), lucid.tensor(128.0))
>>> q(lucid.randn(4)).shape # snapped to the 0.05 grid
(4,)
Integer inputs bypass quantization entirely (a common surprise):
>>> idx = lucid.tensor([1, 2, 3])
>>> bool((q(idx) == idx).all().item())
TrueSee Also
- lucid.nn.quantized.QuantStub—The float-model marker this converts from.
- lucid.nn.quantized.DeQuantize—The matching (identity) exit marker.
- lucid.quantization.fake_quantize—The op applied on each forward.
- lucid.quantization.convert—Installs this layer from a calibrated model.