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.10b0.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.10b0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: masked_norm-2025.10b0.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.10b0.tar.gz
Algorithm Hash digest
SHA256 404c710cd0a535e9cf6e06e3dc89b24080d30b9ba59d97881c15b47f23541a82
MD5 2ecfb17b3ab609575db8ccbead99ffcc
BLAKE2b-256 a2f5469d9e5f9dc09e9736f4a1fac27461d98a3de235628485bb6724b2a5870e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for masked_norm-2025.10b0-py3-none-any.whl
Algorithm Hash digest
SHA256 c00b271dd48e23d36971371e214cb30cc2202663e49100a5c186ab974cf2f9b7
MD5 46b73ae6d36d972242f69f69dce8720b
BLAKE2b-256 d2993eebf088d43aaa0367d2604481477d1b7d0098da560d4f78b16a19347ca3

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