Skip to content
class

QFunctional

extends_QuantizedGridModule
QFunctional(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 tt through

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

with the calibrated (scale, zero_point) S,ZS, Z and grid bounds qmin⁡,qmax⁡q_{\min}, q_{\max} (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

scaleTensor
Per-tensor output scale, from the source FloatFunctional's observer.
zero_pointTensor
Per-tensor output zero-point, from that same observer.
qdtypeQDtype= quint8
Quantized dtype whose quant_min / quant_max bound the grid. Defaults to lucid.quantization.quint8 ([0, 255]).

Attributes

scaleTensor
Scalar output-scale buffer shared by every merge method.
zero_pointTensor
Scalar output zero-point buffer shared by every merge method.

Notes

  • Normally produced by convert / from_float, which reads the calibrated FloatFunctional'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_relu fuses add + ReLU into one requantization; prefer it over F.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())
True

See Also

Used by 1

Class methods

1
cls

from_float

→QFunctional
from_float(mod: nn.Module)

Build from a calibrated FloatFunctional (reads its observer).

Instance methods

6
fn

add_relu

→Tensor
add_relu(x: Tensor, y: Tensor)

Quantized relu(x + y).

fn

add_scalar

→Tensor
add_scalar(x: Tensor, scalar: float)

Quantized x + scalar.

fn

cat

→Tensor
cat(tensors: list[Tensor], dim: int = 0)

Quantized concatenation along dim.

fn

mul_scalar

→Tensor
mul_scalar(x: Tensor, scalar: float)

Quantized x * scalar.