1# Copyright 2020 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""" 16@File : test_tensor.py 17@Author: 18@Date : 2019-03-14 19@Desc : test mindspore tensor's operation 20""" 21import numpy as np 22import pytest 23 24import mindspore as ms 25import mindspore.common.api as me 26import mindspore.nn as nn 27from mindspore import Tensor, context 28from mindspore.common.initializer import initializer 29from mindspore.common.parameter import Parameter 30from ..ut_filter import non_graph_engine 31 32ndarr = np.ones((2, 3)) 33 34context.set_context(mode=context.GRAPH_MODE) 35 36 37def test_tensor_flatten(): 38 with pytest.raises(AttributeError): 39 lst = [1, 2, 3, 4,] 40 tensor_list = ms.Tensor(lst, ms.float32) 41 tensor_list = tensor_list.Flatten() 42 print(tensor_list) 43 44 45def test_tensor_list(): 46 lst = [[1.0, 2.0, 1.0], [1.0, 10.0, 9.0]] 47 tensor_list = ms.Tensor(lst, ms.float32) 48 print(tensor_list) 49 50 51def test_tensor(): 52 """test_tensor""" 53 t1 = ms.Tensor(ndarr) 54 assert isinstance(t1, ms.Tensor) 55 assert t1.dtype == ms.float64 56 57 t2 = ms.Tensor(np.zeros([1, 2, 3]), ms.float32) 58 assert isinstance(t2, ms.Tensor) 59 assert t2.shape == (1, 2, 3) 60 assert t2.dtype == ms.float32 61 62 t3 = ms.Tensor(0.1) 63 assert isinstance(t3, ms.Tensor) 64 assert t3.dtype == ms.float64 65 66 t4 = ms.Tensor(1) 67 assert isinstance(t4, ms.Tensor) 68 assert t4.dtype == ms.int64 69 70 71def test_tensor_type_float16(): 72 t_float16 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float16)) 73 assert isinstance(t_float16, ms.Tensor) 74 assert t_float16.shape == (2, 3) 75 assert t_float16.dtype == ms.float16 76 77def test_tensor_type_complex64(): 78 np_input = np.array( 79 [[1+0.1j, 2j, 3+0.3j], [4-0.4j, 5, 6]], dtype=np.complex64) 80 t_complex64 = ms.Tensor(np_input) 81 assert isinstance(t_complex64, ms.Tensor) 82 assert t_complex64.shape == (2, 3) 83 assert t_complex64.dtype == ms.complex64 84 assert np.all(t_complex64.asnumpy() == np_input) 85 86 87def test_tensor_type_complex64_user_define(): 88 np_input = np.zeros([1, 2, 3]) 89 t_complex64 = ms.Tensor(np_input, ms.complex64) 90 assert isinstance(t_complex64, ms.Tensor) 91 assert t_complex64.shape == (1, 2, 3) 92 assert t_complex64.dtype == ms.complex64 93 assert np.all(t_complex64.asnumpy() == np_input) 94 95 96def test_tensor_type_complex128(): 97 #complex python object 98 py_input = 1 + 2.22222222j 99 t_complex128 = ms.Tensor(py_input) 100 assert t_complex128.shape == () 101 assert t_complex128.dtype == ms.complex128 102 assert np.all(t_complex128.asnumpy() == py_input) 103 104 #complex in numpy array 105 np_input = np.array( 106 [[1+0.1j, 2j, 3+0.3j], [4-0.4j, 5, 6]], dtype=np.complex128) 107 t_complex128 = ms.Tensor(np_input) 108 assert isinstance(t_complex128, ms.Tensor) 109 assert t_complex128.shape == (2, 3) 110 assert t_complex128.dtype == ms.complex128 111 assert np.all(t_complex128.asnumpy() == np_input) 112 113 #complex in tuple 114 py_input = (1, 2.22222222j, 3) 115 t_complex128 = ms.Tensor(py_input) 116 assert np.all(t_complex128.asnumpy() == py_input) 117 118 #complex in list 119 py_input = [[1+0.1j, 2j, 3+0.3j], [4-0.4j, 5, 6]] 120 t_complex128 = ms.Tensor(py_input) 121 assert isinstance(t_complex128, ms.Tensor) 122 assert t_complex128.shape == (2, 3) 123 assert t_complex128.dtype == ms.complex128 124 assert np.all(t_complex128.asnumpy() == py_input) 125 126def test_tensor_type_complex128_user_define(): 127 np_input = np.zeros([1, 2, 3]) 128 t_complex128 = ms.Tensor(np_input, ms.complex128) 129 assert isinstance(t_complex128, ms.Tensor) 130 assert t_complex128.shape == (1, 2, 3) 131 assert t_complex128.dtype == ms.complex128 132 assert np.all(t_complex128.asnumpy() == np_input) 133 134def test_tensor_type_float32(): 135 t_float32 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)) 136 assert isinstance(t_float32, ms.Tensor) 137 assert t_float32.shape == (2, 3) 138 assert t_float32.dtype == ms.float32 139 140 141def test_tensor_type_float32_user_define(): 142 t = ms.Tensor(np.zeros([1, 2, 3]), ms.float32) 143 assert isinstance(t, ms.Tensor) 144 assert t.shape == (1, 2, 3) 145 assert t.dtype == ms.float32 146 147 148def test_tensor_type_float64(): 149 t = ms.Tensor([[1.0, 2, 3], [4, 5, 6]]) 150 assert isinstance(t, ms.Tensor) 151 assert t.shape == (2, 3) 152 assert t.dtype == ms.float64 153 154 t_zero = ms.Tensor(np.zeros([1, 2, 3])) 155 assert isinstance(t_zero, ms.Tensor) 156 assert t_zero.shape == (1, 2, 3) 157 assert t_zero.dtype == ms.float64 158 159 160def test_tensor_type_float64_user_define(): 161 t = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=float)) 162 assert isinstance(t, ms.Tensor) 163 assert t.shape == (2, 3) 164 assert t.dtype == ms.float64 165 166 t_float64 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]]), ms.float64) 167 assert isinstance(t_float64, ms.Tensor) 168 assert t_float64.shape == (2, 3) 169 assert t_float64.dtype == ms.float64 170 171 172def test_tensor_type_bool(): 173 # init a tensor with bool type 174 ts_bool_array = ms.Tensor(np.zeros([2, 3], np.bool), ms.bool_) 175 assert isinstance(ts_bool_array, ms.Tensor) 176 assert ts_bool_array.dtype == ms.bool_ 177 178 t_bool = ms.Tensor(True) 179 assert isinstance(t_bool, ms.Tensor) 180 assert t_bool.dtype == ms.bool_ 181 182 t_bool_array = ms.Tensor(np.array([[True, False, True], [False, False, False]])) 183 assert isinstance(t_bool_array, ms.Tensor) 184 assert t_bool_array.shape == (2, 3) 185 assert t_bool_array.dtype == ms.bool_ 186 187 188def test_tensor_type_int8(): 189 t_int8_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int8)) 190 assert isinstance(t_int8_array, ms.Tensor) 191 assert t_int8_array.shape == (2, 3) 192 assert t_int8_array.dtype == ms.int8 193 194 195def test_tensor_type_int16(): 196 t_int16_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16)) 197 assert isinstance(t_int16_array, ms.Tensor) 198 assert t_int16_array.shape == (2, 3) 199 assert t_int16_array.dtype == ms.int16 200 201 202def test_tensor_type_int32(): 203 t_int = ms.Tensor([[1, 2, 3], [4, 5, 6]]) 204 assert isinstance(t_int, ms.Tensor) 205 assert t_int.shape == (2, 3) 206 assert t_int.dtype == ms.int64 207 208 t_int_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)) 209 assert isinstance(t_int_array, ms.Tensor) 210 assert t_int_array.shape == (2, 3) 211 assert t_int_array.dtype == ms.int32 212 213 214def test_tensor_type_int64(): 215 t_int64 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)) 216 assert isinstance(t_int64, ms.Tensor) 217 assert t_int64.shape == (2, 3) 218 assert t_int64.dtype == ms.int64 219 220 221def test_tensor_type_uint8(): 222 t_uint8_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8)) 223 assert isinstance(t_uint8_array, ms.Tensor) 224 assert t_uint8_array.shape == (2, 3) 225 assert t_uint8_array.dtype == ms.uint8 226 227 228def test_tensor_type_uint16(): 229 t_uint16_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint16)) 230 assert isinstance(t_uint16_array, ms.Tensor) 231 assert t_uint16_array.shape == (2, 3) 232 assert t_uint16_array.dtype == ms.uint16 233 234 235def test_tensor_type_uint32(): 236 t_uint32_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint32)) 237 assert isinstance(t_uint32_array, ms.Tensor) 238 assert t_uint32_array.shape == (2, 3) 239 assert t_uint32_array.dtype == ms.uint32 240 241 242def test_tensor_type_uint64(): 243 t_uint64 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint64)) 244 assert isinstance(t_uint64, ms.Tensor) 245 assert t_uint64.shape == (2, 3) 246 assert t_uint64.dtype == ms.uint64 247 248 249def test_set_type(): 250 t = ms.Tensor(ndarr) 251 t.set_dtype(ms.float32) 252 assert t.dtype == ms.float32 253 254 255@non_graph_engine 256def test_add(): 257 x = ms.Tensor(ndarr) 258 y = ms.Tensor(ndarr) 259 z = x + y 260 assert isinstance(z, ms.Tensor) 261 262 263@non_graph_engine 264def test_sub(): 265 x = ms.Tensor(ndarr) 266 y = ms.Tensor(ndarr) 267 z = x - y 268 assert isinstance(z, ms.Tensor) 269 270 271@non_graph_engine 272def test_div(): 273 x = ms.Tensor(np.array([[2, 6, 10], [12, 4, 8]]).astype(np.float32)) 274 y = ms.Tensor(np.array([[2, 2, 5], [6, 1, 2]]).astype(np.float32)) 275 z = x / y 276 z2 = x / 2 277 assert isinstance(z, ms.Tensor) 278 assert isinstance(z2, ms.Tensor) 279 280 281@non_graph_engine 282def test_parameter(): 283 x = Parameter(initializer(1, [1], ms.float32), name="beta1_power") 284 x = x.init_data() 285 z = x / 2 286 print(z) 287 288 289class Net(nn.Cell): 290 """Net definition""" 291 292 def __init__(self, dim): 293 super(Net, self).__init__() 294 self.dim = dim 295 296 def construct(self, input_x): 297 return input_x 298 299 300@non_graph_engine 301def test_return_tensor(): 302 """test_return_tensor""" 303 net = Net(0) 304 input_data = ms.Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')) 305 input_data.set_dtype(ms.float32) 306 exe = me._cell_graph_executor 307 exe.compile(net, input_data) 308 tensor_ = exe(net, input_data) 309 310 # get shape 311 shape_ = tensor_.shape 312 print("shape = ", shape_) 313 314 # get type 315 type_ = tensor_.dtype 316 print("type = ", type_) 317 318 # get value 319 value_ = tensor_.asnumpy() 320 print("numpy value = ", value_) 321 322 323def test_tensor_contiguous(): 324 """test_tensor_contiguous""" 325 input_c = np.arange(6).reshape(2, 3) 326 input_f = input_c.T 327 np.ascontiguousarray(input_c, dtype=np.float32) 328 assert True, input_c.flags['C_CONTIGUOUS'] 329 330 print("input_f flags = ", input_f.flags) 331 assert True, input_f.flags['F_CONTIGUOUS'] 332 333 tensor_f_float32 = ms.Tensor(input_f) 334 rt_f = tensor_f_float32.asnumpy() 335 assert True, rt_f.flags['C_CONTIGUOUS'] 336 print("rt_f flags = ", rt_f.flags) 337 338 339def test_tensor_contiguous2(): 340 input_data = np.random.randn(32, 112, 112, 3).astype(np.float32) 341 input_me = input_data.transpose(0, 3, 1, 2) 342 print("input_me flags = ", input_me.flags) 343 tensor_f_float32 = ms.Tensor(input_me) 344 out_f = tensor_f_float32.asnumpy() 345 print("out_f flags = ", out_f.flags) 346 347 348def test_tensor_input_string(): 349 with pytest.raises(TypeError): 350 input_data = 'ccc' 351 ms.Tensor(input_data) 352 353 354def test_tensor_input_tuple_string(): 355 with pytest.raises(TypeError): 356 input_data = (2, 3, '4', 5) 357 ms.Tensor(input_data) 358 359 360def test_tensor_input_list_string(): 361 with pytest.raises(TypeError): 362 input_data = [[2, 3, '4', 5], [1, 2, 3, 4]] 363 ms.Tensor(input_data) 364 365 366def test_tensor_input_none(): 367 with pytest.raises(TypeError): 368 input_data = None 369 ms.Tensor(input_data, np.int64) 370 371 372# pylint: disable=no-value-for-parameter 373def test_tensor_input_empty(): 374 with pytest.raises(TypeError): 375 ms.Tensor() 376 377 378def test_tensor_input_ndarray_str(): 379 inp = np.array(["88", 0, 9]) 380 tensor = ms.Tensor(inp) 381 assert str(tensor) == "['88' '0' '9']" 382 383 384def test_tensor_input_ndarray_bool(): 385 inp = np.array([True, 2, 4]) 386 ms.Tensor(inp) 387 388 inp = np.array([False, 2, 4]) 389 ms.Tensor(inp) 390 391def test_tensor_input_ndarray_none(): 392 with pytest.raises(TypeError): 393 inp = np.array([None, 2, 4]) 394 ms.Tensor(inp) 395 396 397def test_tensor_input_ndarray_dict(): 398 with pytest.raises(TypeError): 399 inp = {'a': 6, 'b': 7} 400 ms.Tensor(inp) 401 402 403def test_tensor_input_np_nan(): 404 with pytest.raises(TypeError): 405 input_data = (1, 2, 3, np.nan) 406 ms.Tensor(input_data, np.int64) 407 408 409def test_tensor_input_tuple_inf(): 410 with pytest.raises(TypeError): 411 input_data = (1, 2, 3, float("inf")) 412 ms.Tensor(input_data, np.int64) 413 414 415def test_tensor_input_dict(): 416 with pytest.raises(TypeError): 417 input_data = {'a': 6, 'b': 7} 418 ms.Tensor(input_data, np.int64) 419 420 421def test_tensor_input_complex(): 422 with pytest.raises(TypeError): 423 input_data = (1, 2j, 3) 424 ms.Tensor(input_data, np.int64) 425 426 427def test_tensor_dtype_np_float(): 428 with pytest.raises(TypeError): 429 input_data = np.random.randn(32, 112, 112, 3).astype(np.float) 430 ms.Tensor(input_data, np.float) 431 432 433def test_tensor_dtype_np_float16(): 434 with pytest.raises(TypeError): 435 input_data = np.random.randn(32, 112, 112, 3).astype(np.float16) 436 ms.Tensor(input_data, np.float16) 437 438 439def test_tensor_dtype_np_float32(): 440 with pytest.raises(TypeError): 441 input_data = np.random.randn(32, 112, 112, 3).astype(np.float32) 442 ms.Tensor(input_data, np.float32) 443 444 445def test_tensor_dtype_np_float64(): 446 with pytest.raises(TypeError): 447 input_data = np.random.randn(32, 112, 112, 3).astype(np.float64) 448 ms.Tensor(input_data, np.float64) 449 450 451def test_tensor_dtype_np_int(): 452 with pytest.raises(TypeError): 453 input_data = np.random.randn(32, 112, 112, 3).astype(np.int) 454 ms.Tensor(input_data, np.int) 455 456 457def test_tensor_dtype_np_int8(): 458 with pytest.raises(TypeError): 459 input_data = np.random.randn(32, 112, 112, 3).astype(np.int8) 460 ms.Tensor(input_data, np.int8) 461 462 463def test_tensor_dtype_np_int16(): 464 with pytest.raises(TypeError): 465 input_data = np.random.randn(32, 112, 112, 3).astype(np.int16) 466 ms.Tensor(input_data, np.int16) 467 468 469def test_tensor_dtype_np_int32(): 470 with pytest.raises(TypeError): 471 input_data = np.random.randn(32, 112, 112, 3).astype(np.int32) 472 ms.Tensor(input_data, np.int32) 473 474 475def test_tensor_dtype_np_int64(): 476 with pytest.raises(TypeError): 477 input_data = np.random.randn(32, 112, 112, 3).astype(np.int64) 478 ms.Tensor(input_data, np.int64) 479 480 481def test_tensor_dtype_fp32_to_bool(): 482 input_ = np.random.randn(2, 3, 4, 5).astype(np.float32) 483 input_ = ms.Tensor(input_) 484 t = ms.Tensor(input_, dtype=ms.bool_) 485 assert isinstance(t, ms.Tensor) 486 assert t.shape == (2, 3, 4, 5) 487 assert t.dtype == ms.bool_ 488 489 490def test_tensor_dtype_fp64_to_uint8(): 491 array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64) 492 t = ms.Tensor(array, ms.uint8) 493 assert isinstance(t, ms.Tensor) 494 assert t.shape == (2, 3) 495 assert t.dtype == ms.uint8 496 497def test_tensor_dtype_complex64_to_float32(): 498 array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.complex64) 499 t = ms.Tensor(array, ms.float32) 500 assert isinstance(t, ms.Tensor) 501 assert t.shape == (2, 3) 502 assert t.dtype == ms.float32 503 504def test_tensor_dtype_float32_to_complex64(): 505 array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) 506 t = ms.Tensor(array, ms.complex64) 507 assert isinstance(t, ms.Tensor) 508 assert t.shape == (2, 3) 509 assert t.dtype == ms.complex64 510 511def test_tensor_operation(): 512 x = Tensor(np.ones((3, 3)) * 4) 513 res = x + 1 514 assert np.all(res.asnumpy() == np.ones((3, 3)) * 5) 515 res = 1 + x 516 assert np.all(res.asnumpy() == np.ones((3, 3)) * 5) 517 res = x - 2 518 assert np.all(res.asnumpy() == np.ones((3, 3)) * 2) 519 res = 6 - x 520 assert np.all(res.asnumpy() == np.ones((3, 3)) * 2) 521 res = x * 3 522 assert np.all(res.asnumpy() == np.ones((3, 3)) * 12) 523 res = 3 * x 524 assert np.all(res.asnumpy() == np.ones((3, 3)) * 12) 525 res = x / 2 526 assert np.all(res.asnumpy() == np.ones((3, 3)) * 2) 527 res = 8 / x 528 assert np.all(res.asnumpy() == np.ones((3, 3)) * 2) 529 res = x % 3 530 assert np.all(res.asnumpy() == np.ones((3, 3))) 531 res = x // 3 532 assert np.all(res.asnumpy() == np.ones((3, 3))) 533 x %= 3 534 assert np.all(x.asnumpy() == np.ones((3, 3))) 535 res = x * (2, 3, 4) 536 assert np.all(res.asnumpy() == np.ones((3, 3)) * (2, 3, 4)) 537 res = 5 % x 538 assert np.all(x.asnumpy() == np.ones((3, 3))) 539 res = 5 // x 540 assert np.all(x.asnumpy() == np.ones((3, 3))) 541 542def test_tensor_from_numpy(): 543 a = np.ones((2, 3)) 544 t = ms.Tensor.from_numpy(a) 545 assert isinstance(t, ms.Tensor) 546 assert np.all(t.asnumpy() == 1) 547 # 't' and 'a' share same data. 548 a[1] = 2 549 assert np.all(t.asnumpy()[0] == 1) 550 assert np.all(t.asnumpy()[1] == 2) 551 # 't' is still valid after 'a' deleted. 552 del a 553 assert np.all(t.asnumpy()[0] == 1) 554 assert np.all(t.asnumpy()[1] == 2) 555 with pytest.raises(TypeError): 556 # incorrect input. 557 t = ms.Tensor.from_numpy([1, 2, 3]) 558