• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020-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
22from mindspore.ops.operations import _inner_ops as inner
23
24class Net(nn.Cell):
25    def __init__(self, keep_prob):
26        super(Net, self).__init__()
27        self.drop = P.Dropout(keep_prob)
28
29    def construct(self, x_):
30        return self.drop(x_)
31
32
33@pytest.mark.level0
34@pytest.mark.platform_x86_gpu_training
35@pytest.mark.env_onecard
36def test_dropout():
37    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
38    x_shape = [32, 16, 2, 5]
39    x = np.ones(x_shape).astype(np.float32)
40    keep_prob = 0.4
41    dropout = Net(keep_prob)
42    tx = Tensor(x)
43    output, mask = dropout(tx)
44    # check output
45    output_np = output.asnumpy()
46    elem_count = x.size
47    nonzero_count = np.count_nonzero(output_np)
48    assert (elem_count * (keep_prob - 0.1)) < nonzero_count < (elem_count * (keep_prob + 0.1))
49    output_sum = np.sum(output_np)
50    x_sum = np.sum(x)
51    assert abs(output_sum - x_sum)/x_sum < 0.1
52    # check mask
53    mask_np = mask.asnumpy()
54    mask_sum = np.sum(mask_np)
55    assert np.count_nonzero(mask_np) == nonzero_count
56    assert abs(mask_sum - nonzero_count)/nonzero_count < 0.1
57
58
59class DropoutDynamic(nn.Cell):
60    def __init__(self, keep_prob):
61        super(DropoutDynamic, self).__init__()
62        self.test_dynamic = inner.GpuConvertToDynamicShape()
63        self.drop = P.Dropout(keep_prob)
64
65    def construct(self, x):
66        x = self.test_dynamic(x)
67        return self.drop(x)
68
69
70@pytest.mark.level0
71@pytest.mark.platform_x86_gpu_training
72@pytest.mark.env_onecard
73def test_dropout_dynamic():
74    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
75    x_1 = np.ones([32, 16, 2, 5]).astype(np.float32)
76    x_2 = np.ones([32, 16, 2, 5, 6]).astype(np.float32)
77    keep_prob = 0.4
78    net = DropoutDynamic(keep_prob)
79
80    output_1, mask_1 = net(Tensor(x_1))
81    elem_count_1 = x_1.size
82    nonzero_count_1 = np.count_nonzero(output_1.asnumpy())
83    assert (elem_count_1 * (keep_prob - 0.1)) < nonzero_count_1 < (elem_count_1 * (keep_prob + 0.1))
84    output_sum_1 = np.sum(output_1.asnumpy())
85    x_sum_1 = np.sum(x_1)
86    assert abs(output_sum_1 - x_sum_1)/x_sum_1 < 0.1
87    mask_sum_1 = np.sum(mask_1.asnumpy())
88    assert np.count_nonzero(mask_1.asnumpy()) == nonzero_count_1
89    assert abs(mask_sum_1 - nonzero_count_1)/nonzero_count_1 < 0.1
90
91    output_2, mask_2 = net(Tensor(x_2))
92    elem_count_2 = x_2.size
93    nonzero_count_2 = np.count_nonzero(output_2.asnumpy())
94    assert (elem_count_2 * (keep_prob - 0.1)) < nonzero_count_2 < (elem_count_2 * (keep_prob + 0.1))
95    output_sum_2 = np.sum(output_2.asnumpy())
96    x_sum_2 = np.sum(x_2)
97    assert abs(output_sum_2 - x_sum_2)/x_sum_2 < 0.1
98    mask_sum_2 = np.sum(mask_2.asnumpy())
99    assert np.count_nonzero(mask_2.asnumpy()) == nonzero_count_2
100    assert abs(mask_sum_2 - nonzero_count_2)/nonzero_count_2 < 0.1
101