Skip to content

WIP, ENH: demo for array API support#5426

Open
tylerjereddy wants to merge 1 commit into
MDAnalysis:developfrom
tylerjereddy:treddy_array_api_demo
Open

WIP, ENH: demo for array API support#5426
tylerjereddy wants to merge 1 commit into
MDAnalysis:developfrom
tylerjereddy:treddy_array_api_demo

Conversation

@tylerjereddy

Copy link
Copy Markdown
Member

No LLMs/AI were used in this PR/demonstration, which is mostly aimed at the os4ls grant prep.

  • This feature branch isn't really ever intended to be merged, but is a prototype of sorts for testing out the Python Array API standard in a small part of MDAnalysis -- specifically in MDAnalysis/analysis/bat.py. I'm not sure that this was the best choice, but it did have a large number of np.* function calls, so struck me as potentially interesting to convert. This is mostly related to showing that we've at least poked around a bit for the os4ls grant, rather than proposing something that has no chance of succeeding.

  • On this feature branch locally on ARM Mac, the NumPy-based tests that are relevant continue to pass (as long as you have array-api-compat and array-api-extra installed, which is standard in the ecosystem now and/or vendored by projects) via i.e.:
    python -m pytest MDAnalysisTests/analysis/test_bat.py

  • Some initial/crude single-trial benchmark results (in seconds) from https://github.com/tylerjereddy/mda_array_api_demo don't yet show performance improvements over NumPy CPU baseline:

{'numpy': 0.037562333018286154, 'torch cpu': 0.05449483299162239, 'torch mps': 0.6141375829756726, 'jax': 0.7568245000147726}
  • However, I think there are some reasons to be optimistic here in terms of what this tells us for array API compat support for us:

    • JAX arrays are immutable, and adding JAX support is challenging, but it was relatively straightforward here using array_api_extra with the typical xpx.at(...) style syntax that SciPy and others use to add support for it.
    • The torch CPU timings aren't too far off of NumPy, and keep in mind that this branch has a LOT of array type coercions because we're leveraging our in house NumPy/CPU-based analysis functions--if the task were expanded (as the grant proposes), we'd get two wins in this regard: i) the algorithms that calculated distances and so on may be performed using the array type of interest; ii) the array type coercion performance costs may dissipate.
    • Using the MacOS ARM GPU (metal performance shader or "mps") still succeeds in the benchmark, which may be our first demonstration of algorithmic success on GPU in our analysis code (I think distance analyses have been compared to some GPU benchmarks perhaps)? Furthermore, MPS only supports float32, not float64, and our testsuite still passes here on CPU even with that single precision constraint applied to NumPy.

* This feature branch isn't really ever intended to be merged,
but is a prototype of sorts for testing out the Python Array API
standard in a small part of MDAnalysis -- specifically in
`MDAnalysis/analysis/bat.py`. I'm not sure that this was the best
choice, but it did have a large number of `np.*` function calls,
so struck me as potentially interesting to convert. This is mostly
related to showing that we've at least poked around a bit for the os4ls
grant, rather than proposing something that has no chance of succeeding.

* On this feature branch locally on ARM Mac, the NumPy-based tests
that are relevant continue to pass via i.e.:
`python -m pytest MDAnalysisTests/analysis/test_bat.py`

* Some initial/crude single-trial benchmark results (in seconds)
from https://github.com/tylerjereddy/mda_array_api_demo don't yet
show performance improvements over NumPy CPU baseline:

```python
{'numpy': 0.037562333018286154, 'torch cpu': 0.05449483299162239, 'torch mps': 0.6141375829756726, 'jax': 0.7568245000147726}
```

* However, I think there are some reasons to be optimistic here in
terms of what this tells us for array API compat support for us:
a) JAX arrays are immutable, and adding JAX support is challenging,
but it was relatively straightforward here using `array_api_extra`
with the typical `xpx.at(...)` style syntax that SciPy and other use
to add support for it.
b) The `torch` CPU timings aren't too far off of NumPy, and keep in mind
that this branch has a LOT of array type coercions because we're
leveraging our in house NumPy/CPU-based analysis functions--if the task
were expanded (as the grant proposes), we'd get two wins in this regard:
i) the algorithms that calculated distances and so on may be performed
using the array type of interest; ii) the array type coercion
performance costs may dissipate.
c) Using the MacOS ARM GPU (metal performance shader or "mps") still
succeeds in the benchmark, which may be our first demonstration of
algorithmic success on GPU in our analysis code? Furthermore, MPS
only supports float32, not float64, and our testsuite still passes
here on CPU even with that single precision constraint applied to NumPy.
@read-the-docs-community

Copy link
Copy Markdown

@orbeckst orbeckst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fantastic work @tylerjereddy ! It's great to see that using the array API will work. 🎉

(The current CI failures are due to #5236 and not this PR, which passes its relevant tests. ✅ )

I am going to make my review blocking so that we don't accidentally merge, given that we want to implement the array standard across the package in a consistent manner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants