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# ============================================================================ 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.parameter import ParameterTuple 23from mindspore.ops import operations as P 24from mindspore.ops import composite as C 25 26 27class NetConv3d(nn.Cell): 28 def __init__(self): 29 super(NetConv3d, self).__init__() 30 out_channel = 4 31 kernel_size = 2 32 self.conv = P.Conv3D(out_channel, 33 kernel_size, 34 mode=1, 35 pad_mode="valid", 36 pad=0, 37 stride=1, 38 dilation=1, 39 group=1) 40 41 def construct(self, x, w): 42 return self.conv(x, w) 43 44 45@pytest.mark.level0 46@pytest.mark.platform_x86_gpu_training 47@pytest.mark.env_onecard 48def test_conv3d(): 49 x = Tensor(np.arange(1 * 3 * 3 * 3 * 3).reshape(1, 3, 3, 3, 3).astype(np.float32)) 50 w = Tensor(np.arange(4 * 3 * 2 * 2 * 2).reshape(4, 3, 2, 2, 2).astype(np.float32)) 51 expect = np.array([[[[[12960., 13236.], 52 [13788., 14064.]], 53 [[15444., 15720.], 54 [16272., 16548.]]], 55 [[[32256., 33108.], 56 [34812., 35664.]], 57 [[39924., 40776.], 58 [42480., 43332.]]], 59 [[[51552., 52980.], 60 [55836., 57264.]], 61 [[64404., 65832.], 62 [68688., 70116.]]], 63 [[[70848., 72852.], 64 [76860., 78864.]], 65 [[88884., 90888.], 66 [94896., 96900.]]]]]).astype(np.float32) 67 68 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 69 net = NetConv3d() 70 output = net(x, w) 71 assert (output.asnumpy() == expect).all() 72 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 73 net = NetConv3d() 74 output = net(x, w) 75 assert (output.asnumpy() == expect).all() 76 77 78class MSConv3dNet(nn.Cell): 79 def __init__(self, in_channels, out_channels, kernel_size, pad_mode='pad', padding=0, stride=1, dilation=1, 80 has_bias=False, weight_init='normal'): 81 super(MSConv3dNet, self).__init__() 82 self.cv1 = nn.Conv3d(in_channels=in_channels, 83 out_channels=out_channels, 84 kernel_size=kernel_size, 85 pad_mode=pad_mode, 86 padding=padding, 87 stride=stride, 88 dilation=dilation, 89 group=1, 90 has_bias=has_bias, 91 weight_init=weight_init, 92 data_format='NCDHW') 93 94 def construct(self, x): 95 x = self.cv1(x) 96 return x 97 98 99class MSGradNet(nn.Cell): 100 def __init__(self, network): 101 super(MSGradNet, self).__init__() 102 self.grad = C.GradOperation(get_all=True, sens_param=True, get_by_list=True) 103 self.network = network 104 self.params = ParameterTuple(network.trainable_params()) 105 106 def construct(self, x, dy): 107 grad_op = self.grad(self.network, self.params) 108 output = grad_op(x, dy) 109 return output 110 111 112def test_conv3d_grad(): 113 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 114 dtype = np.float32 115 out_c = 2 116 kernel_size = (2, 2, 2) 117 x = Tensor(np.array([[[[[1.6924546, 0.05080776, -0.6369957], 118 [0.19091548, 2.1002553, 0.12015896], 119 [0.6172031, 0.30017033, -0.35224986]], 120 [[-1.1425182, -0.34934273, -0.20889424], 121 [0.5866232, 0.8389834, 0.9311021], 122 [0.2855873, 0.8851412, -0.7543979]], 123 [[1.2528682, 0.5129298, -0.29809284], 124 [0.48851815, -0.07557172, 1.1316293], 125 [1.5198169, 2.1855755, -1.3964963]]]]]).astype(dtype)) 126 dy = Tensor(np.array([[[[[-1.4441139, -0.5044659], 127 [0.16003707, 0.8761689]], 128 [[0.31563494, -2.0222013], 129 [-0.30620402, 0.8279746]]], 130 [[[0.23009473, 0.7620112], 131 [-0.22232814, -0.20075807]], 132 [[0.18656139, 0.41005164], 133 [0.19829972, 0.11900865]]]]]).astype(dtype)) 134 w = Tensor(np.array([[[[[-0.9358, -0.2679], 135 [0.5304, -0.6917]], 136 [[-0.3968, -0.6872], 137 [-0.8452, -0.6712]]]], 138 [[[[-0.0127, -1.1173], 139 [0.2344, 1.6598]], 140 [[0.7420, -0.1918], 141 [-0.8876, -0.7472]]]]]).astype(dtype)) 142 w_exp = np.array([[[[[-0.9384, -0.2830], 143 [0.5487, -0.6330]], 144 [[-0.4148, -0.7200], 145 [-0.8572, -0.6079]]]], 146 [[[[-0.0109, -1.1089], 147 [0.2138, 1.6478]], 148 [[0.7450, -0.1866], 149 [-0.8992, -0.7629]]]]]).astype(dtype) 150 net = MSConv3dNet(x.shape[1], out_c, kernel_size, weight_init=w) 151 grad_net = MSGradNet(net) 152 optimizer = nn.SGD(net.trainable_params(), learning_rate=0.01, momentum=0.9) 153 grad_net.set_train(True) 154 output = grad_net(x, dy) 155 optimizer(output[1]) 156 assert np.allclose(net.cv1.weight.asnumpy(), w_exp, atol=1.0e-4) 157