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 17 18import mindspore.nn as nn 19from mindspore import Tensor 20import mindspore.context as context 21from mindspore.ops import operations as P 22 23class Dropout3DNet(nn.Cell): 24 def __init__(self, keep_prob): 25 super(Dropout3DNet, self).__init__() 26 self.drop = P.Dropout3D(keep_prob) 27 28 def construct(self, x): 29 return self.drop(x) 30 31 32def dropout_3d(keep_prob, nptype): 33 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 34 35 x_shape = [32, 16, 2, 5, 4] 36 x_np = np.ones(x_shape).astype(nptype) 37 dropout3d_net = Dropout3DNet(keep_prob) 38 tx = Tensor(x_np) 39 output, mask = dropout3d_net(tx) 40 41 ## check output ## 42 output_np = output.asnumpy() 43 elem_count = x_np.size 44 nonzero_count = np.count_nonzero(output_np) 45 # assert correct proportion of elements kept 46 assert (elem_count * (keep_prob - 0.1)) < nonzero_count < (elem_count * (keep_prob + 0.1)) 47 output_sum = np.sum(output_np) 48 x_sum = np.sum(x_np) 49 if keep_prob != 0.0: 50 # assert output scaled correctly (expected value maintained) 51 assert abs(output_sum - x_sum)/x_sum < 0.1 52 53 ## check mask ## 54 mask_np = mask.asnumpy() 55 # specific to input with no zeros. Check for same number of nonzero elements 56 assert np.count_nonzero(mask_np) == nonzero_count 57 # check each channel is entirely True or False 58 non_eq_chan = 0 59 for n in range(mask_np.shape[0]): 60 for c in range(mask_np.shape[1]): 61 if not np.all(mask_np[n][c] == mask_np[n][c][0]): 62 non_eq_chan = non_eq_chan + 1 63 assert non_eq_chan == 0 64 65 # check input, output, mask all have same shape 66 assert x_np.shape == output_np.shape == mask_np.shape 67 68 69@pytest.mark.level0 70@pytest.mark.platform_x86_gpu_training 71@pytest.mark.env_onecard 72def test_dropout3d_float16(): 73 dropout_3d(0.0, np.float16) 74 dropout_3d(1.0, np.float16) 75 76@pytest.mark.level0 77@pytest.mark.platform_x86_gpu_training 78@pytest.mark.env_onecard 79def test_dropout3d_float32(): 80 dropout_3d(0.0, np.float32) 81 dropout_3d(1.0, np.float32) 82 83@pytest.mark.level0 84@pytest.mark.platform_x86_gpu_training 85@pytest.mark.env_onecard 86def test_dropout3d_int8(): 87 dropout_3d(0.0, np.int8) 88 dropout_3d(1.0, np.int8) 89 90@pytest.mark.level0 91@pytest.mark.platform_x86_gpu_training 92@pytest.mark.env_onecard 93def test_dropout3d_int16(): 94 dropout_3d(0.0, np.int16) 95 dropout_3d(1.0, np.int16) 96 97@pytest.mark.level0 98@pytest.mark.platform_x86_gpu_training 99@pytest.mark.env_onecard 100def test_dropout3d_int32(): 101 dropout_3d(0.0, np.int32) 102 dropout_3d(1.0, np.int32) 103 104@pytest.mark.level0 105@pytest.mark.platform_x86_gpu_training 106@pytest.mark.env_onecard 107def test_dropout3d_int64(): 108 dropout_3d(0.0, np.int64) 109 dropout_3d(1.0, np.int64) 110