Skip to content

Fixes Array API with onnxruntime #3

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

Merged
merged 10 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Check Array API with onnxruntime
  • Loading branch information
xadupre committed Mar 20, 2023
commit 602fd9eb9de973b26e5a0744a5b0bfaaf5ce346f
46 changes: 46 additions & 0 deletions _unittests/ut_ort/test_sklearn_array_api_ort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
import numpy as np
from onnx.defs import onnx_opset_version
from sklearn import config_context
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from onnx_array_api.ext_test_case import ExtTestCase
from onnx_array_api.ort.ort_tensors import EagerOrtTensor, OrtTensor


DEFAULT_OPSET = onnx_opset_version()


def take(self, X, indices, *, axis):
# Overwritting method take as it is using iterators.
# When array_api supports `take` we can use this directly
# https://github.com/data-apis/array-api/issues/177
X_np = self._namespace.take(X, indices, axis=axis)
return self._namespace.asarray(X_np)


class TestSklearnArrayAPIOrt(ExtTestCase):
def test_sklearn_array_api_linear_discriminant(self):
from sklearn.utils._array_api import _ArrayAPIWrapper

_ArrayAPIWrapper.take = take
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
y = np.array([1, 1, 1, 2, 2, 2])
ana = LinearDiscriminantAnalysis()
ana = LinearDiscriminantAnalysis()
ana.fit(X, y)
expected = ana.predict(X)

new_x = EagerOrtTensor(OrtTensor.from_array(X))
self.assertEqual(new_x.device_name, "Cpu")
self.assertStartsWith(
"EagerOrtTensor(OrtTensor.from_array(array([[", repr(new_x)
)
with config_context(array_api_dispatch=True):
got = ana.predict(new_x)
self.assertEqualArray(expected, got.numpy())


if __name__ == "__main__":
# import logging
# logging.basicConfig(level=logging.DEBUG)
unittest.main(verbosity=2)
3 changes: 3 additions & 0 deletions onnx_array_api/npx/npx_jit_eager.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,11 @@ def jit_call(self, *values, **kwargs):
raise RuntimeError(
f"Unable to run function for key={key!r}, "
f"types={[type(x) for x in values]}, "
f"dtypes={[x.dtype for x in values]}, "
f"shapes={[x.shape for x in values]}, "
f"kwargs={kwargs}, "
f"self.input_to_kwargs_={self.input_to_kwargs_}, "
f"f={self.f} from module {self.f.__module__!r} "
f"onnx={self.onxs[key]}."
) from e
self.info("-", "jit_call")
Expand Down
6 changes: 0 additions & 6 deletions onnx_array_api/npx/npx_numpy_tensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,6 @@ def get_ir_version(cls, ir_version):
"""
return ir_version

def const_cast(self, to: Any = None) -> "EagerTensor":
"""
Casts a constant without any ONNX conversion.
"""
return self.__class__(self._tensor.astype(to))

# The class should support whatever Var supports.
# This part is not yet complete.

Expand Down
11 changes: 1 addition & 10 deletions onnx_array_api/npx/npx_tensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ class EagerTensor(ArrayApi):
:class:`ArrayApi`.
"""

def const_cast(self, to: Any = None) -> "EagerTensor":
"""
Casts a constant without any ONNX conversion.
"""
raise NotImplementedError(
f"Method 'const_cast' must be overwritten in class "
f"{self.__class__.__name__!r}."
)

def __iter__(self):
"""
This is not implementation in the generic case.
Expand Down Expand Up @@ -141,7 +132,7 @@ def _generic_method_operator(self, method_name, *args: Any, **kwargs: Any) -> An
new_args = []
for a in args:
if isinstance(a, np.ndarray):
new_args.append(self.__class__(a).const_cast(self.dtype))
new_args.append(self.__class__(a.astype(self.dtype)))
else:
new_args.append(a)

Expand Down
17 changes: 16 additions & 1 deletion onnx_array_api/ort/ort_tensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
from onnx import ModelProto, TensorProto
from onnx.defs import onnx_opset_version
from onnx.helper import tensor_dtype_to_np_dtype
from onnxruntime import InferenceSession, RunOptions, get_available_providers
from onnxruntime.capi._pybind_state import OrtDevice as C_OrtDevice
from onnxruntime.capi._pybind_state import OrtMemType
Expand Down Expand Up @@ -128,9 +129,23 @@ def __init__(self, tensor: Union[C_OrtValue, "OrtTensor"]):
self._tensor = tensor
elif isinstance(tensor, OrtTensor):
self._tensor = tensor._tensor
elif isinstance(tensor, np.ndarray):
self._tensor = C_OrtValue.ortvalue_from_numpy(tensor, OrtTensor.CPU)
else:
raise ValueError(f"An OrtValue is expected not {type(tensor)}.")

def __repr__(self) -> str:
"usual"
return f"{self.__class__.__name__}(OrtTensor.from_array({self.numpy()!r}))"

@property
def device_name(self):
return self._tensor.device_name()

@property
def ndim(self):
return len(self.shape)

@property
def shape(self) -> Tuple[int, ...]:
"Returns the shape of the tensor."
Expand All @@ -139,7 +154,7 @@ def shape(self) -> Tuple[int, ...]:
@property
def dtype(self) -> Any:
"Returns the element type of this tensor."
return self._tensor.element_type()
return tensor_dtype_to_np_dtype(self._tensor.element_type())

@property
def key(self) -> Any:
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy