Skip to content
class

LinearReLU

extends_FusedModule
LinearReLU(linear: nn.Linear, relu: nn.ReLU)

Fused float Linear + ReLU marker — a plain-float lucid.nn.Sequential.

A thin lucid.nn.Sequential of [linear, relu] emitted by lucid.quantization.fuse_modules. It runs as plain float — it carries no observers and no fake-quant, and computing through it is identical to running the linear then the ReLU. Its whole purpose is structural: by packaging the two ops as one unit it tells the quantization pipeline to place a single activation observer after the ReLU rather than between the linear and the ReLU.

Why fuse for quantization. If an observer sat on the raw linear output it would calibrate a symmetric range that includes values the ReLU is about to discard, wasting half the int8 codes on negatives that never reach inference. Observing the post-ReLU range instead lets the eventual quantized layer pick a grid over [0,max⁡][0, \max], roughly doubling resolution. Fusion also drops the intermediate float tensor between linear and ReLU, so the converted kernel does matmul → ReLU → requantize in one step.

Where it sits. This marker is the hand-off point between float and quantized worlds. lucid.quantization.prepare_qat replaces it with a trainable lucid.nn.qat.LinearReLU (weight + post-ReLU fake-quant under an STE), and lucid.quantization.convert maps it — or the QAT layer it became — to a fused inference lucid.nn.quantized.LinearReLU.

As a plain-float module it simply computes:

y=ReLU⁡(Wx⊤+b)y = \operatorname{ReLU}(W x^{\top} + b)

Downstream, once an observer is attached, that fused output is fake-quantized under a straight-through estimator — rounded in the forward pass, identity in the backward:

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

Parameters

linearnn.Linear
The linear (fully-connected) layer to run first; the [0] child of the sequential.
The rectifier applied to the linear output; the [1] child.

Notes

  • Structural tag only. It carries no quantization state — no weight_fake_quant, no activation_post_process. Those are attached later by lucid.quantization.prepare_qat (or by lucid.quantization.prepare for the post-training path).
  • Post-ReLU observer placement. The single reason to fuse is so the downstream activation observer sees the non-negative post-ReLU range.
  • Both directions are wired for you. lucid.quantization.fuse_modules emits this marker in; lucid.quantization.prepare_qat / lucid.quantization.convert map it out to the QAT / quantized fused layer.

Examples

>>> import lucid, lucid.nn as nn
>>> import lucid.quantization as Q
>>> import lucid.nn.intrinsic as nni
>>> m = nn.Sequential(nn.Linear(32, 16), nn.ReLU())
>>> fused = Q.fuse_modules(m, [["0", "1"]])
>>> isinstance(fused[0], nni.LinearReLU)     # [linear, relu] collapsed to one marker
True
>>> fused(lucid.randn(8, 32)).shape           # runs as plain float
(8, 16)

See Also

Used by 1

Constructors

1
dunder

__init__

→None
__init__(linear: nn.Linear, relu: nn.ReLU)