fuse_conv_bn_eval
→Modulefuse_conv_bn_eval(conv: object, bn: object)Fold a BatchNorm layer into the preceding Conv weights (inference-only).
Because BN at eval time is an affine map with frozen statistics, the
composition BN(Conv(x)) is itself a single convolution — the BN
can be analytically absorbed into the conv's weight and bias. The
fused module computes exactly the same output but with one fewer
kernel launch and one fewer allocation per call. Standard step in
deployment / quantisation pipelines.
Parameters
convConv1d | Conv2d | Conv3dbnBatchNorm1d | BatchNorm2d | BatchNorm3dconv. Must be in eval mode (or
otherwise be using its frozen running_mean /
running_var); calling on a training-mode graph silently
yields wrong outputs.Returns
ModuleDeep copy of conv with fused parameters. Originals are not
mutated.
Raises
TypeErrorconv or bn is not one of the supported types.Notes
Let , , be the BN running
statistics and , its affine parameters
(treated as and if affine=False). The
fused weight and bias are
where the scale broadcasts along the output-channel axis. When the original conv has no bias, is taken as and the fused module gains one.
Examples
>>> import lucid.nn as nn
>>> from lucid.nn.utils import fuse_conv_bn_eval
>>> conv = nn.Conv2d(3, 16, 3); bn = nn.BatchNorm2d(16)
>>> conv.eval(); bn.eval()
>>> fused = fuse_conv_bn_eval(conv, bn)