Skip to main content

A modern implementation of normalization layers

Project description

Masked Norm

Self-attention-based neural network architectures pad input sequences to an expected length prior to processing. Generally, an attention mask is generated for each sequence in order to retain its original length and further instruct the model on which sequence instances are relevant.

Normalization transformations suppress the internal covariance shift problem in deep neural networks, leading to a reduction training time. Layer normalization - popular in self-attention-based architectures - normalizes each instance of the input sequences. Notably, the output of this transformation is invariant under a re-scaling operation of each sequence instance.

When input sequences are padded with a constant value, the variances of the padded instances are null. Standard layer normalization implementations introduce a stabilising constant $\epsilon$ to prevent arithmetic errors. This can lead to overflows further along the computational graph.

The masked normalization transformation uses the input's attention mask such that only the relevant instances of the sequence are normalized. Moreover, by applying simple shape-shifting operations (such as axis permutation or unfoldings) over the input tensor, masked normalization can reproduce batch, group, layer, and instance normalization.

This masked normalization implementation refrains from tracking the running statistics during training, since this breaks re-scaling invariance. Also, the proposed implementation employs the variance's unbiased estimator, as each scalar tensor element is considered a true sample.

This repository contains a PyTorch implementation of masked normalization.

To use it in your project, install it via pip:

pip install masked_norm

Three core user-facing implementations are presented: a plain masked normalization masked_norm, a batched masked normalization batched_masked_norm, and an affine masked normalization LazyAffineMaskedNorm. Plain masked_norm normalizes values along the last axis of the input, while batched_masked_norm normalizes along the first axis. These two variant cover most functional use cases. LazyAffineMaskedNorm performs an affine transformation after normalization, it can have a batched or unbatched behaviour which is specified by a keyword argument to the constructor.

You can use the functional form of the masked normalization transformation directy in your model's forward call:

from torch import tensor, rand
from masked_norm import masked_norm

inpt = rand(2, 2, 3)
mask = tensor(
    [
        [True, True],
        [True, False]
    ]
)

masked_norm(inpt, mask)

Our use its class equivalent:

from torch import tensor, rand
from masked_norm import MaskedNorm

inpt = rand(4, 2, 2)
mask = tensor(
    [
        [True, True],
        [True, False]
    ]
)

norm_layer = MaskedNorm(batched=True)

norm_layer(inpt, mask)

You can apply the affine masked normalization transformation, by instantiating a lazy layer:

from torch import tensor, rand
from masked_norm import LazyAffineMaskedNorm

inpt = rand(2, 2, 3)
mask = tensor(
    [
        [True, True],
        [True, False]
    ]
)

affine_norm_layer = LazyAffineMaskedNorm()

affine_norm_layer(inpt, mask)

To see how you can reproduce the batch, group, layer, or instance normalization procedures with masked normalization take a look at the test subdirectory. Note that the variance estimator of the official Pytorch implementations of these layers is biased.

If, either by chance or design, a selection of samples is constant, the proposed masked_norm and affine_masked_norm implementations ignore this selection, and pass the values along unaltered.

To run the test suite:

git clone https://github.com/algmarques/masked_norm.git
cd masked_norm
python -m venv .venv
.venv/bin/pip install .
.venv/bin/python -Bm unittest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

masked_norm-2025.10.post0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

masked_norm-2025.10.post0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file masked_norm-2025.10.post0.tar.gz.

File metadata

  • Download URL: masked_norm-2025.10.post0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for masked_norm-2025.10.post0.tar.gz
Algorithm Hash digest
SHA256 b062d1822d69bcd1547e135d9aee5814146d1d016c6af98c16226d67c365dc94
MD5 5c33013b969607264f9bac938c990606
BLAKE2b-256 bda8b80cdc267708d65de2d499d3858bc31c3abdd13483a2abdef99978211c76

See more details on using hashes here.

File details

Details for the file masked_norm-2025.10.post0-py3-none-any.whl.

File metadata

File hashes

Hashes for masked_norm-2025.10.post0-py3-none-any.whl
Algorithm Hash digest
SHA256 f4488a25be5b077ea0b8e2e1167b0b85b936cd2facb2d37cecf1c86b5f7286b5
MD5 d9cbc23542dda81ff6353d58ecdf7c6e
BLAKE2b-256 66aaeca0fd52973545e70ca47933fe2b03d9c2ab60de52a63e0cc9f04e8d2a5a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page