Skip to content
class

LinearReLU

extendsLinear
LinearReLU(in_features: int, out_features: int, bias: bool = True, qconfig: QConfig | None = None)

Quantization-aware fused Linear + ReLU — trainable, fake-quant every forward.

A Linear whose forward applies ReLU before the output fake-quant, so the activation observer sees the true post-ReLU (non-negative) range and calibrates a grid that spends all of its codes on values the layer can actually emit. Fusing the two ops also lets a single activation observer stand in for both, which is exactly the shape lucid.quantization.convert needs to collapse them into one kernel.

Like the base Linear, the weight and the (post-ReLU) output are fake-quantized every forward under a straight-through estimator (STE): rounding happens in the forward pass while gradients pass straight through to the float weight, so it stays fully trainable and learns to compensate for the eventual int8 rounding. Built from a fused float lucid.nn.intrinsic.LinearReLU by lucid.quantization.prepare_qat; lucid.quantization.convert folds it into a single quantized lucid.nn.quantized.LinearReLU.

On each forward the weight is fake-quantized, F.linear runs, ReLU clips, and the result is fake-quantized onto the observed post-ReLU grid:

fake_quant⁡(t)=(clamp⁡(round⁡(t/s)+z, qmin⁡, qmax⁡)−z) s,∂ fake_quant⁡(t)∂t=1\operatorname{fake\_quant}(t) = \bigl( \operatorname{clamp}(\operatorname{round}(t / s) + z,\ q_{\min},\ q_{\max}) - z\bigr)\, s, \qquad \frac{\partial\, \operatorname{fake\_quant}(t)}{\partial t} = 1 y=fake_quant⁡a ⁣(ReLU⁡(fake_quant⁡w(W) x⊤+b))y = \operatorname{fake\_quant}_{a}\!\Bigl( \operatorname{ReLU}\bigl( \operatorname{fake\_quant}_{w}(W)\, x^{\top} + b\bigr)\Bigr)

where s,zs, z are the observer's scale / zero-point and qmin⁡,qmax⁡q_{\min}, q_{\max} the grid bounds. Because the activation observer sits outside the ReLU, the calibrated (s,z)(s, z) describe the non-negative output.

Parameters

in_featuresint
Size of each input sample — forwarded verbatim to the float lucid.nn.Linear parent.
out_featuresint
Size of each output sample — forwarded verbatim to the float parent.
biasbool= True
Whether to add a learnable (float) bias term; forwarded to the float parent.
qconfigQConfig= None
Quantization recipe supplying the weight and activation lucid.quantization.FakeQuantize modules applied during training. Required — constructing the layer without one raises ValueError.

Attributes

weight_fake_quantFakeQuantize
The weight observer + fake-quant built from qconfig.weight(); rounds the float weight each forward and tracks its range for convert.
activation_post_processFakeQuantize
The output observer + fake-quant built from qconfig.activation(); here it observes the post-ReLU range so the baked-in grid matches inference.

Notes

  • STE differentiability. Rounding is applied forward but the gradient passes through as the identity, so both the weight and the pre-ReLU activations keep clean gradients and the float weight trains normally.
  • Post-ReLU calibration. Observing after the ReLU means the calibrated (scale, zero_point) cover only [0,max⁡][0, \max], doubling the effective resolution versus observing a symmetric pre-ReLU range.
  • Both directions are wired for you. lucid.quantization.prepare_qat builds this from a fused float lucid.nn.intrinsic.LinearReLU; lucid.quantization.convert folds it into the quantized lucid.nn.quantized.LinearReLU.

Examples

>>> import lucid, lucid.nn as nn
>>> import lucid.quantization as Q
>>> import lucid.nn.qat as nnqat
>>> m = nn.Sequential(nn.Linear(32, 16), nn.ReLU())
>>> fused = Q.fuse_modules(m, [["0", "1"]])         # -> nni.LinearReLU marker
>>> qat = Q.prepare_qat(fused, Q.get_default_qat_qconfig_mapping())
>>> isinstance(qat[0], nnqat.LinearReLU)
True
>>> loss = (qat(lucid.randn(8, 32)) ** 2).mean()
>>> loss.backward()                                 # STE trains the fused float weight
>>> _ = qat.eval()
>>> qc = Q.convert(qat)                             # -> quantized.LinearReLU
>>> type(qc[0]).__name__
'LinearReLU'

See Also

Used by 1

Class methods

1
cls

from_float

→LinearReLU
from_float(mod: nn.Module)

Build from a fused float nni.LinearReLU (its inner linear).

Instance methods

1
fn

forward

→Tensor
forward(x: Tensor)

Fake-quantize the weight, run linear, ReLU, fake-quantize the output.