-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
46 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#!/bin/bash | ||
|
||
pip install -e . | ||
python setup.py build_ext --inplace | ||
pip install pytest | ||
python -m pytest tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import TYPE_CHECKING, Optional, Type | ||
|
||
if TYPE_CHECKING: | ||
import jax | ||
import torch | ||
|
||
__all__ = ["get_torch", "get_jax"] | ||
|
||
|
||
def get_torch() -> Optional["torch"]: | ||
try: | ||
import torch | ||
|
||
return torch | ||
except ImportError: | ||
return None | ||
|
||
|
||
def get_jax() -> Optional["jax"]: | ||
try: | ||
import jax | ||
|
||
return jax | ||
except ImportError: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
from typing import Any, Union | ||
from typing import TYPE_CHECKING, Any, Union | ||
|
||
import jax | ||
import numpy as np | ||
import torch | ||
|
||
if TYPE_CHECKING: | ||
import jax | ||
import torch | ||
|
||
|
||
__all__ = ["NumpyArray", "Array"] | ||
|
||
|
||
NumpyArray = np.ndarray[Any, Any] | ||
Array = Union[torch.Tensor, jax.Array] | ||
Array = Union["torch.Tensor", "jax.Array"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
import jax | ||
import numpy as np | ||
import torch | ||
|
||
from .imports import get_jax, get_torch | ||
from .types import Array, NumpyArray | ||
|
||
__all__ = ["get_numpy_data"] | ||
|
||
|
||
torch = get_torch() | ||
jax = get_jax() | ||
|
||
|
||
def get_numpy_data(tensor: Array) -> NumpyArray: | ||
if isinstance(tensor, torch.Tensor): | ||
if torch is not None and isinstance(tensor, torch.Tensor): | ||
return tensor.cpu().detach().numpy() # type: ignore | ||
elif isinstance(tensor, jax.Array): | ||
elif jax is not None and isinstance(tensor, jax.Array): | ||
return np.array(tensor) | ||
else: | ||
raise ValueError(f"Unsupported tensor type: {type(tensor)}") |