• 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.context as context
20import mindspore.common.dtype as mstype
21import mindspore.nn as nn
22from mindspore import Tensor
23from mindspore.ops import operations as P
24
25context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
26
27
28class Net(nn.Cell):
29    def __init__(self):
30        super(Net, self).__init__()
31        self.ops = P.SquaredDifference()
32
33    def construct(self, x, y):
34        return self.ops(x, y)
35
36
37@pytest.mark.level0
38@pytest.mark.platform_x86_cpu
39@pytest.mark.env_onecard
40def test_net01():
41    net = Net()
42    np.random.seed(1)
43    x1 = np.random.randn(2, 3).astype(np.int32)
44    y1 = np.random.randn(2, 3).astype(np.int32)
45    output1 = net(Tensor(x1), Tensor(y1)).asnumpy()
46    diff = x1 - y1
47    expect1 = diff * diff
48    assert np.all(expect1 == output1)
49    assert output1.shape == expect1.shape
50
51    x2 = np.random.randn(2, 3).astype(np.float32)
52    y2 = np.random.randn(2, 3).astype(np.float32)
53    output2 = net(Tensor(x2), Tensor(y2)).asnumpy()
54    diff = x2 - y2
55    expect2 = diff * diff
56    assert np.all(expect2 == output2)
57    assert output2.shape == expect2.shape
58
59    x3 = np.random.randn(2, 3).astype(np.bool)
60    y3 = np.random.randn(2, 3).astype(np.bool)
61    try:
62        net(Tensor(x3), Tensor(y3)).asnumpy()
63    except TypeError:
64        assert True
65
66
67@pytest.mark.level0
68@pytest.mark.platform_x86_cpu
69@pytest.mark.env_onecard
70def test_net02():
71    net = Net()
72    x1 = Tensor(1, mstype.float32)
73    y1 = Tensor(np.array([[3, 3], [3, 3]]).astype(np.float32))
74    expect1 = np.array([[4, 4], [4, 4]]).astype(np.float32)
75    output1 = net(x1, y1).asnumpy()
76    assert np.all(expect1 == output1)
77    assert output1.shape == expect1.shape
78
79    np.random.seed(1)
80    x2 = np.random.randn(2, 3).astype(np.float32)
81    y2 = np.random.randn(2, 2, 3).astype(np.float32)
82    output2 = net(Tensor(x2), Tensor(y2)).asnumpy()
83    diff = x2 - y2
84    expect2 = diff * diff
85    assert np.all(expect2 == output2)
86    assert output2.shape == expect2.shape
87
88    x3 = np.random.randn(1, 2).astype(np.float32)
89    y3 = np.random.randn(3, 1).astype(np.float32)
90    output3 = net(Tensor(x3), Tensor(y3)).asnumpy()
91    diff = x3 - y3
92    expect3 = diff * diff
93    assert np.all(expect3 == output3)
94    assert output3.shape == expect3.shape
95
96    x4 = np.random.randn(2, 3).astype(np.float32)
97    y4 = np.random.randn(1, 2).astype(np.float32)
98    try:
99        net(Tensor(x4), Tensor(y4)).asnumpy()
100    except ValueError:
101        assert True
102
103    x5 = np.random.randn(2, 3, 2, 3, 4, 5, 6, 7).astype(np.float32)
104    y5 = np.random.randn(2, 3, 2, 3, 4, 5, 6, 7).astype(np.float32)
105    output5 = net(Tensor(x5), Tensor(y5)).asnumpy()
106    diff = x5 - y5
107    expect5 = diff * diff
108    assert np.all(expect5 == output5)
109    assert output5.shape == expect5.shape
110