• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 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# ============================================================================
15import numpy as np
16import pytest
17import mindspore.context as context
18import mindspore.nn as nn
19import mindspore.ops.operations.array_ops as P
20from mindspore import Tensor
21from mindspore.common.api import ms_function
22from mindspore.common.initializer import initializer
23from mindspore.common.parameter import Parameter
24
25class DepthToSpaceNet(nn.Cell):
26    def __init__(self, nptype, block_size=2, input_shape=(1, 12, 1, 1)):
27        super(DepthToSpaceNet, self).__init__()
28        self.DepthToSpace = P.DepthToSpace(2)
29        input_size = 1
30        for i in input_shape:
31            input_size = input_size*i
32        data_np = np.arange(input_size).reshape(input_shape).astype(nptype)
33        self.x1 = Parameter(initializer(Tensor(data_np), input_shape), name='x1')
34
35
36    @ms_function
37    def construct(self):
38        y1 = self.DepthToSpace(self.x1)
39        return y1
40
41
42def DepthToSpace(nptype, block_size=2, input_shape=(1, 12, 1, 1)):
43    context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
44    input_size = 1
45    for i in input_shape:
46        input_size = input_size*i
47    expect = np.array([[[[0, 3],
48                         [6, 9]],
49                        [[1, 4],
50                         [7, 10]],
51                        [[2, 5],
52                         [8, 11]]]]).astype(nptype)
53
54    dts = DepthToSpaceNet(nptype, block_size, input_shape)
55    output = dts()
56    print(output)
57    assert (output.asnumpy() == expect).all()
58
59def DepthToSpace_pynative(nptype, block_size=2, input_shape=(1, 12, 1, 1)):
60    context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
61    input_size = 1
62    for i in input_shape:
63        input_size = input_size*i
64    expect = np.array([[[[0, 3],
65                         [6, 9]],
66                        [[1, 4],
67                         [7, 10]],
68                        [[2, 5],
69                         [8, 11]]]]).astype(nptype)
70
71    dts = P.DepthToSpace(2)
72    arr_input = Tensor(np.arange(input_size).reshape(input_shape).astype(nptype))
73    output = dts(arr_input)
74
75    assert (output.asnumpy() == expect).all()
76
77
78@pytest.mark.level0
79@pytest.mark.platform_x86_gpu_training
80@pytest.mark.env_onecard
81def test_depthtospace_graph_float32():
82    DepthToSpace(np.float32)
83
84@pytest.mark.level0
85@pytest.mark.platform_x86_gpu_training
86@pytest.mark.env_onecard
87def test_depthtospace_graph_float16():
88    DepthToSpace(np.float16)
89
90@pytest.mark.level1
91@pytest.mark.platform_x86_gpu_training
92@pytest.mark.env_onecard
93def test_depthtospace_graph_int32():
94    DepthToSpace(np.int32)
95
96@pytest.mark.level1
97@pytest.mark.platform_x86_gpu_training
98@pytest.mark.env_onecard
99def test_depthtospace_graph_int64():
100    DepthToSpace(np.int64)
101
102@pytest.mark.level1
103@pytest.mark.platform_x86_gpu_training
104@pytest.mark.env_onecard
105def test_depthtospace_graph_int8():
106    DepthToSpace(np.int8)
107
108@pytest.mark.level1
109@pytest.mark.platform_x86_gpu_training
110@pytest.mark.env_onecard
111def test_depthtospace_graph_int16():
112    DepthToSpace(np.int16)
113
114@pytest.mark.level1
115@pytest.mark.platform_x86_gpu_training
116@pytest.mark.env_onecard
117def test_depthtospace_graph_uint8():
118    DepthToSpace(np.uint8)
119
120@pytest.mark.level1
121@pytest.mark.platform_x86_gpu_training
122@pytest.mark.env_onecard
123def test_depthtospace_graph_uint16():
124    DepthToSpace(np.uint16)
125
126@pytest.mark.level1
127@pytest.mark.platform_x86_gpu_training
128@pytest.mark.env_onecard
129def test_depthtospace_graph_uint32():
130    DepthToSpace(np.uint32)
131
132@pytest.mark.level1
133@pytest.mark.platform_x86_gpu_training
134@pytest.mark.env_onecard
135def test_depthtospace_graph_uint64():
136    DepthToSpace(np.uint64)
137
138@pytest.mark.level0
139@pytest.mark.platform_x86_gpu_training
140@pytest.mark.env_onecard
141def test_depthtospace_pynative_float32():
142    DepthToSpace_pynative(np.float32)
143
144@pytest.mark.level0
145@pytest.mark.platform_x86_gpu_training
146@pytest.mark.env_onecard
147def test_depthtospace_pynative_float16():
148    DepthToSpace_pynative(np.float16)
149
150@pytest.mark.level1
151@pytest.mark.platform_x86_gpu_training
152@pytest.mark.env_onecard
153def test_depthtospace_pynative_int32():
154    DepthToSpace_pynative(np.int32)
155
156@pytest.mark.level1
157@pytest.mark.platform_x86_gpu_training
158@pytest.mark.env_onecard
159def test_depthtospace_pynative_int64():
160    DepthToSpace_pynative(np.int64)
161
162@pytest.mark.level1
163@pytest.mark.platform_x86_gpu_training
164@pytest.mark.env_onecard
165def test_depthtospace_pynative_int8():
166    DepthToSpace_pynative(np.int8)
167
168@pytest.mark.level1
169@pytest.mark.platform_x86_gpu_training
170@pytest.mark.env_onecard
171def test_depthtospace_pynative_int16():
172    DepthToSpace_pynative(np.int16)
173
174@pytest.mark.level1
175@pytest.mark.platform_x86_gpu_training
176@pytest.mark.env_onecard
177def test_depthtospace_pynative_uint8():
178    DepthToSpace_pynative(np.uint8)
179
180@pytest.mark.level1
181@pytest.mark.platform_x86_gpu_training
182@pytest.mark.env_onecard
183def test_depthtospace_pynative_uint16():
184    DepthToSpace_pynative(np.uint16)
185
186@pytest.mark.level1
187@pytest.mark.platform_x86_gpu_training
188@pytest.mark.env_onecard
189def test_depthtospace_pynative_uint32():
190    DepthToSpace_pynative(np.uint32)
191
192@pytest.mark.level1
193@pytest.mark.platform_x86_gpu_training
194@pytest.mark.env_onecard
195def test_depthtospace_pynative_uint64():
196    DepthToSpace_pynative(np.uint64)
197