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 19from mindspore import Tensor 20from mindspore.ops import operations as P 21from mindspore.ops.composite import GradOperation 22 23context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") 24 25dtype = np.float16 26x0 = Tensor(np.random.randn(3, 4, 3, 3, 3).astype(dtype)) 27x1 = Tensor(np.random.randn(3, 4, 3, 3, 3).astype(dtype)) 28 29 30class Net(nn.Cell): 31 def __init__(self, keep_prob): 32 super(Net, self).__init__() 33 self.drop = P.Dropout3D(keep_prob=keep_prob) 34 35 def construct(self, x): 36 return self.drop(x) 37 38 39class Grad(nn.Cell): 40 def __init__(self, network): 41 super(Grad, self).__init__() 42 self.grad = GradOperation(get_all=True, sens_param=True) 43 self.network = network 44 self.network.set_train() 45 46 def construct(self, x, y): 47 return self.grad(self.network)(x, y) 48 49 50@pytest.mark.level0 51@pytest.mark.env_onecard 52@pytest.mark.platform_arm_ascend_training 53@pytest.mark.platform_x86_ascend_training 54@pytest.mark.parametrize("context_mode", [context.GRAPH_MODE, context.PYNATIVE_MODE]) 55def test_net_float32(context_mode): 56 """ 57 Feature: aicpu ops Dropout3D. 58 Description: test Dropout3D forward. 59 Expectation: expect correct result. 60 """ 61 context.set_context(mode=context_mode, device_target="Ascend") 62 net = Net(0.7) 63 output, mask = net(x0) 64 print(x0) 65 print(output) 66 67 y = (output.asnumpy() == (x0.asnumpy()/0.7).astype(dtype)).reshape(3*4, 3*3*3) 68 output_reshape = output.asnumpy().reshape(3*4, 3*3*3) 69 for i in range(3*4): 70 if not y[i].all(): 71 assert output_reshape[i].sum() == 0 72 return output, mask 73 74 75@pytest.mark.level0 76@pytest.mark.env_onecard 77@pytest.mark.platform_arm_ascend_training 78@pytest.mark.platform_x86_ascend_training 79@pytest.mark.parametrize("context_mode", [context.GRAPH_MODE, context.PYNATIVE_MODE]) 80def test_net_grad(context_mode): 81 """ 82 Feature: aicpu ops Dropout3D. 83 Description: test Dropout3D backward. 84 Expectation: expect correct result. 85 """ 86 net = Grad(Net(0.7)) 87 y = test_net_float32(context_mode) 88 output = net(x1, y) 89 print("input: ", x1) 90 print("forward output: ", y) 91 print("backward output: ", output) 92