Skip to content

Support non-numpy array backends#886

Merged
ColmTalbot merged 148 commits into
bilby-dev:mainfrom
ColmTalbot:bilback
Jun 26, 2026
Merged

Support non-numpy array backends#886
ColmTalbot merged 148 commits into
bilby-dev:mainfrom
ColmTalbot:bilback

Conversation

@ColmTalbot

@ColmTalbot ColmTalbot commented Jan 7, 2025

Copy link
Copy Markdown
Collaborator

I've been working on this PR on and off for a few months, it isn't ready yet, but I wanted to share it in case other people had early opinions.

The goal is to make it easier to interface with models/samplers implemented in e.g., JAX, that support GPU/TPU acceleration and JIT compilation.

The general guiding principles are:

  • when possible maintain existing behaviour with numpy/builtin arguments
  • work introspectively so users don't need to specify the target backend, but use input types
  • write as little backend specific code as possible, mostly through using the array-api specification and scipy interoperability

The primary changes so far are:

  • making most priors backend independent, there are a few holdouts where the underlying scipy functionality isn't compatible yet
  • core likelihoods mostly work with data from any backend
  • GW likelihoods work with any backend supported by the source function
  • the GW detector objects don't work via introspection, they need to be manually set
  • GW geometry (currently in bilby_cython) is handled via multiple-dispatch and added back into bilby

Changed behaviour:

Remaining issues:

  • Saving/loading nun-numpy arrays in result files may not work
  • I added some additional parameter conversions that I will remove
  • the bilby.gw.jaxstuff file should be removed and relevant functionality be moved elsewhere, it's currently just used for testing
  • the ROQ likelihood hasn't been ported
  • add more testing with JAX
  • translate some of the hyperparameter functionality, c.f., GWPopulation

@ColmTalbot ColmTalbot added the enhancement New feature or request label Jan 7, 2025
@ColmTalbot ColmTalbot marked this pull request as draft January 7, 2025 19:38
@ColmTalbot ColmTalbot force-pushed the bilback branch 2 times, most recently from ea348fa to 771a8a9 Compare January 22, 2026 17:00
@ColmTalbot ColmTalbot marked this pull request as ready for review January 23, 2026 15:24
@ColmTalbot ColmTalbot changed the title DRAFT: Support non-numpy array backends Support non-numpy array backends Jan 23, 2026
@ColmTalbot ColmTalbot added >100 lines refactoring to discuss To be discussed on an upcoming call labels Jan 23, 2026
@ColmTalbot

Copy link
Copy Markdown
Collaborator Author

This is now ready for review.
There are some things that won't work with JAX at the moment, e.g., various combinations of likelihood marginalization/acceleration.
I think we should accept this at the moment, for at least a bilby v3 alpha/beta release, and keep chipping away at the various subcases over time.

There are a lot of changes, but most of them are essentially np -> xp.
Some things required refactoring to avoid modifying slices of arrays as JAX doesn't like that.

Bilby can once again be installed without bilby.cython.
This should improve our general portability, but when bilby_cython is installed it will be used.

I've managed to keep test changes minimal:

  • I updated the joint prior test to make it more stringent (keys more randomly ordered).
  • I refactored some expensive prior initialization that was dramatically slowing things down.
  • I improved the logic for figuring out when ROQs are available to help my local testing.
  • Some mocks of numpy had to be updated.

@mj-will mj-will added this to the 3.0.0 milestone Jan 27, 2026

@mj-will mj-will left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial comments but I'll need to have another look.

Comment thread bilby/compat/patches.py Outdated
Comment thread bilby/compat/utils.py
Comment thread bilby/compat/utils.py Outdated
Comment thread bilby/compat/utils.py Outdated
Comment thread bilby/core/prior/analytical.py Outdated
Comment thread bilby/core/prior/analytical.py
Comment thread bilby/core/prior/analytical.py Outdated
Comment thread bilby/core/prior/dict.py Outdated
Comment thread bilby/core/prior/dict.py
Comment thread bilby/gw/utils.py Outdated

@ColmTalbot ColmTalbot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the initial comments @mj-will I'll take a pass at them ASAP.

Comment thread bilby/core/prior/analytical.py Outdated
Comment thread bilby/core/prior/analytical.py
Comment thread bilby/core/prior/analytical.py
Comment thread bilby/core/prior/dict.py Outdated
Comment thread bilby/core/prior/dict.py
Comment thread bilby/gw/utils.py Outdated
Comment thread bilby/gw/utils.py Outdated

@GregoryAshton GregoryAshton left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I got through about 60% of the diff and I'm pausing here so will submit the questions so far.

Comment thread bilby/compat/patches.py
Comment thread bilby/compat/patches.py Outdated
Comment thread bilby/core/prior/analytical.py
Comment thread bilby/core/prior/analytical.py
_cdf[val >= self.minimum] = 1. - np.exp(-val[val >= self.minimum] / self.mu)
return _cdf
with np.errstate(divide="ignore"):
return -val / self.mu - xp.log(xp.asarray(self.mu)) + xp.log(val >= self.minimum)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay - are the bounds being implemented here? But, I don't see the upper bound being implemented.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is carried over from the existing implementation.

Comment thread bilby/core/likelihood.py
Comment thread bilby/gw/detector/interferometer.py Outdated
Comment thread bilby/gw/detector/interferometer.py
@mj-will mj-will removed the to discuss To be discussed on an upcoming call label May 14, 2026
@ColmTalbot

ColmTalbot commented May 14, 2026

Copy link
Copy Markdown
Collaborator Author

Python 3.10 doesn't have support for a vmappable version of logsumexp through scipy leading to this job failing (https://github.com/bilby-dev/bilby/actions/runs/25883935510/job/76070707573?pr=886).

How do people feel about dropping support for Python 3.10 in Bilby 3? Numpy dropped support about a year ago.

@mj-will

mj-will commented May 28, 2026

Copy link
Copy Markdown
Collaborator

i've not a proper look yet but one thought on using orng: I think ArrayRNG may be confusing so I'm considering changing it to something else (see sequince-dev/orng#9). Feel free to comment on the MR if you have thoughts.

No meaningful thoughts, I agree it's probably a positive change and I'm happy to update once it is available.

Once that's in, I can make a stable release and get in on conda if that would be useful.

I think we'll definitely want this once we have a release ready. Although, I think I've refactored things so that orng doesn't need to be a top-level dependency.

v0.1.0a6 and above now renames ArrayRNG to RandomGenerator

Comment thread docs/array_api.rst Outdated
@mj-will

mj-will commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

@ColmTalbot orng 0.1.0 is now on conda forge: https://anaconda.org/channels/conda-forge/packages/orng/overview

@ColmTalbot ColmTalbot requested review from a team and mj-will June 24, 2026 15:05

@GregoryAshton GregoryAshton left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to approve and merge this with the idea to then ask all developers to test it and find the issues.

@ColmTalbot ColmTalbot enabled auto-merge June 25, 2026 15:58
Comment thread bilby/core/prior/analytical.py Outdated
Co-authored-by: Michael J. Williams <michaeljw1@googlemail.com>

@mj-will mj-will left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again Colm for all your work on this!

Assuming the CI passes, I'm happy to approve this and we can continue to test it.

@ColmTalbot ColmTalbot added this pull request to the merge queue Jun 26, 2026
Merged via the queue into bilby-dev:main with commit 612c46b Jun 26, 2026
19 of 20 checks passed
@ColmTalbot ColmTalbot deleted the bilback branch June 26, 2026 07:16
@ColmTalbot ColmTalbot mentioned this pull request Jun 26, 2026
mj-will added a commit to mj-will/bilby that referenced this pull request Jun 29, 2026
This was referenced Jun 29, 2026
mj-will added a commit that referenced this pull request Jun 29, 2026
* Fixing #1047

Squeeze the output to one-dimension to fix incompatibility with numpy >
2.4

* Fix typo in interferometer.py to_pickle method (#1081)

* REL: update changelog for 2.8.1

* MAINT: revert 4a92394

This reverts commit 4a92394.

* BUG: apply  from #886 for 1047

* REL: update changelog

* Increase tolerance for log noise evidence comparison (#1087)

closes #1086

* REL: add 1087

---------

Co-authored-by: Gregory Ashton <gregory.ashton@ligo.org>
Co-authored-by: AlexandreGoettel <33029077+AlexandreGoettel@users.noreply.github.com>
Co-authored-by: Matt Pitkin <m@ttpitk.in>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants