← HomOps Reference
HomBatchNorm
Batch-normalization as affine scale and shift on an encrypted activation.
BnML
RunsServer
Changes shapeYes - broadcast
Changes scaleYes - via ConstMul
Levels spentVia ConstMul (typically 0 or 1)
RotatesNo
KeysNone
What it does
HomBatchNorm applies batch-normalization as an affine scale and shift on an encrypted activation:y = (γ / √(σ² + ε)) · x + (β − μ · γ / √(σ² + ε))It is a composite (
SequentialHomOp), not a registered leaf. Children:Reach for it when you need channel-wise BN after conv / linear without folding BN into the convolution weights. For fused conv+BN, see HomConvBnFused.
Signature
HomBatchNorm()No constructor parameters. BN statistics and affine parameters come in through
set_data.Parameters
HomBatchNorm has no public constructor parameters.set_dataParameterTypeDefaultDescription
meanTensorrequiredBN running mean μ.varTensorrequiredBN running variance σ².gammaTensor | NoneNone → ones_like(mean)BN weight.betaTensor | NoneNone → zeros_like(mean)BN bias.epsfloat1e-5Numerical stability term under the square root.What
set_data computesscale = gamma / (var + eps) ** 0.5bias = beta − mean · scaleThose tensors are reshaped to
(-1, 1, 1) and passed to the ConstMul and ConstAdd children.Rules that depend on the value
SettingRuleIf you break it
mean, varBoth must be provided (not None).ValueError.Requirements
set_datacalled. Mean and variance are required before use; gamma and beta default to ones and zeros when omitted.- Broadcast layout. Scale and bias are stored as
(C, 1, 1)afterview; they must broadcast against the ciphertext channel layout. - Keys. None. ConstMul and ConstAdd need no evaluation-key entries.
Shape effect - yes
Rule: the ciphertext broadcasts with scale / bias shaped
(C, 1, 1). Typical activations keep (C, H, W) (or packed equivalents).CiphertextScale / biasOutput
(16, 8, 8)(16, 1, 1)(16, 8, 8) - per-channel affine
Shape and the n axis are covered on Concepts.
Scale effect - yes
Rule: HomConstMul multiplies the ciphertext scale by its plaintext encoding scale. HomConstAdd leaves scale unchanged.
StageEffect on scale
After ConstMul (scale)scale_out = scale_in × ConstMul's
pt_scaleAfter ConstAdd (shift)Unchanged
How scale moves through a pipeline is on Ciphertext state - Scale.
Level budget
ConfigurationLevels spent
ConstMul with with_modswitch=True1 - drops one prime after the scale step
ConstMul with with_modswitch=False0
ConstAdd0
Level cost comes only from the ConstMul child. Details of that child's defaults are on HomConstMul.
Keys
None. ConstMul and ConstAdd need no square key and no rotation key.
Example
Channel-wise BN for a 16-channel activation:
pipeline.pyPYTHON
from lattica_build.operators import HomBatchNorm from lattica_build.base_classes.hom_pipeline import HomomorphicPipeline from lattica_build.operators.composite.sequential import SequentialHomOp import torch C = 16 bn = HomBatchNorm() bn.set_data( mean=torch.zeros(C), var=torch.ones(C), gamma=torch.ones(C), beta=torch.zeros(C), eps=1e-5, ) pipeline = HomomorphicPipeline( hom=SequentialHomOp(bn), input_shape=(C, 8, 8), )
See also
- HomConstMul - the scale step inside
- HomConstAdd - the shift step inside
- HomConvBnFused - conv with BN folded into weights
- Ciphertext state - Scale - how ConstMul grows scale