Skip to content
class

Quantize

extends_QuantizedGridModule
Quantize(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.

fake_quant⁡(x)=(clamp⁡(round⁡(x/S)+Z, qmin⁡, qmax⁡)−Z) S\operatorname{fake\_quant}(x) = \bigl( \operatorname{clamp}(\operatorname{round}(x/S) + Z,\ q_{\min},\ q_{\max}) - Z\bigr)\, S

where S,ZS, Z are the calibrated activation (scale, zero_point) and qmin⁡,qmax⁡q_{\min}, q_{\max} the grid bounds (0, 255 for the default quint8). The inner round-and-clamp is what quantizes; the outer (⋅−Z) S(\cdot - Z)\, S re-expresses the integer code as the nearest representable float.

Parameters

scaleTensor
Per-tensor activation scale from the calibrating observer.
zero_pointTensor
Per-tensor activation zero-point from the calibrating observer.
qdtypeQDtype= quint8
Quantized dtype whose quant_min / quant_max bound the grid. Defaults to lucid.quantization.quint8 ([0, 255]).

Attributes

scaleTensor
Scalar activation-scale buffer, set from the calibrated observer.
zero_pointTensor
Scalar activation zero-point buffer, set from the calibrated observer.

Notes

  • Normally produced by convert / from_float, which reads the calibrated QuantStub's observer via calculate_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 DeQuantize is 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())
True

See Also

Used by 1

Class methods

1
cls

from_float

→Quantize
from_float(stub: nn.Module)

Build from a calibrated QuantStub (reads its observer).

Instance methods

1
fn

forward

→Tensor
forward(X: Tensor)

Quantize X to the calibrated activation grid (float-carried).

Integer inputs (e.g. token indices) are passed through unchanged — only real-valued activations are quantized.