galpy with differentiable, accelerated backends: galactic dynamics in jax and PyTorch

arXiv:2611.14614 · astro-ph.IM, astro-ph.GA · Submitted 2026-11-10 · Read on arXiv

Listen

Radio episode about this paper

Transcript

Introduction to the show: ident: Astrophysics. The day in astrophysics: observations, models and what the data forces.

Vera: We're looking at the paper: "galpy with differentiable, accelerated backends".

Jocelyn: Here's the gist of the paper. We describe an extension of galpy, a Python library for galactic dynamics, that integrates JAX and PyTorch as computational backends alongside its existing NumPy and C implementation.

Vera: This extension allows galpy's gravitational potentials, orbit integration routines, action–angle transformations and their inverses, and distribution functions (DFs), including those for tidal streams, to run on GPUs.

Jocelyn: Furthermore, these operations can be compiled as part of a user's program and differentiated with respect to their inputs and parameters.

Vera: The design philosophy prioritizes maintaining backward compatibility: the NumPy path produces results byte-for-byte identical to the previous version, with only a few approved exceptions, and the user-facing interface remains unchanged.

Title and authors: Vera: So we're looking at a paper called "galpy with differentiable, accelerated backends: galactic dynamics in jax and PyTorch". It’s about taking this existing Python library for galactic dynamics, galpy, and adding JAX and PyTorch as computational backends alongside the NumPy and C versions.

Jocelyn: Basically, the big idea is to let you run all of galpy's core stuff—like gravitational potentials, orbit integrations, action angle transformations—on a GPU using either JAX or PyTorch. And it lets you differentiate those operations with respect to things like the force at a point or the samples coming from a distribution function.

Subrahmanyan: It sounds like they're trying to give people who are doing complex modeling, especially those dealing with tidal streams or galactic bars, a way to do gradient-based fitting on these models. That’s important because it lets you optimize parameters directly from your data without having to rely on slow traditional fitting routines.

Vera: The paper describes a design philosophy focused on keeping things stable for existing users. They say the NumPy path produces results that are byte-for-byte identical to the previous version, with only a few approved exceptions, and the user interface stays exactly as it is.

Jocelyn: And they have this rule about backend selection being driven by what kind of array type you pass in—if you give it a JAX array or a PyTorch tensor, that framework handles the calculation and returns an output of the same kind. That’s supposed to let you compute things on GPUs and get those gradients.

Subrahmanyan: That backend selection based on data type sounds smart for portability, but the real power here seems to be that you can compile this code as part of your own program and differentiate it from any input or parameter, which means you can do gradient-based inference or optimization on these dynamical models.

Vera: It really lets you compute derivatives with respect to things like the potential itself, the distribution function, and even the orbits of objects. That should speed up model fitting significantly because computing those derivatives at a cost comparable to getting the likelihood is a big deal.

Jocelyn: And they handle orbit integration in three ways: using a solver built into the backend like JAX’s diffrax, using galpy’s existing compiled C integrators, or extending the variational equations to include those C integrators. They note that running ODE solvers inside the backend can have a limitation because it might not have a batching rule suitable for PyTorch unless you use something like torchode.

Subrahmanyan: The speedup they claim is one to five orders of magnitude faster than on a single CPU core when you're dealing with large batches, which is huge if you are trying to process millions of stars or simulating large volumes of space. But we have to remember the trade-off between that automatic differentiation path and using those compiled C integrators, because the C path can be much faster for small numbers of points.

Vera: They also made distribution functions differentiable for every model on these backends, and sampling becomes differentiable whenever it involves an inverse transformation, which is useful for deriving derivatives with respect to parameters in things like velocity dispersion models.

Jocelyn: For tidal stream models specifically—like Bovy's action-angle model or particle-spray models—they say the track computation is a pure function of the progenitor’s orbit and the potential that you can vectorize over segments of that track, and it remains differentiable from end to end with respect to a parameter in the potential.

Subrahmanyan: That differentiability for stream models means you can find how changing a parameter in your potential affects the stream construction directly, which is very powerful for understanding structure formation around galaxies. But they also flagged some limitations—for instance, the many-position velocity sampler of the quasiisothermal disk distribution function has exceptions where sampling isn't differentiable.

Vera: So to wrap up this paper on "galpy with differentiable, accelerated backends: galactic dynamics in jax and PyTorch," it’s essentially about making galpy run on GPUs via JAX or PyTorch while keeping the existing NumPy interface identical.

Jocelyn: The main implication is that you can now perform gradient-based fitting on a huge range of galactic dynamics models, from potentials to distribution functions, with significant speedups for large datasets and the ability to differentiate through the whole pipeline.

Subrahmanyan: It opens up new ways to test cosmological structure formation models by allowing us to fit these complex dynamical components directly against observational data in a differentiable way. That kind of capability should help constrain parameters in halo models much more tightly than before, provided we can handle those computational costs on large scales.

Vera: Yeah, it’s a powerful tool for making our current modeling frameworks more flexible and faster when dealing with the sheer size of modern astronomical surveys. We'll keep an eye on how this capability plays out as we start fitting more complex models to real data sets.

Jocelyn: And for us, it means we can push these time-domain simulations and stream analyses further into the GPU acceleration space, which should help settle those lingering questions about the dynamics of stellar populations in nearby galaxies.

Subrahmanyan: It’s a solid step toward making our theoretical predictions more computationally accessible for actual data analysis, but we have to keep an eye on how stable those backend choices remain as array frameworks evolve.

The paper's summary: Vera: So, to recap, this paper is about taking galpy, which is this existing Python library for galactic dynamics, and adding JAX and PyTorch as new computational engines on top of NumPy and C.

Jocelyn: Right, so they're making all the core stuff—the potentials, the orbit solvers—run on GPUs using these new backends.

Vera: And they are making it differentiable so we can calculate how small changes in our inputs affect the final results, which is huge for fitting models.

Jocelyn: They’re focused on keeping things compatible with NumPy so existing code still works perfectly, but they use a smart system where the type of data you give it decides whether to use JAX or PyTorch for that specific calculation.

Vera: That backend switching based on the array type is key because it lets you get those GPU speeds and those gradients when you need them most.

Jocelyn: And they’re giving us options for orbit integration, so you can either use the solver built into the backend, or use galpy's older compiled C code for speed.

Vera: The real win here is that you can do gradient-based fitting on dynamical models—like those distribution functions—at a speed that lets you check many parameters in your potential or your distribution function very quickly.

Jocelyn: It means instead of spending days running slow NumPy fits, you can test different versions of a model much faster, and find the optimal parameters more directly.

Vera: The paper also details how they’ve made sampling from those distribution functions differentiable, which lets us calculate derivatives with respect to things like the scale radius of a disk.

Jocelyn: That's important because it means we can check if our theoretical predictions for velocity dispersion match what we see in observations by seeing how sensitive that prediction is to a specific parameter.

Vera: They also look at tidal stream models, showing that you can track those streams and calculate derivatives with respect to potential parameters right from the start of the construction.

Jocelyn: It’s about connecting the theory on structure formation, which is what Subrahmanyan works on, directly to high-speed computational tools that can handle large datasets.

Vera: The main implication is that we can start doing much more sophisticated model testing and parameter estimation for galactic dynamics right now, with a massive speed boost and the ability to get gradients.

Jocelyn: This opens up new avenues for how we test cosmological models by fitting these complex dynamical components against real observational data in a differentiable way.

Vera: It’s about making the tools we use to model the sky much more powerful when it comes to finding the right parameters for galaxies and streams.

The paper's improvements: Vera: So, we're looking at what the authors suggest to do next with this galpy extension—it’s about making specific tweaks to how they handle different parts of their system.

Jocelyn: They are focusing on those orbit integration methods, where they have three choices: using the solver built into the backend, or using those older compiled C integrators.

Vera: The authors suggest that for small numbers of points, sticking with the C integrators is still better because it’s just way faster than running a full automatic differentiation solver every time.

Jocelyn: That makes sense if you're only doing a quick test run, but they are also pointing out the trade-off where using the backend’s internal solver has a problem with batching in PyTorch unless you use that specific torchode tool.

Vera: That limitation is real for people trying to do large simulations on a GPU, so they acknowledge that if you need massive parallel processing, you might have to stick closer to the C path for those specific integration routines.

Jocelyn: Another big suggestion is around the distribution functions where sampling isn't differentiable everywhere—they flag the velocity sampler for quasiisothermal disks as a place where it just stops working smoothly.

Vera: That’s a clear limitation on what you can derive mathematically from those specific models right now, which means if you need to work with that specific velocity dispersion model, you can’t get gradients with respect to its parameters.

Jocelyn: But they are pushing for making the other DF models differentiable so that we keep gaining that ability to optimize things based on data.

Vera: The big idea is they want a framework where the user can choose their tool—the fast C path or the differentiable backend solver—based on whether they need maximum speed or maximum mathematical flexibility.

Jocelyn: It’s about giving the user control over which method to employ for orbit integration and distribution sampling, rather than just having one single path that does everything.

Vera: This modular approach means that if a specific component, like the stream model construction you mentioned earlier, has a better differentiable way to run on JAX versus PyTorch, the framework supports it without breaking the whole thing.

Jocelyn: So they are essentially building an engine that lets people plug in their preferred computational backend for different tasks within galpy.

Vera: It’s about future-proofing the library so that as JAX and PyTorch keep changing, this structure won't force everyone to rewrite the core physics modules again.

Jocelyn: That way, we can keep adding new acceleration methods without having to worry about losing compatibility with the NumPy version of galpy.

Vera: It sounds like they’re aiming for a system that is both extremely fast when you need it and mathematically rigorous when you need to check parameter sensitivity.

Conclusion: Vera: So we're wrapping up on "galpy with differentiable, accelerated backends: galactic dynamics in jax and PyTorch." Basically, this paper shows how they’ve integrated JAX and PyTorch as new ways to run galpy calculations on GPUs while keeping the NumPy version exactly the same.

Jocelyn: Right, so it’s about giving astronomers a way to use these high-speed accelerators for complex galactic dynamics work without having to start from scratch.

Vera: The main thing is that they’ve made everything differentiable, which means we can finally get gradients back from things like gravitational potentials and distribution functions.

Jocelyn: So you can do gradient-based fitting on these models much faster than before, which helps us find the right parameters for galaxies and streams more efficiently.

Vera: It’s about making our existing modeling tools much more powerful when we need to test many different scenarios quickly and accurately.

Jocelyn: And they show how you can choose your integration path—whether you want the fast backend solver or the older C code—so it's flexible for different types of jobs.

Vera: It’s a neat way to manage those trade-offs between raw speed and mathematical accuracy for specific simulations.

Jocelyn: This is huge because it lets us push our time-domain simulations further into the GPU acceleration space, which should help settle those lingering questions about stellar populations in nearby galaxies.

Vera: We’ve seen how this capability helps us constrain parameters in halo models much tighter when we can run these complex tests at scale.

Jocelyn: It’s a solid step toward making our theoretical predictions more computationally accessible for actual data analysis, but we still have those caveats about where the sampling isn't differentiable.

Vera: So, it’s not a magic bullet, but it is definitely a very strong tool for researchers who are working with massive datasets and need to check model sensitivity.

Jocelyn: Indeed, so this work on "galpy with differentiable, accelerated backends: galactic dynamics in jax and PyTorch" sets a new standard for how we can accelerate these kinds of scientific computations.

Vera: Next week, we’re going to look at some papers focusing on how observational pipelines are handling the massive amount of data from next-generation surveys.

Jocelyn: We'll see what tools are being developed to make that data reduction process much faster and more robust.

Subrahmanyan: I think this paper is important because it shows the physics side of things can actually scale up with these new computational methods, which is what we need to do for structure formation models.

Jo Bovy, David A. Dunlap

University of Toronto

astro-ph.IM, astro-ph.GA

Submitted: 2026-11-10

Updated: 2026-11-10

Comments: 44 pages, 28 figures, 30 code examples. galpy version 2 is available at https://github.com/jobovy/galpy and the code behind this paper's figures, together with the record of the development, at https://github.com/jobovy/galpy-backends

Code: https://github.com/jobovy/galpy

License: http://arxiv.org/licenses/nonexclusive-distrib/1.0/

Importance score: 93/100

The gist: We describe an extension of galpy, a Python library for galactic dynamics, that integrates JAX and PyTorch as computational backends alongside its existing NumPy and C implementation.

Key concepts

Backend Selection
The system automatically chooses the computational backend (NumPy, JAX, or PyTorch) based on the data type of the input array. This allows calculations to run on GPUs and enables differentiation from forces to sampled distribution functions.
Differentiable Orbit Integration
Orbit integration can occur either inside the chosen backend's ODE solver (like JAX's diffrax) or via galpy's compiled C integrators. This provides flexibility, allowing users to choose between automatic differentiation and high-speed C methods for integration.
Gradient-Based Fitting
The framework allows computing derivatives for all core functionalities, including potentials and DFs. This enables gradient-based optimization of dynamical models with many parameters at a cost comparable to the likelihood calculation itself, offering significant speedups on GPUs.

Summary

We describe an extension of galpy, a Python library for galactic dynamics, that integrates JAX and PyTorch as computational backends alongside its existing NumPy and C implementation. p. 0 This extension allows galpy's gravitational potentials, orbit integration routines, action–angle transformations and their inverses, and distribution functions (DFs), including those for tidal streams, to run on GPUs. Furthermore, these operations can be compiled as part of a user's program and differentiated with respect to their inputs and parameters.

The design philosophy prioritizes maintaining backward compatibility: the NumPy path produces results byte-for-byte identical to the previous version, with only a few approved exceptions, and the user-facing interface remains unchanged. p. 2 Backend selection is governed by a greedy principle: any array type passed in—whether it's for coordinates, initial conditions, or object parameters—activates the backend for that specific calculation and its output.

The core functionality of galpy is covered across all modules: potentials and their derivatives (forces), orbit integration (either within the backend solvers or through existing C integrators with their variational equations), action–angle transformations and inverses, and DFs, including those for tidal streams. p. 2 The principle guiding this work is that the numpy path produces results that are byte-for-byte identical to those of the previous version, with a handful of approved exceptions, and the interface is unchanged.

The backend framework is designed to be backend-agnostic. Code in the physics modules resolves the array namespace from its coordinate arguments and uses only that namespace, ensuring portability. p. 4 Numerical building blocks are handled by galpy.backend, which provides SciPy-like replacements for special functions, root finding, quadrature, and interpolation when running on NumPy data, while delegating to JAX or PyTorch otherwise. The framework avoids internal compilation; instead, it requires code to be traceable by JAX's jit and PyTorch's torch.compile at the user level.

For orbit integration, there are three approaches: integrating equations of motion inside the backend using its own ODE solver (diffrax for JAX with an eighth-order Dormand–Prince method), utilizing galpy’s compiled C integrators, and extending the variational equations to include all C integrators, including symplectic ones. A limitation noted is that in-backend orbit integration methods (like torchdiffeq) lack a batching rule suitable for PyTorch unless torchode is used.

Distribution Functions (DFs) are made differentiable for every DF model on the backends, and sampling is differentiable wherever it involves an inverse transformation, with exceptions noted for certain samplers in specific DFs. p. 2 Tidal stream models—such as Bovy's action–angle model, streamdf with gap extensions, and particle-spray models—now run on these backends. Sampling and evaluating these DF models are significantly accelerated under compilation compared to NumPy on a single CPU core, reaching hundreds to thousands of times faster on a GPU. p. 0

A major capability enabled by this work is the gradient-based fitting of dynamical models already present in galpy. p. 0 Derivatives with respect to every parameter—including those of the potential, DF, and object orbits—can be computed at a cost comparable to the likelihood itself. p. 1 The speed-up provided by GPUs for large batches (one to five orders of magnitude) is another significant benefit. p. 0

The development process involved collaboration between JB and an AI coding agent (Claude Code developed by Anthropic) from June 2026 to October 2026, organized in branches of the galpy repository. p. 2 The work was characterized by strict rules enforced throughout, including byte-identity for the NumPy path, an unchanged interface, and the requirement that the existing test suite pass under every backend with identical tolerances. p. 2 The continuous integration process involved running galpy’s test suite across five modes over 22 shards for every backend PR. p. 7

Green — the paper says it, and the page is printed. Red — no passage carries it, so treat that sentence as unverified. The same check runs on your own text: check a summary against its source, $5.

Improvements for AI systems

  1. Backend selection is based on data type, allowing a galpy function to receive a jax array or a PyTorch tensor computes with that framework and returns an array of the same kind. This enables computation to run on GPUs and be differentiated with respect to inputs and parameters from the force at a point to the samples drawn from a distribution function.

  2. The system can compile galpy's code as part of a user’s program, allowing for the orbit integration either inside the backend or through the C integrators with their variational equations to run on accelerators, achieving speedups like one to five orders of magnitude faster than on a single CPU core.

  3. The system supports differentiation with respect to inputs and parameters for all core functionalities, such as galpy’s gravitational potentials, orbit integration, action–angle transformations and their inverses, enabling gradient-based inference or optimization of models with many parameters.

  4. The framework maintains byte-for-byte identical results on the numpy path (the numpy path gives the same results as before) while allowing for critical numerical improvements such as solving the Poisson equation to a relative tolerance of 10-10 rather than scipy’s default of 10-3.

  5. The system supports two distinct integration paths: one that uses the backend’s own ODE solver: diffrax (P. Kidger 2021) for jax, and another that uses galpy’s compiled C integrators, offering trade-offs between automatic differentiation and speed, with the C path being much faster for small numbers of points.

  6. The system provides differentiable action–angle coordinates, where the actionsFreqsAngles for all three; a call with backend arrays, or with a potential that carries a backend parameter, computes with that backend. This allows derivatives to be available with respect to the phase-space position and, through the Python implementations, with respect to the parameters of the potential.

  7. The system enables differentiable sampling from distribution functions where sampling is differentiable wherever it is done by inverse transformation, which is everywhere except for the many-position velocity sampler of the quasiisothermal disk DF. This allows for deriving derivatives with respect to distribution function parameters, such as the derivative of a sampled velocity dispersion with respect to the scale radius agrees with the derivative of the Jeans-equation prediction to within the Monte-Carlo noise.

  8. The system supports differentiable stream models, where the track computation is a pure function of the progenitor’s orbit and the potential that is vectorized over the segments of the track and is differentiable from end to end, allowing for derivatives with respect to a parameter of the potential in stream construction.

Sources

Related papers