• 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
16import numpy as np
17import pytest
18
19import mindspore.common.dtype as mstype
20import mindspore.nn as nn
21from mindspore import Tensor, context
22from mindspore.ops import operations as P
23
24context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
25
26class TensorAdd(nn.Cell):
27    def __init__(self):
28        super(TensorAdd, self).__init__()
29        self.add = P.Add()
30
31    def construct(self, x, y):
32        res = self.add(x, y)
33        return res
34
35
36@pytest.mark.level0
37@pytest.mark.platform_x86_cpu
38@pytest.mark.env_onecard
39def test_tensor_add():
40    x0 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32))
41    y0 = Tensor(np.random.uniform(-2, 2, (1, 1, 1, 1)).astype(np.float32))
42    x1 = Tensor(np.random.uniform(-2, 2, (1, 3, 1, 4)).astype(np.float32))
43    y1 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32))
44    x2 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32))
45    y2 = Tensor(2, mstype.float32)
46    x3 = Tensor(2, mstype.float32)
47    y3 = Tensor(2, mstype.float32)
48    x4 = Tensor(np.random.uniform(-2, 2, (4)).astype(np.float32))
49    y4 = Tensor(np.random.uniform(-2, 2, (4, 4)).astype(np.float32))
50    add = TensorAdd()
51    out = add(x0, y0).asnumpy()
52    exp = x0.asnumpy() + y0.asnumpy()
53    diff = np.abs(out - exp)
54    err = np.ones(shape=exp.shape) * 1.0e-5
55    assert np.all(diff < err)
56    assert out.shape == exp.shape
57
58    out = add(x1, y1).asnumpy()
59    exp = x1.asnumpy() + y1.asnumpy()
60    diff = np.abs(out - exp)
61    err = np.ones(shape=exp.shape) * 1.0e-5
62    assert np.all(diff < err)
63    assert out.shape == exp.shape
64
65    out = add(x2, y2).asnumpy()
66    exp = x2.asnumpy() + y2.asnumpy()
67    diff = np.abs(out - exp)
68    err = np.ones(shape=exp.shape) * 1.0e-5
69    assert np.all(diff < err)
70    assert out.shape == exp.shape
71
72    out = add(x3, y3).asnumpy()
73    exp = x3.asnumpy() + y3.asnumpy()
74    diff = np.abs(out - exp)
75    err = np.ones(shape=exp.shape) * 1.0e-5
76    assert np.all(diff < err)
77    assert out.shape == exp.shape
78
79    out = add(x4, y4).asnumpy()
80    exp = x4.asnumpy() + y4.asnumpy()
81    diff = np.abs(out - exp)
82    err = np.ones(shape=exp.shape) * 1.0e-5
83    assert np.all(diff < err)
84    assert out.shape == exp.shape
85