ConvBnReLU1d
_ConvBnNdConvBnReLU1d(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 ( indexes output channels) is
with BN affine , running statistics , numerical floor , and conv weight / bias (reducing to 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:
where is the 1-D cross-correlation and .
Parameters
convnn.Conv1dconv. 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= Truelucid.quantization.FakeQuantize modules applied during training.
Required — constructing the layer without one raises ValueError.Attributes
weight_ fake_ quantFakeQuantizeqconfig.weight(); rounds the folded
kernel each forward and tracks its range so convert can pick the int8 qparams.activation_ post_ processFakeQuantizeqconfig.activation(); here it observes
the post-ReLU range so the baked-in grid matches inference.Notes
- Batch statistics in training.
bnnormalises with them and updates its running ones;freeze_bn_stats/update_bn_statsstop and resume that. - Post-ReLU calibration. The activation observer sits after the ReLU, so its grid covers only , 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_qatbuilds this fused layer in;lucid.quantization.convertfolds it out into the matchinglucid.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
- lucid.nn.quantized.ConvReLU1d—The fused int8 inference conv
convertbakes into. - lucid.nn.intrinsic.qat.ConvBn1d—Same fold, without the fused ReLU.
- lucid.quantization.prepare_qat—Builds this fused QAT layer from conv + BN + ReLU.
- lucid.quantization.convert—Folds BN a final time and bakes the fused int8 conv.
Used by 1
Constructors
1Instance methods
4Scale and fake-quant the weight → conv → unscale → BN → (ReLU) → fake-quant.
Stop the running-stat updates; bn normalises with its running stats.
Set the mode recursively, except that a frozen bn stays frozen.
Let bn normalise with batch statistics and update its running ones.