An open API service indexing awesome lists of open source software.

https://github.com/srush/anynp

Proof-of-concept of global switching between numpy/jax/pytorch in a library.
https://github.com/srush/anynp

Last synced: about 1 year ago
JSON representation

Proof-of-concept of global switching between numpy/jax/pytorch in a library.

Awesome Lists containing this project

README

          

# anynp
Proof-of-concept of global switching between numpy/jax/pytorch in a library.

This is a wrapper around the Array API and the `array_api_compat` lib. It adds a stub for the `np` type so that mypy doesn't complain and a context handler.

```python
from switcher import switch, XP
from numpy.typing import ArrayLike

def my_fun(x) -> ArrayLike:
array = switch.xp.asarray(x)
print(type(array))
return array

x = my_fun([10])

with switch.set_context(XP.Jax):
x = my_fun([10])
print(x.at[0].set(0))

with switch.set_context(XP.Torch):
my_fun([10])

my_fun([20])
```