Skip to main content

bijx logo
bijection + jax = /ˈbaɪdʒæks/

Bijections & normalizing flows with JAX/NNX

GitHub docs build Tests codecov PyPI DOI

This library provides flexible tools for building normalizing flows and bijections with tractable change of densities, focusing on research and applications in physics. It aims to provide reusable building blocks rather than a simplified interface for common use cases.

The library is built around two fundamental mathematical objects:

  • Bijections: Invertible transformations that track their effect on probability densities.
  • Distributions: Probability distributions with methods for sampling and density evaluation.

Key Features

Core design goals:

  • Modular Building Blocks: Composable components rather than monolithic frameworks
  • Research-Focused: Prioritizes flexibility and expressiveness over convenience
  • Flax NNX Integration: Modern state management for neural network components

Physics & advanced methods:

  • Continuous Normalizing Flows: CNFs as core primitive with flexible ODE solver backends
  • Matrix Lie Group Operations: Automatic differentiation on matrix groups
  • Structure-Preserving Integration: Crouch-Grossmann solvers for ODEs on matrix groups
  • Symmetry-Aware Architectures: Equivariant layers and transformations (e.g., lattice-symmetric CNFs)
  • Fourier-Space Operations: Tools for momentum space transformations and complex field decomposition

Quickstart

Here is a minimal example of building and sampling from a simple normalizing flow. We transform samples from a base distribution (a standard normal) using a chain of bijections to produce samples from a new, transformed distribution.

import jax
import jax.numpy as jnp
import bijx
from flax import nnx

# Define simple base distributions
prior = bijx.IndependentNormal(event_shape=(2,))

# choose a base bijection for the coupling layer
base_bijection = bijx.ModuleReconstructor(
    # for real NVP style, using affine linear as base bijection
    bijx.AffineLinear(rngs=nnx.Rngs(0))
)

# build a simple coupling layer
coupling_layer = bijx.GeneralCouplingLayer(
    # replace with more realistic NN (active -> params)
    embedding_net=lambda x: jnp.zeros(x.shape + (base_bijection.params_total_size,)),
    mask=bijx.BinaryMask.from_boolean_mask(jnp.array([True, False])),
    bijection_reconstructor=base_bijection,
)

# build a very simple continuous flow
# note: automatic divergence is expensive in high dimensions
cnf = bijx.ContFlowDiffrax(
    # vf must return divergence too; AutoJacVF computes it for us
    vf=bijx.AutoJacVF(
        lambda t, x: -x,
    )
)

# Compose bijections
flow = bijx.Chain(
    bijx.Shift(jnp.array([5.0, -2.0])),
    bijx.Scaling(jnp.array([2.0, 0.5])),
    # add more coupling layers for expressivity
    coupling_layer,
    # final continuous flow
    cnf,
)

# sample from the base distribution
rng = jax.random.key(42)
x, lp_x = prior.sample(batch_shape=(5,), rng=rng)

# transform samples with bijection
y, lp_y = flow.forward(x, lp_x)

# these can also be generated by combining prior and flow into a new distribution
dist = bijx.Transformed(prior, flow)

y2, lp_y2 = dist.sample(batch_shape=(5,), rng=rng)

# exactly the same as we used the same random key
assert jnp.allclose(y, y2)

# check bijectivity
x_rec, lp_x_rec = flow.reverse(y, lp_y)
assert jnp.allclose(x, x_rec, atol=1e-6)
assert jnp.allclose(lp_x, lp_x_rec, atol=1e-6)

Design Principles

  • Modularity: Different parts of the library should be usable on their own. Expose as many expressive building blocks as possible.
  • Flexibility: Prioritize flexibility, sometimes at the cost of adding more ways to break things.
  • Runtime Shape Inference: Use a batch + space + channels convention and automatic vectorization for flexible data shape handling.

Installation

This package can be installed directly from PyPI:

pip install bijx

Alternatively, to install locally from source:

pip install -e .

For development, install as an editable package with all dependencies (optionally without docs if documentation building is not needed):

pip install -e ".[dev,docs]"

To keep the codebase tidy, please install pip install pre-commit and run pre-commit install before committing changes.

Documentation

To compile and open a local server for the documentation, run make livehtml in the docs/ directory.

Testing

There are two types of tests that can be run: the unit tests in tests/ and the docstring examples in the source code src/bijx/.

# Run tests
pytest tests/

# Run doctests
pytest src/bijx/ --doctest-modules

# Run all tests
pytest tests/ src/bijx/ --doctest-modules

# Run tests in parallel using xdist
pytest -n auto

Citation

If you use bijx in your research, please cite our JOSS paper:

@article{Gerdes2025Bijx,
  doi = {10.21105/joss.09521},
  url = {https://doi.org/10.21105/joss.09521},
  year = {2025},
  publisher = {The Open Journal},
  volume = {10},
  number = {116},
  pages = {9521},
  author = {Gerdes, Mathis and Cheng, Miranda C. N.},
  title = {Bijx: Bijections and normalizing flows with JAX/NNX},
  journal = {Journal of Open Source Software}
}

Related Publications

This library extends research software developed for the following publications, which you may find useful and may consider citing if you use the relevant components:

Module Layout

The library is organized into core mathematical tools, machine learning components, and specific applications.

bijx/
├── __init__.py           # Main package exports
├── utils.py              # General utilities
├── distributions.py      # Core Distribution classes
├── samplers.py           # Sampling helpers
├── solvers.py            # ODE solver implementations
│
├── bijections/           # All bijection-related code
│   ├── base.py           # Core Bijection, Chain, etc.
│   ├── continuous.py     # Continuous flow wrappers
│   ├── conv_cnf.py       # Convolutional CNF architecture
│   ├── coupling.py       # Coupling layers
│   └── ...
│
├── nn/                   # Neural network components (NNX)
│   ├── conv.py           # Symmetric convolutions
│   ├── embeddings.py     # Time and positional embeddings
│   └── features.py       # Reusable feature-mappers
│
├── fourier.py            # Tools for Fourier-space operations
├── lie.py                # General-purpose Lie group operations
├── cg.py                 # Crouch-Grossmann ODE integrators
│
└── lattice/              # Application: Lattice Field Theory
    ├── scalar.py         # Action and observables for phi^4 theory
    └── gauge.py          # Gauge field symmetry operations

Download files

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

Source Distribution

bijx-1.3.2.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

bijx-1.3.2-py3-none-any.whl (131.9 kB view details)

Uploaded Python 3

File details

Details for the file bijx-1.3.2.tar.gz.

File metadata

  • Download URL: bijx-1.3.2.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for bijx-1.3.2.tar.gz
Algorithm Hash digest
SHA256 a8ad4a6183d8fc717774dad14149852331e7d2bd2c1ec5420b6af8e5437e700e
MD5 f26289a102bbc22dcac9a1a6ea6ed30a
BLAKE2b-256 53c3648a1b1472c7111d3bda3808b6c7c8e5e1190c9d5f8bc46a91295f60cf66

See more details on using hashes here.

Provenance

The following attestation bundles were made for bijx-1.3.2.tar.gz:

Publisher: publish.yml on mathisgerdes/bijx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bijx-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: bijx-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 131.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for bijx-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8d8f87ccde69e90a9468027763202e9d4e4815f5c2692e218ba97ad3def6731
MD5 57682c9205b7376eb1f8444b1703431d
BLAKE2b-256 2321e6fc98994ef60dbfad9b61e539efc853ea450b8e1b2b480f724ba6feefd6

See more details on using hashes here.

Provenance

The following attestation bundles were made for bijx-1.3.2-py3-none-any.whl:

Publisher: publish.yml on mathisgerdes/bijx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.3.2 This release

2 files

1.3.1

2 files

1.3.0

2 files

1.2.1

2 files

1.2.0

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

Supported by

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