• 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
25context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
26class DepthToSpaceNet(nn.Cell):
27    def __init__(self, nptype, block_size=2, input_shape=(1, 12, 1, 1)):
28        super(DepthToSpaceNet, self).__init__()
29        self.DepthToSpace = P.DepthToSpace(block_size)
30
31        input_size = 1
32        for i in input_shape:
33            input_size = input_size*i
34        data_np = np.arange(input_size).reshape(input_shape).astype(nptype)
35        self.x1 = Parameter(initializer(Tensor(data_np), input_shape), name='x1')
36
37    @ms_function
38    def construct(self):
39        y1 = self.DepthToSpace(self.x1)
40        return y1
41
42
43def DepthToSpace(nptype, block_size=2, input_shape=(1, 12, 1, 1)):
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    dts = DepthToSpaceNet(nptype, block_size, input_shape)
54    output = dts()
55    assert (output.asnumpy() == expect).all()
56
57@pytest.mark.level0
58@pytest.mark.platform_x86_cpu
59@pytest.mark.env_onecard
60def test_depthtospace_graph_float32():
61    DepthToSpace(np.float32)
62
63@pytest.mark.level0
64@pytest.mark.platform_x86_cpu
65@pytest.mark.env_onecard
66def test_depthtospace_graph_float16():
67    DepthToSpace(np.float16)
68
69@pytest.mark.level0
70@pytest.mark.platform_x86_cpu
71@pytest.mark.env_onecard
72def test_depthtospace_graph_int32():
73    DepthToSpace(np.int32)
74
75@pytest.mark.level0
76@pytest.mark.platform_x86_cpu
77@pytest.mark.env_onecard
78def test_depthtospace_graph_int64():
79    DepthToSpace(np.int64)
80
81@pytest.mark.level0
82@pytest.mark.platform_x86_cpu
83@pytest.mark.env_onecard
84def test_depthtospace_graph_int8():
85    DepthToSpace(np.int8)
86
87@pytest.mark.level0
88@pytest.mark.platform_x86_cpu
89@pytest.mark.env_onecard
90def test_depthtospace_graph_int16():
91    DepthToSpace(np.int16)
92
93@pytest.mark.level0
94@pytest.mark.platform_x86_cpu
95@pytest.mark.env_onecard
96def test_depthtospace_graph_uint8():
97    DepthToSpace(np.uint8)
98
99@pytest.mark.level0
100@pytest.mark.platform_x86_cpu
101@pytest.mark.env_onecard
102def test_depthtospace_graph_uint16():
103    DepthToSpace(np.uint16)
104
105@pytest.mark.level0
106@pytest.mark.platform_x86_cpu
107@pytest.mark.env_onecard
108def test_depthtospace_graph_uint32():
109    DepthToSpace(np.uint32)
110
111@pytest.mark.level0
112@pytest.mark.platform_x86_cpu
113@pytest.mark.env_onecard
114def test_depthtospace_graph_uint64():
115    DepthToSpace(np.uint64)
116