From b77c31680204f21affb3a30ece25f994aecd444a Mon Sep 17 00:00:00 2001 From: Xavier Dupre Date: Sun, 2 Jul 2023 12:13:34 +0200 Subject: [PATCH 1/3] fix asarray --- _unittests/onnx-numpy-skips.txt | 1 - _unittests/ut_array_api/test_onnx_numpy.py | 17 ++++++++++++++++- onnx_array_api/array_api/_onnx_common.py | 11 +++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/_unittests/onnx-numpy-skips.txt b/_unittests/onnx-numpy-skips.txt index 1eac9e2..d0b47ab 100644 --- a/_unittests/onnx-numpy-skips.txt +++ b/_unittests/onnx-numpy-skips.txt @@ -8,4 +8,3 @@ array_api_tests/test_creation_functions.py::test_empty_like array_api_tests/test_creation_functions.py::test_eye array_api_tests/test_creation_functions.py::test_linspace array_api_tests/test_creation_functions.py::test_meshgrid -array_api_tests/test_creation_functions.py::test_zeros_like diff --git a/_unittests/ut_array_api/test_onnx_numpy.py b/_unittests/ut_array_api/test_onnx_numpy.py index 78f8872..e96e324 100644 --- a/_unittests/ut_array_api/test_onnx_numpy.py +++ b/_unittests/ut_array_api/test_onnx_numpy.py @@ -127,10 +127,25 @@ def test_full_like_mx(self): matnp = mat.numpy() self.assertEqualArray(expected, matnp) + def test_ones_like_mx(self): + c = EagerTensor(np.array([], dtype=np.uint8)) + expected = np.ones_like(c.numpy()) + mat = xp.ones_like(c) + matnp = mat.numpy() + self.assertEqualArray(expected, matnp) + + def test_as_array(self): + r = xp.asarray(9223372036854775809) + self.assertEqual(r.dtype, DType(TensorProto.UINT64)) + self.assertEqual(r.numpy(), 9223372036854775809) + r = EagerTensor(np.array(9223372036854775809, dtype=np.uint64)) + self.assertEqual(r.dtype, DType(TensorProto.UINT64)) + self.assertEqual(r.numpy(), 9223372036854775809) + if __name__ == "__main__": # import logging # logging.basicConfig(level=logging.DEBUG) - # TestOnnxNumpy().test_full_like_mx() + # TestOnnxNumpy().test_as_array() unittest.main(verbosity=2) diff --git a/onnx_array_api/array_api/_onnx_common.py b/onnx_array_api/array_api/_onnx_common.py index 98a89f2..7c2e59e 100644 --- a/onnx_array_api/array_api/_onnx_common.py +++ b/onnx_array_api/array_api/_onnx_common.py @@ -71,10 +71,17 @@ def asarray( elif a is True: v = TEagerTensor(np.array(True, dtype=np.bool_)) else: + va = np.asarray(a) + v = None try: - v = TEagerTensor(np.asarray(a, dtype=np.int64)) + vai = np.asarray(a, dtype=np.int64) except OverflowError: - v = TEagerTensor(np.asarray(a, dtype=np.uint64)) + v = TEagerTensor(va) + if v is None: + if int(va) == int(vai): + v = TEagerTensor(vai) + else: + v = TEagerTensor(va) elif isinstance(a, float): v = TEagerTensor(np.array(a, dtype=np.float64)) elif isinstance(a, bool): From ac6c775a1ab34a26dfbb967a01229feef55e2446 Mon Sep 17 00:00:00 2001 From: Xavier Dupre Date: Sun, 2 Jul 2023 12:21:32 +0200 Subject: [PATCH 2/3] zeros_like --- _unittests/test_array_api.sh | 2 +- onnx_array_api/array_api/__init__.py | 1 + onnx_array_api/npx/npx_functions.py | 24 +++++++++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/_unittests/test_array_api.sh b/_unittests/test_array_api.sh index abab39b..0a003c1 100644 --- a/_unittests/test_array_api.sh +++ b/_unittests/test_array_api.sh @@ -1,4 +1,4 @@ export ARRAY_API_TESTS_MODULE=onnx_array_api.array_api.onnx_numpy -pytest -v -rxXfE ../array-api-tests/array_api_tests/test_creation_functions.py::test_ones_like || exit 1 +pytest -v -rxXfE ../array-api-tests/array_api_tests/test_creation_functions.py::test_zeros_like || exit 1 # pytest ../array-api-tests/array_api_tests/test_creation_functions.py --help pytest -v -rxXfE ../array-api-tests/array_api_tests/test_creation_functions.py --hypothesis-explain --skips-file=_unittests/onnx-numpy-skips.txt || exit 1 diff --git a/onnx_array_api/array_api/__init__.py b/onnx_array_api/array_api/__init__.py index bd762be..6e4d712 100644 --- a/onnx_array_api/array_api/__init__.py +++ b/onnx_array_api/array_api/__init__.py @@ -29,6 +29,7 @@ "sum", "take", "zeros", + "zeros_like", ] diff --git a/onnx_array_api/npx/npx_functions.py b/onnx_array_api/npx/npx_functions.py index 8a886b2..5c202f8 100644 --- a/onnx_array_api/npx/npx_functions.py +++ b/onnx_array_api/npx/npx_functions.py @@ -681,7 +681,7 @@ def ones_like( dtype: OptParType[DType] = None, ) -> TensorType[ElemType.numerics, "T"]: """ - Implements :func:`numpy.zeros`. + Implements :func:`numpy.ones_like`. """ o = make_tensor( name="one", @@ -955,3 +955,25 @@ def zeros( value=make_tensor(name="zero", data_type=dtype.code, dims=[1], vals=[0]), op="ConstantOfShape", ) + + +@npxapi_inline +def zeros_like( + x: TensorType[ElemType.allowed, "T"], + /, + *, + dtype: OptParType[DType] = None, +) -> TensorType[ElemType.numerics, "T"]: + """ + Implements :func:`numpy.zeros_like`. + """ + o = make_tensor( + name="zero", + data_type=TensorProto.INT64 if dtype is None else dtype.code, + dims=[1], + vals=[0], + ) + v = var(x.shape, value=o, op="ConstantOfShape") + if dtype is None: + return var(v, x, op="CastLike") + return v From 6c4e6d02253414bb90b489b68e7009e6747bd41f Mon Sep 17 00:00:00 2001 From: Xavier Dupre Date: Sun, 2 Jul 2023 12:30:21 +0200 Subject: [PATCH 3/3] code coverage --- _doc/index.rst | 2 +- _doc/run_coverage.sh | 1 + azure-pipelines.yml | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 _doc/run_coverage.sh diff --git a/_doc/index.rst b/_doc/index.rst index 79afe2a..30997b0 100644 --- a/_doc/index.rst +++ b/_doc/index.rst @@ -39,7 +39,7 @@ well as to execute it. Sources available on `github/onnx-array-api `_, -see also `code coverage `_. +see also `code coverage <_static/cov_html/index.html>`_. .. runpython:: :showcode: diff --git a/_doc/run_coverage.sh b/_doc/run_coverage.sh new file mode 100644 index 0000000..4384f0c --- /dev/null +++ b/_doc/run_coverage.sh @@ -0,0 +1 @@ +python3 -m pytest --cov --cov-report html:_doc/_static/cov_html _unittests diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 709ced3..f0f887b 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -184,7 +184,7 @@ jobs: black --diff . displayName: 'Black' - script: | - python -m pytest + python -m pytest --cov displayName: 'Runs Unit Tests' - script: | python -u setup.py bdist_wheel 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