Model Components¶
Shared building blocks used across all model architectures.
components
¶
Shared neural network components for FlowPDE models.
This module contains reusable building blocks used across all architectures: - FourierTimeEmbedding: Sinusoidal time encoding - Normalization factories - Activation functions - Dimension-agnostic convolution utilities
FourierTimeEmbedding
¶
Bases: Module
Sinusoidal time embedding for flow matching and diffusion models.
Maps scalar time $\(t \in [0, 1]\)$ to high-dimensional feature vector using sinusoids at exponentially spaced frequencies. This provides a smooth, continuous representation of time that networks can easily learn from.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Embedding dimension (must be even) |
128
|
max_period
|
float
|
Maximum period for lowest frequency sinusoid |
10000.0
|
learnable
|
bool
|
If True, add a learnable linear projection |
False
|
Source code in flowpde/models/components.py
forward(t)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Tensor
|
Time tensor of shape (batch_size,) or (batch_size, 1) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Embedding of shape (batch_size, dim) |
Source code in flowpde/models/components.py
TimeMLPEmbedding
¶
Bases: Module
MLP-based time embedding that first applies Fourier features.
Combines FourierTimeEmbedding with a small MLP for richer representations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
Output embedding dimension |
128
|
hidden_mult
|
int
|
Hidden layer multiplier (default: 4) |
4
|
activation
|
str
|
Activation function name |
'silu'
|
Source code in flowpde/models/components.py
DimensionalConv
¶
Bases: Module
Dimension-agnostic convolution layer.
Automatically uses Conv1d or Conv2d based on spatial_dim parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spatial_dim
|
int
|
Spatial dimensionality (1 or 2) |
required |
in_channels
|
int
|
Number of input channels |
required |
out_channels
|
int
|
Number of output channels |
required |
kernel_size
|
int
|
Convolution kernel size |
3
|
stride
|
int
|
Convolution stride |
1
|
padding
|
Optional[int]
|
Padding (default: kernel_size // 2 for 'same' padding) |
None
|
groups
|
int
|
Number of groups for grouped convolution |
1
|
bias
|
bool
|
Whether to include bias |
True
|
Source code in flowpde/models/components.py
get_activation(name='silu')
¶
Factory function for activation functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Activation name ('silu'/'swish', 'relu', 'gelu', 'tanh', 'leaky_relu') |
'silu'
|
Returns:
| Type | Description |
|---|---|
Module
|
nn.Module activation function |
Source code in flowpde/models/components.py
get_num_groups(channels, preferred=32)
¶
Compute valid number of groups for GroupNorm.
Tries preferred number first, then falls back to largest divisor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels
|
int
|
Number of channels |
required |
preferred
|
int
|
Preferred number of groups |
32
|
Returns:
| Type | Description |
|---|---|
int
|
Valid number of groups that divides channels |
Source code in flowpde/models/components.py
get_norm_layer(norm_type, num_features, spatial_dim=2, **kwargs)
¶
Factory function for normalization layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
norm_type
|
str
|
Type of normalization ('group', 'batch', 'instance', 'layer', 'none') |
required |
num_features
|
int
|
Number of features/channels |
required |
spatial_dim
|
int
|
Spatial dimensionality (1 or 2) for batch/instance norm |
2
|
**kwargs
|
Any
|
Additional arguments passed to norm layer |
{}
|
Returns:
| Type | Description |
|---|---|
Module
|
Normalization layer |
Source code in flowpde/models/components.py
get_conv_layer(spatial_dim)
¶
Get Conv1d or Conv2d class based on spatial dimension.
Source code in flowpde/models/components.py
get_conv_transpose_layer(spatial_dim)
¶
Get ConvTranspose1d or ConvTranspose2d class based on spatial dimension.
Source code in flowpde/models/components.py
get_pool_layer(spatial_dim, pool_type='max')
¶
Get pooling layer class based on spatial dimension.
Source code in flowpde/models/components.py
init_weights(module, zero_init_last=True, final_modules=None)
¶
Initialize weights using Kaiming initialization, zeroing the final layer.
Zero-initializing the output layer makes the model predict v = 0 at step zero, which is the standard stable start for flow-matching and diffusion training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
Module to initialize. |
required |
zero_init_last
|
bool
|
If True, zero-initialize the layers named by
|
True
|
final_modules
|
Optional[Iterable[Module]]
|
The layers that count as "final", given explicitly.
Every architecture in this package passes its own, because name
matching gets this wrong in both directions: it cannot tell a
model's |
None
|
Source code in flowpde/models/components.py
expand_time_embedding(t_emb, spatial_dim)
¶
Expand time embedding for broadcasting with spatial tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t_emb
|
Tensor
|
Time embedding of shape (B, C) |
required |
spatial_dim
|
int
|
Number of spatial dimensions (1 or 2) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Expanded tensor ready for broadcasting: (B, C, 1) or (B, C, 1, 1) |