|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- # Copyright 2020-2021 Huawei Technologies Co., Ltd
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- # ============================================================================
- """unit tests for numpy array operations"""
-
- import pytest
- import numpy as onp
-
- import mindspore.numpy as mnp
-
- from .utils import rand_int, rand_bool, match_array, match_res, match_meta, \
- match_all_arrays, run_multi_test, to_tensor
-
-
- class Cases():
- def __init__(self):
- self.all_shapes = [
- 1, 2, (1,), (2,), (1, 2, 3), [1], [2], [1, 2, 3]
- ]
- self.onp_dtypes = [onp.int32, 'int32', int,
- onp.float32, 'float32', float,
- onp.uint32, 'uint32',
- onp.bool_, 'bool', bool]
-
- self.mnp_dtypes = [mnp.int32, 'int32', int,
- mnp.float32, 'float32', float,
- mnp.uint32, 'uint32',
- mnp.bool_, 'bool', bool]
-
- self.array_sets = [1, 1.1, True, [1, 0, True], [1, 1.0, 2], (1,),
- [(1, 2, 3), (4, 5, 6)], onp.random.random( # pylint: disable=no-member
- (100, 100)).astype(onp.float32).tolist(),
- onp.random.random((100, 100)).astype(onp.bool).tolist()]
-
- self.arrs = [
- rand_int(2),
- rand_int(2, 3),
- rand_int(2, 3, 4),
- rand_int(2, 3, 4, 5),
- ]
-
- # scalars expanded across the 0th dimension
- self.scalars = [
- rand_int(),
- rand_int(1),
- rand_int(1, 1),
- rand_int(1, 1, 1),
- ]
-
- # arrays of the same size expanded across the 0th dimension
- self.expanded_arrs = [
- rand_int(2, 3),
- rand_int(1, 2, 3),
- rand_int(1, 1, 2, 3),
- rand_int(1, 1, 1, 2, 3),
- ]
-
- # arrays with dimensions of size 1
- self.nested_arrs = [
- rand_int(1),
- rand_int(1, 2),
- rand_int(3, 1, 8),
- rand_int(1, 3, 9, 1),
- ]
-
- # arrays which can be broadcast
- self.broadcastables = [
- rand_int(5),
- rand_int(6, 1),
- rand_int(7, 1, 5),
- rand_int(8, 1, 6, 1)
- ]
-
- # boolean arrays which can be broadcast
- self.bool_broadcastables = [
- rand_bool(),
- rand_bool(1),
- rand_bool(5),
- rand_bool(6, 1),
- rand_bool(7, 1, 5),
- rand_bool(8, 1, 6, 1),
- ]
-
- self.mnp_prototypes = [
- mnp.ones((2, 3, 4)),
- mnp.ones((1, 3, 1, 2, 5)),
- mnp.ones((2, 7, 1)),
- [mnp.ones(3), (1, 2, 3), mnp.ones(3), [4, 5, 6]],
- ([(1, 2), mnp.ones(2)], (mnp.ones(2), [3, 4])),
- ]
-
- self.onp_prototypes = [
- onp.ones((2, 3, 4)),
- onp.ones((1, 3, 1, 2, 5)),
- onp.ones((2, 7, 1)),
- [onp.ones(3), (1, 2, 3), onp.ones(3), [4, 5, 6]],
- ([(1, 2), onp.ones(2)], (onp.ones(2), [3, 4])),
- ]
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_asarray():
- test_case = Cases()
- for array in test_case.array_sets:
- # Check for dtype matching
- actual = onp.asarray(array)
- expected = mnp.asarray(array).asnumpy()
- # Since we set float32/int32 as the default dtype in mindspore, we need
- # to make a conversion between numpy.asarray and mindspore.numpy.asarray
- if actual.dtype is onp.dtype('float64'):
- assert expected.dtype == onp.dtype('float32')
- elif actual.dtype is onp.dtype('int64'):
- assert expected.dtype == onp.dtype('int32')
- else:
- assert actual.dtype == expected.dtype
- match_array(actual, expected, error=7)
-
- for i in range(len(test_case.onp_dtypes)):
- actual = onp.asarray(array, test_case.onp_dtypes[i])
- expected = mnp.asarray(array, test_case.mnp_dtypes[i]).asnumpy()
- match_array(actual, expected, error=7)
-
- # Additional tests for nested tensor mixture
- mnp_input = [(mnp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
- onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
-
- actual = onp.asarray(onp_input)
- expected = mnp.asarray(mnp_input).asnumpy()
- match_array(actual, expected, error=7)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_array():
- # array's function is very similar to asarray, so we mainly test the
- # `copy` argument.
- test_case = Cases()
- for array in test_case.array_sets:
- arr1 = mnp.asarray(array)
- arr2 = mnp.array(arr1, copy=False)
- arr3 = mnp.array(arr1)
- arr4 = mnp.asarray(array, dtype='int32')
- arr5 = mnp.asarray(arr4, dtype=mnp.int32)
- assert arr1 is arr2
- assert arr1 is not arr3
- assert arr4 is arr5
-
- # Additional tests for nested tensor/numpy_array mixture
- mnp_input = [(mnp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
- onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
-
- actual = onp.array(onp_input)
- expected = mnp.array(mnp_input).asnumpy()
- match_array(actual, expected, error=7)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_asfarray():
- test_case = Cases()
- for array in test_case.array_sets:
- # Check for dtype matching
- actual = onp.asfarray(array)
- expected = mnp.asfarray(array).asnumpy()
- # Since we set float32/int32 as the default dtype in mindspore, we need
- # to make a conversion between numpy.asarray and mindspore.numpy.asarray
- if actual.dtype is onp.dtype('float64'):
- assert expected.dtype == onp.dtype('float32')
- else:
- assert actual.dtype == expected.dtype
- match_array(actual, expected, error=7)
-
- for i in range(len(test_case.onp_dtypes)):
- actual = onp.asfarray(array, test_case.onp_dtypes[i])
- expected = mnp.asfarray(array, test_case.mnp_dtypes[i]).asnumpy()
- match_array(actual, expected, error=7)
-
- # Additional tests for nested tensor/numpy_array mixture
- mnp_input = [(mnp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
- onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
-
- actual = onp.asfarray(onp_input)
- expected = mnp.asfarray(mnp_input).asnumpy()
- match_array(actual, expected, error=7)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_zeros():
- test_case = Cases()
- for shape in test_case.all_shapes:
- for i in range(len(test_case.onp_dtypes)):
- actual = onp.zeros(shape, test_case.onp_dtypes[i])
- expected = mnp.zeros(shape, test_case.mnp_dtypes[i]).asnumpy()
- match_array(actual, expected)
- actual = onp.zeros(shape)
- expected = mnp.zeros(shape).asnumpy()
- match_array(actual, expected)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_ones():
- test_case = Cases()
- for shape in test_case.all_shapes:
- for i in range(len(test_case.onp_dtypes)):
- actual = onp.ones(shape, test_case.onp_dtypes[i])
- expected = mnp.ones(shape, test_case.mnp_dtypes[i]).asnumpy()
- match_array(actual, expected)
- actual = onp.ones(shape)
- expected = mnp.ones(shape).asnumpy()
- match_array(actual, expected)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_full():
- actual = onp.full((2, 2), [1, 2])
- expected = mnp.full((2, 2), [1, 2]).asnumpy()
- match_array(actual, expected)
-
- actual = onp.full((2, 3), True)
- expected = mnp.full((2, 3), True).asnumpy()
- match_array(actual, expected)
-
- actual = onp.full((3, 4, 5), 7.5)
- expected = mnp.full((3, 4, 5), 7.5).asnumpy()
- match_array(actual, expected)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_eye():
- test_case = Cases()
- for i in range(len(test_case.onp_dtypes)):
- for m in range(1, 5):
- actual = onp.eye(m, dtype=test_case.onp_dtypes[i])
- expected = mnp.eye(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
- match_array(actual, expected)
- for n in range(1, 5):
- for k in range(0, 5):
- actual = onp.eye(m, n, k, dtype=test_case.onp_dtypes[i])
- expected = mnp.eye(
- m, n, k, dtype=test_case.mnp_dtypes[i]).asnumpy()
- match_array(actual, expected)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_identity():
- test_case = Cases()
- for i in range(len(test_case.onp_dtypes)):
- for m in range(1, 5):
- actual = onp.identity(m, dtype=test_case.onp_dtypes[i])
- expected = mnp.identity(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
- match_array(actual, expected)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_arange():
- actual = onp.arange(10)
- expected = mnp.arange(10).asnumpy()
- match_array(actual, expected)
-
- actual = onp.arange(0, 10)
- expected = mnp.arange(0, 10).asnumpy()
- match_array(actual, expected)
-
- actual = onp.arange(start=10)
- expected = mnp.arange(start=10).asnumpy()
- match_array(actual, expected)
-
- actual = onp.arange(start=10, step=0.1)
- expected = mnp.arange(start=10, step=0.1).asnumpy()
- match_array(actual, expected, error=6)
-
- actual = onp.arange(10, step=0.1)
- expected = mnp.arange(10, step=0.1).asnumpy()
- match_array(actual, expected, error=6)
-
- actual = onp.arange(0.1, 9.9)
- expected = mnp.arange(0.1, 9.9).asnumpy()
- match_array(actual, expected, error=6)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_linspace():
- actual = onp.linspace(2.0, 3.0, dtype=onp.float32)
- expected = mnp.linspace(2.0, 3.0).asnumpy()
- match_array(actual, expected, error=6)
-
- actual = onp.linspace(2.0, 3.0, num=5, dtype=onp.float32)
- expected = mnp.linspace(2.0, 3.0, num=5).asnumpy()
- match_array(actual, expected, error=6)
-
- actual = onp.linspace(
- 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
- expected = mnp.linspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
- match_array(actual, expected, error=6)
-
- actual = onp.linspace(2.0, 3.0, num=5, retstep=True, dtype=onp.float32)
- expected = mnp.linspace(2.0, 3.0, num=5, retstep=True)
- match_array(actual[0], expected[0].asnumpy())
- assert actual[1] == expected[1].asnumpy()
-
- actual = onp.linspace(2.0, [3, 4, 5], num=5,
- endpoint=False, dtype=onp.float32)
- expected = mnp.linspace(
- 2.0, [3, 4, 5], num=5, endpoint=False).asnumpy()
- match_array(actual, expected, error=6)
-
- actual = onp.linspace(2.0, [[3, 4, 5]], num=5, endpoint=False, axis=2)
- expected = mnp.linspace(2.0, [[3, 4, 5]], num=5, endpoint=False, axis=2).asnumpy()
- match_array(actual, expected, error=6)
-
- start = onp.random.random([2, 1, 4]).astype("float32")
- stop = onp.random.random([1, 5, 1]).astype("float32")
- actual = onp.linspace(start, stop, num=20, retstep=True,
- endpoint=False, dtype=onp.float32)
- expected = mnp.linspace(to_tensor(start), to_tensor(stop), num=20,
- retstep=True, endpoint=False)
- match_array(actual[0], expected[0].asnumpy(), error=6)
- match_array(actual[1], expected[1].asnumpy(), error=6)
-
- actual = onp.linspace(start, stop, num=20, retstep=True,
- endpoint=False, dtype=onp.int16)
- expected = mnp.linspace(to_tensor(start), to_tensor(stop), num=20,
- retstep=True, endpoint=False, dtype=mnp.int16)
- match_array(actual[0], expected[0].asnumpy(), error=6)
- match_array(actual[1], expected[1].asnumpy(), error=6)
-
- for axis in range(2):
- actual = onp.linspace(start, stop, num=20, retstep=False,
- endpoint=False, dtype=onp.float32, axis=axis)
- expected = mnp.linspace(to_tensor(start), to_tensor(stop), num=20,
- retstep=False, endpoint=False, dtype=mnp.float32, axis=axis)
- match_array(actual, expected.asnumpy(), error=6)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_logspace():
- actual = onp.logspace(2.0, 3.0, dtype=onp.float32)
- expected = mnp.logspace(2.0, 3.0).asnumpy()
- match_array(actual, expected, error=3)
-
- actual = onp.logspace(2.0, 3.0, num=5, dtype=onp.float32)
- expected = mnp.logspace(2.0, 3.0, num=5).asnumpy()
- match_array(actual, expected, error=3)
-
- actual = onp.logspace(
- 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
- expected = mnp.logspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
- match_array(actual, expected, error=3)
-
- actual = onp.logspace(2.0, [3, 4, 5], num=5, base=2,
- endpoint=False, dtype=onp.float32)
- expected = mnp.logspace(
- 2.0, [3, 4, 5], num=5, base=2, endpoint=False).asnumpy()
- match_array(actual, expected, error=3)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_empty():
- test_case = Cases()
- for shape in test_case.all_shapes:
- for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
- test_case.onp_dtypes):
- actual = mnp.empty(shape, mnp_dtype).asnumpy()
- expected = onp.empty(shape, onp_dtype)
- match_meta(actual, expected)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_empty_like():
- test_case = Cases()
- for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
- actual = mnp.empty_like(mnp_proto).asnumpy()
- expected = onp.empty_like(onp_proto)
- assert actual.shape == expected.shape
-
- for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
- test_case.onp_dtypes):
- actual = mnp.empty_like(mnp_proto, dtype=mnp_dtype).asnumpy()
- expected = onp.empty_like(onp_proto, dtype=onp_dtype)
- match_meta(actual, expected)
-
-
- def run_x_like(mnp_fn, onp_fn):
- test_case = Cases()
- for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
- actual = mnp_fn(mnp_proto).asnumpy()
- expected = onp_fn(onp_proto)
- match_array(actual, expected)
-
- for shape in test_case.all_shapes:
- actual = mnp_fn(mnp_proto, shape=shape).asnumpy()
- expected = onp_fn(onp_proto, shape=shape)
- match_array(actual, expected)
- for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
- test_case.onp_dtypes):
- actual = mnp_fn(mnp_proto, dtype=mnp_dtype).asnumpy()
- expected = onp_fn(onp_proto, dtype=onp_dtype)
- match_array(actual, expected)
-
- actual = mnp_fn(mnp_proto, dtype=mnp_dtype,
- shape=shape).asnumpy()
- expected = onp_fn(onp_proto, dtype=onp_dtype, shape=shape)
- match_array(actual, expected)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_ones_like():
- run_x_like(mnp.ones_like, onp.ones_like)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_zeros_like():
- run_x_like(mnp.zeros_like, onp.zeros_like)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_full_like():
- test_case = Cases()
- for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
- shape = onp.zeros_like(onp_proto).shape
- fill_value = rand_int()
- actual = mnp.full_like(mnp_proto, to_tensor(fill_value)).asnumpy()
- expected = onp.full_like(onp_proto, fill_value)
- match_array(actual, expected)
-
- for i in range(len(shape) - 1, 0, -1):
- fill_value = rand_int(*shape[i:])
- actual = mnp.full_like(mnp_proto, to_tensor(fill_value)).asnumpy()
- expected = onp.full_like(onp_proto, fill_value)
- match_array(actual, expected)
-
- fill_value = rand_int(1, *shape[i + 1:])
- actual = mnp.full_like(mnp_proto, to_tensor(fill_value)).asnumpy()
- expected = onp.full_like(onp_proto, fill_value)
- match_array(actual, expected)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_tri_triu_tril():
- x = mnp.ones((16, 32), dtype="bool")
- match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
- match_array(mnp.tril(x, -1).asnumpy(), onp.tril(x.asnumpy(), -1))
- match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
- match_array(mnp.triu(x, -1).asnumpy(), onp.triu(x.asnumpy(), -1))
-
- x = mnp.ones((64, 64), dtype="uint8")
- match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
- match_array(mnp.tril(x, 25).asnumpy(), onp.tril(x.asnumpy(), 25))
- match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
- match_array(mnp.triu(x, 25).asnumpy(), onp.triu(x.asnumpy(), 25))
-
- match_array(mnp.tri(64, 64).asnumpy(), onp.tri(64, 64))
- match_array(mnp.tri(64, 64, -10).asnumpy(), onp.tri(64, 64, -10))
-
-
- @pytest.mark.level1
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_nancumsum():
- x = rand_int(2, 3, 4, 5)
- x[0][2][1][3] = onp.nan
- x[1][0][2][4] = onp.nan
- x[1][1][1][1] = onp.nan
- match_res(mnp.nancumsum, onp.nancumsum, x)
- match_res(mnp.nancumsum, onp.nancumsum, x, axis=-2)
- match_res(mnp.nancumsum, onp.nancumsum, x, axis=0)
- match_res(mnp.nancumsum, onp.nancumsum, x, axis=3)
-
-
- def mnp_diagonal(arr):
- return mnp.diagonal(arr, offset=2, axis1=-1, axis2=0)
-
-
- def onp_diagonal(arr):
- return onp.diagonal(arr, offset=2, axis1=-1, axis2=0)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_diagonal():
-
- arr = rand_int(3, 5)
- for i in [-1, 0, 2]:
- match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=1)
- match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=1, axis2=0)
-
- arr = rand_int(7, 4, 9)
- for i in [-1, 0, 2]:
- match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=-1)
- match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=-2, axis2=2)
- match_res(mnp.diagonal, onp.diagonal, arr,
- offset=i, axis1=-1, axis2=-2)
-
-
- def mnp_trace(arr):
- return mnp.trace(arr, offset=4, axis1=1, axis2=2)
-
-
- def onp_trace(arr):
- return onp.trace(arr, offset=4, axis1=1, axis2=2)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_trace():
-
- arr = rand_int(3, 5)
- for i in [-1, 0]:
- match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=1)
- match_res(mnp.trace, onp.trace, arr, offset=i, axis1=1, axis2=0)
-
- arr = rand_int(7, 4, 9)
- for i in [-1, 0, 2]:
- match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=-1)
- match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-2, axis2=2)
- match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-1, axis2=-2)
-
-
-
- def mnp_meshgrid(*xi):
- a = mnp.meshgrid(*xi)
- b = mnp.meshgrid(*xi, sparse=True)
- c = mnp.meshgrid(*xi, indexing='ij')
- d = mnp.meshgrid(*xi, sparse=False, indexing='ij')
- return a, b, c, d
-
-
- def onp_meshgrid(*xi):
- a = onp.meshgrid(*xi)
- b = onp.meshgrid(*xi, sparse=True)
- c = onp.meshgrid(*xi, indexing='ij')
- d = onp.meshgrid(*xi, sparse=False, indexing='ij')
- return a, b, c, d
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_meshgrid():
- xi = (onp.full(3, 2), onp.full(1, 5), onp.full(
- (2, 3), 9), onp.full((4, 5, 6), 7))
- for i in range(len(xi)):
- arrs = xi[i:]
- mnp_arrs = map(to_tensor, arrs)
- for mnp_res, onp_res in zip(mnp_meshgrid(*mnp_arrs), onp_meshgrid(*arrs)):
- match_all_arrays(mnp_res, onp_res)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_mgrid():
- mnp_res = mnp.mgrid[0:5]
- onp_res = onp.mgrid[0:5]
- match_all_arrays(mnp_res, onp_res, error=5)
-
- mnp_res = mnp.mgrid[2:30:4j, -10:20:7, 2:5:0.5]
- onp_res = onp.mgrid[2:30:4j, -10:20:7, 2:5:0.5]
- match_all_arrays(mnp_res, onp_res, error=5)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_ogrid():
- mnp_res = mnp.ogrid[0:5]
- onp_res = onp.ogrid[0:5]
- match_all_arrays(mnp_res, onp_res, error=5)
-
- mnp_res = mnp.ogrid[2:30:4j, -10:20:7, 2:5:0.5]
- onp_res = onp.ogrid[2:30:4j, -10:20:7, 2:5:0.5]
- match_all_arrays(mnp_res, onp_res, error=5)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_diagflat():
- arrs = [rand_int(2, 3)]
- for arr in arrs:
- for i in [-2, 0, 7]:
- match_res(mnp.diagflat, onp.diagflat, arr, k=i)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_diag():
- arrs = [rand_int(7), rand_int(5, 5), rand_int(3, 8), rand_int(9, 6)]
- for arr in arrs:
- for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
- match_res(mnp.diag, onp.diag, arr, k=i)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_diag_indices():
- mnp_res = mnp.diag_indices(5, 7)
- onp_res = onp.diag_indices(5, 7)
- match_all_arrays(mnp_res, onp_res)
-
-
- def mnp_ix_(*args):
- return mnp.ix_(*args)
-
-
- def onp_ix_(*args):
- return onp.ix_(*args)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_ix_():
- arrs = [rand_int(i + 1) for i in range(10)]
- for i in range(10):
- test_arrs = arrs[:i + 1]
- match_res(mnp_ix_, onp_ix_, *test_arrs)
-
-
- def mnp_indices():
- a = mnp.indices((2, 3))
- b = mnp.indices((2, 3, 4), sparse=True)
- return a, b
-
-
- def onp_indices():
- a = onp.indices((2, 3))
- b = onp.indices((2, 3, 4), sparse=True)
- return a, b
-
-
- def test_indices():
- run_multi_test(mnp_indices, onp_indices, ())
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_geomspace():
- start = onp.arange(1, 7).reshape(2, 3)
- end = [1000, 2000, 3000]
- match_array(mnp.geomspace(1, 256, num=9).asnumpy(),
- onp.geomspace(1, 256, num=9), error=1)
- match_array(mnp.geomspace(1, 256, num=8, endpoint=False).asnumpy(),
- onp.geomspace(1, 256, num=8, endpoint=False), error=1)
- match_array(mnp.geomspace(to_tensor(start), end, num=4).asnumpy(),
- onp.geomspace(start, end, num=4), error=1)
- match_array(mnp.geomspace(to_tensor(start), end, num=4, endpoint=False).asnumpy(),
- onp.geomspace(start, end, num=4, endpoint=False), error=1)
- match_array(mnp.geomspace(to_tensor(start), end, num=4, axis=-1).asnumpy(),
- onp.geomspace(start, end, num=4, axis=-1), error=1)
- match_array(mnp.geomspace(to_tensor(start), end, num=4, endpoint=False, axis=-1).asnumpy(),
- onp.geomspace(start, end, num=4, endpoint=False, axis=-1), error=1)
-
- start = onp.arange(1, 1 + 2*3*4*5).reshape(2, 3, 4, 5)
- end = [1000, 2000, 3000, 4000, 5000]
- for i in range(-5, 5):
- match_array(mnp.geomspace(to_tensor(start), end, num=4, axis=i).asnumpy(),
- onp.geomspace(start, end, num=4, axis=i), error=1)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_vander():
- arrs = [rand_int(i + 3) for i in range(3)]
- for i in range(3):
- mnp_vander = mnp.vander(to_tensor(arrs[i]))
- onp_vander = onp.vander(arrs[i])
- match_all_arrays(mnp_vander, onp_vander, error=1e-4)
- mnp_vander = mnp.vander(to_tensor(arrs[i]), N=2, increasing=True)
- onp_vander = onp.vander(arrs[i], N=2, increasing=True)
- match_all_arrays(mnp_vander, onp_vander, error=1e-4)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_bartlett():
- for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
- match_all_arrays(mnp.bartlett(i), onp.bartlett(i), error=3)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_blackman():
- for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
- match_all_arrays(mnp.blackman(i), onp.blackman(i), error=3)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_hamming():
- for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
- match_all_arrays(mnp.hamming(i), onp.hamming(i), error=3)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_hanning():
- for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
- match_all_arrays(mnp.hanning(i), onp.hanning(i), error=3)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_triu_indices():
- m = rand_int().tolist()
- n = rand_int().tolist()
- k = rand_int().tolist()
- mnp_res = mnp.triu_indices(n, k, m)
- onp_res = onp.triu_indices(n, k, m)
- match_all_arrays(mnp_res, onp_res)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_tril_indices():
- m = rand_int().tolist()
- n = rand_int().tolist()
- k = rand_int().tolist()
- mnp_res = mnp.tril_indices(n, k, m)
- onp_res = onp.tril_indices(n, k, m)
- match_all_arrays(mnp_res, onp_res)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_triu_indices_from():
- m = int(rand_int().tolist())
- n = int(rand_int().tolist())
- t = mnp.asarray(rand_int(m, n).tolist())
- k = rand_int().tolist()
- mnp_res = mnp.triu_indices_from(t, k)
- onp_res = onp.triu_indices_from(t.asnumpy(), k)
- match_all_arrays(mnp_res, onp_res)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_tril_indices_from():
- m = int(rand_int().tolist())
- n = int(rand_int().tolist())
- t = mnp.asarray(rand_int(m, n).tolist())
- k = rand_int().tolist()
- mnp_res = mnp.tril_indices_from(t, k)
- onp_res = onp.tril_indices_from(t.asnumpy(), k)
- match_all_arrays(mnp_res, onp_res)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_histogram_bin_edges():
- x = onp.random.randint(-10, 10, 10)
- for bins in [(1, 2, 3), [2], 1, 5, 10]:
- # pylint: disable=redefined-builtin
- for range in [None, (3, 3), (2, 20)]:
- match_res(mnp.histogram_bin_edges, onp.histogram_bin_edges, x, bins=bins, range=range, error=3)
- match_res(mnp.histogram_bin_edges, onp.histogram_bin_edges, x, onp.arange(5))
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_asarray_exception():
- with pytest.raises(TypeError):
- mnp.asarray({1, 2, 3})
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_linspace_exception():
- with pytest.raises(TypeError):
- mnp.linspace(0, 1, num=2.5)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_empty_like_exception():
- with pytest.raises(ValueError):
- mnp.empty_like([[1, 2, 3], [4, 5]])
-
-
- @pytest.mark.level0
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_pad():
- x_np = onp.random.random([2, 3, 4]).astype("float32")
- x_ms = mnp.asarray(x_np.tolist())
-
- # pad constant
- mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)))
- onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)))
- match_all_arrays(mnp_res, onp_res, error=1e-5)
- mnp_res = mnp.pad(x_ms, ((1, 1), (2, 3), (4, 5)), constant_values=((3, 4), (5, 6), (7, 8)))
- onp_res = onp.pad(x_np, ((1, 1), (2, 3), (4, 5)), constant_values=((3, 4), (5, 6), (7, 8)))
- match_all_arrays(mnp_res, onp_res, error=1e-5)
-
- # pad statistic
- mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="mean", stat_length=((1, 2), (2, 10), (3, 4)))
- onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="mean", stat_length=((1, 2), (2, 10), (3, 4)))
- match_all_arrays(mnp_res, onp_res, error=1e-5)
-
- # pad edge
- mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="edge")
- onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="edge")
- match_all_arrays(mnp_res, onp_res, error=1e-5)
-
- # pad wrap
- mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="wrap")
- onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="wrap")
- match_all_arrays(mnp_res, onp_res, error=1e-5)
-
- # pad linear_ramp
- mnp_res = mnp.pad(x_ms, ((1, 3), (5, 2), (3, 0)), mode="linear_ramp", end_values=((0, 10), (9, 1), (-10, 99)))
- onp_res = onp.pad(x_np, ((1, 3), (5, 2), (3, 0)), mode="linear_ramp", end_values=((0, 10), (9, 1), (-10, 99)))
- match_all_arrays(mnp_res, onp_res, error=1e-5)
-
-
- def pad_with_msfunc(vector, pad_width, iaxis, kwargs):
- pad_value = kwargs.get('padder', 10)
- vector[:pad_width[0]] = pad_value
- vector[-pad_width[1]:] = pad_value
- return vector
-
-
- def pad_with_npfunc(vector, pad_width, iaxis, kwargs):
- pad_value = kwargs.get('padder', 10)
- vector[:pad_width[0]] = pad_value
- vector[-pad_width[1]:] = pad_value
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_pad_gpu():
- x_np = onp.random.random([2, 1, 4, 3]).astype("float32")
- x_ms = mnp.asarray(x_np.tolist())
-
- # pad symmetric odd
- mnp_res = mnp.pad(x_ms, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='symmetric', reflect_type='odd')
- onp_res = onp.pad(x_np, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='symmetric', reflect_type='odd')
- match_all_arrays(mnp_res, onp_res, error=1e-5)
-
- # pad symmetric even
- mnp_res = mnp.pad(x_ms, ((10, 13), (5, 12), (3, 0), (2, 6)), mode='symmetric', reflect_type='even')
- onp_res = onp.pad(x_np, ((10, 13), (5, 12), (3, 0), (2, 6)), mode='symmetric', reflect_type='even')
- match_all_arrays(mnp_res, onp_res, error=1e-5)
-
- # pad reflect odd
- mnp_res = mnp.pad(x_ms, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='reflect', reflect_type='odd')
- onp_res = onp.pad(x_np, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='reflect', reflect_type='odd')
- match_all_arrays(mnp_res, onp_res, error=1e-5)
-
- # pad reflect even
- mnp_res = mnp.pad(x_ms, ((10, 13)), mode='reflect', reflect_type='even')
- onp_res = onp.pad(x_np, ((10, 13)), mode='reflect', reflect_type='even')
- match_all_arrays(mnp_res, onp_res, error=1e-5)
-
- # pad func
- x_np = onp.random.random([2, 4]).astype("float32")
- x_ms = mnp.asarray(x_np.tolist())
- mnp_res = mnp.pad(x_ms, ((5, 5)), mode=pad_with_msfunc, padder=99)
- onp_res = onp.pad(x_np, ((5, 5)), mode=pad_with_npfunc, padder=99)
- match_all_arrays(mnp_res, onp_res, error=1e-5)
|