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 SpaceToDepthNet(nn.Cell): 26 def __init__(self, nptype): 27 super(SpaceToDepthNet, self).__init__() 28 self.SpaceToDepth = P.SpaceToDepth(2) 29 30 data_np = np.array([[[[0, 3], 31 [6, 9]], 32 [[1, 4], 33 [7, 10]], 34 [[2, 5], 35 [8, 11]]]]).astype(nptype) 36 self.data_np = data_np 37 self.x = Parameter(initializer(Tensor(self.data_np), (1, 3, 2, 2)), name='x') 38 39 @ms_function 40 def construct(self): 41 return self.SpaceToDepth(self.x) 42 43 44def SpaceToDepth(nptype): 45 context.set_context(mode=context.GRAPH_MODE, device_target='GPU') 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 51def SpaceToDepth_pynative(nptype): 52 context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU') 53 expect = np.arange(12).reshape((1, 12, 1, 1)).astype(nptype) 54 55 std = P.SpaceToDepth(2) 56 data_np = np.array([[[[0, 3], 57 [6, 9]], 58 [[1, 4], 59 [7, 10]], 60 [[2, 5], 61 [8, 11]]]]).astype(nptype) 62 tensor_input = Tensor(data_np) 63 output = std(tensor_input) 64 assert (output.asnumpy() == expect).all() 65 66 67@pytest.mark.level0 68@pytest.mark.platform_x86_gpu_training 69@pytest.mark.env_onecard 70def test_spacetodepth_graph_float32(): 71 SpaceToDepth(np.float32) 72 73@pytest.mark.level0 74@pytest.mark.platform_x86_gpu_training 75@pytest.mark.env_onecard 76def test_spacetodepth_graph_float16(): 77 SpaceToDepth(np.float16) 78 79@pytest.mark.level1 80@pytest.mark.platform_x86_gpu_training 81@pytest.mark.env_onecard 82def test_spacetodepth_graph_int32(): 83 SpaceToDepth(np.int32) 84 85@pytest.mark.level1 86@pytest.mark.platform_x86_gpu_training 87@pytest.mark.env_onecard 88def test_spacetodepth_graph_int64(): 89 SpaceToDepth(np.int64) 90 91@pytest.mark.level1 92@pytest.mark.platform_x86_gpu_training 93@pytest.mark.env_onecard 94def test_spacetodepth_graph_int8(): 95 SpaceToDepth(np.int8) 96 97@pytest.mark.level1 98@pytest.mark.platform_x86_gpu_training 99@pytest.mark.env_onecard 100def test_spacetodepth_graph_int16(): 101 SpaceToDepth(np.int16) 102 103@pytest.mark.level1 104@pytest.mark.platform_x86_gpu_training 105@pytest.mark.env_onecard 106def test_spacetodepth_graph_uint8(): 107 SpaceToDepth(np.uint8) 108 109@pytest.mark.level1 110@pytest.mark.platform_x86_gpu_training 111@pytest.mark.env_onecard 112def test_spacetodepth_graph_uint16(): 113 SpaceToDepth(np.uint16) 114 115@pytest.mark.level1 116@pytest.mark.platform_x86_gpu_training 117@pytest.mark.env_onecard 118def test_spacetodepth_graph_uint32(): 119 SpaceToDepth(np.uint32) 120 121@pytest.mark.level1 122@pytest.mark.platform_x86_gpu_training 123@pytest.mark.env_onecard 124def test_spacetodepth_graph_uint64(): 125 SpaceToDepth(np.uint64) 126 127@pytest.mark.level0 128@pytest.mark.platform_x86_gpu_training 129@pytest.mark.env_onecard 130def test_spacetodepth_pynative_float32(): 131 SpaceToDepth_pynative(np.float32) 132 133@pytest.mark.level0 134@pytest.mark.platform_x86_gpu_training 135@pytest.mark.env_onecard 136def test_spacetodepth_pynative_float16(): 137 SpaceToDepth_pynative(np.float16) 138 139@pytest.mark.level1 140@pytest.mark.platform_x86_gpu_training 141@pytest.mark.env_onecard 142def test_spacetodepth_pynative_int32(): 143 SpaceToDepth_pynative(np.int32) 144 145@pytest.mark.level1 146@pytest.mark.platform_x86_gpu_training 147@pytest.mark.env_onecard 148def test_spacetodepth_pynative_int64(): 149 SpaceToDepth_pynative(np.int64) 150 151@pytest.mark.level0 152@pytest.mark.platform_x86_gpu_training 153@pytest.mark.env_onecard 154def test_spacetodepth_pynative_int8(): 155 SpaceToDepth_pynative(np.int8) 156 157@pytest.mark.level0 158@pytest.mark.platform_x86_gpu_training 159@pytest.mark.env_onecard 160def test_spacetodepth_pynative_int16(): 161 SpaceToDepth_pynative(np.int16) 162 163@pytest.mark.level1 164@pytest.mark.platform_x86_gpu_training 165@pytest.mark.env_onecard 166def test_spacetodepth_pynative_uint8(): 167 SpaceToDepth_pynative(np.uint8) 168 169@pytest.mark.level1 170@pytest.mark.platform_x86_gpu_training 171@pytest.mark.env_onecard 172def test_spacetodepth_pynative_uint16(): 173 SpaceToDepth_pynative(np.uint16) 174 175@pytest.mark.level1 176@pytest.mark.platform_x86_gpu_training 177@pytest.mark.env_onecard 178def test_spacetodepth_pynative_uint32(): 179 SpaceToDepth_pynative(np.uint32) 180 181@pytest.mark.level1 182@pytest.mark.platform_x86_gpu_training 183@pytest.mark.env_onecard 184def test_spacetodepth_pynative_uint64(): 185 SpaceToDepth_pynative(np.uint64) 186 187test_spacetodepth_graph_float32() 188test_spacetodepth_pynative_int32() 189