Skip to content
class

ConvReLU1d

extendsConv1d
ConvReLU1d(args: object = (), qconfig: QConfig | None = None, kwargs: object = {})

Quantization-aware fused 1-D conv + ReLU — trainable, fake-quant per forward.

A Conv1d 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 conv and ReLU 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 Conv1d, 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 kernel, so it stays fully trainable and learns to compensate for the eventual int8 rounding. Built from a fused float lucid.nn.intrinsic.ConvReLU1d by lucid.quantization.prepare_qat; lucid.quantization.convert folds it into a single quantized lucid.nn.quantized.ConvReLU1d.

On each forward the kernel is fake-quantized, F.conv1d 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) \star x + b\bigr)\Bigr)

where ⋆\star is the 1-D cross-correlation, 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

qconfig(QConfig, keyword - only)= None
Quantization recipe supplying the weight and activation lucid.quantization.FakeQuantize modules applied during training. Required — constructing the layer without one raises ValueError.
*argsobject= ()
All remaining arguments are forwarded to the float lucid.nn.Conv1d constructor, exactly as for Conv1d.
**kwargsobject= ()
All remaining arguments are forwarded to the float lucid.nn.Conv1d constructor, exactly as for Conv1d.

Attributes

weight_fake_quantFakeQuantize
The weight observer + fake-quant built from qconfig.weight(); rounds the float kernel 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 the float kernel trains normally.
  • Post-ReLU calibration. Observing after the ReLU means the calibrated grid covers only [0,max⁡][0, \max], roughly doubling resolution versus a symmetric pre-ReLU range.
  • Deferred string padding. Integer / tuple padding is supported; string padding ("same" / "valid") is deferred, matching the quantized conv.
  • Both directions are wired for you. lucid.quantization.prepare_qat builds this from a fused float lucid.nn.intrinsic.ConvReLU1d; lucid.quantization.convert folds it into the quantized lucid.nn.quantized.ConvReLU1d.

Examples

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

See Also

Used by 1

Class methods

1

Instance methods

1
fn

forward

→Tensor
forward(x: Tensor)

Apply the convolution to the input tensor.

Parameters

inputTensor
Input tensor of shape (N,Cin,∗)(N, C_{\text{in}}, *).

Returns

Tensor

Output tensor of shape (N,Cout,∗)(N, C_{\text{out}}, *) with spatial dimensions determined by stride, padding, dilation, and kernel size.