test_logsumexp.py 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#  Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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 paddle
import unittest
import numpy as np
X
xiaohemaikoo 已提交
18
import paddle.fluid.core as core
19 20 21
from op_test import OpTest


22 23
def ref_logsumexp(x, axis=None, keepdim=False, reduce_all=False):
    if isinstance(axis, int):
24
        axis = (axis,)
25 26 27 28 29 30 31 32
    elif isinstance(axis, list):
        axis = tuple(axis)
    if reduce_all:
        axis = None
    out = np.log(np.exp(x).sum(axis=axis, keepdims=keepdim))
    return out


33 34 35 36 37 38
def logsumexp_wrapper(x, axis=None, keepdim=False, allreduce=False):
    if allreduce:
        return paddle.logsumexp(x, None, keepdim)
    return paddle.logsumexp(x, axis, keepdim)


X
xiaohemaikoo 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
def logsumexp_op_grad(x, axis=None, keepdim=False, reduce_all=False):
    paddle.disable_static()
    tensor_x = paddle.to_tensor(x)
    tensor_x.stop_gradient = False
    out = logsumexp_wrapper(tensor_x, axis, keepdim, reduce_all)
    grad = paddle.grad(out, [tensor_x])
    x_grad = grad[0].numpy()
    paddle.enable_static()
    return x_grad


def logsumexp_ref_grad(x):
    sum = np.exp(x).sum()
    return np.exp(x) / sum


55 56 57
class TestLogsumexp(OpTest):
    def setUp(self):
        self.op_type = 'logsumexp'
58
        self.python_api = logsumexp_wrapper
59 60 61 62 63 64 65 66 67 68 69 70 71 72
        self.shape = [2, 3, 4, 5]
        self.dtype = 'float64'
        self.axis = [-1]
        self.keepdim = False
        self.reduce_all = False
        self.set_attrs()

        np.random.seed(10)
        x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
        out = ref_logsumexp(x, self.axis, self.keepdim, self.reduce_all)

        self.inputs = {'X': x}
        self.outputs = {'Out': out}
        self.attrs = {
73 74
            'axis': self.axis,
            'keepdim': self.keepdim,
75
            'reduce_all': self.reduce_all,
76
        }
77 78 79
        self.user_defined_grads = None
        self.user_defined_grad_outputs = None
        self.set_attrs_addition()
80 81 82 83

    def set_attrs(self):
        pass

84 85 86
    def set_attrs_addition(self):
        pass

87
    def test_check_output(self):
88
        self.check_output(check_eager=True)
89 90

    def test_check_grad(self):
91
        self.check_grad(
92 93
            ['X'],
            ['Out'],
94
            user_defined_grads=self.user_defined_grads,
95
            user_defined_grad_outputs=self.user_defined_grad_outputs,
96 97
            check_eager=True,
        )
98 99 100 101 102 103

    def calc_grad(self):
        dy = np.ones(1, dtype=self.dtype)
        x = self.inputs['X']
        y = self.outputs['Out']
        return dy * np.exp(x - y)
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119


class TestLogsumexp_shape(TestLogsumexp):
    def set_attrs(self):
        self.shape = [4, 5, 6]


class TestLogsumexp_axis(TestLogsumexp):
    def set_attrs(self):
        self.axis = [0, -1]


class TestLogsumexp_axis_all(TestLogsumexp):
    def set_attrs(self):
        self.axis = [0, 1, 2, 3]

120 121 122 123 124
    def set_attrs_addition(self):
        if paddle.fluid.core.is_compiled_with_rocm():
            self.user_defined_grads = [self.calc_grad()]
            self.user_defined_grad_outputs = [np.ones(1, dtype=self.dtype)]

125 126 127 128 129 130 131 132 133 134

class TestLogsumexp_keepdim(TestLogsumexp):
    def set_attrs(self):
        self.keepdim = True


class TestLogsumexp_reduce_all(TestLogsumexp):
    def set_attrs(self):
        self.reduce_all = True

135 136 137 138 139
    def set_attrs_addition(self):
        if paddle.fluid.core.is_compiled_with_rocm():
            self.user_defined_grads = [self.calc_grad()]
            self.user_defined_grad_outputs = [np.ones(1, dtype=self.dtype)]

140

X
xiaohemaikoo 已提交
141 142 143 144 145 146 147 148 149 150 151
class TestLogsumexp_FP32(TestLogsumexp):
    def set_attrs(self):
        self.dtype = 'float32'

    def test_check_grad(self):
        self.__class__.dtype = self.dtype
        x_grad = logsumexp_op_grad(self.inputs['X'])
        ref_x_grad = logsumexp_ref_grad(self.inputs['X'])
        np.testing.assert_allclose(x_grad, ref_x_grad, rtol=1e-08, atol=1e-08)


152 153 154
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
X
xiaohemaikoo 已提交
155 156 157 158 159 160 161 162 163 164 165 166
class TestLogsumexp_FP16(TestLogsumexp):
    def set_attrs(self):
        self.dtype = 'float16'

    def test_check_output(self):
        ref_x = self.inputs['X'].astype(np.float32)
        out_ref = ref_logsumexp(ref_x)
        paddle.disable_static()
        x = self.inputs['X'].astype(np.float16)
        tensor_x = paddle.to_tensor(x)
        out_pad = logsumexp_wrapper(tensor_x)
        paddle.enable_static()
167 168 169
        np.testing.assert_allclose(
            out_pad.numpy(), out_ref, rtol=1e-03, atol=1e-08
        )
X
xiaohemaikoo 已提交
170 171 172 173 174 175 176 177 178 179

    def test_check_grad(self):
        self.__class__.dtype = self.dtype
        ref_x = self.inputs['X'].astype(np.float32)
        ref_x_grad = logsumexp_ref_grad(ref_x)
        x = self.inputs['X'].astype(np.float16)
        x_grad = logsumexp_op_grad(x)
        np.testing.assert_allclose(x_grad, ref_x_grad, rtol=1e-03, atol=1e-05)


180
class TestLogsumexpError(unittest.TestCase):
181
    def test_errors(self):
182 183
        with paddle.static.program_guard(paddle.static.Program()):
            self.assertRaises(TypeError, paddle.logsumexp, 1)
184
            x1 = paddle.fluid.data(name='x1', shape=[120], dtype="int32")
185 186 187 188 189 190 191
            self.assertRaises(TypeError, paddle.logsumexp, x1)


class TestLogsumexpAPI(unittest.TestCase):
    def setUp(self):
        self.shape = [2, 3, 4, 5]
        self.x = np.random.uniform(-1, 1, self.shape).astype(np.float32)
192 193 194
        self.place = (
            paddle.CUDAPlace(0)
            if paddle.fluid.core.is_compiled_with_cuda()
195
            else paddle.CPUPlace()
196
        )
197 198 199 200

    def api_case(self, axis=None, keepdim=False):
        out_ref = ref_logsumexp(self.x, axis, keepdim)
        with paddle.static.program_guard(paddle.static.Program()):
201
            x = paddle.fluid.data('X', self.shape)
202 203 204
            out = paddle.logsumexp(x, axis, keepdim)
            exe = paddle.static.Executor(self.place)
            res = exe.run(feed={'X': self.x}, fetch_list=[out])
205
        np.testing.assert_allclose(res[0], out_ref, rtol=1e-05)
206 207

        paddle.disable_static(self.place)
Z
Zhou Wei 已提交
208
        x = paddle.to_tensor(self.x)
209
        out = paddle.logsumexp(x, axis, keepdim)
210
        np.testing.assert_allclose(out.numpy(), out_ref, rtol=1e-05)
211 212 213 214 215 216 217 218 219 220 221 222
        paddle.enable_static()

    def test_api(self):
        self.api_case()
        self.api_case(2)
        self.api_case([-1])
        self.api_case([2, -3])
        self.api_case((0, 1, -1))
        self.api_case(keepdim=True)

    def test_alias(self):
        paddle.disable_static(self.place)
Z
Zhou Wei 已提交
223
        x = paddle.to_tensor(self.x)
224 225 226 227 228
        out1 = paddle.logsumexp(x)
        out2 = paddle.tensor.logsumexp(x)
        out3 = paddle.tensor.math.logsumexp(x)
        out_ref = ref_logsumexp(self.x)
        for out in [out1, out2, out3]:
229
            np.testing.assert_allclose(out.numpy(), out_ref, rtol=1e-05)
230
        paddle.enable_static()
231 232 233 234


if __name__ == '__main__':
    unittest.main()