Multigrid Components
Multigrid Components¤
Multgrid components are used in the Multigrid Neural Operator MgNO for the convolutional blocks
in the multigrid-style V-cycle
(Pre-Smoothing \(\rightarrow\) Restriction \(\rightarrow\) Coarse Correction \(\rightarrow\) Prolongation \(\rightarrow\) Post-Smoothing).
Multigrid Convolution¤
neojax.nn.mg_conv.MgConv
¤
Multigrid V-cycle convolutional block.
Builds a hierarchy of len(n_its_per_level) levels. Each level
applies pre-smoothing iterations MgIter, restricts to the
next coarser grid, and then, after a prolongated coarse correction,
applies post-smoothing on the upward pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNGKeyArray
|
PRNG key for parameter initialization. |
required |
n_its_per_level
|
Sequence[Sequence[int] | int]
|
Number of iterations per level. A sequence containing
a pair |
required |
n_channels_u
|
int
|
Number of channels of the solution field. |
required |
n_channels_f
|
int
|
Number of channels of the data field. |
required |
padding_mode
|
Literal['zeros', 'reflect', 'replicate', 'circular']
|
Padding mode for the convolutions. Requires |
'zeros'
|
padding_mode_smoothing
|
Optional[Literal['zeros', 'reflect', 'replicate', 'circular']]
|
Padding mode for the smoothing iterations. If set,
overrides |
None
|
padding_mode_prolongation
|
Optional[Literal['zeros', 'reflect', 'replicate', 'circular']]
|
Padding mode for the prolongations. If set,
overrides |
None
|
padding_mode_restriction
|
Optional[Literal['zeros', 'reflect', 'replicate', 'circular']]
|
Padding mode for the restrictions. If set,
override |
None
|
prolongation_padding
|
int
|
The padding to use in the transposed convolutions
of the prolongations. Requires |
1
|
restriction_padding
|
int
|
The padding to use in the convolutions
of the restrictions. Requires |
1
|
smoothing_padding
|
int
|
The padding to use in the convolutions of the
pre- and post-smoothing iterations. Is overwritten only if
|
1
|
use_smoothing_bias
|
bool
|
Whether to use bias in the smoothing convolutions. Requires
|
False
|
use_restriction_pde_bias
|
bool
|
Whether to use bias in the approx PDE operator of
the restrictions. Default is |
False
|
use_residual_restriction
|
bool
|
Whether to use residual-based restrictions
("Full approximation scheme"-style). Default is |
False
|
use_layernorm
|
bool
|
Whether to use layer normalization after each prolongation.
Requires |
True
|
use_layernorm_affine
|
bool
|
Whether the layer normalization has learnable affine parameters.
Default is |
True
|
layer_norm_type
|
Literal['helm', 'halve']
|
The type of layer normalization spatial reduction to use. Requires |
'halve'
|
in_spatial_size
|
int | None
|
Optional size of the spatial dimensions. Required
if |
None
|
prolongation_kernel_type
|
Literal['constant', 'alternating', 'helm']
|
The type of prolongation kernels to use.
Use |
'constant'
|
grow_channels_w_depth
|
bool
|
Whether to increase the number of channels
with depth ( |
False
|
use_xavier_restrict
|
bool
|
Whether to use Xavier initialization for the
restriction operators. Can be |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
TypeError
|
If |
Internal Attributes
These fields store the internal attributes and weights.
- layer_norms (
tuple[eqx.Module, ...] | None): The layer normalizations per level. - prolongation_convs (
tuple[eqx.Module, ...]): The prolongation convolutions per level. - layers (
tuple[tuple[tuple[MgIter, ...], MgRestrict, tuple[MgIter, ...]], ...]): Three element tuples per level containing the pre-smoothing, restriction and post-smoothing iterations. - n_its_per_level (
tuple[tuple[int, int] | int, ...]): The number of iterations per level. - n_levels (
int): The number of multigrid levels. Corresponds tolen(n_its_per_level). - padding_mode_smoothing (
str): The padding mode for the smoothing convolutions. - padding_mode_prolongation (
str): The padding mode for the prolongation convolutions. - padding_mode_restriction (
str): The padding mode for the restriction convolutions.
Using padding_mode="circular"
In order to be compatible with the underlying equinox.nn.Conv
class, all padding parameters in Conv2d instances are set
to padding="SAME" if padding_mode="circular" (in MgNO_NS variant).
The padding of the transposed prolongation convolutions are kept as is.
Restricted Usability
MgConv is not a general n-dimensional implementation. This mirrors the
reference implementation.
In its current architecture it is only usable with 2D inputs matching
the experiments of the original publication (e.g. Helmholtz, Darcy or Navier Stokes)
with their exact grid dimensions.
Configuration of this Class
The configuration of the task specific architectures of the original publication
is all done within this class. Have a look at the parameters to configure
this class as needed. There are no exhaustive safety checks to validate all
correct parameter combinations for each task architecture (e.g. MgConv_helm, ...).
A model with an invalid configuration will just fail. That is the downside of
a single modular class.
__call__(f: Inexact[Array, ...]) -> Inexact[Array, ...]
¤
Runs multigrid V-cycle convolution.
First computes the restriction steps, that is pre-smoothing followed by restricting at each level. Then computes the prolongated coarse corrections, (applies layer norm) and post-smoothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Inexact[Array, ...]
|
The input data field. |
required |
Returns:
| Type | Description |
|---|---|
Inexact[Array, ...]
|
The approximated solution field u. |
Multigrid Iteration¤
neojax.nn.mg_iter.MgIter
¤
Class representing one multigrid smoothing iteration.
Computes the initial smoothing update \(u = S(f)\) or the update \(u \leftarrow u + S(f - A(u))\) on subsequent iterations, where \(S\) is the learnable smoothing operator and \(A\) is the approximated PDE operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smoothing_op
|
Module
|
Learnable smoothing operator S. |
required |
pde_op
|
Module | None
|
Learnable operator approximating the PDE operator A.
For the initial smoothing step, this may be |
None
|
Internal Attributes
These fields store the internal attributes.
- **** (
eqx.Module): Learnable smoothing operator S. - **** (
eqx.Module | None): Learnable PDE operator A.
__call__(f: Inexact[Array, ...], u: Inexact[Array, ...] | None = None, *, key: PRNGKeyArray | None = None) -> tuple[Inexact[Array, ...], Inexact[Array, ...]]
¤
Applies one smoothing step to the solution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Inexact[Array, ...]
|
The data field. |
required |
u
|
Inexact[Array, ...] | None
|
The solution field. |
None
|
key
|
PRNGKeyArray | None
|
Ignored. Kept for compatibility with |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Inexact[Array, ...], Inexact[Array, ...]]
|
Input data field |
Multigrid Restriction¤
neojax.nn.mg_restrict.MgRestrict
¤
Multigrid restriction operator.
Projects (u, f) to a coarser grid.
Optionally computes the residual \(f - A(u)\) before restricting, which corresponds to the 'full approximation scheme' (FAS) if A is given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sol_restrict_op
|
Module
|
Learnable restriction operator for the solution u. |
required |
data_restrict_op
|
Module
|
Learnable restriction operator for the data f. |
required |
pde_op
|
Module | None
|
Learnable operator approximating the PDE operator A. Used to compute
the residual if passed. Default is |
None
|
Internal Attributes
These fields store the internal attributes.
- sol_restrict_op (
eqx.Module): Restriction operator for the solution u. - data_restrict_op (
eqx.Module): Restriction operator for the data f. - pde_op (
eqx.Module | None): Learnable PDE operator.
__call__(f: Inexact[Array, ...], u: Inexact[Array, ...]) -> tuple[Inexact[Array, ...], Inexact[Array, ...]]
¤
Restricts (prolongates) solution and data to coarser grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Inexact[Array, ...]
|
The data field. |
required |
u
|
Inexact[Array, ...]
|
The solution field. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Inexact[Array, ...], Inexact[Array, ...]]
|
The data field |