Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitce37364

Browse files
authored
Enables array tests for asarray (#20)
* Tests asarray* improvments
1 parent5edccab commitce37364

File tree

9 files changed

+100
-10
lines changed

9 files changed

+100
-10
lines changed

‎_unittests/onnx-numpy-skips.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# API failures
2+
# see https://github.com/data-apis/array-api-tests/blob/master/numpy-skips.txt
23
array_api_tests/test_creation_functions.py::test_arange
3-
array_api_tests/test_creation_functions.py::test_asarray_scalars
44
array_api_tests/test_creation_functions.py::test_asarray_arrays
55
array_api_tests/test_creation_functions.py::test_empty
66
array_api_tests/test_creation_functions.py::test_empty_like

‎_unittests/test_array_api.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export ARRAY_API_TESTS_MODULE=onnx_array_api.array_api.onnx_numpy
2-
pytest ../array-api-tests/array_api_tests/test_creation_functions.py::test_zeros
2+
# pytest ../array-api-tests/array_api_tests/test_creation_functions.py::test_asarray_arrays || exit 1
3+
# pytest ../array-api-tests/array_api_tests/test_creation_functions.py --help
4+
pytest ../array-api-tests/array_api_tests/test_creation_functions.py --hypothesis-explain --skips-file=_unittests/onnx-numpy-skips.txt||exit 1

‎_unittests/ut_array_api/test_onnx_numpy.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77

88
classTestOnnxNumpy(ExtTestCase):
9-
deftest_abs(self):
9+
deftest_empty(self):
10+
c=EagerTensor(np.array([4,5],dtype=np.int64))
11+
self.assertRaise(lambda:xp.empty(c,dtype=xp.int64),RuntimeError)
12+
13+
deftest_zeros(self):
1014
c=EagerTensor(np.array([4,5],dtype=np.int64))
1115
mat=xp.zeros(c,dtype=xp.int64)
1216
matnp=mat.numpy()

‎_unittests/ut_npx/test_npx.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,7 +2501,7 @@ def test_numpy_all(self):
25012501
got=ref.run(None, {"A":data})
25022502
self.assertEqualArray(y,got[0])
25032503

2504-
deftest_numpy_all_empty(self):
2504+
deftest_numpy_all_zeros(self):
25052505
data=np.zeros((0,),dtype=np.bool_)
25062506
y=np.all(data)
25072507

@@ -2513,7 +2513,7 @@ def test_numpy_all_empty(self):
25132513
self.assertEqualArray(y,got[0])
25142514

25152515
@unittest.skipIf(True,reason="ReduceMin does not support shape[axis] == 0")
2516-
deftest_numpy_all_empty_axis_0(self):
2516+
deftest_numpy_all_zeros_axis_0(self):
25172517
data=np.zeros((0,1),dtype=np.bool_)
25182518
y=np.all(data,axis=0)
25192519

@@ -2535,7 +2535,13 @@ def test_numpy_all_empty_axis_1(self):
25352535
got=ref.run(None, {"A":data})
25362536
self.assertEqualArray(y,got[0])
25372537

2538+
@unittest.skipIf(True,reason="Fails to follow Array API")
2539+
deftest_get_item(self):
2540+
a=EagerNumpyTensor(np.array([True],dtype=np.bool_))
2541+
i=a[0]
2542+
self.assertEqualArray(i.numpy(),a.numpy()[0])
2543+
25382544

25392545
if__name__=="__main__":
2540-
# TestNpx().test_numpy_all_empty_axis_0()
2546+
# TestNpx().test_get_item()
25412547
unittest.main(verbosity=2)

‎azure-pipelines.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ jobs:
131131
-script:|
132132
export ARRAY_API_TESTS_MODULE=onnx_array_api.array_api.onnx_numpy
133133
cd array-api-tests
134-
python -m pytest -x array_api_tests/test_creation_functions.py --skips-file=../_unittests/onnx-numpy-skips.txt -v
134+
python -m pytest -x array_api_tests/test_creation_functions.py --skips-file=../_unittests/onnx-numpy-skips.txt --hypothesis-explain
135135
displayName: "numpy test_creation_functions.py"
136136
-script:|
137137
export ARRAY_API_TESTS_MODULE=onnx_array_api.array_api.onnx_ort
138138
cd array-api-tests
139-
python -m pytest -x array_api_tests/test_creation_functions.py --skips-file=../_unittests/onnx-ort-skips.txt -v
139+
python -m pytest -x array_api_tests/test_creation_functions.py --skips-file=../_unittests/onnx-ort-skips.txt --hypothesis-explain
140140
displayName: "ort test_creation_functions.py"
141141
#- script: |
142142
# export ARRAY_API_TESTS_MODULE=onnx_array_api.array_api.onnx_numpy

‎onnx_array_api/array_api/_onnx_common.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ def template_asarray(
3434
returna.astype(dtype=dtype)
3535

3636
ifisinstance(a,int):
37-
v=TEagerTensor(np.array(a,dtype=np.int64))
37+
ifaisFalse:
38+
v=TEagerTensor(np.array(False,dtype=np.bool_))
39+
elifaisTrue:
40+
v=TEagerTensor(np.array(True,dtype=np.bool_))
41+
else:
42+
try:
43+
v=TEagerTensor(np.asarray(a,dtype=np.int64))
44+
exceptOverflowError:
45+
v=TEagerTensor(np.asarray(a,dtype=np.uint64))
3846
elifisinstance(a,float):
3947
v=TEagerTensor(np.array(a,dtype=np.float32))
4048
elifisinstance(a,bool):

‎onnx_array_api/array_api/onnx_numpy.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"all",
3030
"asarray",
3131
"astype",
32+
"empty",
3233
"equal",
3334
"isdtype",
3435
"isfinite",
@@ -73,6 +74,17 @@ def ones(
7374
returngeneric_ones(shape,dtype=dtype,order=order)
7475

7576

77+
defempty(
78+
shape:TensorType[ElemType.int64,"I", (None,)],
79+
dtype:OptParType[DType]=DType(TensorProto.FLOAT),
80+
order:OptParType[str]="C",
81+
)->TensorType[ElemType.numerics,"T"]:
82+
raiseRuntimeError(
83+
"ONNX assumes there is no inplace implementation. "
84+
"empty function is only used in that case."
85+
)
86+
87+
7688
defzeros(
7789
shape:TensorType[ElemType.int64,"I", (None,)],
7890
dtype:OptParType[DType]=DType(TensorProto.FLOAT),

‎onnx_array_api/npx/npx_numpy_tensors.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fromtypingimportAny,Callable,List,Optional,Tuple
22
importnumpyasnp
3-
fromonnximportModelProto
3+
fromonnximportModelProto,TensorProto
44
fromonnx.referenceimportReferenceEvaluator
55
from .._helpersimportnp_dtype_to_tensor_dtype
66
from .npx_numpy_tensors_opsimportConstantOfShape
@@ -183,6 +183,60 @@ def __array_namespace__(self, api_version: Optional[str] = None):
183183
f"Unable to return an implementation for api_version={api_version!r}."
184184
)
185185

186+
def__bool__(self):
187+
"Implicit conversion to bool."
188+
ifself.dtype!=DType(TensorProto.BOOL):
189+
raiseTypeError(
190+
f"Conversion to bool only works for bool scalar, not for{self!r}."
191+
)
192+
ifself.shape== (0,):
193+
returnFalse
194+
iflen(self.shape)!=0:
195+
raiseValueError(
196+
f"Conversion to bool only works for scalar, not for{self!r}."
197+
)
198+
returnbool(self._tensor)
199+
200+
def__int__(self):
201+
"Implicit conversion to bool."
202+
iflen(self.shape)!=0:
203+
raiseValueError(
204+
f"Conversion to bool only works for scalar, not for{self!r}."
205+
)
206+
ifself.dtypenotin {
207+
DType(TensorProto.INT64),
208+
DType(TensorProto.INT32),
209+
DType(TensorProto.INT16),
210+
DType(TensorProto.INT8),
211+
DType(TensorProto.UINT64),
212+
DType(TensorProto.UINT32),
213+
DType(TensorProto.UINT16),
214+
DType(TensorProto.UINT8),
215+
}:
216+
raiseTypeError(
217+
f"Conversion to int only works for int scalar, "
218+
f"not for dtype={self.dtype}."
219+
)
220+
returnint(self._tensor)
221+
222+
def__float__(self):
223+
"Implicit conversion to bool."
224+
iflen(self.shape)!=0:
225+
raiseValueError(
226+
f"Conversion to bool only works for scalar, not for{self!r}."
227+
)
228+
ifself.dtypenotin {
229+
DType(TensorProto.FLOAT),
230+
DType(TensorProto.DOUBLE),
231+
DType(TensorProto.FLOAT16),
232+
DType(TensorProto.BFLOAT16),
233+
}:
234+
raiseTypeError(
235+
f"Conversion to int only works for float scalar, "
236+
f"not for dtype={self.dtype}."
237+
)
238+
returnfloat(self._tensor)
239+
186240

187241
classJitNumpyTensor(NumpyTensor,JitTensor):
188242
"""

‎onnx_array_api/npx/npx_var.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ def __getitem__(self, index: Any) -> "Var":
962962

963963
ifisinstance(index,Var):
964964
# scenario 2
965+
# TODO: fix this when index is an integer
965966
new_shape=cst(np.array([-1],dtype=np.int64))
966967
new_self=self.reshape(new_shape)
967968
new_index=index.reshape(new_shape)
@@ -973,6 +974,9 @@ def __getitem__(self, index: Any) -> "Var":
973974

974975
ifnotisinstance(index,tuple):
975976
index= (index,)
977+
eliflen(index)==0:
978+
# The array contains a scalar and it needs to be returned.
979+
returnvar(self,op="Identity")
976980

977981
# only one integer?
978982
ni=None

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp