• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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""" test implicit conversion """
16import numpy as np
17
18from mindspore import Tensor
19
20
21def test_float_tensor_and_int_add():
22    x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
23    y = 2
24    ret_actual = x + y
25    ret_expect = Tensor(np.array([[2.1, 2.2, 2.3], [2.4, 2.5, 2.6]], dtype=np.float32))
26    assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
27
28
29def test_bool_tensor_and_float_add():
30    x = Tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
31    y = 3.3
32    ret_actual = x + y
33    ret_expect = Tensor(np.array([[4.3, 3.3], [3.3, 4.3]], dtype=np.float32))
34    assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
35
36
37def test_bool_tensor_and_int_add():
38    x = Tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
39    y = 3
40    ret_actual = x + y
41    ret_expect = Tensor(np.array([[4, 3], [3, 4]], dtype=np.int32))
42    assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
43
44
45def test_bool_and_int_tensor_add():
46    x = True
47    y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
48    ret_actual = x + y
49    ret_expect = Tensor(np.array([[2, 3, 4], [5, 6, 7]], dtype=np.int32))
50    assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
51
52
53def test_float_tensor_and_int_tensor_add():
54    x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
55    y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
56    ret_actual = x + y
57    ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float32))
58    assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
59
60
61def test_float_tensor_and_float_tensor_add():
62    x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float64))
63    y = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32))
64    ret_actual = x + y
65    ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float64))
66    assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
67
68
69def test_int_tensor_and_int_tensor_add():
70    x = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16))
71    y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
72    ret_actual = x + y
73    ret_expect = Tensor(np.array([[2, 4, 6], [8, 10, 12]], dtype=np.int32))
74    assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
75
76
77def test_float_tensor_and_bool_tensors_add():
78    x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
79    y = Tensor(np.array([[True, True, True], [False, False, False]], dtype=np.bool_))
80    ret_actual = x + y
81    ret_expect = Tensor(np.array([[1.1, 1.2, 1.3], [0.4, 0.5, 0.6]], dtype=np.float32))
82    assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
83