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 math operations""" 16 17import pytest 18import numpy as onp 19import mindspore.numpy as mnp 20from mindspore import context 21from mindspore.common.dtype import dtype_to_nptype 22 23from .utils import rand_int, rand_bool, run_binop_test, run_unary_test, run_multi_test, \ 24 run_single_test, match_res, match_array, match_meta, match_all_arrays, to_tensor 25 26context.set_context(mode=context.PYNATIVE_MODE) 27 28class Cases(): 29 def __init__(self): 30 self.arrs = [ 31 rand_int(2), 32 rand_int(2, 3), 33 rand_int(2, 3, 4), 34 ] 35 36 # scalars expanded across the 0th dimension 37 self.scalars = [ 38 rand_int(), 39 rand_int(1), 40 rand_int(1, 1), 41 ] 42 43 # arrays of the same size expanded across the 0th dimension 44 self.expanded_arrs = [ 45 rand_int(2, 3), 46 rand_int(1, 2, 3), 47 rand_int(1, 1, 2, 3), 48 ] 49 50 # arrays with last dimension aligned 51 self.aligned_arrs = [ 52 rand_int(2, 3), 53 rand_int(1, 4, 3), 54 rand_int(5, 1, 2, 3), 55 rand_int(4, 2, 1, 1, 3), 56 ] 57 58 # arrays which can be broadcast 59 self.broadcastables = [ 60 rand_int(5), 61 rand_int(6, 1), 62 rand_int(7, 1, 5), 63 ] 64 65 # boolean arrays which can be broadcast 66 self.bool_broadcastables = [ 67 rand_bool(), 68 rand_bool(1), 69 rand_bool(5), 70 rand_bool(6, 1), 71 rand_bool(7, 1, 5), 72 rand_bool(8, 1, 6, 1), 73 ] 74 75 # core dimension 0 is matched for each 76 # pair of array[i] and array[i + 1] 77 self.core_broadcastables = [ 78 rand_int(3), 79 rand_int(3), 80 rand_int(6), 81 rand_int(6, 4), 82 rand_int(5, 2), 83 rand_int(2), 84 rand_int(2, 9), 85 rand_int(9, 8), 86 rand_int(6), 87 rand_int(2, 6, 5), 88 rand_int(9, 2, 7), 89 rand_int(7), 90 rand_int(5, 2, 4), 91 rand_int(6, 1, 4, 9), 92 rand_int(7, 1, 5, 3, 2), 93 rand_int(8, 1, 6, 1, 2, 9), 94 ] 95 96 # arrays with dimensions of size 1 97 self.nested_arrs = [ 98 rand_int(1), 99 rand_int(1, 2), 100 rand_int(3, 1, 8), 101 rand_int(1, 3, 9, 1), 102 ] 103 104 105test_case = Cases() 106 107 108def mnp_add(x1, x2): 109 return mnp.add(x1, x2) 110 111 112def onp_add(x1, x2): 113 return onp.add(x1, x2) 114 115 116def mnp_subtract(x1, x2): 117 return mnp.subtract(x1, x2) 118 119 120def onp_subtract(x1, x2): 121 return onp.subtract(x1, x2) 122 123 124def mnp_mutiply(x1, x2): 125 return mnp.multiply(x1, x2) 126 127 128def onp_multiply(x1, x2): 129 return onp.multiply(x1, x2) 130 131 132def mnp_divide(x1, x2): 133 return mnp.divide(x1, x2) 134 135 136def onp_divide(x1, x2): 137 return onp.divide(x1, x2) 138 139 140def mnp_true_divide(x1, x2): 141 return mnp.true_divide(x1, x2) 142 143 144def onp_true_divide(x1, x2): 145 return onp.true_divide(x1, x2) 146 147 148def mnp_power(x1, x2): 149 return mnp.power(x1, x2) 150 151 152def onp_power(x1, x2): 153 return onp.power(x1, x2) 154 155 156def mnp_float_power(x1, x2): 157 return mnp.float_power(x1, x2) 158 159 160def onp_float_power(x1, x2): 161 return onp.float_power(x1, x2) 162 163 164def mnp_minimum(a, b): 165 return mnp.minimum(a, b) 166 167 168def onp_minimum(a, b): 169 return onp.minimum(a, b) 170 171 172@pytest.mark.level1 173@pytest.mark.platform_arm_ascend_training 174@pytest.mark.platform_x86_ascend_training 175@pytest.mark.platform_x86_gpu_training 176@pytest.mark.platform_x86_cpu 177@pytest.mark.env_onecard 178def test_add(): 179 run_binop_test(mnp_add, onp_add, test_case) 180 181 182@pytest.mark.level1 183@pytest.mark.platform_arm_ascend_training 184@pytest.mark.platform_x86_ascend_training 185@pytest.mark.platform_x86_gpu_training 186@pytest.mark.platform_x86_cpu 187@pytest.mark.env_onecard 188def test_subtract(): 189 run_binop_test(mnp_subtract, onp_subtract, test_case) 190 191 192@pytest.mark.level1 193@pytest.mark.platform_arm_ascend_training 194@pytest.mark.platform_x86_ascend_training 195@pytest.mark.platform_x86_gpu_training 196@pytest.mark.platform_x86_cpu 197@pytest.mark.env_onecard 198def test_multiply(): 199 run_binop_test(mnp_mutiply, onp_multiply, test_case) 200 201 202@pytest.mark.level1 203@pytest.mark.platform_arm_ascend_training 204@pytest.mark.platform_x86_ascend_training 205@pytest.mark.platform_x86_gpu_training 206@pytest.mark.platform_x86_cpu 207@pytest.mark.env_onecard 208def test_divide(): 209 run_binop_test(mnp_divide, onp_divide, test_case) 210 211 212@pytest.mark.level1 213@pytest.mark.platform_arm_ascend_training 214@pytest.mark.platform_x86_ascend_training 215@pytest.mark.platform_x86_gpu_training 216@pytest.mark.platform_x86_cpu 217@pytest.mark.env_onecard 218def test_true_divide(): 219 run_binop_test(mnp_true_divide, onp_true_divide, test_case) 220 221 222@pytest.mark.level1 223@pytest.mark.platform_arm_ascend_training 224@pytest.mark.platform_x86_ascend_training 225@pytest.mark.platform_x86_gpu_training 226@pytest.mark.platform_x86_cpu 227@pytest.mark.env_onecard 228def test_power(): 229 run_binop_test(mnp_power, onp_power, test_case, error=1e-5) 230 231 232@pytest.mark.level1 233@pytest.mark.platform_arm_ascend_training 234@pytest.mark.platform_x86_ascend_training 235@pytest.mark.platform_x86_gpu_training 236@pytest.mark.platform_x86_cpu 237@pytest.mark.env_onecard 238def test_float_power(): 239 run_binop_test(mnp_float_power, onp_float_power, test_case, error=1e-5) 240 241 242@pytest.mark.level1 243@pytest.mark.platform_x86_gpu_training 244@pytest.mark.platform_x86_cpu 245@pytest.mark.env_onecard 246def test_minimum(): 247 run_binop_test(mnp_minimum, onp_minimum, test_case) 248 x = onp.random.randint(-10, 10, 20).astype(onp.float32) 249 y = onp.random.randint(-10, 10, 20).astype(onp.float32) 250 x[onp.random.randint(0, 10, 3)] = onp.nan 251 y[onp.random.randint(0, 10, 3)] = onp.nan 252 x[onp.random.randint(0, 10, 3)] = onp.NINF 253 y[onp.random.randint(0, 10, 3)] = onp.NINF 254 x[onp.random.randint(0, 10, 3)] = onp.PINF 255 y[onp.random.randint(0, 10, 3)] = onp.PINF 256 match_res(mnp_minimum, onp_minimum, x, y) 257 match_res(mnp_minimum, onp_minimum, y, x) 258 259 260def mnp_tensordot(x, y): 261 a = mnp.tensordot(x, y) 262 b = mnp.tensordot(x, y, axes=0) 263 c = mnp.tensordot(x, y, axes=1) 264 d = mnp.tensordot(x, y, axes=2) 265 e = mnp.tensordot(x, y, axes=(3, 0)) 266 f = mnp.tensordot(x, y, axes=[2, 1]) 267 g = mnp.tensordot(x, y, axes=((2, 3), (0, 1))) 268 h = mnp.tensordot(x, y, axes=[[3, 2], [1, 0]]) 269 return a, b, c, d, e, f, g, h 270 271 272def onp_tensordot(x, y): 273 a = onp.tensordot(x, y) 274 b = onp.tensordot(x, y, axes=0) 275 c = onp.tensordot(x, y, axes=1) 276 d = onp.tensordot(x, y, axes=2) 277 e = onp.tensordot(x, y, axes=(3, 0)) 278 f = onp.tensordot(x, y, axes=[2, 1]) 279 g = onp.tensordot(x, y, axes=((2, 3), (0, 1))) 280 h = onp.tensordot(x, y, axes=[[3, 2], [1, 0]]) 281 return a, b, c, d, e, f, g, h 282 283 284@pytest.mark.level1 285@pytest.mark.platform_arm_ascend_training 286@pytest.mark.platform_x86_ascend_training 287@pytest.mark.platform_x86_gpu_training 288@pytest.mark.platform_x86_cpu 289@pytest.mark.env_onecard 290def test_tensordot(): 291 x = rand_int(4, 2, 7, 7) 292 y = rand_int(7, 7, 6) 293 run_multi_test(mnp_tensordot, onp_tensordot, (x, y)) 294 295 296def mnp_std(x): 297 a = mnp.std(x) 298 b = mnp.std(x, axis=None) 299 c = mnp.std(x, axis=0) 300 d = mnp.std(x, axis=1) 301 e = mnp.std(x, axis=(-1, 1)) 302 f = mnp.std(x, axis=(0, 1, 2)) 303 g = mnp.std(x, axis=None, ddof=1, keepdims=True) 304 h = mnp.std(x, axis=0, ddof=1, keepdims=True) 305 i = mnp.std(x, axis=(2), ddof=1, keepdims=True) 306 return a, b, c, d, e, f, g, h, i 307 308 309def onp_std(x): 310 a = onp.std(x) 311 b = onp.std(x, axis=None) 312 c = onp.std(x, axis=0) 313 d = onp.std(x, axis=1) 314 e = onp.std(x, axis=(-1, 1)) 315 f = onp.std(x, axis=(0, 1, 2)) 316 g = onp.std(x, axis=None, ddof=1, keepdims=True) 317 h = onp.std(x, axis=0, ddof=1, keepdims=True) 318 i = onp.std(x, axis=(2), ddof=1, keepdims=True) 319 return a, b, c, d, e, f, g, h, i 320 321 322@pytest.mark.level1 323@pytest.mark.platform_arm_ascend_training 324@pytest.mark.platform_x86_ascend_training 325@pytest.mark.platform_x86_gpu_training 326@pytest.mark.platform_x86_cpu 327@pytest.mark.env_onecard 328def test_std(): 329 arr1 = rand_int(2, 3, 4, 5) 330 arr2 = rand_int(4, 5, 4, 3, 3) 331 run_single_test(mnp_std, onp_std, arr1, error=1e-5) 332 run_single_test(mnp_std, onp_std, arr2, error=1e-5) 333 334 335def mnp_nanstd(x): 336 a = mnp.nanstd(x) 337 b = mnp.nanstd(x, axis=None) 338 c = mnp.nanstd(x, axis=0) 339 d = mnp.nanstd(x, axis=1) 340 e = mnp.nanstd(x, axis=(-1, 1)) 341 f = mnp.nanstd(x, axis=(0, 1, 2)) 342 g = mnp.nanstd(x, axis=None, ddof=1, keepdims=True) 343 h = mnp.nanstd(x, axis=0, ddof=1, keepdims=True) 344 i = mnp.nanstd(x, axis=(2), ddof=1, keepdims=True) 345 return a, b, c, d, e, f, g, h, i 346 347 348def onp_nanstd(x): 349 a = onp.nanstd(x) 350 b = onp.nanstd(x, axis=None) 351 c = onp.nanstd(x, axis=0) 352 d = onp.nanstd(x, axis=1) 353 e = onp.nanstd(x, axis=(-1, 1)) 354 f = onp.nanstd(x, axis=(0, 1, 2)) 355 g = onp.nanstd(x, axis=None, ddof=1, keepdims=True) 356 h = onp.nanstd(x, axis=0, ddof=1, keepdims=True) 357 i = onp.nanstd(x, axis=(2), ddof=1, keepdims=True) 358 return a, b, c, d, e, f, g, h, i 359 360 361@pytest.mark.level1 362@pytest.mark.platform_x86_gpu_training 363@pytest.mark.platform_x86_cpu 364@pytest.mark.env_onecard 365def test_nanstd(): 366 arr1 = rand_int(2, 3, 4, 5) 367 arr1[0][2][1][3] = onp.nan 368 arr1[1][0][2][4] = onp.nan 369 arr1[1][1][1][1] = onp.nan 370 arr2 = rand_int(4, 5, 4, 3, 3) 371 arr2[3][1][2][1][0] = onp.nan 372 arr2[1][1][1][1][1] = onp.nan 373 arr2[0][4][3][0][2] = onp.nan 374 run_single_test(mnp_nanstd, onp_nanstd, arr1, error=1e-5) 375 run_single_test(mnp_nanstd, onp_nanstd, arr2, error=1e-5) 376 match_res(mnp.nanstd, onp.nanstd, rand_int()) 377 378 379def mnp_var(x): 380 a = mnp.var(x) 381 b = mnp.var(x, axis=0) 382 c = mnp.var(x, axis=(0)) 383 d = mnp.var(x, axis=(0, 1, 2)) 384 e = mnp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True) 385 return a, b, c, d, e 386 387 388def onp_var(x): 389 a = onp.var(x) 390 b = onp.var(x, axis=0) 391 c = onp.var(x, axis=(0)) 392 d = onp.var(x, axis=(0, 1, 2)) 393 e = onp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True) 394 return a, b, c, d, e 395 396 397@pytest.mark.level1 398@pytest.mark.platform_arm_ascend_training 399@pytest.mark.platform_x86_ascend_training 400@pytest.mark.platform_x86_gpu_training 401@pytest.mark.platform_x86_cpu 402@pytest.mark.env_onecard 403def test_var(): 404 arr1 = rand_int(2, 3, 4, 5) 405 arr2 = rand_int(4, 5, 4, 3, 3) 406 run_single_test(mnp_var, onp_var, arr1, error=1e-5) 407 run_single_test(mnp_var, onp_var, arr2, error=1e-5) 408 409 410def mnp_nanvar(x): 411 a = mnp.var(x) 412 b = mnp.var(x, axis=0) 413 c = mnp.var(x, axis=(0)) 414 d = mnp.var(x, axis=(0, 1, 2)) 415 e = mnp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True) 416 return a, b, c, d, e 417 418 419def onp_nanvar(x): 420 a = onp.var(x) 421 b = onp.var(x, axis=0) 422 c = onp.var(x, axis=(0)) 423 d = onp.var(x, axis=(0, 1, 2)) 424 e = onp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True) 425 return a, b, c, d, e 426 427 428@pytest.mark.level1 429@pytest.mark.platform_x86_gpu_training 430@pytest.mark.platform_x86_cpu 431@pytest.mark.env_onecard 432def test_nanvar(): 433 arr1 = rand_int(2, 3, 4, 5) 434 arr1[0][2][1][3] = onp.nan 435 arr1[1][0][2][4] = onp.nan 436 arr1[1][1][1][1] = onp.nan 437 arr2 = rand_int(4, 5, 4, 3, 3) 438 arr2[3][1][2][1][0] = onp.nan 439 arr2[1][1][1][1][1] = onp.nan 440 arr2[0][4][3][0][2] = onp.nan 441 run_single_test(mnp_nanvar, onp_nanvar, arr1, error=1e-5) 442 run_single_test(mnp_nanvar, onp_nanvar, arr2, error=1e-5) 443 match_res(mnp.nanvar, onp.nanvar, rand_int()) 444 445 446def mnp_average(x): 447 a = mnp.average(x) 448 b = mnp.average(x, axis=None) 449 c = mnp.average(x, axis=0) 450 d = mnp.average(x, axis=1) 451 e = mnp.average(x, axis=(-2, 1)) 452 f = mnp.average(x, axis=(0, 1, 2, 3)) 453 g = mnp.average(x, axis=None, weights=x) 454 h = mnp.average(x, axis=0, weights=x) 455 i = mnp.average(x, axis=(1, 2, 3), weights=x) 456 return a, b, c, d, e, f, g, h, i 457 458 459def onp_average(x): 460 a = onp.average(x) 461 b = onp.average(x, axis=None) 462 c = onp.average(x, axis=0) 463 d = onp.average(x, axis=1) 464 e = onp.average(x, axis=(-2, 1)) 465 f = onp.average(x, axis=(0, 1, 2, 3)) 466 g = onp.average(x, axis=None, weights=x) 467 h = onp.average(x, axis=0, weights=x) 468 i = onp.average(x, axis=(1, 2, 3), weights=x) 469 return a, b, c, d, e, f, g, h, i 470 471 472@pytest.mark.level0 473@pytest.mark.platform_arm_ascend_training 474@pytest.mark.platform_x86_ascend_training 475@pytest.mark.platform_x86_gpu_training 476@pytest.mark.platform_x86_cpu 477@pytest.mark.env_onecard 478def test_average(): 479 arr1 = rand_int(2, 3, 4, 5) 480 arr2 = rand_int(4, 5, 1, 3, 1) 481 run_single_test(mnp_average, onp_average, arr1, error=1e-5) 482 run_single_test(mnp_average, onp_average, arr2, error=1e-5) 483 484 485def mnp_count_nonzero(x): 486 a = mnp.count_nonzero(x) 487 b = mnp.count_nonzero(x, axis=None) 488 c = mnp.count_nonzero(x, axis=0) 489 d = mnp.count_nonzero(x, axis=1) 490 e = mnp.count_nonzero(x, axis=(-2, 1)) 491 f = mnp.count_nonzero(x, axis=(0, 1, 2, 3)) 492 return a, b, c, d, e, f 493 494 495def onp_count_nonzero(x): 496 a = onp.count_nonzero(x) 497 b = onp.count_nonzero(x, axis=None) 498 c = onp.count_nonzero(x, axis=0) 499 d = onp.count_nonzero(x, axis=1) 500 e = onp.count_nonzero(x, axis=(-2, 1)) 501 f = onp.count_nonzero(x, axis=(0, 1, 2, 3)) 502 return a, b, c, d, e, f 503 504 505@pytest.mark.level1 506@pytest.mark.platform_arm_ascend_training 507@pytest.mark.platform_x86_ascend_training 508@pytest.mark.platform_x86_gpu_training 509@pytest.mark.platform_x86_cpu 510@pytest.mark.env_onecard 511def test_count_nonzero(): 512 # minus 5 to make some values below zero 513 arr1 = rand_int(2, 3, 4, 5) - 5 514 arr2 = rand_int(4, 5, 4, 3, 3) - 5 515 run_single_test(mnp_count_nonzero, onp_count_nonzero, arr1) 516 run_single_test(mnp_count_nonzero, onp_count_nonzero, arr2) 517 518 519def mnp_inner(a, b): 520 return mnp.inner(a, b) 521 522 523def onp_inner(a, b): 524 return onp.inner(a, b) 525 526 527@pytest.mark.level1 528@pytest.mark.platform_arm_ascend_training 529@pytest.mark.platform_x86_ascend_training 530@pytest.mark.platform_x86_gpu_training 531@pytest.mark.platform_x86_cpu 532@pytest.mark.env_onecard 533def test_inner(): 534 for arr1 in test_case.aligned_arrs: 535 for arr2 in test_case.aligned_arrs: 536 match_res(mnp_inner, onp_inner, arr1, arr2) 537 538 for scalar1 in test_case.scalars: 539 for scalar2 in test_case.scalars: 540 match_res(mnp_inner, onp_inner, 541 scalar1, scalar2) 542 543 544def mnp_dot(a, b): 545 return mnp.dot(a, b) 546 547 548def onp_dot(a, b): 549 return onp.dot(a, b) 550 551 552@pytest.mark.level1 553@pytest.mark.platform_arm_ascend_training 554@pytest.mark.platform_x86_ascend_training 555@pytest.mark.platform_x86_gpu_training 556@pytest.mark.platform_x86_cpu 557@pytest.mark.env_onecard 558def test_dot(): 559 # test case (1D, 1D) 560 match_res(mnp_dot, onp_dot, rand_int(3), rand_int(3)) 561 562 # test case (2D, 2D) 563 match_res(mnp_dot, onp_dot, rand_int(4, 7), rand_int(7, 2)) 564 565 # test case (0D, _) (_, 0D) 566 match_res(mnp_dot, onp_dot, rand_int(), rand_int(1, 9, 3)) 567 match_res(mnp_dot, onp_dot, rand_int(8, 5, 6, 3), rand_int()) 568 569 # test case (ND, 1D) 570 match_res(mnp_dot, onp_dot, rand_int(2, 4, 5), rand_int(5)) 571 572 # test case (ND, MD) 573 match_res(mnp_dot, onp_dot, rand_int(5, 4, 1, 8), rand_int(8, 3)) 574 575 for i in range(8): 576 match_res(mnp_dot, onp_dot, 577 test_case.core_broadcastables[2*i], test_case.core_broadcastables[2*i + 1]) 578 579 580def mnp_outer(a, b): 581 return mnp.outer(a, b) 582 583 584def onp_outer(a, b): 585 return onp.outer(a, b) 586 587 588@pytest.mark.level1 589@pytest.mark.platform_arm_ascend_training 590@pytest.mark.platform_x86_ascend_training 591@pytest.mark.platform_x86_gpu_training 592@pytest.mark.platform_x86_cpu 593@pytest.mark.env_onecard 594def test_outer(): 595 run_binop_test(mnp_outer, onp_outer, test_case) 596 597 598@pytest.mark.level1 599@pytest.mark.platform_arm_ascend_training 600@pytest.mark.platform_x86_ascend_training 601@pytest.mark.platform_x86_gpu_training 602@pytest.mark.platform_x86_cpu 603@pytest.mark.env_onecard 604def test_type_promotion(): 605 arr = rand_int(2, 3) 606 onp_res = onp_add(arr, arr) 607 608 a = to_tensor(arr, dtype=mnp.float16) 609 b = to_tensor(arr, dtype=mnp.float32) 610 c = to_tensor(arr, dtype=mnp.int32) 611 612 match_array(mnp_add(a, b).asnumpy(), onp_res) 613 match_array(mnp_add(b, c).asnumpy(), onp_res) 614 615 616def mnp_absolute(x): 617 return mnp.absolute(x) 618 619 620def onp_absolute(x): 621 return onp.absolute(x) 622 623 624@pytest.mark.level1 625@pytest.mark.platform_arm_ascend_training 626@pytest.mark.platform_x86_ascend_training 627@pytest.mark.platform_x86_gpu_training 628@pytest.mark.platform_x86_cpu 629@pytest.mark.env_onecard 630def test_absolute(): 631 arr = rand_int(2, 3) 632 633 a = to_tensor(arr, dtype=mnp.float16) 634 b = to_tensor(arr, dtype=mnp.float32) 635 c = to_tensor(arr, dtype=mnp.uint8) 636 d = to_tensor(arr, dtype=mnp.bool_) 637 638 match_array(mnp_absolute(a).asnumpy(), onp_absolute(a.asnumpy())) 639 match_array(mnp_absolute(b).asnumpy(), onp_absolute(b.asnumpy())) 640 match_array(mnp_absolute(c).asnumpy(), onp_absolute(c.asnumpy())) 641 match_array(mnp_absolute(d).asnumpy(), onp_absolute(d.asnumpy())) 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_deg2rad_rad2deg(): 651 arrs = [rand_int(2, 3), rand_int(1, 2, 4), rand_int(2, 4)] 652 for arr in arrs: 653 match_res(mnp.deg2rad, onp.deg2rad, arr) 654 match_res(mnp.rad2deg, onp.rad2deg, arr) 655 656 657def mnp_ptp(x): 658 a = mnp.ptp(x) 659 b = mnp.ptp(x, keepdims=True) 660 c = mnp.ptp(x, axis=(0, 1)) 661 d = mnp.ptp(x, axis=-1) 662 return a, b, c, d 663 664 665def onp_ptp(x): 666 a = onp.ptp(x) 667 b = onp.ptp(x, keepdims=True) 668 c = onp.ptp(x, axis=(0, 1)) 669 d = onp.ptp(x, axis=-1) 670 return a, b, c, d 671 672 673@pytest.mark.level1 674@pytest.mark.platform_arm_ascend_training 675@pytest.mark.platform_x86_ascend_training 676@pytest.mark.platform_x86_gpu_training 677@pytest.mark.platform_x86_cpu 678@pytest.mark.env_onecard 679def test_ptp(): 680 arrs = [rand_int(2, 3), rand_int(1, 2, 4), rand_int(2, 4)] 681 for arr in arrs: 682 match_res(mnp_ptp, onp_ptp, arr) 683 684 685def mnp_add_dtype(x1, x2): 686 return mnp.add(x1, x2, dtype=mnp.float32) 687 688 689def onp_add_dtype(x1, x2): 690 return onp.add(x1, x2, dtype=onp.float32) 691 692 693@pytest.mark.level1 694@pytest.mark.platform_arm_ascend_training 695@pytest.mark.platform_x86_ascend_training 696@pytest.mark.platform_x86_gpu_training 697@pytest.mark.platform_x86_cpu 698@pytest.mark.env_onecard 699def test_add_dtype(): 700 x1 = rand_int(2, 3).astype('int32') 701 x2 = rand_int(2, 3).astype('int32') 702 arrs = (x1, x2) 703 mnp_arrs = map(to_tensor, arrs) 704 mnp_res = mnp_add_dtype(*mnp_arrs) 705 onp_res = onp_add_dtype(*arrs) 706 for actual, expected in zip(mnp_res, onp_res): 707 assert actual.asnumpy().dtype == expected.dtype 708 709 710def mnp_matmul(x1, x2): 711 return mnp.matmul(x1, x2) 712 713 714def onp_matmul(x1, x2): 715 return onp.matmul(x1, x2) 716 717 718@pytest.mark.level1 719@pytest.mark.platform_arm_ascend_training 720@pytest.mark.platform_x86_ascend_training 721@pytest.mark.platform_x86_gpu_training 722@pytest.mark.platform_x86_cpu 723@pytest.mark.env_onecard 724def test_matmul(): 725 for scalar1 in test_case.scalars[1:]: 726 for scalar2 in test_case.scalars[1:]: 727 match_res(mnp_matmul, onp_matmul, 728 scalar1, scalar2) 729 for i in range(8): 730 match_res(mnp_matmul, onp_matmul, 731 test_case.core_broadcastables[2*i], 732 test_case.core_broadcastables[2*i + 1]) 733 734 735def mnp_square(x): 736 return mnp.square(x) 737 738 739def onp_square(x): 740 return onp.square(x) 741 742 743@pytest.mark.level1 744@pytest.mark.platform_arm_ascend_training 745@pytest.mark.platform_x86_ascend_training 746@pytest.mark.platform_x86_gpu_training 747@pytest.mark.platform_x86_cpu 748@pytest.mark.env_onecard 749def test_square(): 750 run_unary_test(mnp_square, onp_square, test_case) 751 752 753def mnp_sqrt(x): 754 return mnp.sqrt(x) 755 756 757def onp_sqrt(x): 758 return onp.sqrt(x) 759 760 761@pytest.mark.level1 762@pytest.mark.platform_arm_ascend_training 763@pytest.mark.platform_x86_ascend_training 764@pytest.mark.platform_x86_gpu_training 765@pytest.mark.platform_x86_cpu 766@pytest.mark.env_onecard 767def test_sqrt(): 768 run_unary_test(mnp_sqrt, onp_sqrt, test_case) 769 770 771def mnp_reciprocal(x): 772 return mnp.reciprocal(x) 773 774 775def onp_reciprocal(x): 776 return onp.reciprocal(x) 777 778 779@pytest.mark.level1 780@pytest.mark.platform_arm_ascend_training 781@pytest.mark.platform_x86_ascend_training 782@pytest.mark.platform_x86_gpu_training 783@pytest.mark.platform_x86_cpu 784@pytest.mark.env_onecard 785def test_reciprocal(): 786 run_unary_test(mnp_reciprocal, onp_reciprocal, test_case) 787 788 789def mnp_log(x): 790 return mnp.log(x) 791 792 793def onp_log(x): 794 return onp.log(x) 795 796 797@pytest.mark.level1 798@pytest.mark.platform_arm_ascend_training 799@pytest.mark.platform_x86_ascend_training 800@pytest.mark.platform_x86_gpu_training 801@pytest.mark.platform_x86_cpu 802@pytest.mark.env_onecard 803def test_log(): 804 run_unary_test(mnp.log, onp.log, test_case, error=1e-5) 805 806 807def mnp_log1p(x): 808 return mnp.log1p(x) 809 810 811def onp_log1p(x): 812 return onp.log1p(x) 813 814 815@pytest.mark.level1 816@pytest.mark.platform_arm_ascend_training 817@pytest.mark.platform_x86_ascend_training 818@pytest.mark.platform_x86_gpu_training 819@pytest.mark.platform_x86_cpu 820@pytest.mark.env_onecard 821def test_log1p(): 822 run_unary_test(mnp_log1p, onp_log1p, test_case, error=1e-5) 823 824 825def mnp_logaddexp(x1, x2): 826 return mnp.logaddexp(x1, x2) 827 828 829def onp_logaddexp(x1, x2): 830 return onp.logaddexp(x1, x2) 831 832 833@pytest.mark.level0 834@pytest.mark.platform_arm_ascend_training 835@pytest.mark.platform_x86_ascend_training 836@pytest.mark.platform_x86_gpu_training 837@pytest.mark.platform_x86_cpu 838@pytest.mark.env_onecard 839def test_logaddexp(): 840 test_cases = [ 841 onp.random.randint(1, 5, (5, 6, 3, 2)).astype('float16')] 842 for _, x1 in enumerate(test_cases): 843 for _, x2 in enumerate(test_cases): 844 expected = onp_logaddexp(x1, x2) 845 actual = mnp_logaddexp(to_tensor(x1), to_tensor(x2)) 846 onp.testing.assert_almost_equal(actual.asnumpy().tolist(), expected.tolist(), 847 decimal=2) 848 849 850def mnp_log2(x): 851 return mnp.log2(x) 852 853 854def onp_log2(x): 855 return onp.log2(x) 856 857 858@pytest.mark.level1 859@pytest.mark.platform_arm_ascend_training 860@pytest.mark.platform_x86_ascend_training 861@pytest.mark.platform_x86_gpu_training 862@pytest.mark.platform_x86_cpu 863@pytest.mark.env_onecard 864def test_log2(): 865 run_unary_test(mnp_log2, onp_log2, test_case, error=1e-5) 866 867 868def mnp_logaddexp2(x1, x2): 869 return mnp.logaddexp2(x1, x2) 870 871 872def onp_logaddexp2(x1, x2): 873 return onp.logaddexp2(x1, x2) 874 875 876@pytest.mark.level1 877@pytest.mark.platform_arm_ascend_training 878@pytest.mark.platform_x86_ascend_training 879@pytest.mark.platform_x86_gpu_training 880@pytest.mark.platform_x86_cpu 881@pytest.mark.env_onecard 882def test_logaddexp2(): 883 test_cases = [ 884 onp.random.randint(1, 5, (2)).astype('float16'), 885 onp.random.randint(1, 5, (3, 2)).astype('float16'), 886 onp.random.randint(1, 5, (1, 3, 2)).astype('float16'), 887 onp.random.randint(1, 5, (5, 6, 3, 2)).astype('float16')] 888 for _, x1 in enumerate(test_cases): 889 for _, x2 in enumerate(test_cases): 890 expected = onp_logaddexp2(x1, x2) 891 actual = mnp_logaddexp2(to_tensor(x1), to_tensor(x2)) 892 onp.testing.assert_almost_equal(actual.asnumpy().tolist(), expected.tolist(), 893 decimal=2) 894 895 896def mnp_log10(x): 897 return mnp.log10(x) 898 899 900def onp_log10(x): 901 return onp.log10(x) 902 903 904@pytest.mark.level1 905@pytest.mark.platform_arm_ascend_training 906@pytest.mark.platform_x86_ascend_training 907@pytest.mark.platform_x86_gpu_training 908@pytest.mark.platform_x86_cpu 909@pytest.mark.env_onecard 910def test_log10(): 911 run_unary_test(mnp_log10, onp_log10, test_case, error=1e-5) 912 913 914def mnp_maximum(x1, x2): 915 return mnp.maximum(x1, x2) 916 917 918def onp_maximum(x1, x2): 919 return onp.maximum(x1, x2) 920 921 922@pytest.mark.level0 923@pytest.mark.platform_x86_gpu_training 924@pytest.mark.platform_x86_cpu 925@pytest.mark.env_onecard 926def test_maximum(): 927 run_binop_test(mnp_maximum, onp_maximum, test_case) 928 x = onp.random.randint(-10, 10, 20).astype(onp.float32) 929 y = onp.random.randint(-10, 10, 20).astype(onp.float32) 930 x[onp.random.randint(0, 10, 3)] = onp.nan 931 y[onp.random.randint(0, 10, 3)] = onp.nan 932 x[onp.random.randint(0, 10, 3)] = onp.NINF 933 y[onp.random.randint(0, 10, 3)] = onp.NINF 934 x[onp.random.randint(0, 10, 3)] = onp.PINF 935 y[onp.random.randint(0, 10, 3)] = onp.PINF 936 match_res(mnp_maximum, onp_maximum, x, y) 937 match_res(mnp_maximum, onp_maximum, y, x) 938 939 940def mnp_clip(x): 941 a = mnp.clip(x, to_tensor(10.0), to_tensor([2,])) 942 b = mnp.clip(x, 0, 1) 943 c = mnp.clip(x, to_tensor(0), to_tensor(10), dtype=mnp.float32) 944 d = x.clip(to_tensor(10.0), to_tensor([2,])) 945 e = x.clip(0, 1) 946 f = x.clip(to_tensor(0), to_tensor(10), dtype=mnp.float32) 947 return a, b, c, d, e, f 948 949 950def onp_clip(x): 951 a = onp.clip(x, onp.asarray(10.0), onp.asarray([2,])) 952 b = onp.clip(x, 0, 1) 953 c = onp.clip(x, onp.asarray(0), onp.asarray(10), dtype=onp.float32) 954 d = x.clip(onp.asarray(10.0), onp.asarray([2,])) 955 e = x.clip(0, 1) 956 f = x.clip(onp.asarray(0), onp.asarray(10), dtype=onp.float32) 957 return a, b, c, d, e, f 958 959 960@pytest.mark.level0 961@pytest.mark.platform_arm_ascend_training 962@pytest.mark.platform_x86_ascend_training 963@pytest.mark.platform_x86_gpu_training 964@pytest.mark.platform_x86_cpu 965@pytest.mark.env_onecard 966def test_clip(): 967 run_unary_test(mnp_clip, onp_clip, test_case) 968 969 970def mnp_amax(x, mask): 971 a = mnp.amax(x) 972 b = mnp.amax(x, axis=-3) 973 c = mnp.amax(x, keepdims=True) 974 d = mnp.amax(x, initial=3) 975 e = mnp.amax(x, axis=(0, 1), keepdims=True) 976 f = mnp.amax(x, initial=4, where=mask) 977 g = mnp.amax(x, initial=5, where=mask, keepdims=True) 978 h = mnp.amax(x, axis=(1, 2, 3), initial=6, where=mask) 979 return a, b, c, d, e, f, g, h 980 981 982def onp_amax(x, mask): 983 a = onp.amax(x) 984 b = onp.amax(x, axis=-3) 985 c = onp.amax(x, keepdims=True) 986 d = onp.amax(x, initial=3) 987 e = onp.amax(x, axis=(0, 1), keepdims=True) 988 f = onp.amax(x, initial=4, where=mask) 989 g = onp.amax(x, initial=5, where=mask, keepdims=True) 990 h = onp.amax(x, axis=(1, 2, 3), initial=6, where=mask) 991 return a, b, c, d, e, f, g, h 992 993 994@pytest.mark.level1 995@pytest.mark.platform_arm_ascend_training 996@pytest.mark.platform_x86_ascend_training 997@pytest.mark.platform_x86_gpu_training 998@pytest.mark.platform_x86_cpu 999@pytest.mark.env_onecard 1000def test_amax(): 1001 a = rand_int(2, 3, 4, 5).astype('float32') 1002 mask = rand_bool(2, 3, 4, 5) 1003 run_multi_test(mnp_amax, onp_amax, (a, mask)) 1004 match_res(mnp.amax, onp.amax, rand_int()) 1005 1006 1007def mnp_amin(x, mask): 1008 a = mnp.amin(x) 1009 b = mnp.amin(x, axis=-3) 1010 c = mnp.amin(x, keepdims=True) 1011 d = mnp.amin(x, initial=-1) 1012 e = mnp.amin(x, axis=(0, 1), keepdims=True) 1013 f = mnp.amin(x, initial=-2) 1014 g = mnp.amin(x, initial=-3, keepdims=True) 1015 h = mnp.amin(x, axis=(1, 2, 3), initial=-4, where=mask) 1016 return a, b, c, d, e, f, g, h 1017 1018 1019def onp_amin(x, mask): 1020 a = onp.amin(x) 1021 b = onp.amin(x, axis=-3) 1022 c = onp.amin(x, keepdims=True) 1023 d = onp.amin(x, initial=-1) 1024 e = onp.amin(x, axis=(0, 1), keepdims=True) 1025 f = onp.amin(x, initial=-2) 1026 g = onp.amin(x, initial=-3, keepdims=True) 1027 h = onp.amin(x, axis=(1, 2, 3), initial=-4, where=mask) 1028 return a, b, c, d, e, f, g, h 1029 1030 1031@pytest.mark.level1 1032@pytest.mark.platform_arm_ascend_training 1033@pytest.mark.platform_x86_ascend_training 1034@pytest.mark.platform_x86_gpu_training 1035@pytest.mark.platform_x86_cpu 1036@pytest.mark.env_onecard 1037def test_amin(): 1038 a = rand_int(2, 3, 4, 5).astype('float32') 1039 mask = rand_bool(2, 3, 4, 5) 1040 run_multi_test(mnp_amin, onp_amin, (a, mask)) 1041 match_res(mnp.amin, onp.amin, rand_int()) 1042 1043 1044def mnp_hypot(x1, x2): 1045 return mnp.hypot(x1, x2) 1046 1047 1048def onp_hypot(x1, x2): 1049 return onp.hypot(x1, x2) 1050 1051 1052@pytest.mark.level1 1053@pytest.mark.platform_arm_ascend_training 1054@pytest.mark.platform_x86_ascend_training 1055@pytest.mark.platform_x86_gpu_training 1056@pytest.mark.platform_x86_cpu 1057@pytest.mark.env_onecard 1058def test_hypot(): 1059 run_binop_test(mnp_hypot, onp_hypot, test_case) 1060 1061 1062def mnp_heaviside(x1, x2): 1063 return mnp.heaviside(x1, x2) 1064 1065 1066def onp_heaviside(x1, x2): 1067 return onp.heaviside(x1, x2) 1068 1069 1070@pytest.mark.level1 1071@pytest.mark.platform_arm_ascend_training 1072@pytest.mark.platform_x86_ascend_training 1073@pytest.mark.platform_x86_gpu_training 1074@pytest.mark.platform_x86_cpu 1075@pytest.mark.env_onecard 1076def test_heaviside(): 1077 broadcastables = test_case.broadcastables 1078 for b1 in broadcastables: 1079 for b2 in broadcastables: 1080 b = onp.subtract(b1, b2) 1081 match_res(mnp_heaviside, onp_heaviside, b, b1) 1082 match_res(mnp_heaviside, onp_heaviside, b, b2) 1083 1084 1085def mnp_floor(x): 1086 return mnp.floor(x) 1087 1088 1089def onp_floor(x): 1090 return onp.floor(x) 1091 1092 1093@pytest.mark.level1 1094@pytest.mark.platform_arm_ascend_training 1095@pytest.mark.platform_x86_ascend_training 1096@pytest.mark.platform_x86_gpu_training 1097@pytest.mark.platform_x86_cpu 1098@pytest.mark.env_onecard 1099def test_floor(): 1100 run_unary_test(mnp_floor, onp_floor, test_case) 1101 x = rand_int(2, 3) * onp.random.rand(2, 3) 1102 match_res(mnp_floor, onp_floor, x) 1103 match_res(mnp_floor, onp_floor, -x) 1104 1105 1106def mnp_floor_divide(x, y): 1107 return mnp.floor_divide(x, y) 1108 1109 1110def onp_floor_divde(x, y): 1111 return onp.floor_divide(x, y) 1112 1113 1114@pytest.mark.level1 1115@pytest.mark.platform_arm_ascend_training 1116@pytest.mark.platform_x86_ascend_training 1117@pytest.mark.platform_x86_gpu_training 1118@pytest.mark.platform_x86_cpu 1119@pytest.mark.env_onecard 1120def test_floor_divide(): 1121 run_binop_test(mnp_floor_divide, onp_floor_divde, test_case) 1122 1123 1124def mnp_remainder(x, y): 1125 return mnp.remainder(x, y) 1126 1127 1128def onp_remainder(x, y): 1129 return onp.remainder(x, y) 1130 1131 1132@pytest.mark.level0 1133@pytest.mark.platform_arm_ascend_training 1134@pytest.mark.platform_x86_ascend_training 1135@pytest.mark.platform_x86_gpu_training 1136@pytest.mark.platform_x86_cpu 1137@pytest.mark.env_onecard 1138def test_remainder(): 1139 x = rand_int(2, 3) 1140 y = rand_int(2, 3) 1141 match_res(mnp_remainder, onp_remainder, x, y) 1142 1143 1144def mnp_mod(x, y): 1145 return mnp.mod(x, y) 1146 1147 1148def onp_mod(x, y): 1149 return onp.mod(x, y) 1150 1151 1152@pytest.mark.level1 1153@pytest.mark.platform_arm_ascend_training 1154@pytest.mark.platform_x86_ascend_training 1155@pytest.mark.platform_x86_gpu_training 1156@pytest.mark.platform_x86_cpu 1157@pytest.mark.env_onecard 1158def test_mod(): 1159 x = rand_int(2, 3) 1160 y = rand_int(2, 3) 1161 match_res(mnp_mod, onp_mod, x, y) 1162 1163 1164def mnp_fmod(x, y): 1165 return mnp.fmod(x, y) 1166 1167 1168def onp_fmod(x, y): 1169 return onp.fmod(x, y) 1170 1171 1172@pytest.mark.level1 1173@pytest.mark.platform_x86_gpu_training 1174@pytest.mark.platform_x86_cpu 1175@pytest.mark.env_onecard 1176def test_fmod(): 1177 x = rand_int(2, 3) 1178 y = rand_int(2, 3) 1179 match_res(mnp_fmod, onp_fmod, x, y) 1180 1181 1182def mnp_fix(x): 1183 return mnp.fix(x) 1184 1185 1186def onp_fix(x): 1187 return onp.fix(x) 1188 1189 1190@pytest.mark.level1 1191@pytest.mark.platform_arm_ascend_training 1192@pytest.mark.platform_x86_ascend_training 1193@pytest.mark.platform_x86_gpu_training 1194@pytest.mark.platform_x86_cpu 1195@pytest.mark.env_onecard 1196def test_fix(): 1197 x = rand_int(2, 3) 1198 y = rand_int(2, 3) 1199 floats = onp.divide(onp.subtract(x, y), y) 1200 match_res(mnp_fix, onp_fix, floats, error=1e-5) 1201 1202 1203def mnp_trunc(x): 1204 return mnp.trunc(x) 1205 1206 1207def onp_trunc(x): 1208 return onp.trunc(x) 1209 1210 1211@pytest.mark.level1 1212@pytest.mark.platform_arm_ascend_training 1213@pytest.mark.platform_x86_ascend_training 1214@pytest.mark.platform_x86_gpu_training 1215@pytest.mark.platform_x86_cpu 1216@pytest.mark.env_onecard 1217def test_trunc(): 1218 x = rand_int(2, 3) 1219 y = rand_int(2, 3) 1220 floats = onp.divide(onp.subtract(x, y), y) 1221 match_res(mnp_trunc, onp_trunc, floats, error=1e-5) 1222 1223 1224def mnp_exp(x): 1225 return mnp.exp(x) 1226 1227 1228def onp_exp(x): 1229 return onp.exp(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_exp(): 1239 run_unary_test(mnp_exp, onp_exp, test_case, error=5) 1240 1241 1242def mnp_expm1(x): 1243 return mnp.expm1(x) 1244 1245 1246def onp_expm1(x): 1247 return onp.expm1(x) 1248 1249 1250@pytest.mark.level1 1251@pytest.mark.platform_arm_ascend_training 1252@pytest.mark.platform_x86_ascend_training 1253@pytest.mark.platform_x86_gpu_training 1254@pytest.mark.platform_x86_cpu 1255@pytest.mark.env_onecard 1256def test_expm1(): 1257 run_unary_test(mnp_expm1, onp_expm1, test_case, error=5) 1258 1259 1260def mnp_exp2(x): 1261 return mnp.exp2(x) 1262 1263 1264def onp_exp2(x): 1265 return onp.exp2(x) 1266 1267 1268@pytest.mark.level1 1269@pytest.mark.platform_arm_ascend_training 1270@pytest.mark.platform_x86_ascend_training 1271@pytest.mark.platform_x86_gpu_training 1272@pytest.mark.platform_x86_cpu 1273@pytest.mark.env_onecard 1274def test_exp2(): 1275 run_unary_test(mnp_exp2, onp_exp2, test_case, error=5) 1276 1277 1278def mnp_kron(x, y): 1279 return mnp.kron(x, y) 1280 1281 1282def onp_kron(x, y): 1283 return onp.kron(x, y) 1284 1285 1286@pytest.mark.level0 1287@pytest.mark.platform_arm_ascend_training 1288@pytest.mark.platform_x86_ascend_training 1289@pytest.mark.platform_x86_gpu_training 1290@pytest.mark.platform_x86_cpu 1291@pytest.mark.env_onecard 1292def test_kron(): 1293 x = rand_int() 1294 y = rand_int(2, 3, 4) 1295 match_res(mnp.kron, onp.kron, x, y) 1296 1297 x = rand_int(6, 1) 1298 y = rand_int(7, 1, 5) 1299 match_res(mnp.kron, onp.kron, x, y) 1300 1301 x = rand_int(1, 1, 2, 3) 1302 y = rand_int(1, 1, 2, 3) 1303 match_res(mnp.kron, onp.kron, x, y) 1304 1305 1306@pytest.mark.level0 1307@pytest.mark.platform_arm_ascend_training 1308@pytest.mark.platform_x86_ascend_training 1309@pytest.mark.platform_x86_gpu_training 1310@pytest.mark.platform_x86_cpu 1311@pytest.mark.env_onecard 1312def test_cross(): 1313 x = onp.arange(8).reshape(2, 2, 1, 2) 1314 y = onp.arange(4).reshape(1, 2, 2) 1315 match_res(mnp.cross, onp.cross, x, y) 1316 match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2) 1317 match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2, axis=1) 1318 x = onp.arange(18).reshape(2, 3, 1, 3) 1319 y = onp.arange(9).reshape(1, 3, 3) 1320 match_res(mnp.cross, onp.cross, x, y) 1321 match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2) 1322 match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2, axis=1) 1323 1324 1325def mnp_ceil(x): 1326 return mnp.ceil(x) 1327 1328 1329def onp_ceil(x): 1330 return onp.ceil(x) 1331 1332 1333@pytest.mark.platform_arm_ascend_training 1334@pytest.mark.platform_x86_ascend_training 1335@pytest.mark.platform_x86_gpu_training 1336@pytest.mark.platform_x86_cpu 1337@pytest.mark.env_onecard 1338def test_ceil(): 1339 run_unary_test(mnp_ceil, onp_ceil, test_case) 1340 1341 1342def mnp_positive(x): 1343 return mnp.positive(x) 1344 1345 1346def onp_positive(x): 1347 return onp.positive(x) 1348 1349 1350@pytest.mark.level1 1351@pytest.mark.platform_arm_ascend_training 1352@pytest.mark.platform_x86_ascend_training 1353@pytest.mark.platform_x86_gpu_training 1354@pytest.mark.platform_x86_cpu 1355@pytest.mark.env_onecard 1356def test_positive(): 1357 arr = onp.arange(-6, 6).reshape((2, 2, 3)).astype('float32') 1358 onp_pos = onp_positive(arr) 1359 mnp_pos = mnp_positive(to_tensor(arr)) 1360 match_array(mnp_pos.asnumpy(), onp_pos) 1361 1362 1363def mnp_negative(x): 1364 return mnp.negative(x) 1365 1366 1367def onp_negative(x): 1368 return onp.negative(x) 1369 1370 1371@pytest.mark.level1 1372@pytest.mark.platform_arm_ascend_training 1373@pytest.mark.platform_x86_ascend_training 1374@pytest.mark.platform_x86_gpu_training 1375@pytest.mark.platform_x86_cpu 1376@pytest.mark.env_onecard 1377def test_negative(): 1378 arr = onp.arange(-6, 6).reshape((2, 2, 3)).astype('float32') 1379 onp_neg = onp_negative(arr) 1380 mnp_neg = mnp_negative(to_tensor(arr)) 1381 match_array(mnp_neg.asnumpy(), onp_neg, 1e-5) 1382 1383 1384@pytest.mark.level1 1385@pytest.mark.platform_arm_ascend_training 1386@pytest.mark.platform_x86_ascend_training 1387@pytest.mark.platform_x86_gpu_training 1388@pytest.mark.platform_x86_cpu 1389@pytest.mark.env_onecard 1390def test_cumsum(): 1391 x = mnp.ones((16, 16), dtype="bool") 1392 match_array(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy())) 1393 match_array(mnp.cumsum(x, axis=0).asnumpy(), 1394 onp.cumsum(x.asnumpy(), axis=0)) 1395 match_meta(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy())) 1396 1397 x = rand_int(3, 4, 5) 1398 match_array(mnp.cumsum(to_tensor(x), dtype="bool").asnumpy(), 1399 onp.cumsum(x, dtype="bool")) 1400 match_array(mnp.cumsum(to_tensor(x), axis=-1).asnumpy(), 1401 onp.cumsum(x, axis=-1)) 1402 1403 1404@pytest.mark.level1 1405@pytest.mark.platform_arm_ascend_training 1406@pytest.mark.platform_x86_ascend_training 1407@pytest.mark.platform_x86_gpu_training 1408@pytest.mark.platform_x86_cpu 1409@pytest.mark.env_onecard 1410def test_promote_types(): 1411 assert mnp.promote_types(mnp.int32, mnp.bool_) == mnp.int32 1412 assert mnp.promote_types(int, mnp.bool_) == mnp.int32 1413 assert mnp.promote_types("float32", mnp.int64) == mnp.float32 1414 assert mnp.promote_types(mnp.int64, mnp.float16) == mnp.float16 1415 assert mnp.promote_types(int, float) == mnp.float32 1416 1417 1418def mnp_diff(input_tensor): 1419 a = mnp.diff(input_tensor, 2, append=3.0) 1420 b = mnp.diff(input_tensor, 4, prepend=6, axis=-2) 1421 c = mnp.diff(input_tensor, 0, append=3.0, axis=-1) 1422 d = mnp.diff(input_tensor, 1, prepend=input_tensor) 1423 e = mnp.ediff1d(input_tensor, to_end=input_tensor) 1424 f = mnp.ediff1d(input_tensor) 1425 g = mnp.ediff1d(input_tensor, to_begin=3) 1426 return a, b, c, d, e, f, g 1427 1428 1429def onp_diff(input_array): 1430 a = onp.diff(input_array, 2, append=3.0) 1431 b = onp.diff(input_array, 4, prepend=6, axis=-2) 1432 c = onp.diff(input_array, 0, append=3.0, axis=-1) 1433 d = onp.diff(input_array, 1, prepend=input_array) 1434 e = onp.ediff1d(input_array, to_end=input_array) 1435 f = onp.ediff1d(input_array) 1436 g = onp.ediff1d(input_array, to_begin=3) 1437 return a, b, c, d, e, f, g 1438 1439 1440@pytest.mark.level0 1441@pytest.mark.platform_arm_ascend_training 1442@pytest.mark.platform_x86_ascend_training 1443@pytest.mark.platform_x86_gpu_training 1444@pytest.mark.platform_x86_cpu 1445@pytest.mark.env_onecard 1446def test_diff(): 1447 arr = rand_int(3, 4, 5) 1448 match_res(mnp_diff, onp_diff, arr) 1449 arr = rand_int(1, 4, 6, 3) 1450 match_res(mnp_diff, onp_diff, arr) 1451 1452 1453def mnp_sin(x): 1454 return mnp.sin(x) 1455 1456 1457def onp_sin(x): 1458 return onp.sin(x) 1459 1460 1461@pytest.mark.level1 1462@pytest.mark.platform_arm_ascend_training 1463@pytest.mark.platform_x86_ascend_training 1464@pytest.mark.platform_x86_gpu_training 1465@pytest.mark.platform_x86_cpu 1466@pytest.mark.env_onecard 1467def test_sin(): 1468 arr = onp.random.rand(2, 3, 4).astype('float32') 1469 expect = onp_sin(arr) 1470 actual = mnp_sin(to_tensor(arr)) 1471 match_array(actual.asnumpy(), expect, error=5) 1472 1473 1474def mnp_cos(x): 1475 return mnp.cos(x) 1476 1477 1478def onp_cos(x): 1479 return onp.cos(x) 1480 1481 1482@pytest.mark.level1 1483@pytest.mark.platform_arm_ascend_training 1484@pytest.mark.platform_x86_ascend_training 1485@pytest.mark.platform_x86_gpu_training 1486@pytest.mark.platform_x86_cpu 1487@pytest.mark.env_onecard 1488def test_cos(): 1489 arr = onp.random.rand(2, 3, 4).astype('float32') 1490 expect = onp_cos(arr) 1491 actual = mnp_cos(to_tensor(arr)) 1492 match_array(actual.asnumpy(), expect, error=5) 1493 1494 1495def mnp_tan(x): 1496 return mnp.tan(x) 1497 1498 1499def onp_tan(x): 1500 return onp.tan(x) 1501 1502 1503@pytest.mark.level1 1504@pytest.mark.platform_arm_ascend_training 1505@pytest.mark.platform_x86_ascend_training 1506@pytest.mark.platform_x86_cpu 1507@pytest.mark.env_onecard 1508def test_tan(): 1509 arr = onp.array([-0.75, -0.5, 0, 0.5, 0.75]).astype('float32') 1510 expect = onp_tan(arr) 1511 actual = mnp_tan(to_tensor(arr)) 1512 match_array(actual.asnumpy(), expect, error=5) 1513 1514 1515def mnp_arcsin(x): 1516 return mnp.arcsin(x) 1517 1518 1519def onp_arcsin(x): 1520 return onp.arcsin(x) 1521 1522 1523@pytest.mark.level1 1524@pytest.mark.platform_arm_ascend_training 1525@pytest.mark.platform_x86_ascend_training 1526@pytest.mark.platform_x86_gpu_training 1527@pytest.mark.platform_x86_cpu 1528@pytest.mark.env_onecard 1529def test_arcsin(): 1530 arr = onp.random.uniform(-1, 1, 12).astype('float32') 1531 onp_asin = onp_arcsin(arr) 1532 mnp_asin = mnp_arcsin(to_tensor(arr)) 1533 match_array(mnp_asin.asnumpy(), onp_asin, error=3) 1534 1535 1536def mnp_arccos(x): 1537 return mnp.arccos(x) 1538 1539 1540def onp_arccos(x): 1541 return onp.arccos(x) 1542 1543 1544@pytest.mark.level1 1545@pytest.mark.platform_arm_ascend_training 1546@pytest.mark.platform_x86_ascend_training 1547@pytest.mark.platform_x86_gpu_training 1548@pytest.mark.platform_x86_cpu 1549@pytest.mark.env_onecard 1550def test_arccos(): 1551 arr = onp.random.uniform(-1, 1, 12).astype('float32') 1552 onp_acos = onp_arccos(arr) 1553 mnp_acos = mnp_arccos(to_tensor(arr)) 1554 match_array(mnp_acos.asnumpy(), onp_acos, error=2) 1555 1556 1557def mnp_arctan(x): 1558 return mnp.arctan(x) 1559 1560 1561def onp_arctan(x): 1562 return onp.arctan(x) 1563 1564 1565@pytest.mark.level1 1566@pytest.mark.platform_arm_ascend_training 1567@pytest.mark.platform_x86_ascend_training 1568@pytest.mark.platform_x86_gpu_training 1569@pytest.mark.platform_x86_cpu 1570@pytest.mark.env_onecard 1571def test_arctan(): 1572 arr = onp.random.uniform(-1, 1, 12).astype('float32') 1573 onp_atan = onp_arctan(arr) 1574 mnp_atan = mnp_arctan(to_tensor(arr)) 1575 match_array(mnp_atan.asnumpy(), onp_atan, error=5) 1576 1577 1578def mnp_sinh(x): 1579 return mnp.sinh(x) 1580 1581 1582def onp_sinh(x): 1583 return onp.sinh(x) 1584 1585 1586@pytest.mark.level1 1587@pytest.mark.platform_arm_ascend_training 1588@pytest.mark.platform_x86_ascend_training 1589@pytest.mark.platform_x86_cpu 1590@pytest.mark.env_onecard 1591def test_sinh(): 1592 arr = onp.random.rand(2, 3, 4).astype('float32') 1593 expect = onp_sinh(arr) 1594 actual = mnp_sinh(to_tensor(arr)) 1595 match_array(actual.asnumpy(), expect, error=5) 1596 1597 1598def mnp_cosh(x): 1599 return mnp.cosh(x) 1600 1601 1602def onp_cosh(x): 1603 return onp.cosh(x) 1604 1605 1606@pytest.mark.level1 1607@pytest.mark.platform_arm_ascend_training 1608@pytest.mark.platform_x86_ascend_training 1609@pytest.mark.platform_x86_cpu 1610@pytest.mark.env_onecard 1611def test_cosh(): 1612 arr = onp.random.rand(2, 3, 4).astype('float32') 1613 expect = onp_cosh(arr) 1614 actual = mnp_cosh(to_tensor(arr)) 1615 match_array(actual.asnumpy(), expect, error=5) 1616 1617 1618def mnp_tanh(x): 1619 return mnp.tanh(x) 1620 1621 1622def onp_tanh(x): 1623 return onp.tanh(x) 1624 1625 1626@pytest.mark.level1 1627@pytest.mark.platform_arm_ascend_training 1628@pytest.mark.platform_x86_ascend_training 1629@pytest.mark.platform_x86_gpu_training 1630@pytest.mark.platform_x86_cpu 1631@pytest.mark.env_onecard 1632def test_tanh(): 1633 arr = onp.random.rand(2, 3, 4).astype('float32') 1634 expect = onp_tanh(arr) 1635 actual = mnp_tanh(to_tensor(arr)) 1636 match_array(actual.asnumpy(), expect, error=5) 1637 1638 1639def mnp_arcsinh(x): 1640 return mnp.arcsinh(x) 1641 1642 1643def onp_arcsinh(x): 1644 return onp.arcsinh(x) 1645 1646 1647@pytest.mark.level1 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_arcsinh(): 1654 arr = onp.random.rand(2, 3, 4).astype('float32') 1655 expect = onp_arcsinh(arr) 1656 actual = mnp_arcsinh(to_tensor(arr)) 1657 match_array(actual.asnumpy(), expect, error=5) 1658 1659 1660def mnp_arccosh(x): 1661 return mnp.arccosh(x) 1662 1663 1664def onp_arccosh(x): 1665 return onp.arccosh(x) 1666 1667 1668@pytest.mark.level1 1669@pytest.mark.platform_arm_ascend_training 1670@pytest.mark.platform_x86_ascend_training 1671@pytest.mark.platform_x86_gpu_training 1672@pytest.mark.platform_x86_cpu 1673@pytest.mark.env_onecard 1674def test_arccosh(): 1675 arr = onp.random.randint(1, 100, size=(2, 3)).astype('float32') 1676 expect = onp_arccosh(arr) 1677 actual = mnp_arccosh(to_tensor(arr)) 1678 match_array(actual.asnumpy(), expect, error=5) 1679 1680 1681def mnp_arctanh(x): 1682 return mnp.arctanh(x) 1683 1684 1685def onp_arctanh(x): 1686 return onp.arctanh(x) 1687 1688 1689@pytest.mark.level1 1690@pytest.mark.platform_arm_ascend_training 1691@pytest.mark.platform_x86_ascend_training 1692@pytest.mark.platform_x86_cpu 1693@pytest.mark.env_onecard 1694def test_arctanh(): 1695 arr = onp.random.uniform(-0.9, 1, 10).astype('float32') 1696 expect = onp_arctanh(arr) 1697 actual = mnp_arctanh(to_tensor(arr)) 1698 match_array(actual.asnumpy(), expect, error=5) 1699 1700 1701def mnp_arctan2(x, y): 1702 return mnp.arctan2(x, y) 1703 1704 1705def onp_arctan2(x, y): 1706 return onp.arctan2(x, y) 1707 1708 1709@pytest.mark.level1 1710@pytest.mark.platform_arm_ascend_training 1711@pytest.mark.platform_x86_ascend_training 1712@pytest.mark.platform_x86_cpu 1713@pytest.mark.env_onecard 1714def test_arctan2(): 1715 run_binop_test(mnp_arctan2, onp_arctan2, test_case, error=5) 1716 1717 1718def mnp_convolve(mode): 1719 a = mnp.convolve([1, 2, 3, 4, 5], 2, mode=mode) 1720 b = mnp.convolve([1, 2, 3, 4, 5], [2, 3], mode=mode) 1721 c = mnp.convolve([1, 2], [2, 5, 10], mode=mode) 1722 d = mnp.convolve(mnp.array([1, 2, 3, 4, 5]), mnp.array([1, 2, 3, 4, 5]), mode=mode) 1723 e = mnp.convolve([1, 2, 3, 4, 5], 2, mode=mode) 1724 return a, b, c, d, e 1725 1726 1727def onp_convolve(mode): 1728 a = onp.convolve([1, 2, 3, 4, 5], 2, mode=mode) 1729 b = onp.convolve([1, 2, 3, 4, 5], [2, 3], mode=mode) 1730 c = onp.convolve([1, 2], [2, 5, 10], mode=mode) 1731 d = onp.convolve(onp.array([1, 2, 3, 4, 5]), onp.array([1, 2, 3, 4, 5]), mode=mode) 1732 e = onp.convolve([1, 2, 3, 4, 5], 2, mode=mode) 1733 return a, b, c, d, e 1734 1735 1736@pytest.mark.level0 1737@pytest.mark.platform_x86_gpu_training 1738@pytest.mark.env_onecard 1739def test_convolve(): 1740 for mode in ['full', 'same', 'valid']: 1741 mnp_res = mnp_convolve(mode) 1742 onp_res = onp_convolve(mode) 1743 match_all_arrays(mnp_res, onp_res) 1744 1745 1746@pytest.mark.level0 1747@pytest.mark.platform_arm_ascend_training 1748@pytest.mark.platform_x86_ascend_training 1749@pytest.mark.platform_x86_gpu_training 1750@pytest.mark.platform_x86_cpu 1751@pytest.mark.env_onecard 1752def test_cov(): 1753 x = onp.random.random((3, 4)).tolist() 1754 w1 = [0, 1, 2, 3] 1755 w2 = [4, 5, 6, 7] 1756 mnp_res = mnp.cov(x, fweights=w1, aweights=w2, ddof=3) 1757 onp_res = onp.cov(x, fweights=w1, aweights=w2, ddof=3) 1758 match_all_arrays(mnp_res, onp_res, error=1e-5) 1759 mnp_res = mnp.cov(x, fweights=w1, aweights=w2, bias=True) 1760 onp_res = onp.cov(x, fweights=w1, aweights=w2, bias=True) 1761 match_all_arrays(mnp_res, onp_res, error=1e-5) 1762 mnp_res = mnp.cov(x, fweights=w1[0:3], aweights=w2[0:3], rowvar=False, bias=True) 1763 onp_res = onp.cov(x, fweights=w1[0:3], aweights=w2[0:3], rowvar=False, bias=True) 1764 match_all_arrays(mnp_res, onp_res, error=1e-5) 1765 1766 1767@pytest.mark.level0 1768@pytest.mark.platform_arm_ascend_training 1769@pytest.mark.platform_x86_ascend_training 1770@pytest.mark.platform_x86_gpu_training 1771@pytest.mark.platform_x86_cpu 1772@pytest.mark.env_onecard 1773def test_trapz(): 1774 y = rand_int(2, 3, 4, 5) 1775 match_res(mnp.trapz, onp.trapz, y) 1776 match_res(mnp.trapz, onp.trapz, y, x=[-5, -3, 0, 7, 10]) 1777 match_res(mnp.trapz, onp.trapz, y, dx=2, axis=3) 1778 match_res(mnp.trapz, onp.trapz, y, x=[1, 5, 6, 9], dx=3, axis=-2) 1779 1780 1781def mnp_gcd(x, y): 1782 return mnp.gcd(x, y) 1783 1784 1785def onp_gcd(x, y): 1786 return onp.gcd(x, y) 1787 1788 1789@pytest.mark.level1 1790@pytest.mark.platform_arm_ascend_training 1791@pytest.mark.platform_x86_ascend_training 1792@pytest.mark.platform_x86_gpu_training 1793@pytest.mark.platform_x86_cpu 1794@pytest.mark.env_onecard 1795def test_gcd(): 1796 x = onp.arange(-12, 12).reshape(2, 3, 4) 1797 y = onp.arange(24).reshape(2, 3, 4) 1798 match_res(mnp_gcd, onp_gcd, x, y) 1799 1800 1801def mnp_lcm(x, y): 1802 return mnp.lcm(x, y) 1803 1804 1805def onp_lcm(x, y): 1806 return onp.lcm(x, y) 1807 1808 1809@pytest.mark.level0 1810@pytest.mark.platform_arm_ascend_training 1811@pytest.mark.platform_x86_ascend_training 1812@pytest.mark.platform_x86_gpu_training 1813@pytest.mark.platform_x86_cpu 1814@pytest.mark.env_onecard 1815def test_lcm(): 1816 x = onp.arange(-12, 12).reshape(2, 3, 4) 1817 y = onp.arange(24).reshape(2, 3, 4) 1818 match_res(mnp_lcm, onp_lcm, x, y) 1819 1820 1821@pytest.mark.level1 1822@pytest.mark.platform_arm_ascend_training 1823@pytest.mark.platform_x86_ascend_training 1824@pytest.mark.platform_x86_gpu_training 1825@pytest.mark.platform_x86_cpu 1826@pytest.mark.env_onecard 1827def test_exception_innner(): 1828 with pytest.raises(ValueError): 1829 mnp.inner(to_tensor(test_case.arrs[0]), 1830 to_tensor(test_case.arrs[1])) 1831 1832 1833@pytest.mark.level1 1834@pytest.mark.platform_arm_ascend_training 1835@pytest.mark.platform_x86_ascend_training 1836@pytest.mark.platform_x86_gpu_training 1837@pytest.mark.platform_x86_cpu 1838@pytest.mark.env_onecard 1839def test_exception_add(): 1840 with pytest.raises(ValueError): 1841 mnp.add(to_tensor(test_case.arrs[1]), to_tensor(test_case.arrs[2])) 1842 1843 1844def mnp_nanmax(x): 1845 a = mnp.nanmax(x) 1846 b = mnp.nanmax(x, keepdims=True) 1847 c = mnp.nanmax(x, axis=-2) 1848 d = mnp.nanmax(x, axis=0, keepdims=True) 1849 e = mnp.nanmax(x, axis=(-2, 3)) 1850 f = mnp.nanmax(x, axis=(-3, -1), keepdims=True) 1851 return a, b, c, d, e, f 1852 1853 1854def onp_nanmax(x): 1855 a = onp.nanmax(x) 1856 b = onp.nanmax(x, keepdims=True) 1857 c = onp.nanmax(x, axis=-2) 1858 d = onp.nanmax(x, axis=0, keepdims=True) 1859 e = onp.nanmax(x, axis=(-2, 3)) 1860 f = onp.nanmax(x, axis=(-3, -1), keepdims=True) 1861 return a, b, c, d, e, f 1862 1863 1864@pytest.mark.level1 1865@pytest.mark.platform_x86_gpu_training 1866@pytest.mark.platform_x86_cpu 1867@pytest.mark.env_onecard 1868def test_nanmax(): 1869 x = rand_int(2, 3, 4, 5) 1870 x[0][2][1][3] = onp.nan 1871 x[1][0][2][4] = onp.nan 1872 x[1][1][1][1] = onp.nan 1873 run_multi_test(mnp_nanmax, onp_nanmax, (x,)) 1874 1875 1876def mnp_nanmin(x): 1877 a = mnp.nanmin(x) 1878 b = mnp.nanmin(x, keepdims=True) 1879 c = mnp.nanmin(x, axis=-2) 1880 d = mnp.nanmin(x, axis=0, keepdims=True) 1881 e = mnp.nanmin(x, axis=(-2, 3)) 1882 f = mnp.nanmin(x, axis=(-3, -1), keepdims=True) 1883 return a, b, c, d, e, f 1884 1885 1886def onp_nanmin(x): 1887 a = onp.nanmin(x) 1888 b = onp.nanmin(x, keepdims=True) 1889 c = onp.nanmin(x, axis=-2) 1890 d = onp.nanmin(x, axis=0, keepdims=True) 1891 e = onp.nanmin(x, axis=(-2, 3)) 1892 f = onp.nanmin(x, axis=(-3, -1), keepdims=True) 1893 return a, b, c, d, e, f 1894 1895 1896@pytest.mark.level1 1897@pytest.mark.platform_x86_gpu_training 1898@pytest.mark.platform_x86_cpu 1899@pytest.mark.env_onecard 1900def test_nanmin(): 1901 x = rand_int(2, 3, 4, 5) 1902 x[0][2][1][3] = onp.nan 1903 x[1][0][2][4] = onp.nan 1904 x[1][1][1][1] = onp.nan 1905 run_multi_test(mnp_nanmin, onp_nanmin, (x,)) 1906 1907 1908def mnp_nansum(x): 1909 a = mnp.nansum(x) 1910 b = mnp.nansum(x, keepdims=True) 1911 c = mnp.nansum(x, axis=-2) 1912 d = mnp.nansum(x, axis=0, keepdims=True) 1913 e = mnp.nansum(x, axis=(-2, 3)) 1914 f = mnp.nansum(x, axis=(-3, -1), keepdims=True) 1915 return a, b, c, d, e, f 1916 1917 1918def onp_nansum(x): 1919 a = onp.nansum(x) 1920 b = onp.nansum(x, keepdims=True) 1921 c = onp.nansum(x, axis=-2) 1922 d = onp.nansum(x, axis=0, keepdims=True) 1923 e = onp.nansum(x, axis=(-2, 3)) 1924 f = onp.nansum(x, axis=(-3, -1), keepdims=True) 1925 return a, b, c, d, e, f 1926 1927 1928@pytest.mark.level1 1929@pytest.mark.platform_x86_gpu_training 1930@pytest.mark.platform_x86_cpu 1931@pytest.mark.env_onecard 1932def test_nansum(): 1933 x = rand_int(2, 3, 4, 5) 1934 x[0][2][1][3] = onp.nan 1935 x[1][0][2][4] = onp.nan 1936 x[1][1][1][1] = onp.nan 1937 run_multi_test(mnp_nansum, onp_nansum, (x,)) 1938 match_res(mnp.nansum, onp.nansum, rand_int()) 1939 1940 1941def mnp_nanmean(x): 1942 a = mnp.nanmean(x, keepdims=True) 1943 b = mnp.nanmean(x, axis=(-2, 3)) 1944 c = mnp.nanmean(x, axis=(-3, -1), keepdims=True) 1945 return a, b, c 1946 1947 1948def onp_nanmean(x): 1949 a = onp.nanmean(x, keepdims=True) 1950 b = onp.nanmean(x, axis=(-2, 3)) 1951 c = onp.nanmean(x, axis=(-3, -1), keepdims=True) 1952 return a, b, c 1953 1954 1955@pytest.mark.level0 1956@pytest.mark.platform_x86_gpu_training 1957@pytest.mark.platform_x86_cpu 1958@pytest.mark.env_onecard 1959def test_nanmean(): 1960 x = rand_int(2, 3, 4, 5) 1961 x[0][2][1][3] = onp.nan 1962 x[1][0][2][4] = onp.nan 1963 x[1][1][1][1] = onp.nan 1964 run_multi_test(mnp_nanmean, onp_nanmean, (x,)) 1965 match_res(mnp.nanmean, onp.nanmean, rand_int()) 1966 1967 1968def mnp_mean(*arrs): 1969 arr1 = arrs[0] 1970 arr2 = arrs[1] 1971 arr3 = arrs[2] 1972 a = mnp.mean(arr1) 1973 b = mnp.mean(arr2, keepdims=True) 1974 c = mnp.mean(arr3, keepdims=False) 1975 d = mnp.mean(arr2, axis=0, keepdims=True) 1976 e = mnp.mean(arr3, axis=(0, -1)) 1977 f = mnp.mean(arr3, axis=-1, keepdims=True) 1978 return a, b, c, d, e, f 1979 1980 1981def onp_mean(*arrs): 1982 arr1 = arrs[0] 1983 arr2 = arrs[1] 1984 arr3 = arrs[2] 1985 a = onp.mean(arr1) 1986 b = onp.mean(arr2, keepdims=True) 1987 c = onp.mean(arr3, keepdims=False) 1988 d = onp.mean(arr2, axis=0, keepdims=True) 1989 e = onp.mean(arr3, axis=(0, -1)) 1990 f = onp.mean(arr3, axis=-1, keepdims=True) 1991 return a, b, c, d, e, f 1992 1993 1994@pytest.mark.level1 1995@pytest.mark.platform_arm_ascend_training 1996@pytest.mark.platform_x86_ascend_training 1997@pytest.mark.platform_x86_gpu_training 1998@pytest.mark.platform_x86_cpu 1999@pytest.mark.env_onecard 2000def test_mean(): 2001 run_multi_test(mnp_mean, onp_mean, test_case.arrs, error=3) 2002 run_multi_test(mnp_mean, onp_mean, test_case.expanded_arrs, error=3) 2003 run_multi_test(mnp_mean, onp_mean, test_case.scalars, error=3) 2004 2005 2006@pytest.mark.level0 2007@pytest.mark.platform_arm_ascend_training 2008@pytest.mark.platform_x86_ascend_training 2009@pytest.mark.platform_x86_gpu_training 2010@pytest.mark.platform_x86_cpu 2011@pytest.mark.env_onecard 2012def test_corrcoef(): 2013 x = onp.random.random((3, 4)).tolist() 2014 mnp_res = mnp.corrcoef(x) 2015 onp_res = onp.corrcoef(x) 2016 match_all_arrays(mnp_res, onp_res, error=1e-5) 2017 mnp_res = mnp.corrcoef(x, rowvar=False) 2018 onp_res = onp.corrcoef(x, rowvar=False) 2019 match_all_arrays(mnp_res, onp_res, error=1e-5) 2020 2021 2022@pytest.mark.level1 2023@pytest.mark.platform_arm_ascend_training 2024@pytest.mark.platform_x86_ascend_training 2025@pytest.mark.platform_x86_gpu_training 2026@pytest.mark.platform_x86_cpu 2027@pytest.mark.env_onecard 2028def test_multi_dot(): 2029 arrays = [rand_int(3), rand_int(3, 5), rand_int(5, 2), rand_int(2, 7), rand_int(7)] 2030 mnp_arrays = [to_tensor(arr) for arr in arrays] 2031 match_all_arrays(mnp.multi_dot(mnp_arrays), onp.linalg.multi_dot(arrays)) 2032 match_all_arrays(mnp.multi_dot(mnp_arrays[1:]), onp.linalg.multi_dot(arrays[1:])) 2033 match_all_arrays(mnp.multi_dot(mnp_arrays[:-1]), onp.linalg.multi_dot(arrays[:-1])) 2034 match_all_arrays(mnp.multi_dot(mnp_arrays[1:-1]), onp.linalg.multi_dot(arrays[1:-1])) 2035 2036 2037@pytest.mark.level0 2038@pytest.mark.platform_arm_ascend_training 2039@pytest.mark.platform_x86_ascend_training 2040@pytest.mark.platform_x86_gpu_training 2041@pytest.mark.platform_x86_cpu 2042@pytest.mark.env_onecard 2043def test_gradient(): 2044 f = onp.random.random((3, 4, 5)).tolist() 2045 mnp_res = mnp.gradient(f, -3, axis=(-1, 1)) 2046 onp_res = onp.gradient(f, -3, axis=(-1, 1)) 2047 match_all_arrays(mnp_res, onp_res, error=1e-5) 2048 mnp_res = mnp.gradient(f, -3, 5, axis=(-1, 0)) 2049 onp_res = onp.gradient(f, -3, 5, axis=(-1, 0)) 2050 match_all_arrays(mnp_res, onp_res, error=1e-5) 2051 2052 2053@pytest.mark.level1 2054@pytest.mark.platform_arm_ascend_training 2055@pytest.mark.platform_x86_ascend_training 2056@pytest.mark.platform_x86_gpu_training 2057@pytest.mark.platform_x86_cpu 2058@pytest.mark.env_onecard 2059def test_argmax(): 2060 match_res(mnp.argmax, onp.argmax, rand_int()) 2061 match_res(mnp.argmax, onp.argmax, rand_int(3)) 2062 match_res(mnp.argmax, onp.argmax, rand_int(1, 1, 1)) 2063 x = onp.random.choice(onp.arange(-100, 100), size=(2, 3, 4, 5), replace=False) 2064 match_res(mnp.argmax, onp.argmax, x) 2065 for i in range(-4, 4): 2066 match_res(mnp.argmax, onp.argmax, x, axis=i) 2067 2068 2069@pytest.mark.level1 2070@pytest.mark.platform_arm_ascend_training 2071@pytest.mark.platform_x86_ascend_training 2072@pytest.mark.platform_x86_gpu_training 2073@pytest.mark.platform_x86_cpu 2074@pytest.mark.env_onecard 2075def test_argmin(): 2076 match_res(mnp.argmin, onp.argmin, rand_int()) 2077 match_res(mnp.argmin, onp.argmin, rand_int(3)) 2078 match_res(mnp.argmin, onp.argmin, rand_int(1, 1, 1)) 2079 x = rand_int(2, 3, 4, 5) 2080 match_res(mnp.argmin, onp.argmin, x) 2081 for i in range(-4, 4): 2082 match_res(mnp.argmin, onp.argmin, x, axis=i) 2083 2084 2085@pytest.mark.level0 2086@pytest.mark.platform_arm_ascend_training 2087@pytest.mark.platform_x86_ascend_training 2088@pytest.mark.platform_x86_gpu_training 2089@pytest.mark.platform_x86_cpu 2090@pytest.mark.env_onecard 2091def test_searchsorted(): 2092 x = onp.arange(-10, 10) 2093 y = onp.random.randint(-15, 15, size=(2, 3, 4)) + onp.random.choice([0, 0.5], (2, 3, 4)) 2094 sorter = onp.random.shuffle(onp.arange(20)) 2095 match_res(mnp.searchsorted, onp.searchsorted, x, y, side='right', sorter=sorter) 2096 2097 2098@pytest.mark.level2 2099@pytest.mark.platform_arm_ascend_training 2100@pytest.mark.platform_x86_ascend_training 2101@pytest.mark.platform_x86_gpu_training 2102@pytest.mark.platform_x86_cpu 2103@pytest.mark.env_onecard 2104def test_interp(): 2105 x = onp.random.randint(-15, 15, size=(2, 3, 4)) + onp.random.choice([0, 0.5], (2, 3, 4)) 2106 xp = onp.arange(-10, 10) 2107 fp = onp.random.uniform(-50, 50, 20) 2108 match_res(mnp.interp, onp.interp, x, xp, fp, error=3) 2109 match_res(mnp.interp, onp.interp, x, xp, fp, left=onp.random.rand(), error=3) 2110 match_res(mnp.interp, onp.interp, x, xp, fp, right=onp.random.rand(), error=3) 2111 match_res(mnp.interp, onp.interp, x, xp, fp, left=onp.random.rand(), right=onp.random.rand(), error=3) 2112 2113 2114@pytest.mark.level2 2115@pytest.mark.platform_arm_ascend_training 2116@pytest.mark.platform_x86_ascend_training 2117@pytest.mark.platform_x86_gpu_training 2118@pytest.mark.platform_x86_cpu 2119@pytest.mark.env_onecard 2120def test_digitize(): 2121 bins = onp.random.randint(-10, 10, size=10) 2122 bins.sort() 2123 x = onp.random.randint(-15, 15, size=(2, 3, 4)) + onp.random.choice([0, 0.5], (2, 3, 4)) 2124 match_res(mnp.digitize, onp.digitize, x, []) 2125 match_res(mnp.digitize, onp.digitize, [], []) 2126 match_res(mnp.digitize, onp.digitize, [], bins) 2127 match_res(mnp.digitize, onp.digitize, x, bins) 2128 match_res(mnp.digitize, onp.digitize, x, bins, right=True) 2129 bins = onp.flip(bins) 2130 match_res(mnp.digitize, onp.digitize, x, bins) 2131 match_res(mnp.digitize, onp.digitize, x, bins, right=True) 2132 2133 2134@pytest.mark.level1 2135@pytest.mark.platform_arm_ascend_training 2136@pytest.mark.platform_x86_ascend_training 2137@pytest.mark.platform_x86_gpu_training 2138@pytest.mark.platform_x86_cpu 2139@pytest.mark.env_onecard 2140def test_bincount(): 2141 x = onp.random.randint(0, 10, 20) 2142 weights = onp.random.randn(20) 2143 match_res(mnp.bincount, onp.bincount, x, dtype=mnp.int32) 2144 match_res(mnp.bincount, onp.bincount, x, minlength=25, dtype=mnp.int32) 2145 match_all_arrays(mnp.bincount(to_tensor(x), to_tensor(weights)), 2146 onp.bincount(x, weights), error=3) 2147 match_all_arrays(mnp.bincount(to_tensor(x), to_tensor(weights), minlength=25), 2148 onp.bincount(x, weights, minlength=25), error=3) 2149 2150 2151@pytest.mark.level0 2152@pytest.mark.platform_arm_ascend_training 2153@pytest.mark.platform_x86_ascend_training 2154@pytest.mark.platform_x86_gpu_training 2155@pytest.mark.platform_x86_cpu 2156@pytest.mark.env_onecard 2157def test_histogram(): 2158 x = onp.random.randint(-10, 10, 10) 2159 weights = onp.random.randn(10) 2160 for bins in [(1, 2, 3), [2], 1, 5]: 2161 # pylint: disable=redefined-builtin 2162 for range in [None, (2, 20)]: 2163 match_res(mnp.histogram, onp.histogram, x, bins=bins, range=range, error=1) 2164 match_res(mnp.histogram, onp.histogram, x, bins=bins, range=range, density=True, error=1) 2165 mnp_res = mnp.histogram(to_tensor(x), bins=bins, range=range, 2166 weights=to_tensor(weights), density=True) 2167 onp_res = onp.histogram(x, bins=bins, range=range, weights=weights, density=True) 2168 match_all_arrays(mnp_res, onp_res, error=1) 2169 2170 2171@pytest.mark.level0 2172@pytest.mark.platform_arm_ascend_training 2173@pytest.mark.platform_x86_ascend_training 2174@pytest.mark.platform_x86_gpu_training 2175@pytest.mark.platform_x86_cpu 2176@pytest.mark.env_onecard 2177def test_histogramdd(): 2178 x = onp.random.randint(-10, 10, (5, 3)) 2179 y = [onp.random.randint(-10, 10, 5), onp.random.randint(-10, 10, 5), onp.random.randint(-10, 10, 5)] 2180 mnp_y = list(map(to_tensor, y)) 2181 weights = onp.random.randn(5) 2182 for bins in [(15, 4, 9)]: 2183 # pylint: disable=redefined-builtin 2184 for range in [[[0, 5], [2, 7], [1, 3]]]: 2185 mnp_res = mnp.histogramdd(to_tensor(x), bins=bins, range=range) 2186 onp_res = onp.histogramdd(x, bins=bins, range=range) 2187 match_all_arrays(mnp_res[0], onp_res[0], error=1) 2188 match_all_arrays(mnp_res[1], onp_res[1], error=1) 2189 mnp_res = mnp.histogramdd(mnp_y, bins=bins, range=range, weights=to_tensor(weights), 2190 density=True) 2191 onp_res = onp.histogramdd(y, bins, range=range, weights=weights, density=True) 2192 match_all_arrays(mnp_res[0], onp_res[0], error=1) 2193 match_all_arrays(mnp_res[1], onp_res[1], error=1) 2194 2195 bins = onp.arange(24).reshape(3, 8) 2196 mnp_res = mnp.histogramdd(to_tensor(x), bins=to_tensor(bins)) 2197 onp_res = onp.histogramdd(x, bins=bins) 2198 match_all_arrays(mnp_res[0], onp_res[0], error=1) 2199 match_all_arrays(mnp_res[1], onp_res[1], error=1) 2200 2201 2202@pytest.mark.level1 2203@pytest.mark.platform_arm_ascend_training 2204@pytest.mark.platform_x86_ascend_training 2205@pytest.mark.platform_x86_gpu_training 2206@pytest.mark.platform_x86_cpu 2207@pytest.mark.env_onecard 2208def test_histogram2d(): 2209 x = onp.random.randint(-10, 10, 10) 2210 y = onp.random.randint(-10, 10, 10) 2211 2212 weights = onp.random.randn(10) 2213 for bins in [4, [8, [1, 2, 3]]]: 2214 # pylint: disable=redefined-builtin 2215 for range in [None, [(3, 3), (2, 20)]]: 2216 match_res(mnp.histogram2d, onp.histogram2d, x, y, bins=bins, range=range, error=1) 2217 mnp_res = mnp.histogram2d(to_tensor(x), to_tensor(y), bins=bins, range=range, 2218 weights=to_tensor(weights), density=True) 2219 onp_res = onp.histogram2d(x, y, bins=bins, range=range, weights=weights, density=True) 2220 match_all_arrays(mnp_res, onp_res, error=1) 2221 2222 2223@pytest.mark.level1 2224@pytest.mark.platform_arm_ascend_training 2225@pytest.mark.platform_x86_ascend_training 2226@pytest.mark.platform_x86_gpu_training 2227@pytest.mark.platform_x86_cpu 2228@pytest.mark.env_onecard 2229def test_exception_mean(): 2230 with pytest.raises(ValueError): 2231 mnp.mean(to_tensor(test_case.arrs[0]), (-1, 0)) 2232 2233 2234def mnp_sum(x): 2235 a = mnp.sum(x) 2236 b = mnp.sum(x, axis=0) 2237 c = mnp.sum(x, axis=(0, 1)) 2238 d = mnp.sum(x, keepdims=True) 2239 e = mnp.sum(x, initial=-1) 2240 f = mnp.sum(x, initial=1) 2241 g = mnp.sum(x, axis=(0, 2, -2), keepdims=True, initial=0.5, dtype=mnp.float64) 2242 return a, b, c, d, e, f, g 2243 2244 2245def onp_sum(x): 2246 a = onp.sum(x) 2247 b = onp.sum(x, axis=0) 2248 c = onp.sum(x, axis=(0, 1)) 2249 d = onp.sum(x, keepdims=True) 2250 e = onp.sum(x, initial=-1) 2251 f = onp.sum(x, initial=1) 2252 g = onp.sum(x, axis=(0, 2, -2), keepdims=True, initial=0.5, dtype=onp.float64) 2253 return a, b, c, d, e, f, g 2254 2255 2256@pytest.mark.level1 2257@pytest.mark.platform_arm_ascend_training 2258@pytest.mark.platform_x86_ascend_training 2259@pytest.mark.platform_x86_gpu_training 2260@pytest.mark.platform_x86_cpu 2261@pytest.mark.env_onecard 2262def test_sum(): 2263 onp_arr = onp.random.rand(2, 3, 4).astype('float32') 2264 mnp_arr = to_tensor(onp_arr) 2265 for actual, expected in zip(mnp_sum(mnp_arr), onp_sum(onp_arr)): 2266 match_array(actual.asnumpy(), expected, error=5) 2267 2268 2269def mnp_sign(x): 2270 return mnp.sign(x) 2271 2272 2273def onp_sign(x): 2274 return onp.sign(x) 2275 2276 2277@pytest.mark.level1 2278@pytest.mark.platform_arm_ascend_training 2279@pytest.mark.platform_x86_ascend_training 2280@pytest.mark.platform_x86_gpu_training 2281@pytest.mark.platform_x86_cpu 2282@pytest.mark.env_onecard 2283def test_sign(): 2284 onp_arr = [ 2285 onp.array(3.5).astype('float32'), 2286 onp.arange(-5, 5).astype('float32'), 2287 onp.random.rand(2, 3, 4).astype('float32') 2288 ] 2289 mnp_arr = list(map(to_tensor, onp_arr)) 2290 for onp_x, mnp_x in zip(onp_arr, mnp_arr): 2291 expected = onp_sign(onp_x) 2292 actual = mnp_sign(mnp_x) 2293 match_array(actual.asnumpy(), expected, error=5) 2294 2295 2296def mnp_copysign(x, y): 2297 return mnp.copysign(x, y) 2298 2299 2300def onp_copysign(x, y): 2301 return onp.copysign(x, y) 2302 2303 2304@pytest.mark.level1 2305@pytest.mark.platform_arm_ascend_training 2306@pytest.mark.platform_x86_ascend_training 2307@pytest.mark.platform_x86_gpu_training 2308@pytest.mark.platform_x86_cpu 2309@pytest.mark.env_onecard 2310def test_copysign(): 2311 onp_arr = [[onp.array([1, -1, 2, -3]).astype('float32'), 2312 onp.array([1, -1, -1, 1]).astype('float32')], 2313 [onp.random.rand(2, 3, 4).astype('float32'), 2314 onp.random.rand(2, 3, 4).astype('float32')]] 2315 mnp_arr = list(map(to_tensor, onp_arr)) 2316 for onp_x, mnp_x in zip(onp_arr, mnp_arr): 2317 expected = onp_copysign(onp_x[0], onp_x[1]) 2318 actual = mnp_copysign(mnp_x[0], mnp_x[1]) 2319 match_array(actual.asnumpy(), expected, error=5) 2320 2321 2322def mnp_matrix_power(x): 2323 a = mnp.matrix_power(x, 0) 2324 b = mnp.matrix_power(x, 1) 2325 c = mnp.matrix_power(x, 2) 2326 d = mnp.matrix_power(x, 3) 2327 return a, b, c, d 2328 2329 2330def onp_matrix_power(x): 2331 a = onp.linalg.matrix_power(x, 0) 2332 b = onp.linalg.matrix_power(x, 1) 2333 c = onp.linalg.matrix_power(x, 2) 2334 d = onp.linalg.matrix_power(x, 3) 2335 return a, b, c, d 2336 2337 2338@pytest.mark.level1 2339@pytest.mark.platform_arm_ascend_training 2340@pytest.mark.platform_x86_ascend_training 2341@pytest.mark.platform_x86_gpu_training 2342@pytest.mark.platform_x86_cpu 2343@pytest.mark.env_onecard 2344def test_matrix_power(): 2345 arrs = [ 2346 onp.random.rand(2, 2).astype('float32'), 2347 onp.random.rand(3, 2, 2).astype('float32'), 2348 onp.random.rand(5, 4, 3, 3).astype('float32'), 2349 ] 2350 for x in arrs: 2351 onp_res = onp_matrix_power(x) 2352 mnp_res = mnp_matrix_power(to_tensor(x)) 2353 for expected, actual in zip(onp_res, mnp_res): 2354 match_array(actual.asnumpy(), expected, error=5) 2355 2356 2357def mnp_around(x): 2358 a = mnp.around(x) 2359 b = mnp.around(x, 1) 2360 c = mnp.around(x, 2) 2361 d = mnp.around(x, 3) 2362 return a, b, c, d 2363 2364 2365def onp_around(x): 2366 a = onp.around(x) 2367 b = onp.around(x, 1) 2368 c = onp.around(x, 2) 2369 d = onp.around(x, 3) 2370 return a, b, c, d 2371 2372 2373@pytest.mark.level1 2374@pytest.mark.platform_arm_ascend_training 2375@pytest.mark.platform_x86_ascend_training 2376@pytest.mark.platform_x86_gpu_training 2377@pytest.mark.platform_x86_cpu 2378@pytest.mark.env_onecard 2379def test_around(): 2380 arrs = [ 2381 onp.random.rand(2, 2).astype('float32'), 2382 onp.random.rand(3, 2, 2).astype('float32'), 2383 onp.random.rand(5, 4, 3, 3).astype('float32'), 2384 ] 2385 for x in arrs: 2386 onp_res = onp_around(x) 2387 mnp_res = mnp_around(to_tensor(x)) 2388 for expected, actual in zip(onp_res, mnp_res): 2389 match_array(actual.asnumpy(), expected, error=5) 2390 2391 2392@pytest.mark.level1 2393@pytest.mark.platform_arm_ascend_training 2394@pytest.mark.platform_x86_ascend_training 2395@pytest.mark.platform_x86_gpu_training 2396@pytest.mark.platform_x86_cpu 2397@pytest.mark.env_onecard 2398def test_polyadd(): 2399 arrs = [rand_int(), rand_int(1), rand_int(3), rand_int(7)] 2400 for x in arrs: 2401 for y in arrs: 2402 match_res(mnp.polyadd, onp.polyadd, x, y) 2403 2404 2405@pytest.mark.level1 2406@pytest.mark.platform_arm_ascend_training 2407@pytest.mark.platform_x86_ascend_training 2408@pytest.mark.platform_x86_gpu_training 2409@pytest.mark.platform_x86_cpu 2410@pytest.mark.env_onecard 2411def test_polysub(): 2412 arrs = [rand_int(), rand_int(1), rand_int(3), rand_int(7)] 2413 for x in arrs: 2414 for y in arrs: 2415 match_res(mnp.polysub, onp.polysub, x, y, error=1) 2416 2417 2418@pytest.mark.level1 2419@pytest.mark.platform_arm_ascend_training 2420@pytest.mark.platform_x86_ascend_training 2421@pytest.mark.platform_x86_gpu_training 2422@pytest.mark.platform_x86_cpu 2423@pytest.mark.env_onecard 2424def test_polyval(): 2425 polys = [rand_int(1), rand_int(3), rand_int(7)] 2426 arrs = [rand_int(), rand_int(1), rand_int(3), rand_int(2, 3, 1), rand_int(1, 5, 4)] 2427 for p in polys: 2428 for x in arrs: 2429 match_res(mnp.polyval, onp.polyval, p, x, error=3) 2430 2431 2432@pytest.mark.level1 2433@pytest.mark.platform_arm_ascend_training 2434@pytest.mark.platform_x86_ascend_training 2435@pytest.mark.platform_x86_gpu_training 2436@pytest.mark.platform_x86_cpu 2437@pytest.mark.env_onecard 2438def test_polyder(): 2439 poly = rand_int(7) 2440 for i in range(5): 2441 match_res(mnp.polyder, onp.polyder, poly, m=i) 2442 2443 2444@pytest.mark.level2 2445@pytest.mark.platform_arm_ascend_training 2446@pytest.mark.platform_x86_ascend_training 2447@pytest.mark.platform_x86_gpu_training 2448@pytest.mark.platform_x86_cpu 2449@pytest.mark.env_onecard 2450def test_polymul(): 2451 arrs = [rand_int(), rand_int(1), rand_int(3), rand_int(7)] 2452 for x in arrs: 2453 for y in arrs: 2454 match_res(mnp.polymul, onp.polymul, x, y) 2455 2456 2457@pytest.mark.level1 2458@pytest.mark.platform_arm_ascend_training 2459@pytest.mark.platform_x86_ascend_training 2460@pytest.mark.platform_x86_gpu_training 2461@pytest.mark.platform_x86_cpu 2462@pytest.mark.env_onecard 2463def test_polyint(): 2464 poly = rand_int(7) 2465 match_res(mnp.polyint, onp.polyint, poly, m=1, k=7, error=3) 2466 match_res(mnp.polyint, onp.polyint, poly, m=1, k=[9], error=3) 2467 match_res(mnp.polyint, onp.polyint, poly, m=3, k=2, error=3) 2468 2469 for i in range(5): 2470 match_res(mnp.polyint, onp.polyint, poly, m=i, k=rand_int(i).tolist(), error=3) 2471 2472 2473@pytest.mark.level1 2474@pytest.mark.platform_arm_ascend_training 2475@pytest.mark.platform_x86_ascend_training 2476@pytest.mark.platform_x86_gpu_training 2477@pytest.mark.platform_x86_cpu 2478@pytest.mark.env_onecard 2479def test_result_type(): 2480 x = ('?', True, mnp.uint16, mnp.ones((2, 3)).astype(mnp.int32), 'float') 2481 y = ('?', True, onp.uint16, onp.ones((2, 3)).astype(onp.int32), 'float') 2482 for i in range(4): 2483 mnp_args = x[:i + 1] 2484 actual = dtype_to_nptype(mnp.result_type(*mnp_args)) 2485 onp_args = y[:i + 1] 2486 expected = onp.result_type(*onp_args) 2487 if expected == onp.int64: 2488 expected = onp.int 2489 elif expected == onp.float64: 2490 expected = onp.float32 2491 assert actual == expected 2492 2493 2494@pytest.mark.level0 2495@pytest.mark.platform_arm_ascend_training 2496@pytest.mark.platform_x86_ascend_training 2497@pytest.mark.platform_x86_gpu_training 2498@pytest.mark.platform_x86_cpu 2499@pytest.mark.env_onecard 2500def test_unwrap(): 2501 x = onp.linspace(onp.linspace((0, 1), (10, 15), 5), onp.linspace((0, 2), (3*onp.pi, 7*onp.pi), 5), 7) 2502 x[5:2] += onp.pi 2503 match_res(mnp.unwrap, onp.unwrap, x, axis=0, error=3) 2504 match_res(mnp.unwrap, onp.unwrap, x, axis=-1, error=3) 2505 2506 2507@pytest.mark.level1 2508@pytest.mark.platform_arm_ascend_training 2509@pytest.mark.platform_x86_ascend_training 2510@pytest.mark.platform_x86_gpu_training 2511@pytest.mark.platform_x86_cpu 2512@pytest.mark.env_onecard 2513def test_exception_amax(): 2514 with pytest.raises(TypeError): 2515 mnp.amax(mnp.array([[1, 2], [3, 4]]).astype(mnp.float32), initial=[1.0, 2.0]) 2516 2517 2518def mnp_cumprod(x): 2519 a = mnp.cumprod(x) 2520 b = mnp.cumprod(x, axis=0) 2521 c = mnp.cumprod(x, axis=1) 2522 return a, b, c 2523 2524 2525def onp_cumprod(x): 2526 a = onp.cumprod(x) 2527 b = onp.cumprod(x, axis=0) 2528 c = onp.cumprod(x, axis=1) 2529 return a, b, c 2530 2531 2532@pytest.mark.level1 2533@pytest.mark.platform_arm_ascend_training 2534@pytest.mark.platform_x86_ascend_training 2535@pytest.mark.env_onecard 2536def test_cumprod(): 2537 mnp_x = mnp.arange(1, 7).reshape(2, 3) 2538 tensors = [mnp_x.astype('bool'), 2539 mnp_x.astype('uint8'), 2540 mnp_x.astype('int16'), 2541 mnp_x.astype('float16'), 2542 mnp_x.astype('float32')] 2543 for x in tensors: 2544 onp_res = onp_cumprod(x.asnumpy()) 2545 mnp_res = mnp_cumprod(x) 2546 for expected, actual in zip(onp_res, mnp_res): 2547 match_array(actual.asnumpy(), expected, error=5) 2548 2549 2550def mnp_ravel_multi_index(x): 2551 a = mnp.ravel_multi_index(x, (7, 6)) 2552 b = mnp.ravel_multi_index(x, (7, 6), order='F') 2553 c = mnp.ravel_multi_index(x, (4, 6), mode='clip') 2554 d = mnp.ravel_multi_index(x, (4, 4), mode='wrap') 2555 return a, b, c, d 2556 2557 2558def onp_ravel_multi_index(x): 2559 a = onp.ravel_multi_index(x, (7, 6)) 2560 b = onp.ravel_multi_index(x, (7, 6), order='F') 2561 c = onp.ravel_multi_index(x, (4, 6), mode='clip') 2562 d = onp.ravel_multi_index(x, (4, 4), mode='wrap') 2563 return a, b, c, d 2564 2565 2566@pytest.mark.level1 2567@pytest.mark.platform_x86_gpu_training 2568@pytest.mark.env_onecard 2569def test_ravel_multi_index(): 2570 x = mnp.array([[3, 6, 6], [4, 5, 1]]) 2571 onp_res = onp_ravel_multi_index(x.asnumpy()) 2572 mnp_res = mnp_ravel_multi_index(x) 2573 for expected, actual in zip(onp_res, mnp_res): 2574 match_array(actual.asnumpy(), expected, error=5) 2575 2576 2577@pytest.mark.level0 2578@pytest.mark.platform_arm_ascend_training 2579@pytest.mark.platform_x86_ascend_training 2580@pytest.mark.platform_x86_gpu_training 2581@pytest.mark.platform_x86_cpu 2582@pytest.mark.env_onecard 2583def test_norm(): 2584 arrs = [rand_int(5, 2, 3, 7)] 2585 for x in arrs: 2586 for keepdims in [True, False]: 2587 match_res(mnp.norm, onp.linalg.norm, x, keepdims=keepdims, error=3) 2588 2589 x = rand_int(3, 6, 4, 5) 2590 axes = [(0, 1), (0, 3), (1, 3), (2, 3)] 2591 order = [None, 'fro', float('inf'), -float('inf'), 1, -1] 2592 for axis in axes: 2593 # pylint: disable=redefined-builtin 2594 for ord in order: 2595 for keepdims in [True, False]: 2596 match_res(mnp.norm, onp.linalg.norm, x, ord=ord, axis=axis, keepdims=keepdims, error=3) 2597 2598 2599@pytest.mark.level1 2600@pytest.mark.platform_arm_ascend_training 2601@pytest.mark.platform_x86_ascend_training 2602@pytest.mark.env_onecard 2603def test_bitwise_and(): 2604 arrs = [onp.random.randint(-100, 100, ()), onp.random.randint(-100, 100, (1,)), 2605 onp.random.randint(-100, 100, (5,)), onp.random.randint(-100, 100, (3, 1)), 2606 onp.random.randint(-100, 100, (4, 1, 5))] 2607 for x in arrs: 2608 for y in arrs: 2609 match_res(mnp.bitwise_and, onp.bitwise_and, x, y, dtype=mnp.int32) 2610 2611 2612@pytest.mark.level1 2613@pytest.mark.platform_arm_ascend_training 2614@pytest.mark.platform_x86_ascend_training 2615@pytest.mark.env_onecard 2616def test_bitwise_or(): 2617 arrs = [onp.random.randint(-100, 100, ()), onp.random.randint(-100, 100, (1,)), 2618 onp.random.randint(-100, 100, (5,)), onp.random.randint(-100, 100, (3, 1)), 2619 onp.random.randint(-100, 100, (4, 1, 5))] 2620 for x in arrs: 2621 for y in arrs: 2622 match_res(mnp.bitwise_or, onp.bitwise_or, x, y, dtype=mnp.int32) 2623 2624 2625@pytest.mark.level1 2626@pytest.mark.platform_arm_ascend_training 2627@pytest.mark.platform_x86_ascend_training 2628@pytest.mark.env_onecard 2629def test_bitwise_xor(): 2630 arrs = [onp.random.randint(-100, 100, ()), onp.random.randint(-100, 100, (1,)), 2631 onp.random.randint(-100, 100, (5,)), onp.random.randint(-100, 100, (3, 1)), 2632 onp.random.randint(-100, 100, (4, 1, 5))] 2633 for x in arrs: 2634 for y in arrs: 2635 match_res(mnp.bitwise_xor, onp.bitwise_xor, x, y, dtype=mnp.int32) 2636 2637 2638@pytest.mark.level1 2639@pytest.mark.platform_arm_ascend_training 2640@pytest.mark.platform_x86_ascend_training 2641@pytest.mark.env_onecard 2642def test_invert(): 2643 x = onp.random.randint(-100, 100, (2, 3)) 2644 match_res(mnp.invert, onp.invert, x, dtype=mnp.int16) 2645 match_res(mnp.invert, onp.invert, x.astype(onp.uint16), dtype=mnp.uint16) 2646 2647 2648@pytest.mark.level1 2649@pytest.mark.platform_arm_ascend_training 2650@pytest.mark.platform_x86_ascend_training 2651@pytest.mark.platform_x86_gpu_training 2652@pytest.mark.platform_x86_cpu 2653@pytest.mark.env_onecard 2654def test_rint(): 2655 arrs = [ 2656 onp.random.rand(2, 2).astype('float32'), 2657 onp.random.rand(3, 2, 2).astype('float32'), 2658 onp.random.rand(5, 4, 3, 3).astype('float32'), 2659 ] 2660 for x in arrs: 2661 for expected, actual in zip(onp.rint(x), mnp.rint(to_tensor(x))): 2662 match_array(actual.asnumpy(), expected, error=5) 2663 2664 2665def mnp_correlate(a, v): 2666 a = mnp.correlate(a, v, mode="valid") 2667 b = mnp.correlate(a, v, mode="full") 2668 c = mnp.correlate(a, v, mode="same") 2669 d = mnp.correlate(a, v) 2670 return a, b, c, d 2671 2672 2673def onp_correlate(a, v): 2674 a = onp.correlate(a, v, mode="valid") 2675 b = onp.correlate(a, v, mode="full") 2676 c = onp.correlate(a, v, mode="same") 2677 d = onp.correlate(a, v) 2678 return a, b, c, d 2679 2680 2681@pytest.mark.level0 2682@pytest.mark.platform_x86_gpu_training 2683@pytest.mark.env_onecard 2684def test_correlate(): 2685 mnp_res = mnp_correlate([1, 2, 3, 4, 5], [1, 2, 3]) 2686 onp_res = onp_correlate([1, 2, 3, 4, 5], [1, 2, 3]) 2687 match_all_arrays(mnp_res, onp_res) 2688 2689 2690@pytest.mark.level1 2691@pytest.mark.platform_arm_ascend_training 2692@pytest.mark.platform_x86_ascend_training 2693@pytest.mark.platform_x86_gpu_training 2694@pytest.mark.platform_x86_cpu 2695@pytest.mark.env_onecard 2696def test_tensor_searchsorted(): 2697 x = onp.arange(-10, 10) 2698 mnp_x = to_tensor(x) 2699 y = onp.random.randint(-15, 15, size=(2, 3, 4)) + onp.random.choice([0, 0.5], (2, 3, 4)) 2700 sorter = onp.random.shuffle(onp.arange(20)) 2701 match_res(mnp_x.searchsorted, x.searchsorted, y) 2702 match_res(mnp_x.searchsorted, x.searchsorted, y, side='right') 2703 match_res(mnp_x.searchsorted, x.searchsorted, y, sorter=sorter) 2704 match_res(mnp_x.searchsorted, x.searchsorted, y, side='right', sorter=sorter) 2705