QFunctional
ModuleQFunctional(scale: Tensor, zero_point: Tensor, qdtype: QDtype = quint8)Quantized element-wise merges — each result fake-quantized to one grid.
The converted, inference-time form of FloatFunctional, installed by
lucid.quantization.convert (or built via from_float). It
exposes the identical surface — add, mul, add_relu, cat,
add_scalar, mul_scalar — but every call now fake-quantizes its
output onto the single (scale, zero_point) grid the source
FloatFunctional observed during calibration.
This is what makes residual quantization correct. A skip-add feeds two
branches that were quantized on their own scales; requantizing the sum to one
calibrated grid re-aligns them, so the block's output matches int8 numerics
instead of drifting off at full precision. Under the sidecar representation
(design B) the requantized result is carried as float32 with int8
numerics — no packed-integer container is produced.
Every merge routes its raw float result through
with the calibrated (scale, zero_point) and grid bounds
(0, 255 for the default quint8). Only the
output is snapped; add_relu applies the ReLU before this step so the
grid matches the post-ReLU range calibration saw.
Parameters
Attributes
Notes
- Normally produced by
convert/from_float, which reads the calibratedFloatFunctional's observer; construct directly only when you already hold the qparams. - All merge methods share one grid — the module is bound to a single merge
site, mirroring the one-instance-per-site rule for
FloatFunctional. add_relufuses add + ReLU into one requantization; prefer it overF.relu(qf.add(x, y))so the observed and executed ranges agree.
Examples
>>> import lucid, lucid.nn as nn
>>> qf = nn.quantized.QFunctional(lucid.tensor(0.1), lucid.tensor(0.0))
>>> a = lucid.randn(4)
>>> qf.add(a, a).shape # residual add, requantized
(4,)
The fused residual-add of a ResNet block clamps negatives *before* the grid:
>>> out = qf.add_relu(lucid.randn(4), lucid.randn(4))
>>> bool((out >= 0).all().item())
TrueSee Also
- lucid.nn.quantized.FloatFunctional—The calibration-time counterpart.
- lucid.quantization.fake_quantize—The op applied to each merge output.
- lucid.quantization.convert—Installs this module from a calibrated model.
Used by 1
Constructors
1Class methods
1from_float(mod: nn.Module)Build from a calibrated FloatFunctional (reads its observer).
Instance methods
6Quantized x + y.
Quantized relu(x + y).
Quantized x + scalar.
Quantized concatenation along dim.
Quantized x * y.
Quantized x * scalar.