{"id":3297,"library":"torchsde","title":"torchsde: SDE Solvers and Adjoint Sensitivity in PyTorch","description":"torchsde provides robust stochastic differential equation (SDE) solvers with GPU support and efficient adjoint sensitivity analysis in PyTorch. It enables differentiable SDE simulations, crucial for deep learning models involving stochastic processes. The current version is 0.2.6, and after a significant hiatus, it recently saw a maintenance release to address dependency issues and improve stability, indicating an active but potentially slow release cadence.","status":"active","version":"0.2.6","language":"en","source_language":"en","source_url":"https://github.com/google-research/torchsde","tags":["pytorch","sde","stochastic differential equations","deep learning","numerical methods","adjoint sensitivity","gpu"],"install":[{"cmd":"pip install torchsde","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core deep learning framework dependency; torchsde builds on PyTorch tensors and autograd.","package":"torch","optional":false}],"imports":[{"symbol":"sdeint","correct":"from torchsde import sdeint"},{"note":"Use this for memory-efficient backpropagation through SDEs, especially for long trajectories.","symbol":"sdeint_adjoint","correct":"from torchsde import sdeint_adjoint"},{"note":"For optimal performance, use the C++ backend Brownian motion classes from `brownian_lib` (since v0.1.1).","wrong":"from torchsde.brownian import BrownianPath","symbol":"BrownianPath","correct":"from torchsde.brownian_lib import BrownianPath"}],"quickstart":{"code":"import torch\nimport torchsde\n\nclass SDE(torch.nn.Module):\n    def __init__(self, d, m):\n        super().__init__()\n        self.mu = torch.nn.Linear(d, d)\n        self.sigma = torch.nn.Linear(d, m)\n\n    def f(self, t, y):\n        return self.mu(y)\n\n    def g(self, t, y):\n        return self.sigma(y)\n\n    def f_and_g(self, t, y):\n        return self.mu(y), self.sigma(y)\n\n# Define parameters\nD = 2  # State dimension\nM = 2  # Noise dimension\nT = 1.0 # End time\n\nsde = SDE(D, M)\nts = torch.linspace(0, T, 10)\neps = 0.1 # Small initial perturbation\ny0 = torch.rand(1, D) * eps # Initial state\n\n# Solve the SDE\nwith torch.no_grad(): # For inference, use no_grad\n    ys = torchsde.sdeint(sde, y0, ts)\n\nprint(\"SDE solved, output shape:\", ys.shape)\n# Expected output shape: (len(ts), batch_size, D)\n# e.g., (10, 1, 2)","lang":"python","description":"This quickstart demonstrates how to define a simple SDE model by implementing the drift `f` and diffusion `g` functions as part of a `torch.nn.Module`. It then uses `torchsde.sdeint` to solve the SDE over a given time interval `ts` starting from an initial state `y0`. For optimal performance, defining `f_and_g` to compute both drift and diffusion simultaneously is recommended."},"warnings":[{"fix":"Upgrade to torchsde v0.2.6 or later to benefit from corrected dependency lists and improved CI. `pip install --upgrade torchsde`","message":"Older versions (pre-0.2.6) of torchsde had known dependency resolution issues on PyPI, leading to installation failures or incorrect dependency versions.","severity":"breaking","affected_versions":"<0.2.6"},{"fix":"For training deep learning models involving SDEs, always prefer `sdeint_adjoint` when backpropagating through the SDE, unless you have explicit reasons or memory is not a concern.","message":"The choice between `sdeint` and `sdeint_adjoint` is crucial for memory efficiency during training. `sdeint_adjoint` uses adjoint sensitivity analysis to compute gradients with O(1) memory cost with respect to the SDE trajectory length, which is vital for long simulations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Change your import statement from `from torchsde.brownian import BrownianPath` to `from torchsde.brownian_lib import BrownianPath`.","message":"For optimal performance with Brownian motion generation, ensure you are importing `BrownianPath` or `BrownianTree` from `torchsde.brownian_lib` (the C++ backend) rather than `torchsde.brownian` (the older Python backend).","severity":"gotcha","affected_versions":"All versions since v0.1.1"},{"fix":"Always check the `requires_python` and `install_requires` in `setup.py` or PyPI for `torchsde` to ensure your PyTorch version meets the minimum requirements. Currently, `torchsde==0.2.6` requires `torch>=1.6.0`.","message":"torchsde is tightly integrated with PyTorch and specific PyTorch versions. Incompatibility between torchsde and your installed PyTorch version can lead to runtime errors or unexpected behavior.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}