test_logspace.py 9.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2022 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 unittest
16

17
import numpy as np
C
chenxujun 已提交
18
from eager_op_test import OpTest, convert_float_to_uint16
19

20
import paddle
C
chenxujun 已提交
21
from paddle.fluid import core
22 23 24 25 26


class TestLogspaceOpCommonCase(OpTest):
    def setUp(self):
        self.op_type = "logspace"
27
        self.python_api = paddle.logspace
C
Chen Weihang 已提交
28 29 30
        self.init_data()

    def init_data(self):
31 32 33 34 35 36 37 38 39 40 41 42 43 44
        dtype = 'float32'
        self.inputs = {
            'Start': np.array([0]).astype(dtype),
            'Stop': np.array([10]).astype(dtype),
            'Num': np.array([11]).astype('int32'),
            'Base': np.array([2]).astype(dtype),
        }
        self.attrs = {'dtype': int(paddle.float32)}
        self.outputs = {'Out': np.power(2, np.arange(0, 11)).astype(dtype)}

    def test_check_output(self):
        self.check_output()


C
chenxujun 已提交
45 46 47 48 49 50 51 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 87 88 89 90 91 92
class TestLogspaceFP16Op(TestLogspaceOpCommonCase):
    def init_data(self):
        self.dtype = np.float16
        self.inputs = {
            'Start': np.array([0]).astype(self.dtype),
            'Stop': np.array([10]).astype(self.dtype),
            'Num': np.array([11]).astype('int32'),
            'Base': np.array([2]).astype(self.dtype),
        }
        self.attrs = {'dtype': int(paddle.float16)}
        self.outputs = {'Out': np.power(2, np.arange(0, 11)).astype(self.dtype)}


@unittest.skipIf(
    not core.is_compiled_with_cuda()
    or not core.is_bfloat16_supported(core.CUDAPlace(0)),
    "core is not compiled with CUDA or not support bfloat16",
)
class TestLogspaceBF16Op(OpTest):
    def setUp(self):
        self.op_type = "logspace"
        self.python_api = paddle.logspace
        self.init_data()

    def init_data(self):
        self.dtype = np.uint16
        self.np_dtype = np.float32
        self.inputs = {
            'Start': np.array([0]).astype(self.np_dtype),
            'Stop': np.array([10]).astype(self.np_dtype),
            'Num': np.array([11]).astype('int32'),
            'Base': np.array([2]).astype(self.np_dtype),
        }
        self.attrs = {'dtype': int(paddle.bfloat16)}
        self.outputs = {
            'Out': np.power(2, np.arange(0, 11)).astype(self.np_dtype)
        }

        self.inputs["Start"] = convert_float_to_uint16(self.inputs["Start"])
        self.inputs["Stop"] = convert_float_to_uint16(self.inputs["Stop"])
        self.inputs["Base"] = convert_float_to_uint16(self.inputs["Base"])
        self.outputs["Out"] = convert_float_to_uint16(self.outputs["Out"])
        self.place = core.CUDAPlace(0)

    def test_check_output(self):
        self.check_output_with_place(self.place)


C
Chen Weihang 已提交
93 94
class TestLogspaceOpReverseCase(TestLogspaceOpCommonCase):
    def init_data(self):
95 96 97 98 99
        dtype = 'float32'
        self.inputs = {
            'Start': np.array([10]).astype(dtype),
            'Stop': np.array([0]).astype(dtype),
            'Num': np.array([11]).astype('int32'),
100
            'Base': np.array([2]).astype(dtype),
101 102 103 104 105
        }
        self.attrs = {'dtype': int(paddle.float32)}
        self.outputs = {'Out': np.power(2, np.arange(10, -1, -1)).astype(dtype)}


C
Chen Weihang 已提交
106 107
class TestLogspaceOpNumOneCase(TestLogspaceOpCommonCase):
    def init_data(self):
108 109 110 111 112
        dtype = 'float32'
        self.inputs = {
            'Start': np.array([10]).astype(dtype),
            'Stop': np.array([0]).astype(dtype),
            'Num': np.array([1]).astype('int32'),
113
            'Base': np.array([2]).astype(dtype),
114 115
        }
        self.attrs = {'dtype': int(paddle.float32)}
116
        self.outputs = {'Out': np.power(2, np.array([10])).astype(dtype)}
117 118


C
Chen Weihang 已提交
119 120
class TestLogspaceOpMinusBaseCase(TestLogspaceOpCommonCase):
    def init_data(self):
121 122 123 124 125 126 127 128 129 130 131
        dtype = 'float32'
        self.inputs = {
            'Start': np.array([0]).astype(dtype),
            'Stop': np.array([10]).astype(dtype),
            'Num': np.array([11]).astype('int32'),
            'Base': np.array([-2]).astype(dtype),
        }
        self.attrs = {'dtype': int(paddle.float32)}
        self.outputs = {'Out': np.power(-2, np.arange(0, 11)).astype(dtype)}


C
Chen Weihang 已提交
132 133
class TestLogspaceOpZeroBaseCase(TestLogspaceOpCommonCase):
    def init_data(self):
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        dtype = 'float32'
        self.inputs = {
            'Start': np.array([0]).astype(dtype),
            'Stop': np.array([10]).astype(dtype),
            'Num': np.array([11]).astype('int32'),
            'Base': np.array([0]).astype(dtype),
        }
        self.attrs = {'dtype': int(paddle.float32)}
        self.outputs = {'Out': np.power(0, np.arange(0, 11)).astype(dtype)}


class TestLogspaceAPI(unittest.TestCase):
    def test_variable_input1(self):
        paddle.enable_static()
        prog = paddle.static.Program()
        with paddle.static.program_guard(prog):
            start = paddle.full(shape=[1], fill_value=0, dtype='float32')
            stop = paddle.full(shape=[1], fill_value=10, dtype='float32')
            num = paddle.full(shape=[1], fill_value=5, dtype='int32')
            base = paddle.full(shape=[1], fill_value=2, dtype='float32')
            out = paddle.logspace(start, stop, num, base, dtype='float32')

        exe = paddle.static.Executor()
        res = exe.run(prog, fetch_list=[out])
        np_res = np.logspace(0, 10, 5, base=2, dtype='float32')
        self.assertEqual((res == np_res).all(), True)
        paddle.disable_static()

    def test_variable_input2(self):
        paddle.disable_static()
        start = paddle.full(shape=[1], fill_value=0, dtype='float32')
        stop = paddle.full(shape=[1], fill_value=10, dtype='float32')
        num = paddle.full(shape=[1], fill_value=5, dtype='int32')
        base = paddle.full(shape=[1], fill_value=2, dtype='float32')
        out = paddle.logspace(start, stop, num, base, dtype='float32')
        np_res = np.logspace(0, 10, 5, base=2, dtype='float32')
        self.assertEqual((out.numpy() == np_res).all(), True)
        paddle.enable_static()

    def test_dtype(self):
        paddle.enable_static()
        prog = paddle.static.Program()
        with paddle.static.program_guard(prog):
            out_1 = paddle.logspace(0, 10, 5, 2, dtype='float32')
            out_2 = paddle.logspace(0, 10, 5, 2, dtype=np.float32)

        exe = paddle.static.Executor()
        res_1, res_2 = exe.run(prog, fetch_list=[out_1, out_2])
        assert np.array_equal(res_1, res_2)
        paddle.disable_static()

    def test_name(self):
        with paddle.static.program_guard(paddle.static.Program()):
187 188 189
            out = paddle.logspace(
                0, 10, 5, 2, dtype='float32', name='logspace_res'
            )
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
            assert 'logspace_res' in out.name

    def test_imperative(self):
        paddle.disable_static()
        out1 = paddle.logspace(0, 10, 5, 2, dtype='float32')
        np_out1 = np.logspace(0, 10, 5, base=2, dtype='float32')
        out2 = paddle.logspace(0, 10, 5, 2, dtype='int32')
        np_out2 = np.logspace(0, 10, 5, base=2, dtype='int32')
        out3 = paddle.logspace(0, 10, 200, 2, dtype='int32')
        np_out3 = np.logspace(0, 10, 200, base=2, dtype='int32')
        paddle.enable_static()
        self.assertEqual((out1.numpy() == np_out1).all(), True)
        self.assertEqual((out2.numpy() == np_out2).all(), True)
        self.assertEqual((out3.numpy() == np_out3).all(), True)


class TestLogspaceOpError(unittest.TestCase):
    def test_errors(self):
        with paddle.static.program_guard(paddle.static.Program()):

            def test_dtype():
                paddle.logspace(0, 10, 1, 2, dtype="int8")

            self.assertRaises(TypeError, test_dtype)

            def test_dtype1():
                paddle.logspace(0, 10, 1.33, 2, dtype="int32")

            self.assertRaises(TypeError, test_dtype1)

            def test_start_type():
                paddle.logspace([0], 10, 1, 2, dtype="float32")

            self.assertRaises(TypeError, test_start_type)

            def test_end_type():
                paddle.logspace(0, [10], 1, 2, dtype="float32")

            self.assertRaises(TypeError, test_end_type)

            def test_num_type():
                paddle.logspace(0, 10, [0], 2, dtype="float32")

            self.assertRaises(TypeError, test_num_type)

            def test_start_dtype():
236 237 238
                start = paddle.static.data(
                    shape=[1], dtype="float64", name="start"
                )
239 240 241 242 243 244 245 246 247 248 249
                paddle.logspace(start, 10, 1, 2, dtype="float32")

            self.assertRaises(ValueError, test_start_dtype)

            def test_end_dtype():
                end = paddle.static.data(shape=[1], dtype="float64", name="end")
                paddle.logspace(0, end, 1, 2, dtype="float32")

            self.assertRaises(ValueError, test_end_dtype)

            def test_num_dtype():
250 251 252
                num = paddle.static.data(
                    shape=[1], dtype="float32", name="step"
                )
253 254 255 256 257
                paddle.logspace(0, 10, num, 2, dtype="float32")

            self.assertRaises(TypeError, test_num_dtype)

            def test_base_dtype():
258 259 260
                base = paddle.static.data(
                    shape=[1], dtype="float64", name="end"
                )
261 262 263 264 265 266 267
                paddle.logspace(0, 10, 1, base, dtype="float32")

            self.assertRaises(ValueError, test_base_dtype)


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