torchdiffeq is a Python library providing ordinary differential equation (ODE) solvers implemented in PyTorch. It supports backpropagation through ODE solutions using the adjoint method, ensuring constant memory cost. The library offers a clean API for usage in deep learning applications, fully supporting GPU execution. The current version is 0.2.5, last released in November 2024, indicating an active development and maintenance cadence.
pip install torchdiffeqVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a simple ODE function as an `nn.Module` and use `torchdiffeq.odeint` to solve it over a specified time interval. The output `solution` tensor contains the evaluated states at each time point.
Ensure your ODE dynamics `func` inherits from `torch.nn.Module` if you intend to use `odeint_adjoint`.
For memory-efficient training, import and use `odeint_adjoint` (often aliased as `odeint`) instead of the default `odeint`.
Experiment with `rtol` and `atol` (e.g., `odeint(..., rtol=1e-3, atol=1e-5)`) to find a balance between speed and desired accuracy for your specific problem. Higher values mean faster but less accurate solutions.
Consider setting `options={'dtype': torch.float32}` within the `odeint` call if you need higher performance and have verified numerical stability with single-precision floats.As a workaround, avoid using torch.compile() with torchdiffeq until support for enum types is implemented in TorchDynamo.
Ensure that both PyTorch and NumPy are updated to compatible versions. For example, updating NumPy to version 1.19.3 has resolved similar issues in the past.
Ensure that you have the latest version of torchdiffeq installed, as 'odeint_adjoint' is available in version 0.2.5 and later.
Verify that the ODE function is correctly implemented and is not None before passing it to 'odeint'.
Check the ODE function and input data for potential issues, and consider running the code on the CPU to get more informative error messages.