{"id":4808,"library":"torchdiffeq","title":"torchdiffeq","description":"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.","status":"active","version":"0.2.5","language":"python","source_language":"en","source_url":"https://github.com/rtqichen/torchdiffeq","tags":["PyTorch","ODE solvers","deep learning","differentiable programming","adjoint method","neural ODEs"],"install":[{"cmd":"pip install torchdiffeq","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core deep learning framework dependency.","package":"torch","optional":false},{"reason":"Used for additional solver wrappers and numerical utilities.","package":"scipy","optional":false}],"imports":[{"note":"Standard ODE solver for direct backpropagation.","symbol":"odeint","correct":"from torchdiffeq import odeint"},{"note":"The common pattern is to alias `odeint_adjoint` to `odeint` for easy switching. When using `odeint_adjoint`, the ODE function (`func`) *must* be an `nn.Module` to collect parameters.","wrong":"from torchdiffeq import odeint_adjoint","symbol":"odeint_adjoint","correct":"from torchdiffeq import odeint_adjoint as odeint"}],"quickstart":{"code":"import torch\nimport torch.nn as nn\nfrom torchdiffeq import odeint\n\n# Define the ODE function as an nn.Module\nclass ODEFunc(nn.Module):\n    def forward(self, t, y):\n        # Example ODE: dy/dt = -0.1y + t\n        # y and t are torch.Tensor\n        return -0.1 * y + t\n\n# Initial state y(t=0)\ny0 = torch.tensor([0.7])\n\n# Time points at which to evaluate the solution\nt = torch.linspace(0., 10., 100) # 100 points from t=0 to t=10\n\n# Solve the ODE using the default (dopri5) solver\nsolution = odeint(ODEFunc(), y0, t)\n\nprint(\"Shape of solution (time_steps, initial_dim):\")\nprint(solution.shape) # Expected: (100, 1)\nprint(\"\\nFirst 5 values of the solution:\")\nprint(solution[:5])","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your ODE dynamics `func` inherits from `torch.nn.Module` if you intend to use `odeint_adjoint`.","message":"When using `odeint_adjoint` for O(1) memory backpropagation, the ODE function (`func`) must be an instance of `torch.nn.Module`. This is crucial for the adjoint method to correctly identify and collect parameters for gradient computation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For memory-efficient training, import and use `odeint_adjoint` (often aliased as `odeint`) instead of the default `odeint`.","message":"Direct backpropagation through `odeint` (without `odeint_adjoint`) can be memory-intensive, especially for complex ODE trajectories or long integration times, as it stores all intermediate states. For O(1) memory cost, use the adjoint method (`odeint_adjoint`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Adaptive ODE solvers (like the default `dopri5`) use `rtol` (relative tolerance) and `atol` (absolute tolerance) to control the accuracy and number of steps. Incorrectly set tolerances can lead to either excessively slow computations or inaccurate solutions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider setting `options={'dtype': torch.float32}` within the `odeint` call if you need higher performance and have verified numerical stability with single-precision floats.","message":"The `dtype` for timelike quantities in solvers defaults to `torch.float64`. While more stable, using `torch.float32` can significantly improve speed but might lead to numerical instability or underflow issues in certain scenarios.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'0.2.5':50 '2024':55 'activ':58 'adjoint':24,70 'api':35 'applic':41 'backpropag':18 'cadenc':62 'clean':34 'constant':27 'cost':29 'current':47 'deep':39,66 'develop':59 'differenti':9,68 'ensur':26 'equat':10 'execut':45 'fulli':42 'gpu':44 'implement':13 'indic':56 'last':51 'learn':40,67 'librari':6,31 'mainten':61 'memori':28 'method':25,71 'neural':72 'novemb':54 'ode':11,20,64,73 'offer':32 'ordinari':8 'program':69 'provid':7 'python':5 'pytorch':15,63 'releas':52 'solut':21 'solver':12,65 'support':17,43 'torchdiffeq':1,2 'usag':37 'use':22 'version':48","created_at":"2026-04-12T14:08:28.928407+00:00","updated_at":"2026-04-17T14:50:57.734024+00:00","problems":[{"fix":"As a workaround, avoid using torch.compile() with torchdiffeq until support for enum types is implemented in TorchDynamo.","cause":"This error occurs when using torch.compile() on models that incorporate torchdiffeq for ODE solving, due to TorchDynamo's inability to handle enum types from torchdiffeq during compilation.","error":"NotImplementedError: UserDefinedObjectVariable(EnumMeta) is not a constant"},{"fix":"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.","cause":"This error can occur when there's a version mismatch between PyTorch and NumPy, leading to compatibility issues.","error":"ImportError: numpy.core.multiarray failed to import"},{"fix":"Ensure that you have the latest version of torchdiffeq installed, as 'odeint_adjoint' is available in version 0.2.5 and later.","cause":"This error occurs when attempting to use the adjoint method for backpropagation, but the 'odeint_adjoint' function is not found in the torchdiffeq module.","error":"AttributeError: module 'torchdiffeq' has no attribute 'odeint_adjoint'"},{"fix":"Verify that the ODE function is correctly implemented and is not None before passing it to 'odeint'.","cause":"This error can occur if the ODE function passed to 'odeint' is not properly defined or is set to None.","error":"TypeError: 'NoneType' object is not callable"},{"fix":"Check the ODE function and input data for potential issues, and consider running the code on the CPU to get more informative error messages.","cause":"This error indicates that a CUDA assertion failed on the device side, often due to invalid operations or out-of-bounds memory access.","error":"RuntimeError: CUDA error: device-side assert triggered"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.2.5","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/rtqichen/torchdiffeq","docs":null,"changelog":null,"pypi":"https://pypi.org/project/torchdiffeq/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["ai-ml"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-30","next_check":"2026-07-28","install_tag":null}}