• 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.common.api import ms_function
23from mindspore.ops import operations as P
24
25
26class Net(nn.Cell):
27    def __init__(self):
28        super(Net, self).__init__()
29        self.add = P.AddN()
30
31    @ms_function
32    def construct(self, x, y, z):
33        return self.add((x, y, z))
34
35
36@pytest.mark.level0
37@pytest.mark.platform_x86_gpu_training
38@pytest.mark.env_onecard
39def test_net():
40    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
41    x = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.float32)
42    y = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.float32)
43    z = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.float32)
44    add = Net()
45    output = add(Tensor(x), Tensor(y), Tensor(z))
46    expect_result = [[[[0., 3., 6., 9.],
47                       [12., 15., 18., 21.],
48                       [24., 27., 30., 33.]],
49                      [[36., 39., 42., 45.],
50                       [48., 51., 54., 57.],
51                       [60., 63., 66., 69.]],
52                      [[72., 75., 78., 81.],
53                       [84., 87., 90., 93.],
54                       [96., 99., 102., 105.]]]]
55
56    assert (output.asnumpy() == expect_result).all()
57
58
59@pytest.mark.level0
60@pytest.mark.platform_x86_gpu_training
61@pytest.mark.env_onecard
62def test_net_float64():
63    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
64    x = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.float64)
65    y = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.float64)
66    z = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.float64)
67    add = Net()
68    output = add(Tensor(x), Tensor(y), Tensor(z))
69    expect_result = np.array([[[[0., 3., 6., 9.],
70                                [12., 15., 18., 21.],
71                                [24., 27., 30., 33.]],
72                               [[36., 39., 42., 45.],
73                                [48., 51., 54., 57.],
74                                [60., 63., 66., 69.]],
75                               [[72., 75., 78., 81.],
76                                [84., 87., 90., 93.],
77                                [96., 99., 102., 105.]]]]).astype(np.float64)
78    assert (output.asnumpy() == expect_result).all()
79
80    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
81    x = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.float64)
82    y = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.float64)
83    z = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.float64)
84    add = Net()
85    output = add(Tensor(x), Tensor(y), Tensor(z))
86    expect_result = np.array([[[[0., 3., 6., 9.],
87                                [12., 15., 18., 21.],
88                                [24., 27., 30., 33.]],
89                               [[36., 39., 42., 45.],
90                                [48., 51., 54., 57.],
91                                [60., 63., 66., 69.]],
92                               [[72., 75., 78., 81.],
93                                [84., 87., 90., 93.],
94                                [96., 99., 102., 105.]]]]).astype(np.float64)
95    assert (output.asnumpy() == expect_result).all()
96
97
98@pytest.mark.level0
99@pytest.mark.platform_x86_gpu_training
100@pytest.mark.env_onecard
101def test_net_int64():
102    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
103    x = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.int64)
104    y = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.int64)
105    z = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.int64)
106    add = Net()
107    output = add(Tensor(x), Tensor(y), Tensor(z))
108    expect_result = np.array([[[[0., 3., 6., 9.],
109                                [12., 15., 18., 21.],
110                                [24., 27., 30., 33.]],
111                               [[36., 39., 42., 45.],
112                                [48., 51., 54., 57.],
113                                [60., 63., 66., 69.]],
114                               [[72., 75., 78., 81.],
115                                [84., 87., 90., 93.],
116                                [96., 99., 102., 105.]]]]).astype(np.int64)
117    assert (output.asnumpy() == expect_result).all()
118
119    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
120    x = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.int64)
121    y = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.int64)
122    z = np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4).astype(np.int64)
123    add = Net()
124    output = add(Tensor(x), Tensor(y), Tensor(z))
125    expect_result = np.array([[[[0., 3., 6., 9.],
126                                [12., 15., 18., 21.],
127                                [24., 27., 30., 33.]],
128                               [[36., 39., 42., 45.],
129                                [48., 51., 54., 57.],
130                                [60., 63., 66., 69.]],
131                               [[72., 75., 78., 81.],
132                                [84., 87., 90., 93.],
133                                [96., 99., 102., 105.]]]]).astype(np.int64)
134    assert (output.asnumpy() == expect_result).all()
135