SpikeDE.solver
This module delivers a comprehensive, differentiable numerical engine designed to simulate Spiking Neural Networks (SNNs) governed by Fractional Differential Equations (FDEs). Bridging the gap between fractional calculus and deep learning, this module supports both Riemann-Liouville and Caputo formulations through a diverse array of high-order discretization schemes, including Grünwald-Letnikov (GL), Product Trapezoidal, L1, and Adams-Bashforth methods. It enables precise modeling of complex temporal dynamics while maintaining full compatibility with gradient-based optimization.
Whether used for forward inference via snn_solve or for training sophisticated fractional SNNs, the module provides a mathematically rigorous foundation for next-generation neural dynamics. Its architecture is built to handle advanced requirements such as per-layer fractional orders, multi-term distributed-order equations, and efficient memory management, ensuring scalability for long-sequence modeling.
Key Features
- Diverse Discretization Schemes: Implements multiple high-precision numerical methods (Grünwald-Letnikov, Product Trapezoidal, L1, Adams-Bashforth) to solve FDEs under both Riemann-Liouville and Caputo definitions.
- Advanced Fractional Configurations: Natively supports per-layer fractional orders, allowing different layers to exhibit distinct memory properties, and handles multi-term distributed-order equations for complex dynamical systems.
- Flexible Solver Interface: Provides a unified API (
snn_solve) alongside low-level integration primitives (gl_integrate_tuple,l1_integrate_tuple, etc.) for custom solver development and fine-grained control over state evolution.
PerLayerAlphaInfo
dataclass
PerLayerAlphaInfo(
alpha: Tensor,
is_multi_term: bool,
coefficient: Tensor | None = None,
h_alpha: Tensor | None = None,
h_alpha_gamma: Tensor | None = None,
h_alpha_over_alpha_gamma: Tensor | None = None,
)
Metadata container for the fractional order (\(\alpha\)) configuration of a single layer.
Stores the fractional order(s) and precomputed constants required for numerical integration.
To ensure gradient flow during backpropagation when \(\alpha\) is learnable, all values are
stored as torch.Tensor objects rather than Python floats.
Attributes:
-
alpha(Tensor) –A tensor containing the fractional order(s). Shape
(1,)for single-term, shape(M,)for multi-term with \(M\) terms. -
is_multi_term(bool) –Boolean flag indicating if the layer has multiple fractional terms (\(M > 1\)).
-
coefficient(Tensor | None) –Optional tensor of coefficients \([c_1, ..., c_M]\) for multi-term equations. Defaults to ones if not provided.
-
h_alpha(Tensor | None) –Precomputed \(h^\alpha\) (Single-term only).
-
h_alpha_gamma(Tensor | None) –Precomputed \(h^\alpha \cdot \Gamma(2-\alpha)\) (Single-term only).
-
h_alpha_over_alpha_gamma(Tensor | None) –Precomputed \(h^\alpha / (\alpha \cdot \Gamma(\alpha))\) (Single-term only).
- API Reference SpikeDE.solver SNNSolverConfig
SNNSolverConfig
dataclass
SNNSolverConfig(
N: int,
h: Tensor,
device: device,
dtype: dtype,
n_components: int,
n_integrate: int,
per_layer_info: list[PerLayerAlphaInfo] = list(),
)
Central configuration object for SNN fractional solvers.
Aggregates simulation parameters, device information, and per-layer fractional metadata to streamline the solver execution loop.
Attributes:
-
N(int) –Number of time points in the grid.
-
h(Tensor) –Step size tensor (scalar), assumed uniform \(h = t_{k+1} - t_k\).
-
device(device) –Torch device for computation.
-
dtype(dtype) –Torch data type for computation.
-
n_components(int) –Total number of state components (neurons + boundaries).
-
n_integrate(int) –Number of components to integrate (excludes boundary outputs).
-
per_layer_info(list[PerLayerAlphaInfo]) –List of
PerLayerAlphaInfoobjects, one per integrated layer.
- API Reference SpikeDE.solver SNNSolverConfig from_inputs
- API Reference SpikeDE.solver SNNFractionalMethod compute_convolution
- API Reference SpikeDE.solver SNNFractionalMethod compute_update_for_layer
- API Reference SpikeDE.solver SNNFractionalMethod compute_weights_for_layer
- API Reference SpikeDE.solver SNNFractionalMethod initialize
from_inputs
classmethod
from_inputs(
y0_tuple: tuple[Tensor, ...],
per_layer_alpha: list[Any],
t_grid: Tensor,
per_layer_coefficient: list[Tensor | None] | None = None,
) -> SNNSolverConfig
Constructs a solver configuration from user inputs.
Processes raw alpha inputs (scalars, lists, or tensors) into standardized
PerLayerAlphaInfo objects. Precomputes constants involving the Gamma function
for single-term solvers to optimize the main integration loop.
Parameters:
-
y0_tuple(tuple[Tensor, ...]) –Tuple of initial state tensors. Used to infer device and dtype.
-
per_layer_alpha(list[Any]) –List of alpha values. Each element can be:
float: Single-term scalar.torch.Tensor: 1-element (single-term) or M-element (multi-term).list: Converted to tensor.
-
t_grid(Tensor) –Time grid tensor.
-
per_layer_coefficient(list[Tensor | None] | None, default:None) –Optional list of coefficient tensors for multi-term layers.
Returns:
-
SNNSolverConfig–A configured
SNNSolverConfiginstance.
Raises:
-
AssertionError–If
t_gridhas fewer than 2 points or coefficient dimensions mismatch alpha.
Source code in spikeDE/solver.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
SNNFractionalMethod
Bases: ABC
Abstract Base Class (ABC) for SNN fractional differential equation solvers.
Defines the interface for various numerical methods (GL, L1, Trapezoidal, etc.). Implementations must define how weights are computed, how convolutions are performed, and how the state update is calculated.
Subclasses distinguish themselves by:
- The formulation used (Riemann-Liouville vs. Caputo).
- The type of history stored (\(y\) values vs. \(f(t,y)\) values).
- Support for single-term vs. multi-term equations.
- API Reference SpikeDE.solver AdamsBashforthSNN
- API Reference SpikeDE.solver GrunwaldLetnikovMultitermSNN
- API Reference SpikeDE.solver GrunwaldLetnikovSNN
- API Reference SpikeDE.solver L1MethodSNN
- API Reference SpikeDE.solver ProductTrapezoidalSNN
- API Reference SpikeDE.solver snn_solve
stores_f_history
abstractmethod
property
stores_f_history: bool
Indicates whether the method stores function evaluations \(f(t, y)\) or state values \(y\) in history.
Returns:
-
bool–Trueif the method (e.g., Adams-Bashforth) relies on \(f\)-history,Falseif the method (e.g., GL, L1) relies on \(y\)-history.
compute_convolution
abstractmethod
compute_convolution(
k: int,
start_idx: int,
weights: Any,
history_i: list[Tensor],
config: SNNSolverConfig,
layer_idx: int,
) -> Any
Computes the weighted sum (convolution) of history values.
Calculates \(\sum w_j \cdot h_j\), where \(h_j\) is either \(y_j\) or \(f_j\) depending on stores_f_history.
Parameters:
-
k(int) –Current time step index.
-
start_idx(int) –Start index of the history window.
-
weights(Any) –Weights computed by
compute_weights_for_layer. -
history_i(list[Tensor]) –List of historical tensors for the current component.
-
config(SNNSolverConfig) –Solver configuration.
-
layer_idx(int) –Index of the layer.
Returns:
-
Any–The result of the convolution sum (tensor).
Source code in spikeDE/solver.py
compute_update_for_layer
abstractmethod
compute_update_for_layer(
f_k_i: Tensor, convolution_sum: Any, config: SNNSolverConfig, layer_idx: int
) -> Tensor
Computes the next state \(y_{k+1}\) for a specific layer.
Combines the current derivative \(f_k\) and the convolution sum according to the method's formula.
Note: The method name in the original code was slightly misleading; this function computes the update,
while compute_convolution computes the sum. Based on usage in snn_solve, this function
applies the final formula. However, looking at the implementation in subclasses,
compute_convolution actually performs the summation loop, and this function applies the scaling.
Correction based on code analysis:
compute_convolution returns the sum \(\sum w_j h_j\).
compute_update_for_layer takes that sum and \(f_k\) to return \(y_{k+1}\).
Parameters:
-
f_k_i(Tensor) –The derivative/value \(f(t_k, y_k)\) for this layer.
-
convolution_sum(Any) –The result of the history convolution.
-
config(SNNSolverConfig) –Solver configuration.
-
layer_idx(int) –Index of the layer.
Returns:
-
Tensor–The updated state tensor \(y_{k+1}\).
Source code in spikeDE/solver.py
compute_weights_for_layer
abstractmethod
compute_weights_for_layer(
k: int, start_idx: int, config: SNNSolverConfig, layer_idx: int
) -> Any
Computes the convolution weights for a specific layer at time step \(k\).
Parameters:
-
k(int) –Current time step index.
-
start_idx(int) –Start index of the history window.
-
config(SNNSolverConfig) –Solver configuration containing layer metadata.
-
layer_idx(int) –Index of the layer being processed.
Returns:
-
Any–A tensor or structure containing the weights \(w_j\) for the convolution sum.
Source code in spikeDE/solver.py
initialize
initialize(config: SNNSolverConfig) -> None
Optional hook for method-specific precomputation before the time loop.
Used to precompute static coefficients (e.g., GL binomial coefficients) that depend on \(\alpha\) and \(N\) but not on the state \(y\).
Parameters:
-
config(SNNSolverConfig) –Solver configuration.
Source code in spikeDE/solver.py
GrunwaldLetnikovSNN
Bases: SNNFractionalMethod
Grünwald-Letnikov (GL) solver for single-term Riemann-Liouville Fractional Differential Equations (FDEs).
This class implements the standard GL discretization scheme, which approximates the Riemann-Liouville fractional derivative \(D^\alpha y(t)\) using a finite difference convolution.
Mathematical Formulation: The update rule for the state \(y\) at step \(k+1\) is given by:
where \(h\) is the step size, \(f(t, y)\) is the ODE function, and \(c_j^{(\alpha)}\) are the Grünwald-Letnikov coefficients generated recursively:
Key Characteristics:
- Formulation: Riemann-Liouville.
- Accuracy: First-order \(O(h)\).
- Memory: Requires full history of states \(y\) unless truncated.
- Constraint: Strictly supports single-term fractional orders (\(\alpha\) is a scalar per layer).
Attempting to use multi-term \(\alpha\) will raise a
ValueError. For multi-term support, useGrunwaldLetnikovMultitermSNN.
Source code in spikeDE/solver.py
ProductTrapezoidalSNN
Bases: SNNFractionalMethod
Product Trapezoidal solver for single-term Riemann-Liouville FDEs.
This method offers higher accuracy (\(O(h^2)\)) compared to the Grünwald-Letnikov scheme by using a piecewise linear interpolation of the integrand. It is particularly effective for smooth solutions.
The update rule is:
The weights \(A_{j,k+1}\) are position-dependent and defined as:
- For \(j=0\):
$\(A_{0,k+1} = k^{1-\alpha} - (k+\alpha)(k+1)^{-\alpha}\)$
- For \(j \ge 1\):
$\(A_{j,k+1} = (k+2-j)^{1-\alpha} + (k-j)^{1-\alpha} - 2(k+1-j)^{1-\alpha}\)$
Key Characteristics:
- Formulation: Riemann-Liouville.
- Accuracy: Second-order \(O(h^2)\).
- Constraint: Supports single-term \(\alpha\) only.
L1MethodSNN
Bases: SNNFractionalMethod
L1 scheme solver for single-term Caputo Fractional Differential Equations.
The L1 method is the most widely used numerical scheme for Caputo derivatives, offering an accuracy of \(O(h^{2-\alpha})\) for smooth solutions. It approximates the fractional derivative using piecewise linear interpolation of the function.
Mathematical Formulation: The update rule is:
The coefficients \(c_j^{(k)}\) are defined as:
- For \(j=0\):
$\(c_0^{(k)} = -\left((k+1)^{1-\alpha} - k^{1-\alpha}\right)\)$
- For \(j \ge 1\):
$\(c_j^{(k)} = (k-j+2)^{1-\alpha} - 2(k-j+1)^{1-\alpha} + (k-j)^{1-\alpha}\)$
Key Characteristics:
- Formulation: Caputo.
- Accuracy: \(O(h^{2-\alpha})\).
- Constraint: Single-term \(\alpha\) only.
AdamsBashforthSNN
Bases: SNNFractionalMethod
Adams-Bashforth predictor method for single-term Caputo FDEs.
This method serves as a predictor step in predictor-corrector schemes (like PECE). Unlike the other methods which convolve state history \(y_j\), Adams-Bashforth convolves the history of function evaluations \(f(t_j, y_j)\).
The update rule is:
where the weights are:
Key Characteristics:
- Formulation: Caputo (Predictor).
- History Type: Stores \(f(t, y)\) instead of \(y\).
- Constraint: Single-term \(\alpha\) only.
GrunwaldLetnikovMultitermSNN
Bases: SNNFractionalMethod
Unified Grünwald-Letnikov solver for multi-term Riemann-Liouville FDEs.
This solver handles distributed-order or multi-term equations of the form:
It generalizes the single-term GL method by aggregating the coefficients from each term into a single effective convolution kernel.
The discretization leads to the update rule:
where the aggregated coefficients \(\tilde{c}_m\) are computed as:
Here, \(c_i\) are the user-defined equation coefficients, \(h^{-\alpha_i}\) scales by step size, and \(c_m^{(\alpha_i)}\) are the standard GL coefficients for order \(\alpha_i\).
Key Characteristics:
- Formulation: Riemann-Liouville (Multi-term).
- Flexibility: Supports both single-term (as a 1-term case) and multi-term layers.
- Gradient Flow: Fully differentiable with respect to \(\alpha_m\) and coefficients \(c_m\).
Source code in spikeDE/solver.py
FDEAdjointMethod
Bases: Function
Custom Autograd Function for Fractional Differential Equations with Adjoint Sensitivity.
This class implements the forward and backward passes required for differentiating through FDE solvers. It supports various numerical schemes (GL, Trapezoidal, L1, Adams-Bashforth) and handles the complexity of fractional memory terms during backpropagation.
Mathematical Formulation: The adjoint state \(\lambda(t)\) satisfies the fractional adjoint equation:
solved backwards from \(t=T\) to \(t=0\). Parameter gradients are computed via:
backward
staticmethod
Performs the backward adjoint integration to compute gradients.
Reconstructs the augmented dynamics system and solves it backwards in time to obtain gradients with respect to initial states (\(y_0\)) and model parameters (\(\theta\)).
Parameters:
-
ctx(FunctionCtx) –Context object containing saved tensors from forward pass.
-
*grad_output(Tensor, default:()) –Gradients of the loss with respect to the output states \(y(t_{end})\).
Returns:
-
tuple[Any | None, ...]–A tuple of gradients corresponding to the inputs of
forward:(grad_func, grad_n_state, grad_n_params, grad_y0..., grad_alpha, grad_t_grid, grad_method, grad_params..., grad_memory). Non-tensor inputs returnNone.
Source code in spikeDE/solver.py
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 | |
forward
staticmethod
forward(
ctx: FunctionCtx,
ode_func: Callable,
n_state: int,
n_params: int,
*args: Any,
) -> tuple[Tensor, ...]
Performs the forward integration of the FDE.
Unpacks arguments, selects the appropriate solver based on method, and computes
the state trajectory. Saves necessary context for the backward pass.
Parameters:
-
ctx(FunctionCtx) –Context object to save tensors for backward pass.
-
ode_func(Callable) –The ODE function \(f(t, y)\).
-
n_state(int) –Number of state components in
y0_tuple. -
n_params(int) –Number of learnable parameters in
ode_func. -
*args(Any, default:()) –Packed arguments containing:
y0_tuple: Initial states (n_state tensors).alpha: Fractional order.t_grid: Time grid.method: Solver method string.func_params: Model parameters (n_params tensors).memory: Memory truncation limit.
Returns:
Source code in spikeDE/solver.py
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 | |
snn_solve
snn_solve(
ode_func: Callable[[Tensor, Tuple], tuple],
y0_tuple: tuple[Tensor, ...],
per_layer_alpha: list[Any],
t_grid: Tensor,
method: SNNFractionalMethod,
memory: int | None = None,
per_layer_coefficient: list[Tensor | None] | None = None,
) -> list[list[Tensor]]
Unified driver function for solving SNN fractional differential equations.
Orchestrates the time-stepping loop, managing state history, memory truncation, and dispatching to the specific numerical method provided.
Parameters:
-
ode_func(Callable[[Tensor, Tuple], tuple]) –Function
f(t, y_tuple)returning derivatives. -
y0_tuple(tuple[Tensor, ...]) –Initial state tuple.
-
per_layer_alpha(list[Any]) –List of fractional orders per layer.
-
t_grid(Tensor) –Time points tensor.
-
method(SNNFractionalMethod) –Instance of
SNNFractionalMethod(e.g.,GrunwaldLetnikovSNN). -
memory(int | None, default:None) –Optional integer to limit history length for convolution.
-
per_layer_coefficient(list[Tensor | None] | None, default:None) –Coefficients for multi-term layers.
Returns:
Source code in spikeDE/solver.py
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 | |
euler_integrate_tuple
euler_integrate_tuple(
ode_func: Callable[[Tensor, Tuple[Tensor, ...]], tuple[Tensor, ...]],
y0_tuple: tuple[Tensor, ...],
t_grid: Tensor,
neuron_count: int,
) -> list[list[Tensor]]
Performs standard explicit Euler integration for integer-order ODEs (\(D^1 y = f(t, y)\)).
This function distinguishes between dynamic state variables (neurons) which are integrated, and boundary outputs (e.g., spike outputs) which are treated as pass-through values computed directly from the derivative without accumulation.
The update rule for integrated components is:
Parameters:
-
ode_func(Callable[[Tensor, Tuple[Tensor, ...]], tuple[Tensor, ...]]) –A callable
f(t, y_tuple)returning a tuple of derivatives. Expected format:(dy_1, ..., dy_N, boundary_1, ...). -
y0_tuple(tuple[Tensor, ...]) –A tuple of initial state tensors
(y_1, ..., y_N, boundary_1, ...). -
t_grid(Tensor) –A 1D tensor of time points
[t_0, t_1, ..., t_N]. Step sizes can be non-uniform. -
neuron_count(int) –The number of components in the state tuple representing dynamic neurons to be integrated. Components beyond this index are treated as pass-through boundaries.
Returns:
-
list[list[Tensor]]–A list of lists, where
history[i][k]is the state of componentiat time stepk+1. -
list[list[Tensor]]–The length of each inner list is
len(t_grid) - 1.
Raises:
-
AssertionError–If
y0_tupleis not a tuple ort_gridhas fewer than 2 points.
Source code in spikeDE/solver.py
gl_integrate_tuple
gl_integrate_tuple(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
per_layer_alpha: list[Any],
t_grid: Tensor,
memory: int | None = None,
per_layer_coefficient: list[Tensor | None] | None = None,
) -> list[list[Tensor]]
Solves FDEs using the Grünwald-Letnikov (GL) method with per-layer alpha support.
Automatically switches to GrunwaldLetnikovMultitermSNN if any layer has multi-term alpha.
Suitable for Riemann-Liouville formulations.
Parameters:
-
ode_func(Callable) –Function
f(t, y_tuple)returning derivatives. -
y0_tuple(tuple[Tensor, ...]) –Initial state tuple.
-
per_layer_alpha(list[Any]) –List of alpha values, one per integrated component. Each can be scalar (single-term) or list/tensor (multi-term).
-
t_grid(Tensor) –Time points tensor.
-
memory(int | None, default:None) –Optional memory truncation length.
-
per_layer_coefficient(list[Tensor | None] | None, default:None) –Coefficients for multi-term layers.
Returns:
Source code in spikeDE/solver.py
trap_integrate_tuple
trap_integrate_tuple(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
per_layer_alpha: list[Any],
t_grid: Tensor,
memory: int | None = None,
per_layer_coefficient: list[Tensor | None] | None = None,
) -> list[list[Tensor]]
Solves FDEs using the Product Trapezoidal method with per-layer alpha support.
Note
If any layer has multi-term alpha, automatically falls back to GL multiterm with a warning. Offers higher accuracy (\(O(h^2)\)) for single-term Riemann-Liouville equations.
Parameters:
-
ode_func(Callable) –Function
f(t, y_tuple)returning derivatives. -
y0_tuple(tuple[Tensor, ...]) –Initial state tuple.
-
per_layer_alpha(list[Any]) –List of alpha values.
-
t_grid(Tensor) –Time points tensor.
-
memory(int | None, default:None) –Optional memory truncation length.
-
per_layer_coefficient(list[Tensor | None] | None, default:None) –Coefficients for multi-term layers.
Returns:
Source code in spikeDE/solver.py
l1_integrate_tuple
l1_integrate_tuple(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
per_layer_alpha: list[Any],
t_grid: Tensor,
memory: int | None = None,
per_layer_coefficient: list[Tensor | None] | None = None,
) -> list[list[Tensor]]
Solves FDEs using the L1 scheme with per-layer alpha support.
Note
If any layer has multi-term alpha, automatically falls back to GL multiterm with a warning. Commonly used for Caputo formulations with accuracy \(O(h^{2-\alpha})\).
Parameters:
-
ode_func(Callable) –Function
f(t, y_tuple)returning derivatives. -
y0_tuple(tuple[Tensor, ...]) –Initial state tuple.
-
per_layer_alpha(list[Any]) –List of alpha values.
-
t_grid(Tensor) –Time points tensor.
-
memory(int | None, default:None) –Optional memory truncation length.
-
per_layer_coefficient(list[Tensor | None] | None, default:None) –Coefficients for multi-term layers.
Returns:
Source code in spikeDE/solver.py
pred_integrate_tuple
pred_integrate_tuple(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
per_layer_alpha: list[Any],
t_grid: Tensor,
memory: int | None = None,
per_layer_coefficient: list[Tensor | None] | None = None,
) -> list[list[Tensor]]
Solves FDEs using the Adams-Bashforth predictor with per-layer alpha support.
Note
If any layer has multi-term alpha, automatically falls back to GL multiterm with a warning. Uses \(f\)-history instead of \(y\)-history.
Parameters:
-
ode_func(Callable) –Function
f(t, y_tuple)returning derivatives. -
y0_tuple(tuple[Tensor, ...]) –Initial state tuple.
-
per_layer_alpha(list[Any]) –List of alpha values.
-
t_grid(Tensor) –Time points tensor.
-
memory(int | None, default:None) –Optional memory truncation length.
-
per_layer_coefficient(list[Tensor | None] | None, default:None) –Coefficients for multi-term layers.
Returns:
Source code in spikeDE/solver.py
fdeint_adjoint
fdeint_adjoint(
func: Callable[[Tensor, Tuple[Tensor, ...]], tuple[Tensor, ...]],
y0_tuple: tuple[Tensor, ...],
alpha: float | Tensor | list[float],
t_grid: Tensor,
method: str,
memory: int | None = None,
) -> tuple[Tensor, ...]
Solves a Fractional Differential Equation (FDE) with adjoint sensitivity analysis.
This function enables gradient-based optimization of both the initial states \(y_0\) and
the parameters of the ODE function func (e.g., neural network weights) with respect to
a loss function defined on the solution trajectory. It uses the continuous adjoint method
adapted for fractional calculus.
The workflow involves:
- Forward Pass: Solving \(D^\alpha y(t) = f(t, y(t), \theta)\) to obtain \(y(T)\).
- Backward Pass: Solving the augmented adjoint equation to compute \(\frac{\partial L}{\partial y_0}\) and \(\frac{\partial L}{\partial \theta}\).
Parameters:
-
func(Callable[[Tensor, Tuple[Tensor, ...]], tuple[Tensor, ...]]) –The ODE function \(f(t, y, \theta)\). Must accept
(t, y_tuple)and return a tuple of tensors. Parameters \(\theta\) are implicitly captured from the function's scope or registered modules. -
y0_tuple(tuple[Tensor, ...]) –A tuple of initial state tensors \((y_1^0, \dots, y_N^0)\).
-
alpha(float | Tensor | list[float]) –The fractional order(s). Can be a scalar, a tensor, or a list depending on the solver configuration.
-
t_grid(Tensor) –A 1D tensor of time points \([t_0, t_1, \dots, t_T]\) defining the integration interval.
-
method(str) –The numerical integration scheme identifier (e.g.,
'gl-f','trap-f','l1-f'). Suffixes-findicate full history storage required for adjoint,-ofor optimized/no-history. -
memory(int | None, default:None) –Optional integer to limit the memory length for convolution sums (short-memory principle). If
None, full history is used.
Returns:
-
tuple[Tensor, ...]–A tuple of tensors representing the solution at the final time point \(y(t_T)\), compatible with
torch.autogradfor backpropagation.
Note
This function wraps FDEAdjointMethod.apply. Ensure func contains parameters that require gradients if parameter optimization is desired.
Source code in spikeDE/solver.py
forward_euler_wo_history
forward_euler_wo_history(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
alpha: Any,
t_grid: Tensor,
memory: int | None = None,
) -> list[Tensor]
Explicit Euler integration without storing full history.
Solves \(y_{k+1} = y_k + h \cdot f(t_k, y_k)\). This variant is memory-efficient (\(O(1)\)) but insufficient for methods requiring history-dependent adjoints unless combined with checkpointing. Used primarily for integer-order baselines or specific optimized paths.
Parameters:
-
ode_func(Callable) –Function \(f(t, y)\).
-
y0_tuple(tuple[Tensor, ...]) –Initial state tuple.
-
alpha(Any) –Fractional order (unused in standard Euler, kept for signature compatibility).
-
t_grid(Tensor) –Time grid tensor.
-
memory(int | None, default:None) –Unused.
Returns:
Source code in spikeDE/solver.py
backward_euler_wo_history
backward_euler_wo_history(
ode_func: Callable,
y_aug: tuple[list, list, list],
alpha: Any,
t_grid: Tensor,
y_finalstate: list[Tensor],
memory: int | None = None,
) -> tuple[list[Tensor], list[Tensor]]
Backward integration for Euler method without full history dependency.
Since Euler has no memory term, the backward pass simply integrates the adjoint equation using the reconstructed forward trajectory (or re-evaluation).
Parameters:
-
ode_func(Callable) –Augmented dynamics function.
-
y_aug(tuple[list, list, list]) –Initial augmented state
(dummy_y, adj_y0, adj_params0). -
alpha(Any) –Unused.
-
t_grid(Tensor) –Flipped time grid.
-
y_finalstate(list[Tensor]) –Final state from forward pass (used as starting point for reconstruction if needed).
-
memory(int | None, default:None) –Unused.
Returns:
Source code in spikeDE/solver.py
forward_euler_w_history
forward_euler_w_history(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
alpha: Any,
t_grid: Tensor,
memory: int | None = None,
) -> list[list[Tensor]]
Explicit Euler integration storing full history.
Required for adjoint methods that expect a history list structure consistent with fractional solvers, even if the method itself is memory-less.
Parameters:
-
ode_func(Callable) –Function \(f(t, y)\).
-
y0_tuple(tuple[Tensor, ...]) –Initial state tuple.
-
alpha(Any) –Unused.
-
t_grid(Tensor) –Time grid.
-
memory(int | None, default:None) –Unused.
Returns:
-
list[list[Tensor]]–List of lists, where each inner list contains the trajectory of one state component.
Source code in spikeDE/solver.py
backward_euler_w_history
backward_euler_w_history(
ode_func: Callable,
y_aug: tuple[list, list, list],
alpha: Any,
t_grid: Tensor,
yhistory: list[list[Tensor]],
memory: int | None = None,
) -> tuple[list[Tensor], list[Tensor]]
Backward integration for Euler method using stored history.
Iterates backwards through the provided yhistory to compute adjoint updates.
Parameters:
-
ode_func(Callable) –Augmented dynamics.
-
y_aug(tuple[list, list, list]) –Initial augmented state.
-
alpha(Any) –Unused.
-
t_grid(Tensor) –Flipped time grid.
-
yhistory(list[list[Tensor]]) –Full forward trajectory.
-
memory(int | None, default:None) –Unused.
Returns:
Source code in spikeDE/solver.py
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 | |
forward_gl
forward_gl(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
alpha: float | Tensor,
t_grid: Tensor,
memory: int | None = None,
) -> list[list[Tensor]]
Forward Grünwald-Letnikov (GL) integration.
Implements the Riemann-Liouville approximation:
where coefficients \(c_j^{(\alpha)}\) are computed recursively.
Parameters:
-
ode_func(Callable) –Function \(f(t, y)\).
-
y0_tuple(tuple[Tensor, ...]) –Initial state tuple.
-
alpha(float | Tensor) –Fractional order \(\alpha \in (0, 1)\).
-
t_grid(Tensor) –Uniform time grid.
-
memory(int | None, default:None) –Max history length for truncation.
Returns:
Source code in spikeDE/solver.py
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 | |
backward_gl
backward_gl(
ode_func: Callable,
y_aug: tuple[list, list, list],
alpha: float | Tensor,
t_grid: Tensor,
yhistory: list[list[Tensor]],
memory: int | None = None,
) -> tuple[list[Tensor], list[Tensor]]
Backward Grünwald-Letnikov integration for adjoint sensitivity.
Solves the adjoint equation using the same GL discretization structure, accumulating gradients from the future (which is the past in reversed time).
Parameters:
-
ode_func(Callable) –Augmented dynamics.
-
y_aug(tuple[list, list, list]) –Initial augmented state (at reversed \(t=0\), i.e., forward \(t=T\)).
-
alpha(float | Tensor) –Fractional order.
-
t_grid(Tensor) –Flipped time grid.
-
yhistory(list[list[Tensor]]) –Forward trajectory (accessed in reverse).
-
memory(int | None, default:None) –Memory truncation limit.
Returns:
Source code in spikeDE/solver.py
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 | |
forward_trap
forward_trap(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
alpha: float | Tensor,
t_grid: Tensor,
memory: int | None = None,
) -> list[list[Tensor]]
Forward Product Trapezoidal method.
Provides \(O(h^2)\) accuracy for Riemann-Liouville FDEs. Formula:
where weights \(A_{j,k+1}\) depend on the distance from the current step.
Parameters:
-
ode_func(Callable) –Function \(f(t, y)\).
-
y0_tuple(tuple[Tensor, ...]) –Initial state.
-
alpha(float | Tensor) –Fractional order.
-
t_grid(Tensor) –Time grid.
-
memory(int | None, default:None) –Memory limit.
Returns:
Source code in spikeDE/solver.py
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 | |
backward_trap
backward_trap(
ode_func: Callable,
y_aug: tuple[list, list, list],
alpha: float | Tensor,
t_grid: Tensor,
yhistory: list[list[Tensor]],
memory: int | None = None,
) -> tuple[list[Tensor], list[Tensor]]
Backward Product Trapezoidal method for adjoint sensitivity.
Parameters:
-
ode_func(Callable) –Augmented dynamics.
-
y_aug(tuple[list, list, list]) –Initial augmented state.
-
alpha(float | Tensor) –Fractional order.
-
t_grid(Tensor) –Flipped time grid.
-
yhistory(list[list[Tensor]]) –Forward trajectory.
-
memory(int | None, default:None) –Memory limit.
Returns:
Source code in spikeDE/solver.py
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 | |
forward_l1
forward_l1(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
alpha: float | Tensor,
t_grid: Tensor,
memory: int | None = None,
) -> list[list[Tensor]]
Forward L1 scheme for Caputo FDEs.
Accuracy \(O(h^{2-\alpha})\). Formula:
Parameters:
-
ode_func(Callable) –Function \(f(t, y)\).
-
y0_tuple(tuple[Tensor, ...]) –Initial state.
-
alpha(float | Tensor) –Fractional order.
-
t_grid(Tensor) –Time grid.
-
memory(int | None, default:None) –Memory limit.
Returns:
Source code in spikeDE/solver.py
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 | |
backward_l1
backward_l1(
ode_func: Callable,
y_aug: tuple[list, list, list],
alpha: float | Tensor,
t_grid: Tensor,
yhistory: list[list[Tensor]],
memory: int | None = None,
) -> tuple[list[Tensor], list[Tensor]]
Backward L1 scheme for adjoint sensitivity.
Parameters:
-
ode_func(Callable) –Augmented dynamics.
-
y_aug(tuple[list, list, list]) –Initial augmented state.
-
alpha(float | Tensor) –Fractional order.
-
t_grid(Tensor) –Flipped time grid.
-
yhistory(list[list[Tensor]]) –Forward trajectory.
-
memory(int | None, default:None) –Memory limit.
Returns:
Source code in spikeDE/solver.py
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 | |
forward_pred
forward_pred(
ode_func: Callable,
y0_tuple: tuple[Tensor, ...],
alpha: float | Tensor,
t_grid: Tensor,
memory: int | None = None,
) -> list[list[Tensor]]
Forward Adams-Bashforth predictor method.
Uses history of function evaluations \(f(t, y)\) instead of states \(y\). Formula:
where \(b_{j,k+1} = \frac{h^\alpha}{\alpha \Gamma(\alpha)} [(k+1-j)^\alpha - (k-j)^\alpha]\).
Parameters:
-
ode_func(Callable) –Function \(f(t, y)\).
-
y0_tuple(tuple[Tensor, ...]) –Initial state.
-
alpha(float | Tensor) –Fractional order.
-
t_grid(Tensor) –Time grid.
-
memory(int | None, default:None) –Memory limit.
Returns:
Source code in spikeDE/solver.py
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 | |
backward_pred
backward_pred(
ode_func: Callable,
y_aug: tuple[list, list, list],
alpha: float | Tensor,
t_grid: Tensor,
yhistory: list[list[Tensor]],
memory: int | None = None,
) -> tuple[list[Tensor], list[Tensor]]
Backward Adams-Bashforth predictor method.
Parameters:
-
ode_func(Callable) –Augmented dynamics.
-
y_aug(tuple[list, list, list]) –Initial augmented state.
-
alpha(float | Tensor) –Fractional order.
-
t_grid(Tensor) –Flipped time grid.
-
yhistory(list[list[Tensor]]) –Forward trajectory.
-
memory(int | None, default:None) –Memory limit.
Returns:
Source code in spikeDE/solver.py
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 | |
find_parameters
Extracts all trainable parameters from a PyTorch module.
Handles special cases such as DataParallel replicas where parameters might not
be registered in the standard .parameters() iterator.
Parameters:
-
module(Module) –The
nn.Moduleto inspect.
Returns:
Source code in spikeDE/solver.py
get_memory_bounds
Calculates the range of history indices to include in the convolution sum.
Supports memory truncation for long sequences to reduce computational complexity from \(O(N^2)\) to \(O(N \cdot M)\), where \(M\) is the memory length.
Parameters:
-
k(int) –Current time step index.
-
memory(int | None) –Maximum number of history steps to retain. If
Noneor-1, uses full history.
Returns:
-
tuple[int, int]–A tuple
(start_idx, memory_length)defining the slice of history to use.start_idx: The starting index in the history list;memory_length: The number of elements to include.
Source code in spikeDE/solver.py
step_dynamics
step_dynamics(
ode_func: Callable[[Tensor, Tuple], tuple],
y0_tuple: tuple[Tensor, ...],
t_grid: Tensor,
) -> list[Tensor]
Steps through a discrete-time dynamical system, collecting boundary outputs.
This function drives the for-loop of an SNN/RNN without numerical integration scaling (no \(dt\)). The update function directly computes the next state: \(y_{k+1} = f(t_k, y_k)\).
Parameters:
-
ode_func(Callable[[Tensor, Tuple], tuple]) –Callable
(t, y_tuple) -> tuple. State update function. -
y0_tuple(tuple[Tensor, ...]) –Tuple of initial state tensors.
-
t_grid(Tensor) –1D tensor of time points (length T+1).
Returns: