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 SpaceToDepthNet(nn.Cell): 27 def __init__(self, nptype): 28 super(SpaceToDepthNet, self).__init__() 29 self.SpaceToDepth = P.SpaceToDepth(2) 30 31 data_np = np.array([[[[0, 3], 32 [6, 9]], 33 [[1, 4], 34 [7, 10]], 35 [[2, 5], 36 [8, 11]]]]).astype(nptype) 37 self.data_np = data_np 38 self.x = Parameter(initializer(Tensor(self.data_np), (1, 3, 2, 2)), name='x') 39 40 @ms_function 41 def construct(self): 42 return self.SpaceToDepth(self.x) 43 44 45def SpaceToDepth(nptype): 46 expect = np.arange(12).reshape((1, 12, 1, 1)).astype(nptype) 47 std = SpaceToDepthNet(nptype) 48 output = std() 49 assert (output.asnumpy() == expect).all() 50 51@pytest.mark.level0 52@pytest.mark.platform_x86_cpu 53@pytest.mark.env_onecard 54def test_spacetodepth_graph_float32(): 55 SpaceToDepth(np.float32) 56 57@pytest.mark.level0 58@pytest.mark.platform_x86_cpu 59@pytest.mark.env_onecard 60def test_spacetodepth_graph_float16(): 61 SpaceToDepth(np.float16) 62 63@pytest.mark.level0 64@pytest.mark.platform_x86_cpu 65@pytest.mark.env_onecard 66def test_spacetodepth_graph_int32(): 67 SpaceToDepth(np.int32) 68 69@pytest.mark.level0 70@pytest.mark.platform_x86_cpu 71@pytest.mark.env_onecard 72def test_spacetodepth_graph_int64(): 73 SpaceToDepth(np.int64) 74 75@pytest.mark.level0 76@pytest.mark.platform_x86_cpu 77@pytest.mark.env_onecard 78def test_spacetodepth_graph_int8(): 79 SpaceToDepth(np.int8) 80 81@pytest.mark.level0 82@pytest.mark.platform_x86_cpu 83@pytest.mark.env_onecard 84def test_spacetodepth_graph_int16(): 85 SpaceToDepth(np.int16) 86 87@pytest.mark.level0 88@pytest.mark.platform_x86_cpu 89@pytest.mark.env_onecard 90def test_spacetodepth_graph_uint8(): 91 SpaceToDepth(np.uint8) 92 93@pytest.mark.level0 94@pytest.mark.platform_x86_cpu 95@pytest.mark.env_onecard 96def test_spacetodepth_graph_uint16(): 97 SpaceToDepth(np.uint16) 98 99@pytest.mark.level0 100@pytest.mark.platform_x86_cpu 101@pytest.mark.env_onecard 102def test_spacetodepth_graph_uint32(): 103 SpaceToDepth(np.uint32) 104 105@pytest.mark.level0 106@pytest.mark.platform_x86_cpu 107@pytest.mark.env_onecard 108def test_spacetodepth_graph_uint64(): 109 SpaceToDepth(np.uint64) 110