Skip to content
class

ConvBnReLU1d

extends_ConvBnNd
ConvBnReLU1d(conv: nn.Module, bn: nn.Module, relu: bool = True, qconfig: QConfig | None = None)

QAT fused Conv1d + BatchNorm1d + ReLU — BN folded every forward.

Like ConvBn1d, but a ReLU follows the (BN-folded) convolution and the output fake-quant observes the post-ReLU range, so the calibrated activation grid covers only the non-negative values inference can actually produce. This is the fully-fused conv→BN→ReLU block that becomes a single quantized kernel after conversion.

As in ConvBn1d, BN is re-folded into the conv weight on every forward and the folded weight is fake-quantized under a straight-through estimator (STE): rounding happens forward, gradients pass straight through, so the conv weight and the BN affine keep training while bn updates its running statistics from each training batch. Built by lucid.quantization.prepare_qat; lucid.quantization.convert folds BN a final time, quantizes the folded weight to int8, and bakes it into a fused inference lucid.nn.quantized.ConvReLU1d.

The per-output-channel BN fold (cc indexes output channels) is

wc′=γc wcσc2+ϵ,bc′=βc+γc bc−μcσc2+ϵw'_{c} = \gamma_c\,\frac{w_c}{\sqrt{\sigma_c^2 + \epsilon}}, \qquad b'_{c} = \beta_c + \gamma_c\,\frac{b_c - \mu_c}{\sqrt{\sigma_c^2 + \epsilon}}

with BN affine (γ,β)(\gamma, \beta), running statistics (μ,σ2)(\mu, \sigma^2), numerical floor ϵ\epsilon, and conv weight / bias (wc,bc)(w_c, b_c) (reducing to bc′=βc−γc μc/σc2+ϵb'_c = \beta_c - \gamma_c\,\mu_c/\sqrt{\sigma_c^2+\epsilon} for a bias-free conv). In eval mode (or once the BN statistics are frozen) the layer is that folded conv: the folded weight is fake-quantized, convolved, passed through ReLU, then the output is fake-quantized on the observed post-ReLU grid:

y=fake_quant⁡a ⁣(ReLU⁡(fake_quant⁡w(w′)⋆x+b′)),∂ fake_quant⁡(t)∂t=1y = \operatorname{fake\_quant}_{a}\!\Bigl( \operatorname{ReLU}\bigl( \operatorname{fake\_quant}_{w}(w') \star x + b'\bigr)\Bigr), \qquad \frac{\partial\, \operatorname{fake\_quant}(t)}{\partial t} = 1

where ⋆\star is the 1-D cross-correlation and fake_quant⁡(t)=(clamp⁡(round⁡(t/s)+z,qmin⁡,qmax⁡)−z) s\operatorname{fake\_quant}(t) = (\operatorname{clamp}(\operatorname{round}(t/s) + z,q_{\min}, q_{\max}) - z)\, s.

Parameters

The float 1-D convolution whose weight receives the folded BN affine.
The batch-norm layer folded into conv. It must track running statistics; its affine parameters train under the STE and its running statistics update from each training batch (ValueError if track_running_stats=False).
relubool= True
Whether the fused ReLU is applied before the output fake-quant.
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 from qconfig.weight(); rounds the folded kernel each forward and tracks its range so convert can pick the int8 qparams.
activation_post_processFakeQuantize
The output observer + fake-quant from qconfig.activation(); here it observes the post-ReLU range so the baked-in grid matches inference.

Notes

  • Batch statistics in training. bn normalises with them and updates its running ones; freeze_bn_stats / update_bn_stats stop and resume that.
  • Post-ReLU calibration. The activation observer sits after the ReLU, so its grid covers only [0,max⁡][0, \max], roughly doubling resolution versus a symmetric range.
  • STE differentiability. The folded weight is rounded forward but the gradient is passed through as the identity, so the conv weight and the BN parameters train.
  • Both directions are wired for you. lucid.quantization.prepare_qat builds this fused layer in; lucid.quantization.convert folds it out into the matching lucid.nn.quantized.ConvReLU1d.

Examples

>>> import lucid, lucid.nn as nn
>>> import lucid.quantization as Q
>>> import lucid.nn.intrinsic.qat as nniqat
>>> cbr = nniqat.ConvBnReLU1d(
...     nn.Conv1d(3, 8, 3, padding=1, bias=False),
...     nn.BatchNorm1d(8),
...     relu=True,
...     qconfig=Q.get_default_qat_qconfig(),
... )
>>> loss = (cbr(lucid.randn(2, 3, 8)) ** 2).mean()
>>> loss.backward()                    # STE trains conv weight AND BN params
>>> _ = cbr.eval()
>>> from lucid.nn.intrinsic.qat.modules import convbnrelu2d_to_quantized
>>> qc = convbnrelu2d_to_quantized(cbr)     # rank-generic bake
>>> type(qc).__name__
'ConvReLU1d'

See Also

Used by 1

Constructors

1
dunder

__init__

→None
__init__(conv: nn.Module, bn: nn.Module, relu: bool = True, qconfig: QConfig | None = None)

Instance methods

4
fn

forward

→Tensorfrom _ConvBnNd
forward(x: Tensor)

Scale and fake-quant the weight → conv → unscale → BN → (ReLU) → fake-quant.

fn

freeze_bn_stats

→Selffrom _ConvBnNd
freeze_bn_stats()

Stop the running-stat updates; bn normalises with its running stats.

fn

train

→Selffrom _ConvBnNd
train(mode: bool = True)

Set the mode recursively, except that a frozen bn stays frozen.

fn

update_bn_stats

→Selffrom _ConvBnNd
update_bn_stats()

Let bn normalise with batch statistics and update its running ones.