• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019-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
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.ops import operations as P
23from mindspore.ops.operations import _inner_ops as inner
24
25class Net(nn.Cell):
26    def __init__(self):
27        super(Net, self).__init__()
28        self.sub = P.Sub()
29
30    def construct(self, x, y):
31        return self.sub(x, y)
32
33class NetDynamic(nn.Cell):
34    def __init__(self):
35        super(NetDynamic, self).__init__()
36        self.d = inner.GpuConvertToDynamicShape()
37        self.sub = P.Sub()
38
39    def construct(self, x, y):
40        x = self.d(x)
41        y = self.d(y)
42        out = self.sub(x, y)
43        return out
44
45
46def sub(nptype):
47    np_x0 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype)
48    np_y0 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype)
49    np_x1 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype)
50    np_y1 = np.random.uniform(-2, 2, (2, 1, 4, 4)).astype(nptype)
51    np_x2 = np.random.uniform(-2, 2, (2, 1, 1, 4)).astype(nptype)
52    np_y2 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype)
53    np_x3 = np.random.uniform(-2, 2, 1).astype(nptype)
54    np_y3 = np.random.uniform(-2, 2, 1).astype(nptype)
55    np_x4 = np.array(768).astype(nptype)
56    np_y4 = np.array(3072.5).astype(nptype)
57    x0 = Tensor(np_x0)
58    y0 = Tensor(np_y0)
59    x1 = Tensor(np_x1)
60    y1 = Tensor(np_y1)
61    x2 = Tensor(np_x2)
62    y2 = Tensor(np_y2)
63    x3 = Tensor(np_x3)
64    y3 = Tensor(np_y3)
65    x4 = Tensor(np_x4)
66    y4 = Tensor(np_y4)
67
68    expect0 = np.subtract(np_x0, np_y0)
69    error0 = np.ones(shape=expect0.shape) * 1.0e-5
70    expect1 = np.subtract(np_x1, np_y1)
71    error1 = np.ones(shape=expect1.shape) * 1.0e-5
72    expect2 = np.subtract(np_x2, np_y2)
73    error2 = np.ones(shape=expect2.shape) * 1.0e-5
74    expect3 = np.subtract(np_x3, np_y3)
75    error3 = np.ones(shape=expect3.shape) * 1.0e-5
76    expect4 = np.subtract(np_x4, np_y4)
77    error4 = np.ones(shape=expect4.shape) * 1.0e-5
78
79    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
80    sub_net = Net()
81    output0 = sub_net(x0, y0)
82    output1 = sub_net(x1, y1)
83    output2 = sub_net(x2, y2)
84    output3 = sub_net(x3, y3)
85    output4 = sub_net(x4, y4)
86    diff0 = output0.asnumpy() - expect0
87    assert np.all(diff0 < error0)
88    assert output0.shape == expect0.shape
89    diff1 = output1.asnumpy() - expect1
90    assert np.all(diff1 < error1)
91    assert output1.shape == expect1.shape
92    diff2 = output2.asnumpy() - expect2
93    assert np.all(diff2 < error2)
94    assert output2.shape == expect2.shape
95    diff3 = output3.asnumpy() - expect3
96    assert np.all(diff3 < error3)
97    assert output3.shape == expect3.shape
98    diff4 = output4.asnumpy() - expect4
99    assert np.all(diff4 < error4)
100    assert output4.shape == expect4.shape
101
102    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
103    sub_net = Net()
104    output0 = sub_net(x0, y0)
105    output1 = sub_net(x1, y1)
106    output2 = sub_net(x2, y2)
107    output3 = sub_net(x3, y3)
108    output4 = sub_net(x4, y4)
109    diff0 = output0.asnumpy() - expect0
110    assert np.all(diff0 < error0)
111    assert output0.shape == expect0.shape
112    diff1 = output1.asnumpy() - expect1
113    assert np.all(diff1 < error1)
114    assert output1.shape == expect1.shape
115    diff2 = output2.asnumpy() - expect2
116    assert np.all(diff2 < error2)
117    assert output2.shape == expect2.shape
118    diff3 = output3.asnumpy() - expect3
119    assert np.all(diff3 < error3)
120    assert output3.shape == expect3.shape
121    diff4 = output4.asnumpy() - expect4
122    assert np.all(diff4 < error4)
123    assert output4.shape == expect4.shape
124
125    #dynamic shape
126    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
127    d_sub_net = NetDynamic()
128    output3 = d_sub_net(x3, y3)
129    output0 = d_sub_net(x0, y0)
130    diff3 = output3.asnumpy() - expect3
131    assert np.all(diff3 < error3)
132    assert output3.shape == expect3.shape
133    diff0 = output0.asnumpy() - expect0
134    assert np.all(diff0 < error0)
135    assert output0.shape == expect0.shape
136
137
138@pytest.mark.level0
139@pytest.mark.platform_x86_gpu_training
140@pytest.mark.env_onecard
141def test_sub_float64():
142    sub(np.float64)
143
144@pytest.mark.level0
145@pytest.mark.platform_x86_gpu_training
146@pytest.mark.env_onecard
147def test_sub_float32():
148    sub(np.float32)
149
150@pytest.mark.level0
151@pytest.mark.platform_x86_gpu_training
152@pytest.mark.env_onecard
153def test_sub_float16():
154    sub(np.float16)
155
156@pytest.mark.level1
157@pytest.mark.platform_x86_gpu_training
158@pytest.mark.env_onecard
159def test_sub_int64():
160    sub(np.int64)
161
162@pytest.mark.level1
163@pytest.mark.platform_x86_gpu_training
164@pytest.mark.env_onecard
165def test_sub_int32():
166    sub(np.int32)
167