1# Copyright 2020-2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15"""unit tests for numpy array operations""" 16 17import functools 18 19import pytest 20import numpy as onp 21 22import mindspore.numpy as mnp 23from mindspore import context 24from mindspore.nn import Cell 25 26from .utils import rand_int, run_non_kw_test, check_all_results, match_array, \ 27 rand_bool, match_res, run_multi_test, to_tensor, match_all_arrays 28 29context.set_context(mode=context.PYNATIVE_MODE) 30 31 32class Cases(): 33 def __init__(self): 34 self.all_shapes = [ 35 1, 2, (1,), (2,), (1, 2, 3), [1], [2], [1, 2, 3] 36 ] 37 self.onp_dtypes = [onp.int32, 'int32', int, 38 onp.float32, 'float32', float, 39 onp.uint32, 'uint32', 40 onp.bool_, 'bool', bool] 41 42 self.mnp_dtypes = [mnp.int32, 'int32', int, 43 mnp.float32, 'float32', float, 44 mnp.uint32, 'uint32', 45 mnp.bool_, 'bool', bool] 46 47 self.array_sets = [1, 1.1, True, [1, 0, True], [1, 1.0, 2], (1,), 48 [(1, 2, 3), (4, 5, 6)], onp.random.random( # pylint: disable=no-member 49 (100, 100)).astype(onp.float32), 50 onp.random.random((100, 100)).astype(onp.bool)] 51 52 self.arrs = [ 53 rand_int(2), 54 rand_int(2, 3), 55 rand_int(2, 3, 4), 56 rand_int(2, 3, 4, 5), 57 ] 58 59 # scalars expanded across the 0th dimension 60 self.scalars = [ 61 rand_int(), 62 rand_int(1), 63 rand_int(1, 1), 64 rand_int(1, 1, 1), 65 ] 66 67 # arrays of the same size expanded across the 0th dimension 68 self.expanded_arrs = [ 69 rand_int(2, 3), 70 rand_int(1, 2, 3), 71 rand_int(1, 1, 2, 3), 72 rand_int(1, 1, 1, 2, 3), 73 ] 74 75 # arrays with dimensions of size 1 76 self.nested_arrs = [ 77 rand_int(1), 78 rand_int(1, 2), 79 rand_int(3, 1, 8), 80 rand_int(1, 3, 9, 1), 81 ] 82 83 # arrays which can be broadcast 84 self.broadcastables = [ 85 rand_int(5), 86 rand_int(6, 1), 87 rand_int(7, 1, 5), 88 rand_int(8, 1, 6, 1) 89 ] 90 91 # boolean arrays which can be broadcast 92 self.bool_broadcastables = [ 93 rand_bool(), 94 rand_bool(1), 95 rand_bool(5), 96 rand_bool(6, 1), 97 rand_bool(7, 1, 5), 98 rand_bool(8, 1, 6, 1), 99 ] 100 101 self.mnp_prototypes = [ 102 mnp.ones((2, 3, 4)), 103 [mnp.ones(3), (1, 2, 3), onp.ones(3), [4, 5, 6]], 104 ([(1, 2), mnp.ones(2)], (onp.ones(2), [3, 4])), 105 ] 106 107 self.onp_prototypes = [ 108 onp.ones((2, 3, 4)), 109 [onp.ones(3), (1, 2, 3), onp.ones(3), [4, 5, 6]], 110 ([(1, 2), onp.ones(2)], (onp.ones(2), [3, 4])), 111 ] 112 113 114# Test np.transpose and np.ndarray.transpose 115def mnp_transpose(input_tensor): 116 a = mnp.transpose(input_tensor, (0, 2, 1)) 117 b = mnp.transpose(input_tensor, [2, 1, 0]) 118 c = mnp.transpose(input_tensor, (1, 0, 2)) 119 d = mnp.transpose(input_tensor) 120 return a, b, c, d 121 122 123def onp_transpose(input_array): 124 a = onp.transpose(input_array, (0, 2, 1)) 125 b = onp.transpose(input_array, [2, 1, 0]) 126 c = onp.transpose(input_array, (1, 0, 2)) 127 d = onp.transpose(input_array) 128 return a, b, c, d 129 130 131@pytest.mark.level1 132@pytest.mark.platform_arm_ascend_training 133@pytest.mark.platform_x86_ascend_training 134@pytest.mark.platform_x86_gpu_training 135@pytest.mark.platform_x86_cpu 136@pytest.mark.env_onecard 137def test_transpose(): 138 onp_array = onp.random.random((3, 4, 5)).astype('float32') 139 mnp_array = to_tensor(onp_array) 140 o_transposed = onp_transpose(onp_array) 141 m_transposed = mnp_transpose(mnp_array) 142 check_all_results(o_transposed, m_transposed) 143 144 145# Test np.expand_dims 146def mnp_expand_dims(input_tensor): 147 a = mnp.expand_dims(input_tensor, 0) 148 b = mnp.expand_dims(input_tensor, -1) 149 c = mnp.expand_dims(input_tensor, axis=2) 150 d = mnp.expand_dims(input_tensor, axis=-2) 151 return a, b, c, d 152 153 154def onp_expand_dims(input_array): 155 a = onp.expand_dims(input_array, 0) 156 b = onp.expand_dims(input_array, -1) 157 c = onp.expand_dims(input_array, axis=2) 158 d = onp.expand_dims(input_array, axis=-2) 159 return a, b, c, d 160 161 162@pytest.mark.level1 163@pytest.mark.platform_arm_ascend_training 164@pytest.mark.platform_x86_ascend_training 165@pytest.mark.platform_x86_gpu_training 166@pytest.mark.platform_x86_cpu 167@pytest.mark.env_onecard 168def test_expand_dims(): 169 onp_array = onp.random.random((3, 4, 5)).astype('float32') 170 mnp_array = to_tensor(onp_array) 171 o_expanded = onp_expand_dims(onp_array) 172 m_expanded = mnp_expand_dims(mnp_array) 173 check_all_results(o_expanded, m_expanded) 174 175 176# Test np.squeeze 177def mnp_squeeze(input_tensor): 178 a = mnp.squeeze(input_tensor) 179 b = mnp.squeeze(input_tensor, 0) 180 c = mnp.squeeze(input_tensor, axis=None) 181 d = mnp.squeeze(input_tensor, axis=-3) 182 e = mnp.squeeze(input_tensor, (2,)) 183 f = mnp.squeeze(input_tensor, (0, 2)) 184 return a, b, c, d, e, f 185 186 187def onp_squeeze(input_array): 188 a = onp.squeeze(input_array) 189 b = onp.squeeze(input_array, 0) 190 c = onp.squeeze(input_array, axis=None) 191 d = onp.squeeze(input_array, axis=-3) 192 e = onp.squeeze(input_array, (2,)) 193 f = onp.squeeze(input_array, (0, 2)) 194 return a, b, c, d, e, f 195 196 197@pytest.mark.level1 198@pytest.mark.platform_arm_ascend_training 199@pytest.mark.platform_x86_ascend_training 200@pytest.mark.platform_x86_gpu_training 201@pytest.mark.platform_x86_cpu 202@pytest.mark.env_onecard 203def test_squeeze(): 204 onp_array = onp.random.random((1, 3, 1, 4, 2)).astype('float32') 205 mnp_array = to_tensor(onp_array) 206 o_squeezed = onp_squeeze(onp_array) 207 m_squeezed = mnp_squeeze(mnp_array) 208 check_all_results(o_squeezed, m_squeezed) 209 210 onp_array = onp.random.random((1, 1, 1, 1, 1)).astype('float32') 211 mnp_array = to_tensor(onp_array) 212 o_squeezed = onp_squeeze(onp_array) 213 m_squeezed = mnp_squeeze(mnp_array) 214 check_all_results(o_squeezed, m_squeezed) 215 216 217# Test np.rollaxis 218def mnp_rollaxis(input_tensor): 219 a = mnp.rollaxis(input_tensor, 0, 1) 220 b = mnp.rollaxis(input_tensor, 0, 2) 221 c = mnp.rollaxis(input_tensor, 2, 1) 222 d = mnp.rollaxis(input_tensor, 2, 2) 223 e = mnp.rollaxis(input_tensor, 0) 224 f = mnp.rollaxis(input_tensor, 1) 225 return a, b, c, d, e, f 226 227 228def onp_rollaxis(input_array): 229 a = onp.rollaxis(input_array, 0, 1) 230 b = onp.rollaxis(input_array, 0, 2) 231 c = onp.rollaxis(input_array, 2, 1) 232 d = onp.rollaxis(input_array, 2, 2) 233 e = onp.rollaxis(input_array, 0) 234 f = onp.rollaxis(input_array, 1) 235 return a, b, c, d, e, f 236 237 238@pytest.mark.level1 239@pytest.mark.platform_arm_ascend_training 240@pytest.mark.platform_x86_ascend_training 241@pytest.mark.platform_x86_gpu_training 242@pytest.mark.platform_x86_cpu 243@pytest.mark.env_onecard 244def test_rollaxis(): 245 onp_array = onp.random.random((3, 4, 5)).astype('float32') 246 mnp_array = to_tensor(onp_array) 247 o_rolled = onp_rollaxis(onp_array) 248 m_rolled = mnp_rollaxis(mnp_array) 249 check_all_results(o_rolled, m_rolled) 250 251 252# Test np.swapaxes 253def mnp_swapaxes(input_tensor): 254 a = mnp.swapaxes(input_tensor, 0, 1) 255 b = mnp.swapaxes(input_tensor, 1, 0) 256 c = mnp.swapaxes(input_tensor, 1, 1) 257 d = mnp.swapaxes(input_tensor, 2, 1) 258 e = mnp.swapaxes(input_tensor, 1, 2) 259 f = mnp.swapaxes(input_tensor, 2, 2) 260 return a, b, c, d, e, f 261 262 263def onp_swapaxes(input_array): 264 a = onp.swapaxes(input_array, 0, 1) 265 b = onp.swapaxes(input_array, 1, 0) 266 c = onp.swapaxes(input_array, 1, 1) 267 d = onp.swapaxes(input_array, 2, 1) 268 e = onp.swapaxes(input_array, 1, 2) 269 f = onp.swapaxes(input_array, 2, 2) 270 return a, b, c, d, e, f 271 272 273@pytest.mark.level1 274@pytest.mark.platform_arm_ascend_training 275@pytest.mark.platform_x86_ascend_training 276@pytest.mark.platform_x86_gpu_training 277@pytest.mark.platform_x86_cpu 278@pytest.mark.env_onecard 279def test_swapaxes(): 280 onp_array = onp.random.random((3, 4, 5)).astype('float32') 281 mnp_array = to_tensor(onp_array) 282 o_swaped = onp_swapaxes(onp_array) 283 m_swaped = mnp_swapaxes(mnp_array) 284 check_all_results(o_swaped, m_swaped) 285 286 287# Test np.reshape 288def mnp_reshape(input_tensor): 289 a = mnp.reshape(input_tensor, (3, 8)) 290 b = mnp.reshape(input_tensor, [3, -1]) 291 c = mnp.reshape(input_tensor, (-1, 12)) 292 d = mnp.reshape(input_tensor, (-1,)) 293 e = mnp.reshape(input_tensor, 24) 294 f = mnp.reshape(input_tensor, [2, 4, -1]) 295 g = input_tensor.reshape(3, 8) 296 h = input_tensor.reshape(3, -1) 297 i = input_tensor.reshape([-1, 3]) 298 j = input_tensor.reshape(-1) 299 return a, b, c, d, e, f, g, h, i, j 300 301 302def onp_reshape(input_array): 303 a = onp.reshape(input_array, (3, 8)) 304 b = onp.reshape(input_array, [3, -1]) 305 c = onp.reshape(input_array, (-1, 12)) 306 d = onp.reshape(input_array, (-1,)) 307 e = onp.reshape(input_array, 24) 308 f = onp.reshape(input_array, [2, 4, -1]) 309 g = input_array.reshape(3, 8) 310 h = input_array.reshape(3, -1) 311 i = input_array.reshape([-1, 3]) 312 j = input_array.reshape(-1) 313 return a, b, c, d, e, f, g, h, i, j 314 315 316@pytest.mark.level1 317@pytest.mark.platform_arm_ascend_training 318@pytest.mark.platform_x86_ascend_training 319@pytest.mark.platform_x86_gpu_training 320@pytest.mark.platform_x86_cpu 321@pytest.mark.env_onecard 322def test_reshape(): 323 onp_array = onp.random.random((2, 3, 4)).astype('float32') 324 mnp_array = to_tensor(onp_array) 325 o_reshaped = onp_reshape(onp_array) 326 m_reshaped = mnp_reshape(mnp_array) 327 check_all_results(o_reshaped, m_reshaped) 328 329 330# Test np.ravel 331def mnp_ravel(input_tensor): 332 a = mnp.ravel(input_tensor) 333 return a 334 335 336def onp_ravel(input_array): 337 a = onp.ravel(input_array) 338 return a 339 340 341@pytest.mark.level1 342@pytest.mark.platform_arm_ascend_training 343@pytest.mark.platform_x86_ascend_training 344@pytest.mark.platform_x86_gpu_training 345@pytest.mark.platform_x86_cpu 346@pytest.mark.env_onecard 347def test_ravel(): 348 onp_array = onp.random.random((2, 3, 4)).astype('float32') 349 mnp_array = to_tensor(onp_array) 350 o_ravel = onp_ravel(onp_array) 351 m_ravel = mnp_ravel(mnp_array).asnumpy() 352 match_array(o_ravel, m_ravel) 353 354 355# Test np.concatenate 356def mnp_concatenate(input_tensor): 357 a = mnp.concatenate(input_tensor, None) 358 b = mnp.concatenate(input_tensor, 0) 359 c = mnp.concatenate(input_tensor, 1) 360 d = mnp.concatenate(input_tensor, 2) 361 return a, b, c, d 362 363 364def onp_concatenate(input_array): 365 a = onp.concatenate(input_array, None) 366 b = onp.concatenate(input_array, 0) 367 c = onp.concatenate(input_array, 1) 368 d = onp.concatenate(input_array, 2) 369 return a, b, c, d 370 371 372@pytest.mark.level1 373@pytest.mark.platform_arm_ascend_training 374@pytest.mark.platform_x86_ascend_training 375@pytest.mark.platform_x86_gpu_training 376@pytest.mark.platform_x86_cpu 377@pytest.mark.env_onecard 378def test_concatenate(): 379 onp_array = onp.random.random((5, 4, 3, 2)).astype('float32') 380 mnp_array = to_tensor(onp_array) 381 o_concatenate = onp_concatenate(onp_array) 382 m_concatenate = mnp_concatenate(mnp_array) 383 check_all_results(o_concatenate, m_concatenate) 384 385 386def mnp_append(arr1, arr2): 387 a = mnp.append(arr1, arr2) 388 b = mnp.append(arr1, arr2, axis=0) 389 c = mnp.append(arr1, arr2, axis=-1) 390 return a, b, c 391 392def onp_append(arr1, arr2): 393 a = onp.append(arr1, arr2) 394 b = onp.append(arr1, arr2, axis=0) 395 c = onp.append(arr1, arr2, axis=-1) 396 return a, b, c 397 398@pytest.mark.level1 399@pytest.mark.platform_arm_ascend_training 400@pytest.mark.platform_x86_ascend_training 401@pytest.mark.platform_x86_gpu_training 402@pytest.mark.platform_x86_cpu 403@pytest.mark.env_onecard 404def test_append(): 405 onp_array = onp.random.random((4, 3, 2)).astype('float32') 406 onp_value = onp.random.random((4, 3, 2)).astype('float32') 407 mnp_array = to_tensor(onp_array) 408 mnp_value = to_tensor(onp_value) 409 onp_res = onp_append(onp_array, onp_value) 410 mnp_res = mnp_append(mnp_array, mnp_value) 411 check_all_results(onp_res, mnp_res) 412 413 414def construct_arrays(n=1, ndim=1, axis=None, low=1, high=5): 415 onp_array_lst = [] 416 mnp_array_lst = [] 417 shape = onp.random.randint(low=low, high=high, size=ndim) 418 new_shape = [sh for sh in shape] 419 while n > 0: 420 n -= 1 421 onp_array1 = onp.random.randint( 422 low=low, high=high, size=shape).astype(onp.float32) 423 onp_array_lst.append(onp_array1) 424 mnp_array_lst.append(to_tensor(onp_array1)) 425 if axis is not None and axis < ndim: 426 new_shape[axis] += onp.random.randint(2) 427 onp_array2 = onp.random.randint( 428 low=low, high=high, size=new_shape).astype(onp.float32) 429 onp_array_lst.append(onp_array2) 430 mnp_array_lst.append(to_tensor(onp_array2)) 431 return onp_array_lst, mnp_array_lst 432 433# Test np.xstack 434 435 436def prepare_array_sequences(n_lst, ndim_lst, axis=None, low=1, high=5): 437 onp_seq_lst = [] 438 mnp_seq_lst = [] 439 for n in n_lst: 440 for ndim in ndim_lst: 441 onp_array_lst, mnp_array_lst = construct_arrays( 442 n=n, ndim=ndim, axis=axis, low=low, high=high) 443 onp_seq_lst.append(onp_array_lst) 444 mnp_seq_lst.append(mnp_array_lst) 445 return onp_seq_lst, mnp_seq_lst 446 447 448def mnp_column_stack(input_tensor): 449 return mnp.column_stack(input_tensor) 450 451 452def onp_column_stack(input_array): 453 return onp.column_stack(input_array) 454 455 456@pytest.mark.level1 457@pytest.mark.platform_arm_ascend_training 458@pytest.mark.platform_x86_ascend_training 459@pytest.mark.platform_x86_gpu_training 460@pytest.mark.platform_x86_cpu 461@pytest.mark.env_onecard 462def test_column_stack(): 463 onp_seq_lst, mnp_seq_lst = prepare_array_sequences( 464 n_lst=[1, 5], ndim_lst=[1, 2, 3, 4], axis=1) 465 for i, onp_seq in enumerate(onp_seq_lst): 466 onp_seq = onp_seq_lst[i] 467 mnp_seq = mnp_seq_lst[i] 468 o_column_stack = onp_column_stack(onp_seq) 469 m_column_stack = mnp_column_stack(mnp_seq) 470 check_all_results(o_column_stack, m_column_stack) 471 472 473def mnp_hstack(input_tensor): 474 return mnp.hstack(input_tensor) 475 476 477def onp_hstack(input_array): 478 return onp.hstack(input_array) 479 480 481@pytest.mark.level1 482@pytest.mark.platform_arm_ascend_training 483@pytest.mark.platform_x86_ascend_training 484@pytest.mark.platform_x86_gpu_training 485@pytest.mark.platform_x86_cpu 486@pytest.mark.env_onecard 487def test_hstack(): 488 onp_seq_lst0, mnp_seq_lst0 = prepare_array_sequences( 489 n_lst=[1, 5], ndim_lst=[2, 3, 4], axis=1) 490 onp_seq_lst1, mnp_seq_lst1 = prepare_array_sequences( 491 n_lst=[1, 5], ndim_lst=[1], axis=0) 492 onp_seq_lst = onp_seq_lst0 + onp_seq_lst1 493 mnp_seq_lst = mnp_seq_lst0 + mnp_seq_lst1 494 for i, onp_seq in enumerate(onp_seq_lst): 495 mnp_seq = mnp_seq_lst[i] 496 o_hstack = onp_hstack(onp_seq) 497 m_hstack = mnp_hstack(mnp_seq) 498 check_all_results(o_hstack, m_hstack) 499 500 501def mnp_dstack(input_tensor): 502 return mnp.dstack(input_tensor) 503 504 505def onp_dstack(input_array): 506 return onp.dstack(input_array) 507 508 509@pytest.mark.level1 510@pytest.mark.platform_arm_ascend_training 511@pytest.mark.platform_x86_ascend_training 512@pytest.mark.platform_x86_gpu_training 513@pytest.mark.platform_x86_cpu 514@pytest.mark.env_onecard 515def test_dstack(): 516 onp_seq_lst, mnp_seq_lst = prepare_array_sequences( 517 n_lst=[1, 5], ndim_lst=[1, 2, 3, 4], axis=2) 518 for i, onp_seq in enumerate(onp_seq_lst): 519 mnp_seq = mnp_seq_lst[i] 520 o_dstack = onp_dstack(onp_seq) 521 m_dstack = mnp_dstack(mnp_seq) 522 check_all_results(o_dstack, m_dstack) 523 524 525def mnp_vstack(input_tensor): 526 return mnp.vstack(input_tensor) 527 528 529def onp_vstack(input_array): 530 return onp.vstack(input_array) 531 532 533@pytest.mark.level1 534@pytest.mark.platform_arm_ascend_training 535@pytest.mark.platform_x86_ascend_training 536@pytest.mark.platform_x86_gpu_training 537@pytest.mark.platform_x86_cpu 538@pytest.mark.env_onecard 539def test_vstack(): 540 onp_seq_lst, mnp_seq_lst = prepare_array_sequences( 541 n_lst=[1], ndim_lst=[2], axis=0) 542 for i, onp_seq in enumerate(onp_seq_lst): 543 mnp_seq = mnp_seq_lst[i] 544 o_vstack = onp_vstack(onp_seq) 545 m_vstack = mnp_vstack(mnp_seq) 546 check_all_results(o_vstack, m_vstack) 547# Test np.atleastxd 548 549 550def mnp_atleast1d(*arys): 551 return mnp.atleast_1d(*arys) 552 553 554def onp_atleast1d(*arys): 555 return onp.atleast_1d(*arys) 556 557 558def mnp_atleast2d(*arys): 559 return mnp.atleast_2d(*arys) 560 561 562def onp_atleast2d(*arys): 563 return onp.atleast_2d(*arys) 564 565 566def mnp_atleast3d(*arys): 567 return mnp.atleast_3d(*arys) 568 569 570def onp_atleast3d(*arys): 571 return onp.atleast_3d(*arys) 572 573 574@pytest.mark.level1 575@pytest.mark.platform_arm_ascend_training 576@pytest.mark.platform_x86_ascend_training 577@pytest.mark.platform_x86_gpu_training 578@pytest.mark.platform_x86_cpu 579@pytest.mark.env_onecard 580def test_atleast1d(): 581 run_non_kw_test(mnp_atleast1d, onp_atleast1d, Cases()) 582 583 584@pytest.mark.level1 585@pytest.mark.platform_arm_ascend_training 586@pytest.mark.platform_x86_ascend_training 587@pytest.mark.platform_x86_gpu_training 588@pytest.mark.platform_x86_cpu 589@pytest.mark.env_onecard 590def test_atleast2d(): 591 run_non_kw_test(mnp_atleast2d, onp_atleast2d, Cases()) 592 593 594@pytest.mark.level1 595@pytest.mark.platform_arm_ascend_training 596@pytest.mark.platform_x86_ascend_training 597@pytest.mark.platform_x86_gpu_training 598@pytest.mark.platform_x86_cpu 599@pytest.mark.env_onecard 600def test_atleast3d(): 601 run_non_kw_test(mnp_atleast3d, onp_atleast3d, Cases()) 602 603 604# Test np.where 605def mnp_where(condition, x, y): 606 return mnp.where(condition, x, y) 607 608 609def onp_where(condition, x, y): 610 return onp.where(condition, x, y) 611 612 613@pytest.mark.level0 614@pytest.mark.platform_arm_ascend_training 615@pytest.mark.platform_x86_ascend_training 616@pytest.mark.platform_x86_gpu_training 617@pytest.mark.platform_x86_cpu 618@pytest.mark.env_onecard 619def test_where(): 620 test_case = Cases() 621 for condition1 in test_case.bool_broadcastables[:2]: 622 for x in test_case.broadcastables[:2]: 623 for y in test_case.broadcastables[:2]: 624 for condition2 in test_case.broadcastables[:2]: 625 match_res(mnp_where, onp_where, condition1, x, y) 626 match_res(mnp_where, onp_where, condition2, x, y) 627 628 629# Test ndarray.flatten 630def mnp_ndarray_flatten(input_tensor): 631 a = input_tensor.flatten() 632 b = input_tensor.flatten(order='F') 633 c = input_tensor.flatten(order='C') 634 return a, b, c 635 636 637def onp_ndarray_flatten(input_array): 638 a = input_array.flatten() 639 b = input_array.flatten(order='F') 640 c = input_array.flatten(order='C') 641 return a, b, c 642 643 644@pytest.mark.level1 645@pytest.mark.platform_arm_ascend_training 646@pytest.mark.platform_x86_ascend_training 647@pytest.mark.platform_x86_gpu_training 648@pytest.mark.platform_x86_cpu 649@pytest.mark.env_onecard 650def test_ndarray_flatten(): 651 onp_array = onp.random.random((3, 4, 5)).astype('float32') 652 mnp_array = to_tensor(onp_array) 653 o_flatten = onp_ndarray_flatten(onp_array) 654 m_flatten = mnp_ndarray_flatten(mnp_array) 655 check_all_results(o_flatten, m_flatten) 656 657 658# Test ndarray.transpose 659def mnp_ndarray_transpose(input_tensor): 660 a = input_tensor.T 661 b = input_tensor.transpose() 662 c = input_tensor.transpose((0, 2, 1)) 663 d = input_tensor.transpose([0, 2, 1]) 664 return a, b, c, d 665 666 667def onp_ndarray_transpose(input_array): 668 a = input_array.T 669 b = input_array.transpose() 670 c = input_array.transpose((0, 2, 1)) 671 d = input_array.transpose([0, 2, 1]) 672 return a, b, c, d 673 674 675@pytest.mark.level1 676@pytest.mark.platform_arm_ascend_training 677@pytest.mark.platform_x86_ascend_training 678@pytest.mark.platform_x86_gpu_training 679@pytest.mark.platform_x86_cpu 680@pytest.mark.env_onecard 681def test_ndarray_transpose(): 682 onp_array = onp.random.random((3, 4, 5)).astype('float32') 683 mnp_array = to_tensor(onp_array) 684 o_transposed = onp_ndarray_transpose(onp_array) 685 m_transposed = mnp_ndarray_transpose(mnp_array) 686 check_all_results(o_transposed, m_transposed) 687 688 689# Test ndarray.astype 690def mnp_ndarray_astype(input_tensor): 691 a = input_tensor.astype("float16") 692 b = input_tensor.astype(onp.float64) 693 c = input_tensor.astype(mnp.bool_) 694 return a, b, c 695 696 697def onp_ndarray_astype(input_array): 698 a = input_array.astype("float16") 699 b = input_array.astype(onp.float64) 700 c = input_array.astype(onp.bool_) 701 return a, b, c 702 703 704@pytest.mark.level1 705@pytest.mark.platform_arm_ascend_training 706@pytest.mark.platform_x86_ascend_training 707@pytest.mark.platform_x86_gpu_training 708@pytest.mark.platform_x86_cpu 709@pytest.mark.env_onecard 710def test_ndarray_astype(): 711 onp_array = onp.random.random((3, 4, 5)).astype('float32') 712 mnp_array = to_tensor(onp_array) 713 o_astype = onp_ndarray_astype(onp_array) 714 m_astype = mnp_ndarray_astype(mnp_array) 715 for arr1, arr2 in zip(o_astype, m_astype): 716 assert arr1.dtype == arr2.asnumpy().dtype 717 718 719def onp_concatenate_type_promotion(onp_array1, onp_array2, onp_array3, onp_array4): 720 o_concatenate = onp.concatenate((onp_array1, 721 onp_array2, 722 onp_array3, 723 onp_array4), -1) 724 return o_concatenate 725 726 727def mnp_concatenate_type_promotion(mnp_array1, mnp_array2, mnp_array3, mnp_array4): 728 m_concatenate = mnp.concatenate([mnp_array1, 729 mnp_array2, 730 mnp_array3, 731 mnp_array4], -1) 732 return m_concatenate 733 734 735@pytest.mark.level1 736@pytest.mark.platform_arm_ascend_training 737@pytest.mark.platform_x86_ascend_training 738@pytest.mark.platform_x86_gpu_training 739@pytest.mark.platform_x86_cpu 740@pytest.mark.env_onecard 741def test_concatenate_type_promotion(): 742 onp_array = onp.random.random((5, 1)).astype('float32') 743 mnp_array = to_tensor(onp_array) 744 onp_array1 = onp_array.astype(onp.float16) 745 onp_array2 = onp_array.astype(onp.bool_) 746 onp_array3 = onp_array.astype(onp.float32) 747 onp_array4 = onp_array.astype(onp.int32) 748 749 mnp_array1 = mnp_array.astype(onp.float16) 750 mnp_array2 = mnp_array.astype(onp.bool_) 751 mnp_array3 = mnp_array.astype(onp.float32) 752 mnp_array4 = mnp_array.astype(onp.int32) 753 o_concatenate = onp_concatenate_type_promotion( 754 onp_array1, onp_array2, onp_array3, onp_array4).astype('float32') 755 m_concatenate = mnp_concatenate_type_promotion( 756 mnp_array1, mnp_array2, mnp_array3, mnp_array4) 757 check_all_results(o_concatenate, m_concatenate, error=1e-7) 758 759 760def mnp_stack(*arrs): 761 a = mnp.stack(arrs, axis=-4) 762 b = mnp.stack(arrs, axis=-3) 763 c = mnp.stack(arrs, axis=0) 764 d = mnp.stack(arrs, axis=3) 765 e = mnp.stack(arrs, axis=2) 766 return a, b, c, d, e 767 768 769def onp_stack(*arrs): 770 a = onp.stack(arrs, axis=-4) 771 b = onp.stack(arrs, axis=-3) 772 c = onp.stack(arrs, axis=0) 773 d = onp.stack(arrs, axis=3) 774 e = onp.stack(arrs, axis=2) 775 return a, b, c, d, e 776 777 778@pytest.mark.level1 779@pytest.mark.platform_arm_ascend_training 780@pytest.mark.platform_x86_ascend_training 781@pytest.mark.platform_x86_gpu_training 782@pytest.mark.platform_x86_cpu 783@pytest.mark.env_onecard 784def test_stack(): 785 arr = rand_int(3, 4, 5, 6) 786 match_res(mnp.stack, onp.stack, arr) 787 for i in range(-4, 4): 788 match_res(mnp.stack, onp.stack, arr, axis=i) 789 790 arrs = [rand_int(3, 4, 5) for i in range(10)] 791 match_res(mnp.stack, onp.stack, arrs) 792 match_res(mnp.stack, onp.stack, tuple(arrs)) 793 match_res(mnp_stack, onp_stack, *arrs) 794 for i in range(-4, 4): 795 match_res(mnp.stack, onp.stack, arrs, axis=i) 796 797 798def mnp_roll(input_tensor): 799 a = mnp.roll(input_tensor, -3) 800 b = mnp.roll(input_tensor, [-2, -3], 1) 801 c = mnp.roll(input_tensor, (3, 0, -5), (-1, -2, 0)) 802 d = mnp.roll(input_tensor, (4,), [0, 0, 1]) 803 return a, b, c, d 804 805 806def onp_roll(input_array): 807 a = onp.roll(input_array, -3) 808 b = onp.roll(input_array, [-2, -3], 1) 809 c = onp.roll(input_array, (3, 0, -5), (-1, -2, 0)) 810 d = onp.roll(input_array, (4,), [0, 0, 1]) 811 return a, b, c, d 812 813 814@pytest.mark.level1 815@pytest.mark.platform_arm_ascend_training 816@pytest.mark.platform_x86_ascend_training 817@pytest.mark.platform_x86_gpu_training 818@pytest.mark.platform_x86_cpu 819@pytest.mark.env_onecard 820def test_roll(): 821 arr = rand_int(3, 4, 5) 822 match_res(mnp_roll, onp_roll, arr) 823 arr = rand_int(1, 4, 6).astype("int64") 824 match_res(mnp_roll, onp_roll, arr) 825 826 827def mnp_moveaxis(a): 828 a = mnp.moveaxis(a, 3, 3) 829 b = mnp.moveaxis(a, -1, 4) 830 c = mnp.moveaxis(a, (2, 1, 4), (0, 3, 2)) 831 d = mnp.moveaxis(a, [-2, -5], [2, -4]) 832 return a, b, c, d 833 834 835def onp_moveaxis(a): 836 a = onp.moveaxis(a, 3, 3) 837 b = onp.moveaxis(a, -1, 4) 838 c = onp.moveaxis(a, (2, 1, 4), (0, 3, 2)) 839 d = onp.moveaxis(a, [-2, -5], [2, -4]) 840 return a, b, c, d 841 842 843@pytest.mark.level1 844@pytest.mark.platform_arm_ascend_training 845@pytest.mark.platform_x86_ascend_training 846@pytest.mark.platform_x86_gpu_training 847@pytest.mark.platform_x86_cpu 848@pytest.mark.env_onecard 849def test_moveaxis(): 850 a = rand_int(2, 4, 5, 9, 6) 851 match_res(mnp_moveaxis, onp_moveaxis, a) 852 853 854def mnp_tile(x): 855 a = mnp.tile(x, 1) 856 b = mnp.tile(x, 3) 857 c = mnp.tile(x, [5, 1]) 858 d = mnp.tile(x, [5, 1, 2, 3, 7]) 859 return a, b, c, d 860 861 862def onp_tile(x): 863 a = onp.tile(x, 1) 864 b = onp.tile(x, 3) 865 c = onp.tile(x, [5, 1]) 866 d = onp.tile(x, [5, 1, 2, 3, 7]) 867 return a, b, c, d 868 869 870@pytest.mark.level1 871@pytest.mark.platform_arm_ascend_training 872@pytest.mark.platform_x86_ascend_training 873@pytest.mark.platform_x86_gpu_training 874@pytest.mark.platform_x86_cpu 875@pytest.mark.env_onecard 876def test_tile(): 877 a = rand_int(2, 3, 4) 878 match_res(mnp_tile, onp_tile, a) 879 880 881def mnp_broadcast_to(x): 882 a = mnp.broadcast_to(x, (2, 3)) 883 b = mnp.broadcast_to(x, (8, 1, 3)) 884 return a, b 885 886 887def onp_broadcast_to(x): 888 a = onp.broadcast_to(x, (2, 3)) 889 b = onp.broadcast_to(x, (8, 1, 3)) 890 return a, b 891 892 893@pytest.mark.level0 894@pytest.mark.platform_arm_ascend_training 895@pytest.mark.platform_x86_ascend_training 896@pytest.mark.platform_x86_gpu_training 897@pytest.mark.platform_x86_cpu 898@pytest.mark.env_onecard 899def test_broadcast_to(): 900 x = rand_int() 901 match_res(mnp_broadcast_to, onp_broadcast_to, x) 902 x = rand_int(3) 903 match_res(mnp_broadcast_to, onp_broadcast_to, x) 904 x = rand_int(1, 3) 905 match_res(mnp_broadcast_to, onp_broadcast_to, x) 906 907 908def mnp_broadcast_arrays(*args): 909 return mnp.broadcast_arrays(*args) 910 911 912def onp_broadcast_arrays(*args): 913 return onp.broadcast_arrays(*args) 914 915 916@pytest.mark.level1 917@pytest.mark.platform_arm_ascend_training 918@pytest.mark.platform_x86_ascend_training 919@pytest.mark.platform_x86_gpu_training 920@pytest.mark.platform_x86_cpu 921@pytest.mark.env_onecard 922def test_broadcast_arrays(): 923 test_case = Cases() 924 broadcastables = test_case.broadcastables 925 for i in range(len(broadcastables)): 926 arrs = broadcastables[i:] 927 match_res(mnp_broadcast_arrays, onp_broadcast_arrays, *arrs) 928 929 930def mnp_flip(x): 931 a = mnp.flip(x) 932 b = mnp.flip(x, 0) 933 c = mnp.flip(x, 1) 934 d = mnp.flip(x, (-3, -1)) 935 return a, b, c, d 936 937 938def onp_flip(x): 939 a = onp.flip(x) 940 b = onp.flip(x, 0) 941 c = onp.flip(x, 1) 942 d = onp.flip(x, (-3, -1)) 943 return a, b, c, d 944 945 946@pytest.mark.level2 947@pytest.mark.platform_arm_ascend_training 948@pytest.mark.platform_x86_ascend_training 949@pytest.mark.platform_x86_gpu_training 950@pytest.mark.platform_x86_cpu 951@pytest.mark.env_onecard 952def test_flip(): 953 x = rand_int(2, 3, 4) 954 run_multi_test(mnp_flip, onp_flip, (x,)) 955 956 957def mnp_flipud(x): 958 return mnp.flipud(x) 959 960 961def onp_flipud(x): 962 return onp.flipud(x) 963 964 965@pytest.mark.level2 966@pytest.mark.platform_arm_ascend_training 967@pytest.mark.platform_x86_ascend_training 968@pytest.mark.platform_x86_gpu_training 969@pytest.mark.platform_x86_cpu 970@pytest.mark.env_onecard 971def test_flipud(): 972 x = rand_int(2, 3, 4) 973 run_multi_test(mnp_flipud, onp_flipud, (x,)) 974 975 976def mnp_fliplr(x): 977 return mnp.fliplr(x) 978 979 980def onp_fliplr(x): 981 return onp.fliplr(x) 982 983 984@pytest.mark.level2 985@pytest.mark.platform_arm_ascend_training 986@pytest.mark.platform_x86_ascend_training 987@pytest.mark.platform_x86_gpu_training 988@pytest.mark.platform_x86_cpu 989@pytest.mark.env_onecard 990def test_fliplr(): 991 x = rand_int(2, 3, 4) 992 run_multi_test(mnp_fliplr, onp_fliplr, (x,)) 993 994 995def mnp_split(input_tensor): 996 a = mnp.split(input_tensor, indices_or_sections=1) 997 b = mnp.split(input_tensor, indices_or_sections=3) 998 return a, b 999 1000 1001def onp_split(input_array): 1002 a = onp.split(input_array, indices_or_sections=1) 1003 b = onp.split(input_array, indices_or_sections=3) 1004 return a, b 1005 1006 1007@pytest.mark.level1 1008@pytest.mark.platform_arm_ascend_training 1009@pytest.mark.platform_x86_ascend_training 1010@pytest.mark.platform_x86_gpu_training 1011@pytest.mark.platform_x86_cpu 1012@pytest.mark.env_onecard 1013def test_split(): 1014 onp_arrs = [ 1015 onp.random.randint(1, 5, size=(9, 4, 5)).astype('float32') 1016 ] 1017 mnp_arrs = [to_tensor(arr) for arr in onp_arrs] 1018 for onp_arr, mnp_arr in zip(onp_arrs, mnp_arrs): 1019 o_split = onp_split(onp_arr) 1020 m_split = mnp_split(mnp_arr) 1021 for expect_lst, actual_lst in zip(o_split, m_split): 1022 for expect, actual in zip(expect_lst, actual_lst): 1023 match_array(expect, actual.asnumpy()) 1024 1025 1026def mnp_array_split(input_tensor): 1027 a = mnp.array_split(input_tensor, indices_or_sections=4, axis=2) 1028 b = mnp.array_split(input_tensor, indices_or_sections=3, axis=1) 1029 c = mnp.array_split(input_tensor, indices_or_sections=6) 1030 return a, b, c 1031 1032 1033def onp_array_split(input_array): 1034 a = onp.array_split(input_array, indices_or_sections=4, axis=2) 1035 b = onp.array_split(input_array, indices_or_sections=3, axis=1) 1036 c = onp.array_split(input_array, indices_or_sections=6) 1037 return a, b, c 1038 1039 1040@pytest.mark.level1 1041@pytest.mark.platform_arm_ascend_training 1042@pytest.mark.platform_x86_ascend_training 1043@pytest.mark.platform_x86_gpu_training 1044@pytest.mark.platform_x86_cpu 1045@pytest.mark.env_onecard 1046def test_array_split(): 1047 onp_arr = onp.random.randint(1, 5, size=(9, 7, 13)).astype('float32') 1048 mnp_arr = to_tensor(onp_arr) 1049 o_split = onp_split(onp_arr) 1050 m_split = mnp_split(mnp_arr) 1051 for expect_lst, actual_lst in zip(o_split, m_split): 1052 for expect, actual in zip(expect_lst, actual_lst): 1053 match_array(expect, actual.asnumpy()) 1054 1055 1056def mnp_vsplit(input_tensor): 1057 a = mnp.vsplit(input_tensor, indices_or_sections=3) 1058 return a 1059 1060 1061def onp_vsplit(input_array): 1062 a = onp.vsplit(input_array, indices_or_sections=3) 1063 return a 1064 1065 1066@pytest.mark.level1 1067@pytest.mark.platform_arm_ascend_training 1068@pytest.mark.platform_x86_ascend_training 1069@pytest.mark.platform_x86_gpu_training 1070@pytest.mark.platform_x86_cpu 1071@pytest.mark.env_onecard 1072def test_vsplit(): 1073 onp_arrs = [ 1074 onp.random.randint(1, 5, size=(9, 4, 5)).astype('float32') 1075 ] 1076 mnp_arrs = [to_tensor(arr) for arr in onp_arrs] 1077 for onp_arr, mnp_arr in zip(onp_arrs, mnp_arrs): 1078 o_vsplit = onp_vsplit(onp_arr) 1079 m_vsplit = mnp_vsplit(mnp_arr) 1080 for expect_lst, actual_lst in zip(o_vsplit, m_vsplit): 1081 for expect, actual in zip(expect_lst, actual_lst): 1082 match_array(expect, actual.asnumpy()) 1083 1084 1085def mnp_hsplit(input_tensor): 1086 a = mnp.hsplit(input_tensor, indices_or_sections=3) 1087 return a 1088 1089 1090def onp_hsplit(input_array): 1091 a = onp.hsplit(input_array, indices_or_sections=3) 1092 return a 1093 1094 1095@pytest.mark.level1 1096@pytest.mark.platform_arm_ascend_training 1097@pytest.mark.platform_x86_ascend_training 1098@pytest.mark.platform_x86_gpu_training 1099@pytest.mark.platform_x86_cpu 1100@pytest.mark.env_onecard 1101def test_hsplit(): 1102 onp_arrs = [ 1103 onp.random.randint(1, 5, size=(4, 9, 5)).astype('float32') 1104 ] 1105 mnp_arrs = [to_tensor(arr) for arr in onp_arrs] 1106 for onp_arr, mnp_arr in zip(onp_arrs, mnp_arrs): 1107 o_hsplit = onp_hsplit(onp_arr) 1108 m_hsplit = mnp_hsplit(mnp_arr) 1109 for expect_lst, actual_lst in zip(o_hsplit, m_hsplit): 1110 for expect, actual in zip(expect_lst, actual_lst): 1111 match_array(expect, actual.asnumpy()) 1112 1113 1114def mnp_dsplit(input_tensor): 1115 a = mnp.dsplit(input_tensor, indices_or_sections=3) 1116 return a 1117 1118def onp_dsplit(input_array): 1119 a = onp.dsplit(input_array, indices_or_sections=3) 1120 return a 1121 1122@pytest.mark.level1 1123@pytest.mark.platform_arm_ascend_training 1124@pytest.mark.platform_x86_ascend_training 1125@pytest.mark.platform_x86_gpu_training 1126@pytest.mark.platform_x86_cpu 1127@pytest.mark.env_onecard 1128def test_dsplit(): 1129 onp_arrs = [ 1130 onp.random.randint(1, 5, size=(5, 4, 9)).astype('float32') 1131 ] 1132 mnp_arrs = [to_tensor(arr) for arr in onp_arrs] 1133 for onp_arr, mnp_arr in zip(onp_arrs, mnp_arrs): 1134 o_dsplit = onp_dsplit(onp_arr) 1135 m_dsplit = mnp_dsplit(mnp_arr) 1136 for expect_lst, actual_lst in zip(o_dsplit, m_dsplit): 1137 for expect, actual in zip(expect_lst, actual_lst): 1138 match_array(expect, actual.asnumpy()) 1139 1140 1141def mnp_take_along_axis(*arrs): 1142 x = arrs[0] 1143 a = mnp.take_along_axis(x, arrs[1], axis=None) 1144 b = mnp.take_along_axis(x, arrs[2], axis=1) 1145 c = mnp.take_along_axis(x, arrs[3], axis=-1) 1146 d = mnp.take_along_axis(x, arrs[4], axis=0) 1147 return a, b, c, d 1148 1149 1150def onp_take_along_axis(*arrs): 1151 x = arrs[0] 1152 a = onp.take_along_axis(x, arrs[1], axis=None) 1153 b = onp.take_along_axis(x, arrs[2], axis=1) 1154 c = onp.take_along_axis(x, arrs[3], axis=-1) 1155 d = onp.take_along_axis(x, arrs[4], axis=0) 1156 return a, b, c, d 1157 1158 1159@pytest.mark.level1 1160@pytest.mark.platform_arm_ascend_training 1161@pytest.mark.platform_x86_ascend_training 1162@pytest.mark.platform_x86_gpu_training 1163@pytest.mark.platform_x86_cpu 1164@pytest.mark.env_onecard 1165def test_take_along_axis(): 1166 x = rand_int(6, 7, 8, 9) 1167 indices1 = rand_int(2).astype(onp.int32) 1168 indices2 = rand_int(6, 3, 8, 1).astype(onp.int32) 1169 indices3 = rand_int(6, 1, 8, 5).astype(onp.int32) 1170 indices4 = rand_int(4, 1, 1, 1).astype(onp.int32) 1171 run_multi_test(mnp_take_along_axis, onp_take_along_axis, 1172 (x, indices1, indices2, indices3, indices4)) 1173 1174 1175def mnp_take(x, indices): 1176 a = mnp.take(x, indices) 1177 b = mnp.take(x, indices, axis=-1) 1178 c = mnp.take(x, indices, axis=0, mode='wrap') 1179 d = mnp.take(x, indices, axis=1, mode='clip') 1180 return a, b, c, d 1181 1182 1183def onp_take(x, indices): 1184 a = onp.take(x, indices) 1185 b = onp.take(x, indices, axis=-1) 1186 c = onp.take(x, indices, axis=0, mode='wrap') 1187 d = onp.take(x, indices, axis=1, mode='clip') 1188 return a, b, c, d 1189 1190 1191@pytest.mark.level1 1192@pytest.mark.platform_arm_ascend_training 1193@pytest.mark.platform_x86_ascend_training 1194@pytest.mark.platform_x86_gpu_training 1195@pytest.mark.platform_x86_cpu 1196@pytest.mark.env_onecard 1197def test_take(): 1198 x = rand_int(2, 3, 4, 5) 1199 indices = rand_int(2, 3).astype(onp.int32) 1200 run_multi_test(mnp_take, onp_take, (x, indices)) 1201 1202 1203def mnp_repeat(x): 1204 a = mnp.repeat(x, 2) 1205 b = mnp.repeat(x, 3, axis=0) 1206 c = mnp.repeat(x, (4, 1, 5), axis=1) 1207 d = mnp.repeat(x, (3, 2, 1, 0, 4), axis=-1) 1208 e = mnp.repeat(x, 0) 1209 return a, b, c, d, e 1210 1211 1212def onp_repeat(x): 1213 a = onp.repeat(x, 2) 1214 b = onp.repeat(x, 3, axis=0) 1215 c = onp.repeat(x, (4, 1, 5), axis=1) 1216 d = onp.repeat(x, (3, 2, 1, 0, 4), axis=-1) 1217 e = onp.repeat(x, 0) 1218 return a, b, c, d, e 1219 1220 1221@pytest.mark.level1 1222@pytest.mark.platform_arm_ascend_training 1223@pytest.mark.platform_x86_ascend_training 1224@pytest.mark.platform_x86_gpu_training 1225@pytest.mark.platform_x86_cpu 1226@pytest.mark.env_onecard 1227def test_repeat(): 1228 x = rand_int(2, 3, 4, 5) 1229 run_multi_test(mnp_repeat, onp_repeat, (x,)) 1230 1231 1232@pytest.mark.level1 1233@pytest.mark.platform_arm_ascend_training 1234@pytest.mark.platform_x86_ascend_training 1235@pytest.mark.platform_x86_gpu_training 1236@pytest.mark.platform_x86_cpu 1237@pytest.mark.env_onecard 1238def test_select(): 1239 choicelist = rand_int(2, 3, 4, 5) 1240 condlist = choicelist > 2 1241 match_res(mnp.select, onp.select, condlist, choicelist) 1242 match_res(mnp.select, onp.select, condlist, choicelist, default=10) 1243 1244 condlist = rand_bool(5, 4, 1, 3) 1245 choicelist = rand_int(5, 3) 1246 match_res(mnp.select, onp.select, condlist, choicelist) 1247 match_res(mnp.select, onp.select, condlist, choicelist, default=10) 1248 1249 condlist = rand_bool(3, 1, 7) 1250 choicelist = rand_int(3, 5, 2, 1) 1251 match_res(mnp.select, onp.select, condlist, choicelist) 1252 match_res(mnp.select, onp.select, condlist, choicelist, default=10) 1253 1254 1255@pytest.mark.level0 1256@pytest.mark.platform_x86_gpu_training 1257@pytest.mark.platform_x86_cpu 1258@pytest.mark.env_onecard 1259def test_choose(): 1260 x = rand_int(2, 1, 4).astype(onp.int32) 1261 y = rand_int(3, 2, 5, 4).astype(onp.int32) 1262 match_res(mnp.choose, onp.choose, x, y, mode='wrap', dtype=mnp.int32) 1263 match_res(mnp.choose, onp.choose, x, y, mode='clip', dtype=mnp.int32) 1264 1265 x = rand_int(5, 3, 1, 7).astype(onp.int32) 1266 y1 = rand_int(7).astype(onp.int32) 1267 y2 = rand_int(1, 3, 1).astype(onp.int32) 1268 y3 = rand_int(5, 1, 1, 7).astype(onp.int32) 1269 onp_arrays = (x, (y1, y2, y3)) 1270 mnp_arrays = (to_tensor(x), tuple(map(to_tensor, (y1, y2, y3)))) 1271 match_all_arrays(mnp.choose(*mnp_arrays, mode='wrap'), onp.choose(*onp_arrays, mode='wrap')) 1272 match_all_arrays(mnp.choose(*mnp_arrays, mode='clip'), onp.choose(*onp_arrays, mode='clip')) 1273 1274 1275class ReshapeExpandSqueeze(Cell): 1276 def __init__(self): 1277 super(ReshapeExpandSqueeze, self).__init__() 1278 1279 def construct(self, x): 1280 x = mnp.expand_dims(x, 2) 1281 x = mnp.reshape(x, (1, 2, 3, 4, 1, 1)) 1282 x = mnp.squeeze(x) 1283 return x 1284 1285 1286class TransposeConcatRavel(Cell): 1287 def __init__(self): 1288 super(TransposeConcatRavel, self).__init__() 1289 1290 def construct(self, x1, x2, x3): 1291 x1 = mnp.transpose(x1, [0, 2, 1]) 1292 x2 = x2.transpose(0, 2, 1) 1293 x = mnp.concatenate((x1, x2, x3), -1) 1294 x = mnp.ravel(x) 1295 return x 1296 1297 1298class RollSwap(Cell): 1299 def __init__(self): 1300 super(RollSwap, self).__init__() 1301 1302 def construct(self, x): 1303 x = mnp.rollaxis(x, 2) 1304 x = mnp.swapaxes(x, 0, 1) 1305 return x 1306 1307 1308test_case_array_ops = [ 1309 ('ReshapeExpandSqueeze', { 1310 'block': ReshapeExpandSqueeze(), 1311 'desc_inputs': [mnp.ones((2, 3, 4))]}), 1312 1313 ('TransposeConcatRavel', { 1314 'block': TransposeConcatRavel(), 1315 'desc_inputs': [mnp.ones((2, 3, 4)), 1316 mnp.ones((2, 3, 4)), 1317 mnp.ones((2, 4, 1))]}), 1318 1319 ('RollSwap', { 1320 'block': RollSwap(), 1321 'desc_inputs': [mnp.ones((2, 3, 4))]}) 1322] 1323 1324test_case_lists = [test_case_array_ops] 1325test_exec_case = functools.reduce(lambda x, y: x + y, test_case_lists) 1326# use -k to select certain testcast 1327# pytest tests/python/ops/test_ops.py::test_backward -k LayerNorm 1328 1329 1330@pytest.mark.level1 1331@pytest.mark.platform_arm_ascend_training 1332@pytest.mark.platform_x86_ascend_training 1333@pytest.mark.platform_x86_gpu_training 1334@pytest.mark.platform_x86_cpu 1335@pytest.mark.env_onecard 1336def test_expand_dims_exception(): 1337 with pytest.raises(TypeError): 1338 mnp.expand_dims(mnp.ones((3, 3)), 1.2) 1339 1340 1341@pytest.mark.level1 1342@pytest.mark.platform_arm_ascend_training 1343@pytest.mark.platform_x86_ascend_training 1344@pytest.mark.platform_x86_gpu_training 1345@pytest.mark.platform_x86_cpu 1346@pytest.mark.env_onecard 1347def test_swapaxes_exception(): 1348 with pytest.raises(ValueError): 1349 mnp.swapaxes(mnp.ones((3, 3)), 1, 10) 1350 1351 1352@pytest.mark.level1 1353@pytest.mark.platform_arm_ascend_training 1354@pytest.mark.platform_x86_ascend_training 1355@pytest.mark.platform_x86_gpu_training 1356@pytest.mark.platform_x86_cpu 1357@pytest.mark.env_onecard 1358def test_tensor_flatten(): 1359 lst = [[1.0, 2.0], [3.0, 4.0]] 1360 tensor_list = to_tensor(lst) 1361 assert tensor_list.flatten().asnumpy().tolist() == [1.0, 2.0, 3.0, 4.0] 1362 assert tensor_list.flatten(order='F').asnumpy().tolist() == [ 1363 1.0, 3.0, 2.0, 4.0] 1364 1365 1366@pytest.mark.level1 1367@pytest.mark.platform_arm_ascend_training 1368@pytest.mark.platform_x86_ascend_training 1369@pytest.mark.platform_x86_gpu_training 1370@pytest.mark.platform_x86_cpu 1371@pytest.mark.env_onecard 1372def test_tensor_reshape(): 1373 lst = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] 1374 tensor_list = to_tensor(lst) 1375 with pytest.raises(TypeError): 1376 tensor_list = tensor_list.reshape({0, 1, 2}) 1377 with pytest.raises(ValueError): 1378 tensor_list = tensor_list.reshape(1, 2, 3) 1379 assert tensor_list.reshape([-1, 4]).shape == (2, 4) 1380 assert tensor_list.reshape(1, -1, 4).shape == (1, 2, 4) 1381 1382 1383@pytest.mark.level1 1384@pytest.mark.platform_arm_ascend_training 1385@pytest.mark.platform_x86_ascend_training 1386@pytest.mark.platform_x86_gpu_training 1387@pytest.mark.platform_x86_cpu 1388@pytest.mark.env_onecard 1389def test_tensor_squeeze(): 1390 lst = [[[1.0], [2.0], [3.0]]] 1391 tensor_list = to_tensor(lst) 1392 with pytest.raises(TypeError): 1393 tensor_list = tensor_list.squeeze(1.2) 1394 with pytest.raises(ValueError): 1395 tensor_list = tensor_list.squeeze(4) 1396 assert tensor_list.squeeze().shape == (3,) 1397 assert tensor_list.squeeze(axis=2).shape == (1, 3) 1398 1399 1400@pytest.mark.level1 1401@pytest.mark.platform_arm_ascend_training 1402@pytest.mark.platform_x86_ascend_training 1403@pytest.mark.platform_x86_gpu_training 1404@pytest.mark.platform_x86_cpu 1405@pytest.mark.env_onecard 1406def test_tensor_ravel(): 1407 lst = [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]] 1408 tensor_list = to_tensor(lst) 1409 assert tensor_list.ravel().shape == (8,) 1410 assert tensor_list.ravel().asnumpy().tolist() == [ 1411 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] 1412 1413 1414@pytest.mark.level1 1415@pytest.mark.platform_arm_ascend_training 1416@pytest.mark.platform_x86_ascend_training 1417@pytest.mark.platform_x86_gpu_training 1418@pytest.mark.platform_x86_cpu 1419@pytest.mark.env_onecard 1420def test_tensor_swapaxes(): 1421 lst = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] 1422 tensor_list = to_tensor(lst) 1423 with pytest.raises(TypeError): 1424 tensor_list = tensor_list.swapaxes(0, (1,)) 1425 with pytest.raises(ValueError): 1426 tensor_list = tensor_list.swapaxes(0, 3) 1427 assert tensor_list.swapaxes(0, 1).shape == (3, 2) 1428 1429 1430def mnp_rot90(input_tensor): 1431 a = mnp.rot90(input_tensor) 1432 b = mnp.rot90(input_tensor, 2) 1433 c = mnp.rot90(input_tensor, 3) 1434 d = mnp.rot90(input_tensor, 4) 1435 e = mnp.rot90(input_tensor, 5, (0, -1)) 1436 f = mnp.rot90(input_tensor, 1, (2, 0)) 1437 g = mnp.rot90(input_tensor, -3, (-1, -2)) 1438 h = mnp.rot90(input_tensor, 3, (2, 1)) 1439 return a, b, c, d, e, f, g, h 1440 1441 1442def onp_rot90(input_array): 1443 a = onp.rot90(input_array) 1444 b = onp.rot90(input_array, 2) 1445 c = onp.rot90(input_array, 3) 1446 d = onp.rot90(input_array, 4) 1447 e = onp.rot90(input_array, 5, (0, -1)) 1448 f = onp.rot90(input_array, 1, (2, 0)) 1449 g = onp.rot90(input_array, -3, (-1, -2)) 1450 h = onp.rot90(input_array, 3, (2, 1)) 1451 return a, b, c, d, e, f, g, h 1452 1453 1454@pytest.mark.level2 1455@pytest.mark.platform_arm_ascend_training 1456@pytest.mark.platform_x86_ascend_training 1457@pytest.mark.platform_x86_gpu_training 1458@pytest.mark.platform_x86_cpu 1459@pytest.mark.env_onecard 1460def test_rot90(): 1461 onp_array = rand_int(3, 4, 5).astype('float32') 1462 mnp_array = to_tensor(onp_array) 1463 o_rot = onp_rot90(onp_array) 1464 m_rot = mnp_rot90(mnp_array) 1465 check_all_results(o_rot, m_rot) 1466 1467 1468def mnp_size(x): 1469 a = mnp.size(x) 1470 b = mnp.size(x, axis=0) 1471 return a, b 1472 1473 1474def onp_size(x): 1475 a = onp.size(x) 1476 b = onp.size(x, axis=0) 1477 return a, b 1478 1479 1480@pytest.mark.level1 1481@pytest.mark.platform_arm_ascend_training 1482@pytest.mark.platform_x86_ascend_training 1483@pytest.mark.platform_x86_gpu_training 1484@pytest.mark.platform_x86_cpu 1485@pytest.mark.env_onecard 1486def test_size(): 1487 onp_arr = onp.random.rand(2, 3, 4).astype('float32') 1488 mnp_arr = to_tensor(onp_arr) 1489 for actual, expected in zip(mnp_size(mnp_arr), onp_size(onp_arr)): 1490 match_array(actual, expected) 1491 1492 1493def mnp_array_str(x): 1494 return mnp.array_str(x) 1495 1496 1497def onp_array_str(x): 1498 return onp.array_str(x) 1499 1500 1501@pytest.mark.level1 1502@pytest.mark.platform_arm_ascend_training 1503@pytest.mark.platform_x86_ascend_training 1504@pytest.mark.platform_x86_gpu_training 1505@pytest.mark.platform_x86_cpu 1506@pytest.mark.env_onecard 1507def test_array_str(): 1508 onp_arr = onp.random.rand(2, 3, 4).astype('float32') 1509 mnp_arr = to_tensor(onp_arr) 1510 for actual, expected in zip(mnp_size(mnp_arr), onp_size(onp_arr)): 1511 match_array(actual, expected) 1512 1513 1514@pytest.mark.level1 1515@pytest.mark.platform_arm_ascend_training 1516@pytest.mark.platform_x86_ascend_training 1517@pytest.mark.platform_x86_gpu_training 1518@pytest.mark.platform_x86_cpu 1519@pytest.mark.env_onecard 1520def test_apply_along_axis(): 1521 onp_arr = rand_int(5, 3, 7) 1522 mnp_arr = to_tensor(onp_arr) 1523 for i in range(-3, 3): 1524 mnp_res = mnp.apply_along_axis(mnp.diag, i, mnp_arr) 1525 onp_res = onp.apply_along_axis(onp.diag, i, onp_arr) 1526 match_all_arrays(mnp_res, onp_res) 1527 mnp_res = mnp.apply_along_axis(lambda x: x[0], 2, mnp_arr) 1528 onp_res = onp.apply_along_axis(lambda x: x[0], 2, onp_arr) 1529 match_all_arrays(mnp_res, onp_res) 1530 mnp_res = mnp.apply_along_axis(lambda x, y, offset=0: (x[4] - y)*offset, 2, mnp_arr, 1, offset=3) 1531 onp_res = onp.apply_along_axis(lambda x, y, offset=0: (x[4] - y)*offset, 2, onp_arr, 1, offset=3) 1532 match_all_arrays(mnp_res, onp_res) 1533 1534 1535@pytest.mark.level1 1536@pytest.mark.platform_arm_ascend_training 1537@pytest.mark.platform_x86_ascend_training 1538@pytest.mark.platform_x86_gpu_training 1539@pytest.mark.platform_x86_cpu 1540@pytest.mark.env_onecard 1541def test_tensor_resize(): 1542 x = rand_int(3, 5) 1543 mnp_x = to_tensor(x) 1544 1545 x.resize(2, 4, refcheck=False) 1546 mnp_x = mnp_x.resize(2, 4) 1547 match_array(mnp_x.asnumpy(), x) 1548 1549 x.resize((3, 1), refcheck=False) 1550 mnp_x = mnp_x.resize((3, 1)) 1551 match_array(mnp_x.asnumpy(), x) 1552 1553 x.resize(7, 4, refcheck=False) 1554 mnp_x = mnp_x.resize(7, 4) 1555 match_array(mnp_x.asnumpy(), x) 1556 1557 1558@pytest.mark.level1 1559@pytest.mark.platform_arm_ascend_training 1560@pytest.mark.platform_x86_ascend_training 1561@pytest.mark.platform_x86_gpu_training 1562@pytest.mark.platform_x86_cpu 1563@pytest.mark.env_onecard 1564def test_piecewise(): 1565 x = rand_int(2, 4) 1566 mnp_x = to_tensor(x) 1567 condlist = [x < 2, x == 2, x > 2] 1568 mnp_condlist = [mnp_x < 2, mnp_x == 2, mnp_x > 2] 1569 funclist = [lambda x, offset=0: x - offset, lambda x, offset=0: x, lambda x, offset=0: x*offset] 1570 mnp_res = mnp.piecewise(mnp_x, mnp_condlist, funclist, offset=2) 1571 onp_res = onp.piecewise(x, condlist, funclist, offset=2) 1572 match_all_arrays(mnp_res, onp_res) 1573 1574 funclist = [-1, 0, 1] 1575 mnp_res = mnp.piecewise(mnp_x, mnp_condlist, funclist) 1576 onp_res = onp.piecewise(x, condlist, funclist) 1577 match_all_arrays(mnp_res, onp_res) 1578 1579 condlist = [x > 10, x < 0] 1580 mnp_x = to_tensor(x) 1581 mnp_condlist = [mnp_x > 10, mnp_x < 0] 1582 funclist = [lambda x: x - 2, lambda x: x - 1, lambda x: x*2] 1583 mnp_res = mnp.piecewise(mnp_x, mnp_condlist, funclist) 1584 onp_res = onp.piecewise(x, condlist, funclist) 1585 match_all_arrays(mnp_res, onp_res) 1586 1587 x = 2 1588 condlist = True 1589 funclist = [lambda x: x - 1] 1590 mnp_res = mnp.piecewise(x, condlist, funclist) 1591 onp_res = onp.piecewise(x, condlist, funclist) 1592 match_all_arrays(mnp_res, onp_res) 1593 1594 1595@pytest.mark.level0 1596@pytest.mark.platform_arm_ascend_training 1597@pytest.mark.platform_x86_ascend_training 1598@pytest.mark.platform_x86_gpu_training 1599@pytest.mark.platform_x86_cpu 1600@pytest.mark.env_onecard 1601def test_unravel_index(): 1602 shapes = [(2, 6, 3)] 1603 dims = [(5, 4, 7), 5*4*7] 1604 for shape in shapes: 1605 x = onp.random.randint(0, 5*4*7, shape) 1606 for dim in dims: 1607 for order in ('C', 'F'): 1608 mnp_res = mnp.unravel_index(to_tensor(x), dim, order=order) 1609 onp_res = onp.unravel_index(x, dim, order=order) 1610 match_all_arrays(mnp_res, onp_res) 1611 1612 1613def mnp_apply_over_axes(x): 1614 a = mnp.apply_over_axes(mnp.sum, x, axes=0) 1615 b = mnp.apply_over_axes(mnp.sum, x, axes=(0, 1)) 1616 c = mnp.apply_over_axes(mnp.std, x, axes=1) 1617 d = mnp.apply_over_axes(mnp.mean, x, axes=(-1,)) 1618 return a, b, c, d 1619 1620 1621def onp_apply_over_axes(x): 1622 a = onp.apply_over_axes(onp.sum, x, axes=0) 1623 b = onp.apply_over_axes(onp.sum, x, axes=(0, 1)) 1624 c = onp.apply_over_axes(onp.std, x, axes=1) 1625 d = onp.apply_over_axes(onp.mean, x, axes=(-1,)) 1626 return a, b, c, d 1627 1628 1629@pytest.mark.level1 1630@pytest.mark.platform_arm_ascend_training 1631@pytest.mark.platform_x86_ascend_training 1632@pytest.mark.platform_x86_gpu_training 1633@pytest.mark.platform_x86_cpu 1634@pytest.mark.env_onecard 1635def test_apply_over_axes(): 1636 arrs = [ 1637 onp.random.rand(2, 2).astype('float32'), 1638 onp.random.rand(3, 2, 2).astype('float32'), 1639 onp.random.rand(5, 4, 3, 3).astype('float32'), 1640 ] 1641 for x in arrs: 1642 for expected, actual in zip(onp_apply_over_axes(x), 1643 mnp_apply_over_axes(to_tensor(x))): 1644 match_array(actual.asnumpy(), expected, error=5) 1645 1646 1647@pytest.mark.level2 1648@pytest.mark.platform_arm_ascend_training 1649@pytest.mark.platform_x86_ascend_training 1650@pytest.mark.platform_x86_gpu_training 1651@pytest.mark.platform_x86_cpu 1652@pytest.mark.env_onecard 1653def test_tensor_choose(): 1654 x = rand_int(2, 1, 4).astype(onp.int32) 1655 mnp_x = to_tensor(x) 1656 y = rand_int(3, 2, 5, 4).astype(onp.int32) 1657 match_res(mnp_x.choose, x.choose, y, mode='wrap') 1658 match_res(mnp_x.choose, x.choose, y, mode='clip') 1659 1660 x = rand_int(5, 3, 1, 7).astype(onp.int32) 1661 mnp_x = to_tensor(x) 1662 y1 = rand_int(7).astype(onp.int32) 1663 y2 = rand_int(1, 3, 1).astype(onp.int32) 1664 y3 = rand_int(5, 1, 1, 7).astype(onp.int32) 1665 onp_arrays = (y1, y2, y3) 1666 mnp_arrays = tuple(map(to_tensor, (y1, y2, y3))) 1667 match_all_arrays(mnp_x.choose(mnp_arrays, mode='wrap'), x.choose(onp_arrays, mode='wrap')) 1668 match_all_arrays(mnp_x.choose(mnp_arrays, mode='clip'), x.choose(onp_arrays, mode='clip')) 1669