torchdiffeq

repository·master·Indexed 24 days ago

https://github.com/rtqichen/torchdiffeq

A PyTorch implementation of differentiable Ordinary Differential Equation (ODE) solvers. It provides the `odeint` interface for solving initial value problems and `odeint_adjoint` for backpropagation with constant memory cost using the adjoint method. The library supports differentiable event handling via `odeint_event` and includes a variety of adaptive-step (e.g., dopri5, bosh3), fixed-step (e.g., rk4, euler), and SciPy-wrapped solvers.

Tokens
1.5K
Snippets
0
Records
11
Agent score
42%

What's inside torchdiffeq

  1. Implement callbacks in ODE functions

    master

    You can trigger callbacks during a solve by defining specific methods on the func object passed to odeint or odeint_adjoint.

    Supported Forward Callbacks:

    • callback_step(self, t0, y0, dt): Called immediately before taking a step. Supported by all solvers except scipy_solver.
    • callback_accept_step(self, t0, y0, dt): Called when a step is accepted. Supported by adaptive solvers (dopri8, dopri5, bosh3, adaptive_heun).
    • callback_reject_step(self, t0, y0, dt): Called when a step is rejected. Supported by adaptive solvers.

    Adjoint Callbacks: To trigger these during the adjoint pass, append _adjoint to the method name (e.g., callback_step_adjoint).

  2. Handle differentiable events with odeint_event

    master

    Use odeint_event to terminate an ODE solution when an event function triggers (i.e., when an element of event_fn(t, y) equals zero).

    Arguments:

    • func: The ODE callable.
    • y0: Initial values.
    • t0: Scalar representing the initial time value.
    • event_fn: A required keyword argument event_fn(t, y) that returns a tensor. The solve terminates when any element in the returned tensor is zero.
    • reverse_time: Boolean (default False) to solve in reverse time.
    • odeint_interface: Specifies whether to use odeint or odeint_adjoint for differentiation (default is odeint).
    • **kwargs: Passed to the chosen odeint_interface.

    Note: To obtain gradients for the event function, its parameters must be contained within the state y itself.

  3. Solve an Initial Value Problem with odeint

    master

    The odeint function is the main interface for solving initial value problems (IVP) of the form dy/dt = f(t, y) with initial condition y(t_0) = y_0.

    Arguments:

    • func: A callable implementing the ODE f(t, x).
    • y0: An any-D Tensor representing the initial values.
    • t: A 1-D Tensor containing the evaluation points. The initial time is taken to be t[0].
  4. Use the adjoint method with odeint_adjoint

    master

    For backpropagation through ODE solutions with constant memory cost, use odeint_adjoint. It uses $O(1)$ memory by solving an adjoint ODE during the backward pass.

    Critical Requirement: When using the adjoint method, func must be a torch.nn.Module so that the solver can collect the parameters of the differential equation.

  5. Configure adjoint options for odeint_adjoint

    master

    When using odeint_adjoint, you can specify parameters for the backward pass:

    • adjoint_rtol, adjoint_atol, adjoint_method, adjoint_options: Controls for the backward pass. Defaults to the values used in the forward pass.
    • adjoint_options: Can include {"norm": "seminorm"} to provide a more efficient adjoint solve when using adaptive step solvers.
    • adjoint_params: A tuple of tensors representing the parameters to compute gradients with respect to. Defaults to tuple(func.parameters()). If func has no parameters, you must specify adjoint_params=().
  6. Configure options for fixed solvers

    master

    Fixed solvers (euler, midpoint, rk4, explicit_adams, implicit_adams) use the following options:

    • step_size: The size of each discrete step. If not provided, the solver steps between values in t. Note: step_size is mutually exclusive with grid_constructor.
    • grid_constructor: A callable func(func, y0, t) -> grid that returns a 1D tensor representing the desired step locations.
    • perturb: If True, adds small perturbations to the start and end of each step to assist with stepping to discontinuities.
  7. Configure odeint solver options and methods

    master

    You can pass several keyword arguments to odeint or odeint_adjoint to control the solver behavior:

    • rtol: Relative tolerance.
    • atol: Absolute tolerance.
    • method: The solver algorithm to use.
    • options: A dictionary of solver-specific options.

    Available Solvers

    Adaptive-step solvers:

    • dopri5 (Default): Runge-Kutta of order 5 (Dormand-Prince-Shampine).
    • dopri8: Runge-Kutta of order 8 (Dormand-Prince-Shampine).
    • bosh3: Runge-Kutta of order 3 (Bogacki-Shampine).
    • fehlberg2: Runge-Kutta-Fehlberg of order 2.
    • adaptive_heun: Runge-Kutta of order 2.

    Fixed-step solvers:

    • euler: Euler method.
    • midpoint: Midpoint method.
    • rk4: Fourth-order Runge-Kutta with 3/8 rule.
    • explicit_adams: Explicit Adams-Bashforth.
    • implicit_adams: Implicit Adams-Bashforth-Moulton.

    SciPy solvers:

    • scipy_solver: Wraps all solvers available through SciPy.
  8. Configure specific Adams solvers

    master

    The Adams solvers have specialized parameters:

    explicit_adams

    • max_order: The maximum order of the Adams-Bashforth predictor. Note that rtol and atol are ignored for this solver.

    implicit_adams

    • max_order: The maximum order of the Adams-Bashforth-Moulton predictor-corrector.
    • max_iters: Maximum number of iterations for the Adams-Moulton corrector.
    • rtol and atol: Correspond to the tolerance for convergence of the Adams-Moulton corrector.
  9. Configure options for adaptive solvers

    master

    Adaptive solvers (dopri8, dopri5, bosh3, adaptive_heun) use rtol and atol for step acceptance/rejection. Use the following options to control solver behavior:

    • first_step: Size of the first step (defaults to empirical selection).
    • safety: Factor to shrink the next optimal step size (default: 0.9).
    • ifactor: Maximum factor the step size can grow by (default: 10.0).
    • dfactor: Maximum factor the step size can shrink by (default: 0.2).
    • max_num_steps: Maximum allowed steps (default: 2**31 - 1).
    • dtype: Dtype for timelike quantities (default: torch.float64). Use torch.float32 for speed at the risk of underflow.
    • step_t: A torch.Tensor of times where a step must be made. Useful for handling derivative discontinuities (kinks).
    • jump_t: A torch.Tensor of times where a step must be made and func re-evaluated. Useful for handling discontinuities where the FSAL property does not hold.
    • norm: A function that takes a tensor/tuple and returns a scalar norm. For adaptive solvers, this controls the accept/reject criterion.