Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the JAX backend #504

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
python --version
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install jax
python -m pip install torch
displayName: 'Install dependencies'

Expand All @@ -46,7 +47,7 @@ jobs:
python -m pytest -v tslearn/ --doctest-modules
displayName: 'Test'

- job: 'linux_without_torch'
- job: 'linux_without_torch_and_jax'
pool:
vmImage: 'ubuntu-latest'
strategy:
Expand All @@ -69,6 +70,7 @@ jobs:
python --version
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip uninstall jax
python -m pip uninstall torch
displayName: 'Install dependencies'

Expand Down Expand Up @@ -110,6 +112,7 @@ jobs:
python --version
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install jax
python -m pip install torch
displayName: 'Install dependencies'

Expand Down Expand Up @@ -163,6 +166,7 @@ jobs:
export OPENBLAS=$(brew --prefix openblas)
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install jax
python -m pip install torch
displayName: 'Install dependencies'

Expand Down Expand Up @@ -209,6 +213,7 @@ jobs:
python --version
python -m pip install --upgrade pip
python -m pip install -r requirements_nocast.txt
python -m pip install jax
python -m pip install torch
displayName: 'Install dependencies'

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
packages=find_packages(),
package_data={"tslearn": [".cached_datasets/Trace.npz"]},
install_requires=['numpy', 'scipy', 'scikit-learn', 'numba', 'joblib'],
extras_require={'tests': ['pytest', 'torch'], 'pytorch': ['torch']},
extras_require={'tests': ['pytest', 'torch', 'jax'], 'pytorch': ['torch'], 'jax': ['jax']},
version=VERSION,
url="http://tslearn.readthedocs.io/",
project_urls={
Expand Down
46 changes: 34 additions & 12 deletions tslearn/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

from tslearn.backend.numpy_backend import NumPyBackend

try:
import jax

from tslearn.backend.jax_backend import JAXBackend
except ImportError:

class JAXBackend:
def __init__(self):
raise ValueError("Could not use JAX backend since JAX is not installed.")


try:
import torch

Expand All @@ -11,7 +22,7 @@
class PyTorchBackend:
def __init__(self):
raise ValueError(
"Could not use pytorch backend since torch is not installed"
"Could not use PyTorch backend since Torch is not installed."
)


Expand All @@ -28,14 +39,14 @@ def instantiate_backend(*args):
backend : Backend instance
The backend instance.
"""
backends_str = ["numpy", "jax", "torch"]
for arg in args:
if isinstance(arg, Backend):
return arg
arg_string = (str(type(arg)) + str(arg)).lower()
if "numpy" in arg_string:
return Backend("numpy")
if "torch" in arg_string:
return Backend("pytorch")
arg_str = (str(type(arg)) + str(arg)).lower()
for backend_str in backends_str:
if backend_str in arg_str:
return Backend(backend_str)
return Backend("numpy")


Expand All @@ -54,11 +65,15 @@ def select_backend(data):
The backend class.
If data is a Numpy array or data equals 'numpy' or data is None,
backend equals NumpyBackend().
If data is a PyTorch array or data equals 'pytorch',
If data is a JAX array or data equals 'jax',
backend equals JAXBackend().
If data is a PyTorch tensor or data equals 'pytorch',
backend equals PytorchBackend().
"""
arg_string = (str(type(data)) + str(data)).lower()
if "torch" in arg_string:
arg_str = (str(type(data)) + str(data)).lower()
if "jax" in arg_str:
return JAXBackend()
if "torch" in arg_str:
return PyTorchBackend()
return NumPyBackend()

Expand All @@ -72,6 +87,8 @@ class Backend(object):
Indicates the backend to choose.
If data is a Numpy array or data equals 'numpy' or data is None,
self.backend is set to NumpyBackend().
If data is a JAX array or data equals 'jax',
self.backend is set to JAXBackend().
If data is a PyTorch array or data equals 'pytorch',
self.backend is set to PytorchBackend().
Optional, default equals None.
Expand All @@ -85,6 +102,7 @@ def __init__(self, data=None):
setattr(self, element, getattr(self.backend, element))

self.is_numpy = self.backend_string == "numpy"
self.is_jax = self.backend_string == "jax"
self.is_pytorch = self.backend_string == "pytorch"

def get_backend(self):
Expand All @@ -103,20 +121,24 @@ def cast(data, array_type="numpy"):
The input data should be a list or numpy array or torch array.
The data to cast.
array_type: string
The type to cast the data. It can be "numpy", "pytorch" or "list".
The type to cast the data. It can be "numpy", "jax", "pytorch" or "list".

Returns
--------
data_cast: array-like
Data cast to array_type.
"""
data_type_string = str(type(data)).lower()
data_type_str = str(type(data)).lower()
array_type = array_type.lower()
if array_type == "pytorch":
array_type = "torch"
if array_type in data_type_string:
if array_type in data_type_str:
return data
if array_type == "list":
return data.tolist()
be = Backend(array_type)
backends_str = ["numpy", "jax", "torch"]
for backend_str in backends_str:
if backend_str in data_type_str:
data = data.tolist()
return be.array(data)
Loading
Loading