Add a Model
Skip this page if your example uses a model ScireX already has (FNO,
FNOClassic2d, WNO, GINO, Transolver, or the experimental GIWO).
scirex.models.available_models() lists them.
The running example on this page is PointMLP, a small pointwise network. Its
code below was run against this repository: it registers, builds through the
factory and refuses unknown keys as shown.
1. File location
A new model starts in the experimental tier:
src/scirex/experimental/<model>.py e.g. src/scirex/experimental/point_mlp.py
tests/experimental/test_<model>.py its tests
The API policy says why:
a name in scirex.models is covered by the 1.x stability promise, a name in
scirex.experimental is not. GIWO (src/scirex/experimental/giwo.py) is
the model to copy. A model moves to src/scirex/models/ only in a release
that promotes it (step 5 below).
Reusable layers go in src/scirex/layers/, not in the model file.
2. Class interface
A model is a Flax linen module that subclasses scirex.models.BaseModel:
# SPDX-FileCopyrightText: 2026 ZenteiQ AiTech Innovations Pvt. Ltd.
# SPDX-License-Identifier: LGPL-3.0-only
"""PointMLP: a pointwise multilayer perceptron, the smallest operator baseline."""
import flax.linen as nn
import jax.numpy as jnp
from ..models.base import BaseModel
class PointMLP(BaseModel, name="PointMLP"):
"""Pointwise MLP: the same small network applied at every grid point.
It sees one point at a time, so it cannot learn a non-local operator; it
is a baseline that a neural operator must beat.
Parameters
----------
in_channels : int
Channels of the input function. Determined by the problem.
out_channels : int
Channels of the output function. Determined by the problem.
hidden_channels : int, optional
Width of every hidden layer. Default: 32.
n_layers : int, optional
Number of hidden layers. Default: 2.
Notes
-----
Input and output are channels-first, ``(batch, channels, *spatial)``,
like ``FNO`` and ``FNOClassic2d``.
"""
in_channels: int
out_channels: int
hidden_channels: int = 32
n_layers: int = 2
@nn.compact
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
"""Map ``(batch, in_channels, *spatial)`` to ``(batch, out_channels, *spatial)``."""
h = jnp.moveaxis(x, 1, -1)
for _ in range(self.n_layers):
h = nn.gelu(nn.Dense(self.hidden_channels)(h))
return jnp.moveaxis(nn.Dense(self.out_channels)(h), -1, 1)
The rules behind it:
- Subclass
BaseModelwith aname=.BaseModel.__init_subclass__registers the class undername.lower()("pointmlp"), andBaseModel.__new__records the constructor arguments, sosave_checkpointandfrom_checkpointwork without extra code. - Hyperparameters are dataclass fields with type annotations. They are
the model's allowed keys: Flax's generated
__init__refuses any other keyword with aTypeError. - Use
in_channelsandout_channelsfor the input and output widths if the model has them.get_model_jaxfillsin_channelsfrom the config keydata_channels. A model with another parameterisation (Transolver'sspace_dim,fun_dim,out_dim) is fine: the factory then dropsdata_channelsandout_channels. - A documented layout. This guide uses channels-first,
(batch, channels, *spatial), likeFNO;WNOis channels-last,Transolvertakes point lists andGINOtakes coordinates and query points. Pick the one that fits the model and state it in the docstring. - Choose a name nobody uses. The registry is keyed by the lower-case
name, and a second class with the same name silently replaces the first.
Check
available_models()first. - Follow the JAX/Flax rules:
setup()or@nn.compact, no in-place mutation, explicitdtype=jnp.float32on dtype-sensitive initialisers, noblock_until_readyin model code. - Bump
_version(a class attribute,"0.1.0"by default) whenever a change makes old checkpoints load into a different network; loading then warns about the mismatch.
3. Registration with allowed config keys
Registration has three parts.
a. The model registry. Subclassing BaseModel registers the class, but
only once its module has been imported. So that get_model_jax can build the
model without the caller importing it, add it to _BUILTIN_ARCHS in
src/scirex/models/base.py:
_BUILTIN_ARCHS: dict[str, str] = {
"fno": "scirex.models.fno",
...
"giwo": "scirex.experimental.giwo",
"pointmlp": "scirex.experimental.point_mlp",
}
and add it to _BUILTIN and _CONFIG_MODELS in tests/models/test_init.py,
which build every built-in architecture in a fresh interpreter.
b. The config class. The allowed config keys of a model are the fields of
its config class, a subclass of scirex.config.ModelConfig in
src/scirex/config/models.py. model_arch is the registry name; every other
field is a constructor argument of the model, except data_channels, which
the factory renames to in_channels:
class PointMLPConfig(ModelConfig):
"""Hyperparameters of :class:`scirex.experimental.point_mlp.PointMLP`."""
model_arch: str = "pointmlp"
data_channels: int = 1
out_channels: int = 1
hidden_channels: int = 32
n_layers: int = 2
Presets for a problem subclass it and change defaults only, like
FNOSmall2d or TransolverPipe. Export the config class from
src/scirex/config/__init__.py; that module is imported eagerly so that
presets register with zencfg and can be selected by name.
c. Building it. Any experiment config with a model field then builds
the model through the factory:
from zencfg import ConfigBase
from scirex.config import ModelConfig
from scirex.models import get_model_jax
class Experiment(ConfigBase):
model: ModelConfig = PointMLPConfig()
model = get_model_jax(Experiment().to_dict()) # a PointMLP with in_channels=1
Where unknown keys are caught. zencfg does not refuse an unknown key:
PointMLPConfig(hidden_chanels=8) (note the typo) is accepted and the key is
kept. The typo is caught one step later, when get_model_jax checks the keys of
config.model against the constructor:
So every config key must be a constructor argument, and the
required test checks
that an unknown key fails. A problem config that is not a model config (data
split, training recipe) must check its keys itself, the way
make_config does in the worked example.
4. Docstring
The docstring is numpy-style, and it is what the API reference shows:
- a one-line summary, then what the model does and when to use it;
Parameters: every constructor argument, with its type, its meaning and its default; put rarely changed ones underOther Parameters, asFNOdoes;Notes: the input and output layout and anything a user must know (for example that the dimension is set bylen(n_modes));References: the paper the model comes from, if any;- a docstring on
__call__saying the input and output shapes.
A public name without a docstring fails tests/test_public_api.py; Ruff's
D rules check the format in public modules.
5. Making it stable (later)
Promotion to scirex.models is a separate, deliberate change. Follow
Adding a public name:
move the file to src/scirex/models/, add the name to __all__,
_LAZY_EXPORTS and the TYPE_CHECKING block of
src/scirex/models/__init__.py, add ::: scirex.models.<Name> to
doc/api/models.md, regenerate the snapshot with
python scripts/dev/update_api_snapshot.py, update the expected list in
tests/models/test_init.py, and add the name under Added in
CHANGELOG.md.
Next
Required tests lists the four tests every model needs.