RandomFourierKernelND#
- class RandomFourierKernelND(
- out_dim,
- data_dim,
- mlp_hidden_dim,
- num_layers,
- embedding_dim,
- omega_0,
- L_cache,
- use_bias,
- nonlinear_cfg,
- init_method=None,
Bases:
ModuleLearned convolutional kernel parametrised via Random Fourier Features and an MLP.
Mathematical form#
The kernel at grid coordinate
xis:k(x) = Linear_out( MLP( phi(x) ) )
where:
phi(x) = [cos(W x + b), sin(W x + b)]is the RFF positional embedding (seeRandomFourierPositionalEmbeddingND).MLPis a stack ofnum_layers - 1fully-connected layers each followed by thenonlinear_cfgactivation.Linear_outis a final linear layer that maps toout_dimchannels.
The output is a kernel tensor of shape
[1, *spatial_dims, out_dim]suitable for passing directly to the FFT convolution primitives innvsubquadratic.ops(after rearranging to channels-first layout via the consumingCKConvNDmodule).Hyperparameters controlling bandwidth / smoothness#
omega_0: Controls the frequency content of the RFF features. Higher values concentrate the kernel’s spectral energy at higher frequencies, producing a narrower, higher-bandwidth filter. Typical range: 1.0–100.0.mlp_hidden_dim: Width of the hidden MLP layers. Larger values allow more expressive kernel shapes.num_layers: Depth of the MLP. Must be >= 2 (one hidden layer + output layer minimum).
Initialisation#
Hidden MLP layers are initialised with the user-supplied
init_methodif provided; otherwise use PyTorch defaults.The output layer applies Wang initialisation: weights are scaled by
sqrt(1 / kernel_volume)wherekernel_volume = prod(L_cache_per_axis)(collapses toL_cache**data_dimfor an isotropic grid). This normalises the kernel’s initial energy to be independent of grid size.
Hidden width of the MLP.
- Type:
- positional_embedding#
RFF encoder.
- kernel_network#
Hidden MLP layers.
- Type:
- out_linear#
Final projection to
out_dimchannels.- Type:
- __init__(
- out_dim,
- data_dim,
- mlp_hidden_dim,
- num_layers,
- embedding_dim,
- omega_0,
- L_cache,
- use_bias,
- nonlinear_cfg,
- init_method=None,
Initialize the RandomFourierKernelND class.
- Parameters:
out_dim (int) – Number of output channels for the generated kernel.
data_dim (int) – Number of spatial/temporal input dimensions (size of coordinate vector).
mlp_hidden_dim (int) – Hidden width of the network.
num_layers (int) – Total number of layers including the first and hidden layers (>= 2).
embedding_dim (int) – Dimensionality of the positional embeddings.
omega_0 (float) – Frequency scaling factor for the positional embeddings.
L_cache (int | Sequence[int]) – Per-axis cache extents. Either a scalar int (broadcast to every axis, isotropic grid of size
L_cache**data_dim) or a sequence of lengthdata_dim(anisotropic grid of sizeprod(L_cache)). The Wang init below uses the product of per-axis extents so it stays correct in both cases.use_bias (bool) – Whether to use bias in the network and embedding layers.
nonlinear_cfg (LazyConfig) – Configuration for the nonlinear activation function.
init_method (Callable[[int], Callable[[Tensor], Tensor]] | None) – Optional initialization method for the kernel network.
- forward(seq_lens, conditioning=None)#
Compute the RFF kernel for a given grid of spatial dimensions.
- Parameters:
- Returns:
torch.Tensor: Kernel values of shape
[1, *spatial_dims, out_dim].torch.Tensor: Coordinate grid of shape
[1, *spatial_dims, data_dim].
- Return type: