1# Copyright 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""" test math ops """ 16import numpy as np 17import mindspore.context as context 18import mindspore.nn as nn 19from mindspore import Tensor 20from mindspore.ops import operations as P 21 22context.set_context(mode=context.GRAPH_MODE) 23 24 25class Add(nn.Cell): 26 def __init__(self): 27 super(Add, self).__init__() 28 self.add = P.Add() 29 30 def construct(self, x, y): 31 z = self.add(x, y) 32 return z 33 34 35def test_number_add_number(): 36 input_x = 0.1 37 input_y = -3.2 38 result1 = input_x + input_y 39 add_net = Add() 40 result2 = add_net(input_x, input_y) 41 expect = -3.1 42 assert result1 == expect 43 assert result2 == expect 44 45 46def test_tensor_add_tensor_int8(): 47 input_x = Tensor(np.ones(shape=[3])).astype(np.int8) 48 input_y = Tensor(np.zeros(shape=[3])).astype(np.int8) 49 result1 = input_x + input_y 50 add_net = Add() 51 result2 = add_net(input_x, input_y) 52 expect = np.ones(shape=[3]) 53 assert np.all(result1.asnumpy() == expect) 54 assert np.all(result2.asnumpy() == expect) 55 56 57def test_tensor_add_tensor_int16(): 58 input_x = Tensor(np.ones(shape=[3])).astype(np.int16) 59 input_y = Tensor(np.zeros(shape=[3])).astype(np.int16) 60 result1 = input_x + input_y 61 add_net = Add() 62 result2 = add_net(input_x, input_y) 63 expect = np.ones(shape=[3]) 64 assert np.all(result1.asnumpy() == expect) 65 assert np.all(result2.asnumpy() == expect) 66 67 68def test_tensor_add_tensor_int32(): 69 input_x = Tensor(np.ones(shape=[3])).astype(np.int32) 70 input_y = Tensor(np.zeros(shape=[3])).astype(np.int32) 71 result1 = input_x + input_y 72 add_net = Add() 73 result2 = add_net(input_x, input_y) 74 expect = np.ones(shape=[3]) 75 assert np.all(result1.asnumpy() == expect) 76 assert np.all(result2.asnumpy() == expect) 77 78 79def test_tensor_add_tensor_int64(): 80 input_x = Tensor(np.ones(shape=[3])).astype(np.int64) 81 input_y = Tensor(np.zeros(shape=[3])).astype(np.int64) 82 result1 = input_x + input_y 83 add_net = Add() 84 result2 = add_net(input_x, input_y) 85 expect = np.ones(shape=[3]) 86 assert np.all(result1.asnumpy() == expect) 87 assert np.all(result2.asnumpy() == expect) 88 89 90def test_tensor_add_tensor_uint8(): 91 input_x = Tensor(np.ones(shape=[3])).astype(np.uint8) 92 input_y = Tensor(np.zeros(shape=[3])).astype(np.uint8) 93 result1 = input_x + input_y 94 add_net = Add() 95 result2 = add_net(input_x, input_y) 96 expect = np.ones(shape=[3]) 97 assert np.all(result1.asnumpy() == expect) 98 assert np.all(result2.asnumpy() == expect) 99 100 101def test_tensor_add_tensor_uint16(): 102 input_x = Tensor(np.ones(shape=[3])).astype(np.uint16) 103 input_y = Tensor(np.zeros(shape=[3])).astype(np.uint16) 104 result1 = input_x + input_y 105 add_net = Add() 106 result2 = add_net(input_x, input_y) 107 expect = np.ones(shape=[3]) 108 assert np.all(result1.asnumpy() == expect) 109 assert np.all(result2.asnumpy() == expect) 110 111 112def test_tensor_add_tensor_uint32(): 113 input_x = Tensor(np.ones(shape=[3])).astype(np.uint32) 114 input_y = Tensor(np.zeros(shape=[3])).astype(np.uint32) 115 result1 = input_x + input_y 116 add_net = Add() 117 result2 = add_net(input_x, input_y) 118 expect = np.ones(shape=[3]) 119 assert np.all(result1.asnumpy() == expect) 120 assert np.all(result2.asnumpy() == expect) 121 122 123def test_tensor_add_tensor_uint64(): 124 input_x = Tensor(np.ones(shape=[3])).astype(np.uint64) 125 input_y = Tensor(np.zeros(shape=[3])).astype(np.uint64) 126 result1 = input_x + input_y 127 add_net = Add() 128 result2 = add_net(input_x, input_y) 129 expect = np.ones(shape=[3]) 130 assert np.all(result1.asnumpy() == expect) 131 assert np.all(result2.asnumpy() == expect) 132 133 134def test_tensor_add_tensor_float16(): 135 input_x = Tensor(np.ones(shape=[3])).astype(np.float16) 136 input_y = Tensor(np.zeros(shape=[3])).astype(np.float16) 137 result1 = input_x + input_y 138 add_net = Add() 139 result2 = add_net(input_x, input_y) 140 expect = np.ones(shape=[3]) 141 assert np.all(result1.asnumpy() == expect) 142 assert np.all(result2.asnumpy() == expect) 143 144 145def test_tensor_add_tensor_float32(): 146 input_x = Tensor(np.ones(shape=[3])).astype(np.float32) 147 input_y = Tensor(np.zeros(shape=[3])).astype(np.float32) 148 result1 = input_x + input_y 149 add_net = Add() 150 result2 = add_net(input_x, input_y) 151 expect = np.ones(shape=[3]) 152 assert np.all(result1.asnumpy() == expect) 153 assert np.all(result2.asnumpy() == expect) 154 155 156def test_tensor_add_tensor_float64(): 157 input_x = Tensor(np.ones(shape=[3])).astype(np.float64) 158 input_y = Tensor(np.zeros(shape=[3])).astype(np.float64) 159 result1 = input_x + input_y 160 add_net = Add() 161 result2 = add_net(input_x, input_y) 162 expect = np.ones(shape=[3]) 163 assert np.all(result1.asnumpy() == expect) 164 assert np.all(result2.asnumpy() == expect) 165 166 167def test_tensor_add_number(): 168 input_x = Tensor(np.ones(shape=[3])).astype(np.float32) 169 input_y = -0.4 170 result1 = input_x + input_y 171 add_net = Add() 172 result2 = add_net(input_x, input_y) 173 expect = np.ones(shape=[3]) * 0.6 174 assert np.all(result1.asnumpy() == expect.astype(np.float32)) 175 assert np.all(result2.asnumpy() == expect.astype(np.float32)) 176 177 178def test_tuple_add_tuple(): 179 input_x = (Tensor(np.ones(shape=[3])).astype(np.float32)) 180 input_y = (Tensor(np.ones(shape=[3])).astype(np.float32) * 2) 181 result1 = input_x + input_y 182 add_net = Add() 183 result2 = add_net(input_x, input_y) 184 expect = (np.ones(shape=[3]) * 3) 185 assert np.all(result1.asnumpy() == expect.astype(np.float32)) 186 assert np.all(result2.asnumpy() == expect.astype(np.float32)) 187 188 189def test_tuple_add_tuple_shape(): 190 input_x = (Tensor(np.ones(shape=[3])).astype(np.float32)) 191 input_y = (Tensor(np.ones(shape=[4])).astype(np.float32) * 2) 192 193 result1 = input_x + input_y 194 add_net = Add() 195 result2 = add_net(input_x, input_y) 196 expect = (np.ones(shape=[3]) * 3) 197 assert np.all(result1.asnumpy() == expect.astype(np.float32)) 198 assert np.all(result2.asnumpy() == expect.astype(np.float32)) 199 200 201def test_string_add_string(): 202 input_x = "string111_" 203 input_y = "add_string222" 204 result = input_x + input_y 205 expect = "string111_add_string222" 206 assert result == expect 207 208 209def test_list_add_list(): 210 input_x = [1, 3, 5, 7, 9] 211 input_y = ["0", "6"] 212 result = input_x + input_y 213 expect = [1, 3, 5, 7, 9, "0", "6"] 214 assert result == expect 215 216 217class Sub(nn.Cell): 218 def __init__(self): 219 super(Sub, self).__init__() 220 self.sub = P.Sub() 221 222 def construct(self, x, y): 223 z = self.sub(x, y) 224 return z 225 226 227def test_number_sub_number(): 228 input_x = 10.11 229 input_y = 902 230 result1 = input_x - input_y 231 sub_net = Sub() 232 result2 = sub_net(input_x, input_y) 233 expect = -891.89 234 assert np.all(result1 == expect) 235 assert np.all(result2 == expect) 236 237 238def test_tensor_sub_tensor(): 239 input_x = Tensor(np.array([[2, 2], [3, 3]])) 240 input_y = Tensor(np.array([[1, 2], [-3, 3]])) 241 result1 = input_x - input_y 242 sub_net = Sub() 243 result2 = sub_net(input_x, input_y) 244 expect = Tensor(np.array([[1, 0], [6, 0]])) 245 assert np.all(result1.asnumpy() == expect.asnumpy()) 246 assert np.all(result2.asnumpy() == expect.asnumpy()) 247 248 249def test_tensor_sub_number(): 250 input_x = Tensor(np.array([[2, 2], [3, 3]])) 251 input_y = -2 252 result1 = input_x - input_y 253 sub_net = Sub() 254 result2 = sub_net(input_x, input_y) 255 expect = Tensor(np.array([[4, 4], [5, 5]])) 256 assert np.all(result1.asnumpy() == expect.asnumpy()) 257 assert np.all(result2.asnumpy() == expect.asnumpy()) 258 259 260def test_number_sub_tensor(): 261 input_x = Tensor(np.array([[2, 2], [3, 3]])) 262 input_y = -2 263 result1 = input_x - input_y 264 sub_net = Sub() 265 result2 = sub_net(input_x, input_y) 266 expect = Tensor(np.array([[-4, -4], [-5, -5]])) 267 assert np.all(result1.asnumpy() == expect.asnumpy()) 268 assert np.all(result2.asnumpy() == expect.asnumpy()) 269 270 271class Mul(nn.Cell): 272 def __init__(self): 273 super(Mul, self).__init__() 274 self.mul = P.Mul() 275 276 def construct(self, x, y): 277 z = self.mul(x, y) 278 return z 279 280 281def test_number_mul_number(): 282 input_x = 4.91 283 input_y = 0.16 284 result1 = input_x * input_y 285 mul_net = Mul() 286 result2 = mul_net(input_x, input_y) 287 expect = 0.7856 288 diff1 = result1 - expect 289 diff2 = result2 - expect 290 error = 1.0e-6 291 assert np.all(diff1 < error) 292 assert np.all(-diff1 < error) 293 assert np.all(diff2 < error) 294 assert np.all(-diff2 < error) 295 296 297def test_tensor_mul_tensor(): 298 input_x = Tensor(np.array([[2, 2], [3, 3]])).astype(np.float32) 299 input_y = Tensor(np.array([[1, 2], [3, 1]])).astype(np.float32) 300 result1 = input_x * input_y 301 mul_net = Mul() 302 result2 = mul_net(input_x, input_y) 303 expect = Tensor(np.array([[2, 4], [9, 3]])) 304 assert np.all(result1.asnumpy() == expect.asnumpy()) 305 assert np.all(result2.asnumpy() == expect.asnumpy()) 306 307 308def test_tensor_mul_number(): 309 input_x = Tensor(np.array([[2, 2], [3, 3]])).astype(np.float32) 310 input_y = -1 311 result1 = input_x * input_y 312 mul_net = Mul() 313 result2 = mul_net(input_x, input_y) 314 expect = Tensor(np.array([[-2, -2], [-3, -3]])) 315 assert np.all(result1.asnumpy() == expect.asnumpy()) 316 assert np.all(result2.asnumpy() == expect.asnumpy()) 317 318 319def test_number_mul_tensor(): 320 input_x = Tensor(np.array([[2, 2], [3, 3]])).astype(np.float32) 321 input_y = -1 322 result1 = input_x * input_y 323 mul_net = Mul() 324 result2 = mul_net(input_x, input_y) 325 expect = Tensor(np.array([[-2, -2], [-3, -3]])) 326 assert np.all(result1.asnumpy() == expect.asnumpy()) 327 assert np.all(result2.asnumpy() == expect.asnumpy()) 328 329 330class Div(nn.Cell): 331 def __init__(self): 332 super(Div, self).__init__() 333 self.div = P.Div() 334 335 def construct(self, x, y): 336 z = self.div(x, y) 337 return z 338 339 340def test_number_div_number(): 341 input_x = 4 342 input_y = -1 343 result1 = input_x / input_y 344 div_net = Div() 345 result2 = div_net(input_x, input_y) 346 expect = -4 347 assert np.all(result1 == expect) 348 assert np.all(result2 == expect) 349 350 351def test_tensor_div_tensor(): 352 input_x = Tensor(np.array([[2, 2], [3, 3]])).astype(np.float32) 353 input_y = Tensor(np.array([[1, 2], [3, 1]])).astype(np.float32) 354 result1 = input_x / input_y 355 div_net = Div() 356 result2 = div_net(input_x, input_y) 357 expect = Tensor(np.array([[2, 1], [1, 3]])) 358 assert np.all(result1.asnumpy() == expect.asnumpy()) 359 assert np.all(result2.asnumpy() == expect.asnumpy()) 360 361 362def test_tensor_div_number(): 363 input_x = Tensor(np.array([[2, 2], [3, 3]])).astype(np.float32) 364 input_y = 2 365 result1 = input_x / input_y 366 div_net = Div() 367 result2 = div_net(input_x, input_y) 368 expect = Tensor(np.array([[1, 1], [1.5, 1.5]])) 369 assert np.all(result1.asnumpy() == expect.asnumpy()) 370 assert np.all(result2.asnumpy() == expect.asnumpy()) 371 372 373def test_number_div_tensor(): 374 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 375 input_y = 2 376 result1 = input_x / input_y 377 div_net = Div() 378 result2 = div_net(input_x, input_y) 379 expect = Tensor(np.array([[1, 1], [0.5, 0.5]])) 380 assert np.all(result1.asnumpy() == expect.asnumpy()) 381 assert np.all(result2.asnumpy() == expect.asnumpy()) 382 383 384class Mod(nn.Cell): 385 def __init__(self): 386 super(Mod, self).__init__() 387 self.mod = P.Mod() 388 389 def construct(self, x, y): 390 z = self.mod(x, y) 391 return z 392 393 394def test_number_mod_number(): 395 input_x = 19 396 input_y = 2 397 result1 = input_x % input_y 398 mod_net = Mod() 399 result2 = mod_net(input_x, input_y) 400 expect = 1 401 assert np.all(result1 == expect) 402 assert np.all(result2 == expect) 403 404 405def test_tensor_mod_tensor(): 406 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 407 input_y = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 408 result1 = input_x % input_y 409 mod_net = Mod() 410 result2 = mod_net(input_x, input_y) 411 expect = Tensor(np.array([[0, 0], [0, 0]])).astype(np.float32) 412 assert np.all(result1.asnumpy() == expect.asnumpy()) 413 assert np.all(result2.asnumpy() == expect.asnumpy()) 414 415 416def test_tensor_mod_number(): 417 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 418 input_y = -1 419 result1 = input_x % input_y 420 mod_net = Mod() 421 result2 = mod_net(input_x, input_y) 422 expect = Tensor(np.array([[0, 0], [0, 0]])).astype(np.float32) 423 assert np.all(result1.asnumpy() == expect.asnumpy()) 424 assert np.all(result2.asnumpy() == expect.asnumpy()) 425 426 427def test_number_mod_tensor(): 428 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 429 input_y = 5 430 result1 = input_x % input_y 431 mod_net = Mod() 432 result2 = mod_net(input_x, input_y) 433 expect = Tensor(np.array([[1, 1], [1, 1]])).astype(np.float32) 434 assert np.all(result1.asnumpy() == expect.asnumpy()) 435 assert np.all(result2.asnumpy() == expect.asnumpy()) 436 437 438class Pow(nn.Cell): 439 def __init__(self): 440 super(Pow, self).__init__() 441 self.pow = P.Pow() 442 443 def construct(self, x, y): 444 z = self.pow(x, y) 445 return z 446 447 448def test_number_pow_number(): 449 input_x = 2 450 input_y = 5 451 result1 = input_x ** input_y 452 pow_net = Pow() 453 result2 = pow_net(input_x, input_y) 454 expect = 32 455 assert np.all(result1 == expect) 456 assert np.all(result2 == expect) 457 458 459def test_tensor_pow_tensor(): 460 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 461 input_y = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 462 result1 = input_x ** input_y 463 pow_net = Pow() 464 result2 = pow_net(input_x, input_y) 465 expect = Tensor(np.array([[4, 4], [256, 256]])).astype(np.float32) 466 assert np.all(result1.asnumpy() == expect.asnumpy()) 467 assert np.all(result2.asnumpy() == expect.asnumpy()) 468 469 470def test_tensor_pow_number(): 471 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 472 input_y = 3 473 result1 = input_x ** input_y 474 pow_net = Pow() 475 result2 = pow_net(input_x, input_y) 476 expect = Tensor(np.array([[8, 8], [64, 64]])).astype(np.float32) 477 assert np.all(result1.asnumpy() == expect.asnumpy()) 478 assert np.all(result2.asnumpy() == expect.asnumpy()) 479 480 481def test_number_pow_tensor(): 482 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 483 input_y = 3 484 result1 = input_x ** input_y 485 pow_net = Pow() 486 result2 = pow_net(input_x, input_y) 487 expect = Tensor(np.array([[9, 9], [81, 81]])).astype(np.float32) 488 assert np.all(result1.asnumpy() == expect.asnumpy()) 489 assert np.all(result2.asnumpy() == expect.asnumpy()) 490 491 492class FloorDiv(nn.Cell): 493 def __init__(self): 494 super(FloorDiv, self).__init__() 495 self.floordiv = P.FloorDiv() 496 497 def construct(self, x, y): 498 z = self.floordiv(x, y) 499 return z 500 501 502def test_number_floordiv_number(): 503 input_x = 2 504 input_y = 5 505 result1 = input_x // input_y 506 floordiv_net = FloorDiv() 507 result2 = floordiv_net(input_x, input_y) 508 expect = 0 509 assert np.all(result1 == expect) 510 assert np.all(result2 == expect) 511 512 513def test_tensor_floordiv_tensor(): 514 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 515 input_y = Tensor(np.array([[1, 2], [-2, 4]])).astype(np.float32) 516 result1 = input_x // input_y 517 floordiv_net = FloorDiv() 518 result2 = floordiv_net(input_x, input_y) 519 expect = Tensor(np.array([[2, 1], [-2, 1]])).astype(np.float32) 520 assert np.all(result1.asnumpy() == expect.asnumpy()) 521 assert np.all(result2.asnumpy() == expect.asnumpy()) 522 523 524def test_tensor_floordiv_number(): 525 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 526 input_y = 3 527 result1 = input_x // input_y 528 floordiv_net = FloorDiv() 529 result2 = floordiv_net(input_x, input_y) 530 expect = Tensor(np.array([[0, 0], [1, 1]])).astype(np.float32) 531 assert np.all(result1.asnumpy() == expect.asnumpy()) 532 assert np.all(result2.asnumpy() == expect.asnumpy()) 533 534 535def test_number_floordiv_tensor(): 536 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 537 input_y = 3 538 result1 = input_x // input_y 539 floordiv_net = FloorDiv() 540 result2 = floordiv_net(input_x, input_y) 541 expect = Tensor(np.array([[1, 1], [0, 0]])).astype(np.float32) 542 assert np.all(result1.asnumpy() == expect.asnumpy()) 543 assert np.all(result2.asnumpy() == expect.asnumpy()) 544 545 546def test_number_floormod_number(): 547 input_x = 2 548 input_y = 5 549 result1 = input_x // input_y 550 floordiv_net = FloorDiv() 551 result2 = floordiv_net(input_x, input_y) 552 expect = 2 553 assert np.all(result1 == expect) 554 assert np.all(result2 == expect) 555 556 557def test_tensor_floormod_tensor(): 558 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 559 input_y = Tensor(np.array([[1, 2], [-2, 4]])).astype(np.float32) 560 result1 = input_x // input_y 561 floordiv_net = FloorDiv() 562 result2 = floordiv_net(input_x, input_y) 563 expect = Tensor(np.array([[1, 0], [-2, 0]])).astype(np.float32) 564 assert np.all(result1.asnumpy() == expect.asnumpy()) 565 assert np.all(result2.asnumpy() == expect.asnumpy()) 566 567 568def test_tensor_floormod_number(): 569 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 570 input_y = 3 571 result1 = input_x // input_y 572 floordiv_net = FloorDiv() 573 result2 = floordiv_net(input_x, input_y) 574 expect = Tensor(np.array([[2, 2], [1, 1]])).astype(np.float32) 575 assert np.all(result1.asnumpy() == expect.asnumpy()) 576 assert np.all(result2.asnumpy() == expect.asnumpy()) 577 578 579def test_number_floormod_tensor(): 580 input_x = Tensor(np.array([[2, 2], [4, 4]])).astype(np.float32) 581 input_y = 3 582 result1 = input_x // input_y 583 floordiv_net = FloorDiv() 584 result2 = floordiv_net(input_x, input_y) 585 expect = Tensor(np.array([[1, 1], [3, 3]])).astype(np.float32) 586 assert np.all(result1.asnumpy() == expect.asnumpy()) 587 assert np.all(result2.asnumpy() == expect.asnumpy()) 588