test_linspace.py 8.8 KB
Newer Older
Z
zhoukunsheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 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

Z
zhoukunsheng 已提交
17
import numpy as np
18
from eager_op_test import OpTest, convert_float_to_uint16, paddle_static_guard
19

20
import paddle
21
from paddle import fluid
22
from paddle.fluid import Program, core, program_guard
Z
zhoukunsheng 已提交
23 24 25 26 27


class TestLinspaceOpCommonCase(OpTest):
    def setUp(self):
        self.op_type = "linspace"
28
        self.python_api = paddle.linspace
29 30 31 32 33 34 35 36 37 38
        self._set_dtype()
        self._set_data()
        self.attrs = {'dtype': self.attr_dtype}

    def _set_dtype(self):
        self.dtype = "float32"
        self.attr_dtype = int(core.VarDesc.VarType.FP32)

    def _set_data(self):
        self.outputs = {'Out': np.arange(0, 11).astype(self.dtype)}
Z
zhoukunsheng 已提交
39
        self.inputs = {
40 41
            'Start': np.array([0]).astype(self.dtype),
            'Stop': np.array([10]).astype(self.dtype),
42
            'Num': np.array([11]).astype('int32'),
Z
zhoukunsheng 已提交
43 44 45
        }

    def test_check_output(self):
W
wanghuancoder 已提交
46
        self.check_output()
Z
zhoukunsheng 已提交
47 48


49 50
class TestLinspaceOpReverseCase(TestLinspaceOpCommonCase):
    def _set_data(self):
Z
zhoukunsheng 已提交
51
        self.inputs = {
52 53
            'Start': np.array([10]).astype(self.dtype),
            'Stop': np.array([0]).astype(self.dtype),
54
            'Num': np.array([11]).astype('int32'),
Z
zhoukunsheng 已提交
55
        }
56
        self.outputs = {'Out': np.arange(10, -1, -1).astype(self.dtype)}
Z
zhoukunsheng 已提交
57 58

    def test_check_output(self):
W
wanghuancoder 已提交
59
        self.check_output()
Z
zhoukunsheng 已提交
60 61


62 63
class TestLinspaceOpNumOneCase(TestLinspaceOpCommonCase):
    def _set_data(self):
Z
zhoukunsheng 已提交
64
        self.inputs = {
65 66
            'Start': np.array([10]).astype(self.dtype),
            'Stop': np.array([0]).astype(self.dtype),
67
            'Num': np.array([1]).astype('int32'),
Z
zhoukunsheng 已提交
68
        }
69
        self.outputs = {'Out': np.array([10], dtype=self.dtype)}
Z
zhoukunsheng 已提交
70 71

    def test_check_output(self):
W
wanghuancoder 已提交
72
        self.check_output()
Z
zhoukunsheng 已提交
73 74


75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
class TestLinspaceOpCommonCaseFP16(TestLinspaceOpCommonCase):
    def _set_dtype(self):
        self.dtype = np.float16
        self.attr_dtype = int(core.VarDesc.VarType.FP16)


class TestLinspaceOpReverseCaseFP16(TestLinspaceOpReverseCase):
    def _set_dtype(self):
        self.dtype = np.float16
        self.attr_dtype = int(core.VarDesc.VarType.FP16)


class TestLinspaceOpNumOneCaseFP16(TestLinspaceOpNumOneCase):
    def _set_dtype(self):
        self.dtype = np.float16
        self.attr_dtype = int(core.VarDesc.VarType.FP16)


@unittest.skipIf(
    not core.is_compiled_with_cuda()
    or not core.is_bfloat16_supported(core.CUDAPlace(0)),
    'not supported bf16',
)
class TestLinspaceOpCommonCaseBF16(TestLinspaceOpCommonCaseFP16):
    def _set_dtype(self):
        self.dtype = np.uint16
        self.attr_dtype = int(core.VarDesc.VarType.BF16)

    def _set_data(self):
        self.outputs = {
            'Out': convert_float_to_uint16(np.arange(0, 11).astype("float32"))
        }
        self.inputs = {
            'Start': convert_float_to_uint16(np.array([0]).astype("float32")),
            'Stop': convert_float_to_uint16(np.array([10]).astype("float32")),
            'Num': np.array([11]).astype('int32'),
        }

    def test_check_output(self):
        return self.check_output_with_place(core.CUDAPlace(0))


class TestLinspaceOpReverseCaseBF16(TestLinspaceOpCommonCaseBF16):
    def _set_data(self):
        self.inputs = {
            'Start': convert_float_to_uint16(np.array([10]).astype("float32")),
            'Stop': convert_float_to_uint16(np.array([0]).astype("float32")),
            'Num': np.array([11]).astype('int32'),
        }
        self.outputs = {
            'Out': convert_float_to_uint16(
                np.arange(10, -1, -1).astype("float32")
            )
        }


class TestLinspaceOpNumOneCaseBF16(TestLinspaceOpCommonCaseBF16):
    def _set_data(self):
        self.inputs = {
            'Start': convert_float_to_uint16(np.array([10]).astype("float32")),
            'Stop': convert_float_to_uint16(np.array([0]).astype("float32")),
            'Num': np.array([1]).astype('int32'),
        }
        self.outputs = {
139
            'Out': convert_float_to_uint16(np.array([10], dtype="float32"))
140 141 142
        }


143
class TestLinspaceAPI(unittest.TestCase):
144
    def test_variable_input1(self):
W
wanghuancoder 已提交
145 146 147 148 149 150 151 152 153
        with paddle_static_guard():
            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')
            out = paddle.linspace(start, stop, num, dtype='float32')
            exe = fluid.Executor(place=fluid.CPUPlace())
            res = exe.run(fluid.default_main_program(), fetch_list=[out])
            np_res = np.linspace(0, 10, 5, dtype='float32')
            self.assertEqual((res == np_res).all(), True)
154 155 156 157 158 159 160 161 162

    def test_variable_input2(self):
        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')
        out = paddle.linspace(start, stop, num, dtype='float32')
        np_res = np.linspace(0, 10, 5, dtype='float32')
        self.assertEqual((out.numpy() == np_res).all(), True)

163
    def test_dtype(self):
W
wanghuancoder 已提交
164 165 166 167 168 169 170 171 172
        with paddle_static_guard():
            out_1 = paddle.linspace(0, 10, 5, dtype='float32')
            out_2 = paddle.linspace(0, 10, 5, dtype=np.float32)
            out_3 = paddle.linspace(0, 10, 5, dtype=core.VarDesc.VarType.FP32)
            exe = fluid.Executor(place=fluid.CPUPlace())
            res_1, res_2, res_3 = exe.run(
                fluid.default_main_program(), fetch_list=[out_1, out_2, out_3]
            )
            assert np.array_equal(res_1, res_2)
173 174

    def test_name(self):
W
wanghuancoder 已提交
175 176 177 178 179 180
        with paddle_static_guard():
            with paddle.static.program_guard(paddle.static.Program()):
                out = paddle.linspace(
                    0, 10, 5, dtype='float32', name='linspace_res'
                )
                assert 'linspace_res' in out.name
181

182
    def test_imperative(self):
183 184 185 186 187 188 189 190 191
        out1 = paddle.linspace(0, 10, 5, dtype='float32')
        np_out1 = np.linspace(0, 10, 5, dtype='float32')
        out2 = paddle.linspace(0, 10, 5, dtype='int32')
        np_out2 = np.linspace(0, 10, 5, dtype='int32')
        out3 = paddle.linspace(0, 10, 200, dtype='int32')
        np_out3 = np.linspace(0, 10, 200, dtype='int32')
        self.assertEqual((out1.numpy() == np_out1).all(), True)
        self.assertEqual((out2.numpy() == np_out2).all(), True)
        self.assertEqual((out3.numpy() == np_out3).all(), True)
192

193 194 195

class TestLinspaceOpError(unittest.TestCase):
    def test_errors(self):
W
wanghuancoder 已提交
196 197
        with paddle_static_guard():
            with program_guard(Program(), Program()):
198

W
wanghuancoder 已提交
199 200
                def test_dtype():
                    paddle.linspace(0, 10, 1, dtype="int8")
201

W
wanghuancoder 已提交
202
                self.assertRaises(TypeError, test_dtype)
203

W
wanghuancoder 已提交
204 205
                def test_dtype1():
                    paddle.linspace(0, 10, 1.33, dtype="int32")
206

W
wanghuancoder 已提交
207
                self.assertRaises(TypeError, test_dtype1)
208

W
wanghuancoder 已提交
209 210
                def test_start_type():
                    paddle.linspace([0], 10, 1, dtype="float32")
211

W
wanghuancoder 已提交
212
                self.assertRaises(TypeError, test_start_type)
213

W
wanghuancoder 已提交
214 215
                def test_end_type():
                    paddle.linspace(0, [10], 1, dtype="float32")
216

W
wanghuancoder 已提交
217
                self.assertRaises(TypeError, test_end_type)
218

W
wanghuancoder 已提交
219 220
                def test_step_dtype():
                    paddle.linspace(0, 10, [0], dtype="float32")
221

W
wanghuancoder 已提交
222
                self.assertRaises(TypeError, test_step_dtype)
223

W
wanghuancoder 已提交
224 225 226 227 228
                def test_start_dtype():
                    start = paddle.static.data(
                        shape=[1], dtype="float64", name="start"
                    )
                    paddle.linspace(start, 10, 1, dtype="float32")
229

W
wanghuancoder 已提交
230
                self.assertRaises(ValueError, test_start_dtype)
231

W
wanghuancoder 已提交
232 233 234 235 236
                def test_end_dtype():
                    end = paddle.static.data(
                        shape=[1], dtype="float64", name="end"
                    )
                    paddle.linspace(0, end, 1, dtype="float32")
237

W
wanghuancoder 已提交
238
                self.assertRaises(ValueError, test_end_dtype)
239

W
wanghuancoder 已提交
240 241 242 243 244
                def test_num_dtype():
                    num = paddle.static.data(
                        shape=[1], dtype="int32", name="step"
                    )
                    paddle.linspace(0, 10, num, dtype="float32")
245

W
wanghuancoder 已提交
246
                self.assertRaises(TypeError, test_step_dtype)
247

248

Z
zhoukunsheng 已提交
249 250
if __name__ == "__main__":
    unittest.main()