test_label_smooth_op.py 5.6 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Y
Yibing Liu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
16

Y
Yibing Liu 已提交
17
import numpy as np
18
from op_test import OpTest, convert_float_to_uint16
19

H
hong 已提交
20
import paddle
21
from paddle.fluid import core
Y
Yibing Liu 已提交
22 23 24


class TestLabelSmoothOp(OpTest):
25
    def config(self):
Y
Yibing Liu 已提交
26
        self.op_type = "label_smooth"
H
hong 已提交
27
        self.python_api = paddle.nn.functional.label_smooth
28
        self.init_dtype()
29
        self.epsilon = 0.1
30
        batch_size, self.label_dim = 10, 12
31
        self.label = np.zeros((batch_size, self.label_dim)).astype(self.dtype)
32 33 34 35 36
        nonzero_index = np.random.randint(self.label_dim, size=(batch_size))
        self.label[np.arange(batch_size), nonzero_index] = 1

    def setUp(self):
        self.config()
37
        smoothed_label = (
38 39
            1 - self.epsilon
        ) * self.label + self.epsilon / self.label_dim
40 41
        self.inputs = {'X': self.label}
        self.attrs = {'epsilon': self.epsilon}
Y
Yibing Liu 已提交
42 43
        self.outputs = {'Out': smoothed_label}

44 45 46
    def init_dtype(self):
        self.dtype = np.float64

Y
Yibing Liu 已提交
47
    def test_check_output(self):
W
wanghuancoder 已提交
48
        self.check_output()
Y
Yibing Liu 已提交
49 50

    def test_check_grad(self):
W
wanghuancoder 已提交
51
        self.check_grad(["X"], "Out")
Y
Yibing Liu 已提交
52 53


54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
@unittest.skipIf(
    not core.is_compiled_with_cuda() or not core.supports_bfloat16(),
    "core is not compiled with CUDA or place do not support bfloat16",
)
class TestLabelSmoothOpBF16(OpTest):
    def config(self):
        self.op_type = "label_smooth"
        self.python_api = paddle.nn.functional.label_smooth
        self.epsilon = 0.1
        self.dtype = np.uint16
        batch_size, self.label_dim = 10, 12
        self.label = np.zeros((batch_size, self.label_dim)).astype(np.float32)
        nonzero_index = np.random.randint(self.label_dim, size=(batch_size))
        self.label[np.arange(batch_size), nonzero_index] = 1

    def setUp(self):
        self.config()
        smoothed_label = (
            1 - self.epsilon
        ) * self.label + self.epsilon / self.label_dim
        self.inputs = {'X': convert_float_to_uint16(self.label)}
        self.attrs = {'epsilon': self.epsilon}
        self.outputs = {'Out': convert_float_to_uint16(smoothed_label)}

    def test_check_output(self):
        place = core.CUDAPlace(0)
        self.check_output_with_place(place, check_eager=True)

    def test_check_grad(self):
        place = core.CUDAPlace(0)
        self.check_grad_with_place(place, ["X"], "Out", check_eager=True)


87 88 89 90 91
class TestLabelSmoothFP16OP(TestLabelSmoothOp):
    def init_dtype(self):
        self.dtype = np.float16


92 93 94
class TestLabelSmoothOpWithPriorDist(TestLabelSmoothOp):
    def setUp(self):
        self.config()
95
        dist = np.random.random((1, self.label_dim)).astype(self.dtype)
96 97 98 99 100 101
        smoothed_label = (1 - self.epsilon) * self.label + self.epsilon * dist
        self.inputs = {'X': self.label, 'PriorDist': dist}
        self.attrs = {'epsilon': self.epsilon}
        self.outputs = {'Out': smoothed_label}


102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
class TestLabelSmoothFP16OPWithPriorDist(TestLabelSmoothOpWithPriorDist):
    def init_dtype(self):
        self.dtype = np.float16


class TestLabelSmoothBF16OPWithPriorDist(TestLabelSmoothOpBF16):
    def setUp(self):
        self.config()
        dist = np.random.random((1, self.label_dim)).astype(np.float32)
        smoothed_label = (1 - self.epsilon) * self.label + self.epsilon * dist
        self.inputs = {
            'X': convert_float_to_uint16(self.label),
            'PriorDist': convert_float_to_uint16(dist),
        }
        self.attrs = {'epsilon': self.epsilon}
        self.outputs = {'Out': convert_float_to_uint16(smoothed_label)}


120 121
class TestLabelSmoothOp3D(TestLabelSmoothOp):
    def setUp(self):
122
        super().setUp()
123
        self.inputs['X'] = self.inputs['X'].reshape(
124 125
            [2, -1, self.inputs['X'].shape[-1]]
        )
126
        self.outputs['Out'] = self.outputs['Out'].reshape(
127 128
            self.inputs['X'].shape
        )
129 130


131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
class TestLabelSmoothOp3DBF16(TestLabelSmoothOpBF16):
    def setUp(self):
        super().setUp()
        self.inputs['X'] = self.inputs['X'].reshape(
            [2, -1, self.inputs['X'].shape[-1]]
        )
        self.outputs['Out'] = self.outputs['Out'].reshape(
            self.inputs['X'].shape
        )


class TestLabelSmoothFP16OP3D(TestLabelSmoothOp3D):
    def init_dtype(self):
        self.dtype = np.float16


147 148
class TestLabelSmoothOpWithPriorDist3D(TestLabelSmoothOpWithPriorDist):
    def setUp(self):
149
        super().setUp()
150
        self.inputs['X'] = self.inputs['X'].reshape(
151 152
            [2, -1, self.inputs['X'].shape[-1]]
        )
153
        self.outputs['Out'] = self.outputs['Out'].reshape(
154 155
            self.inputs['X'].shape
        )
156 157


158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
class TestLabelSmoothFP16OPWithPriorDist3D(TestLabelSmoothOpWithPriorDist3D):
    def init_dtype(self):
        self.dtype = np.float16


class TestLabelSmoothBF16OpWithPriorDist3D(TestLabelSmoothBF16OPWithPriorDist):
    def setUp(self):
        super().setUp()
        self.inputs['X'] = self.inputs['X'].reshape(
            [2, -1, self.inputs['X'].shape[-1]]
        )
        self.outputs['Out'] = self.outputs['Out'].reshape(
            self.inputs['X'].shape
        )


Y
Yibing Liu 已提交
174
if __name__ == '__main__':
H
hong 已提交
175
    paddle.enable_static()
Y
Yibing Liu 已提交
176
    unittest.main()