ODE Attention
ODE-style continuous-depth attention block.
Models the attention transformation as an ordinary differential equation initial value problem:
z(1) = z(0) + integral_0^1 f(z(t), t) dt
where f is a self-attention derivative function. The ODE is solved via
explicit numerical integration (Euler or RK4) with a configurable number of
steps. Weights are shared across all integration steps, providing a
continuous-depth parameterization.
- Reference:
Zhang et al. (2021), “ODE Transformer: An Ordinary Differential Equation Inspired Transformer”, ACL 2022.
- class src.model.attention.ode.ODEAttentionBlock(*args: Any, **kwargs: Any)[source]
Bases:
ModuleContinuous-depth attention block via ODE integration.
Solves the initial value problem:
z(1) = z(0) + integral_0^1 f(z(t), t) dt
using an explicit numerical ODE solver (Euler or classical RK4). The number of integration steps is configurable. Weights in
ODEFuncare shared across all steps, so the parameter count is independent of the number of solver steps.- Reference:
Zhang et al. (2021), “ODE Transformer”, ACL 2022.
- Parameters:
config – Model configuration object with attributes
hidden_size,num_heads,dropout,use_bitnet,norm_type,ode_solver("euler"or"rk4"),ode_steps, and optionallymode.
- ode_func
The
ODEFuncmodule representingf(z, t).
- solver
Integration method (
"euler"or"rk4").
- steps
Number of integration steps from
t=0tot=1.
- class src.model.attention.ode.ODEFunc(*args: Any, **kwargs: Any)[source]
Bases:
ModuleDerivative function
dx/dtmodeled as multi-head self-attention.This module represents the right-hand side
f(z(t), t)of the ODE. It applies normalization, a fused QKV projection, scaled dot-product attention with softmax, and an output projection. The same weights are reused at every integration step, enabling a continuous-depth parameterization with constant parameter count regardless of the number of solver steps.- Reference:
Zhang et al. (2021), “ODE Transformer”, ACL 2022.
- Parameters:
config – Model configuration object with attributes
hidden_size,num_heads,dropout,use_bitnet,norm_type, and optionallymode("encoder"or"decoder").
- dim
Dimensionality of the input and output embeddings.
- num_heads
Number of parallel attention heads.
- head_dim
Dimensionality of each attention head (
dim // num_heads).
- scale
Scaling factor
1 / sqrt(head_dim)applied to dot products.
- qkv
Fused linear (or BitLinear) projection for queries, keys, and values (outputs
dim * 3features).
- out_proj
Linear (or BitLinear) output projection.
- norm
Normalization layer applied before the QKV projection.
- dropout
Dropout layer applied to attention weights.
- mode
"encoder"for bidirectional,"decoder"for causal.
- forward(t, x)[source]
Evaluate the derivative function at time
t.- Parameters:
t – Current integration time (float; unused in the attention computation but accepted for ODE solver interface).
x – State tensor of shape
(batch_size, seq_len, dim).
- Returns:
Derivative tensor
dx/dtof shape(batch_size, seq_len, dim).