Skip to main content

HomOps Reference

HomConvBnFused

Fuse Conv2d and BatchNorm2d into one encrypted convolution.

CbML
RunsServer (HomConv subclass)
Changes shapeSame as HomConv with bias
Changes scaleYes - via HomConv packed mul
Levels spentSame as HomConv
RotatesYes - kernel tap alignments
KeysRotation key

What it does

HomConvBnFused folds batch-norm into the convolution weights and bias, then runs as a single HomConv with bias=True. Fusion at set_data time:
W_fused = γ · W / √(σ² + ε)
B_fused = β + (B − μ) · γ / √(σ² + ε)
Reach for it when you want a deploy-time fuse of Conv2d + BatchNorm2d into one encrypted conv - fewer ops and levels than a separate HomBatchNorm. It is not separately registered; it inherits OP_TYPE.Conv.

Signature

HomConvBnFused(
    in_channels,                        # required
    out_channels,                       # required
    kernel_size,                        # int | (h, w)
    stride,                             # int | (h, w)
    padding,                            # int | (h, w)
    groups=1,
    dilation=(1, 1),
    image_hw=None,
)
Always constructed with bias=True on HomConv. Pass constructor arguments by keyword. Attach fused parameters through set_data(weight, bias, mean, var, gamma, beta, eps).

Parameters

ParameterTypeDefaultDescription
in_channelsintrequiredC_in
out_channelsintrequiredC_out
kernel_sizeint | (h, w)requiredKernel size.
strideint | (h, w)requiredStride.
paddingint | (h, w)requiredPadding (unlike HomConv, not optional / None).
groupsint1Grouped conv.
dilationint | (h, w)(1, 1)Spatial dilation of the kernel, as a scalar or (dh, dw).
image_hw(h, w) | NoneNoneOptional input image spatial size hint used when the packed layout needs an explicit (H, W).
set_data
ParameterTypeDefaultDescription
weightTensorrequiredConv weight W (shape matches kernel_shape).
biasTensor | NoneNone → zeros like meanConv bias B.
meanTensorrequiredBN running mean μ.
varTensorrequiredBN running variance σ².
gammaTensor | NoneNone → onesBN scale.
betaTensor | NoneNone → zerosBN shift.
epsfloat1e-5Stability constant.
Fusion body (conceptually):
StepExpression
scalegamma / (var + eps)**0.5
W_fusedweight * scale.view(-1, 1, 1, 1)
B_fusedbeta + (bias - mean) * scale
attachsuper().set_data(W_fused, B_fused)

Requirements

  • Same as HomConv with bias. Rotation keys, square packing (C, H*W) with H == W, and internal_n ≥ H*W.
  • set_data with BN statistics. Pass weight, bias, mean, var, gamma, and beta (plus optional eps) before prep.
These surface as compilation errors; see Compilation errors.

Shape / scale / levels / keys

Identical to HomConv with bias=True after fusion: same shape rule, packed-mul scale growth, typical mod-switch level, and rotation key.

Example

Construct with keywords, then fuse BN statistics into the conv:
pipeline.pyPYTHON
from lattica_build.operators import HomConvBnFused
import torch

C_in, C_out = 3, 16
conv_bn = HomConvBnFused(
    in_channels=C_in,
    out_channels=C_out,
    kernel_size=3,
    stride=1,
    padding=1,
    groups=1,
)
conv_bn.set_data(
    weight=torch.randn(C_out, C_in, 3, 3),
    bias=torch.zeros(C_out),
    mean=torch.zeros(C_out),
    var=torch.ones(C_out),
    gamma=torch.ones(C_out),
    beta=torch.zeros(C_out),
    eps=1e-5,
)

See also