test_full_op.py 8.6 KB
Newer Older
W
wangchaochaohu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#   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
import numpy as np
from op_test import OpTest

import paddle.fluid.core as core
from paddle.fluid.op import Operator
import paddle.fluid as fluid
22
import paddle
W
wangchaochaohu 已提交
23
from paddle.fluid import compiler, Program, program_guard
24
from paddle.fluid.framework import _test_eager_guard
W
wangchaochaohu 已提交
25 26 27 28


# Test python API
class TestFullAPI(unittest.TestCase):
29

W
wangchaochaohu 已提交
30 31 32 33
    def test_api(self):
        positive_2_int32 = fluid.layers.fill_constant([1], "int32", 2)

        positive_2_int64 = fluid.layers.fill_constant([1], "int64", 2)
34 35 36
        shape_tensor_int32 = fluid.data(name="shape_tensor_int32",
                                        shape=[2],
                                        dtype="int32")
W
wangchaochaohu 已提交
37

38 39 40
        shape_tensor_int64 = fluid.data(name="shape_tensor_int64",
                                        shape=[2],
                                        dtype="int64")
W
wangchaochaohu 已提交
41

42
        out_1 = paddle.full(shape=[1, 2], dtype="float32", fill_value=1.1)
W
wangchaochaohu 已提交
43

44 45 46
        out_2 = paddle.full(shape=[1, positive_2_int32],
                            dtype="float32",
                            fill_value=1.1)
W
wangchaochaohu 已提交
47

48 49 50
        out_3 = paddle.full(shape=[1, positive_2_int64],
                            dtype="float32",
                            fill_value=1.1)
W
wangchaochaohu 已提交
51

52 53 54
        out_4 = paddle.full(shape=shape_tensor_int32,
                            dtype="float32",
                            fill_value=1.2)
W
wangchaochaohu 已提交
55

56 57 58
        out_5 = paddle.full(shape=shape_tensor_int64,
                            dtype="float32",
                            fill_value=1.1)
W
wangchaochaohu 已提交
59

60 61 62
        out_6 = paddle.full(shape=shape_tensor_int64,
                            dtype=np.float32,
                            fill_value=1.1)
W
wangchaochaohu 已提交
63

64
        val = fluid.layers.fill_constant(shape=[1], dtype=np.float32, value=1.1)
65 66 67
        out_7 = paddle.full(shape=shape_tensor_int64,
                            dtype=np.float32,
                            fill_value=val)
68

W
wangchaochaohu 已提交
69
        exe = fluid.Executor(place=fluid.CPUPlace())
70
        res_1, res_2, res_3, res_4, res_5, res_6, res_7 = exe.run(
W
wangchaochaohu 已提交
71 72 73 74 75
            fluid.default_main_program(),
            feed={
                "shape_tensor_int32": np.array([1, 2]).astype("int32"),
                "shape_tensor_int64": np.array([1, 2]).astype("int64"),
            },
76
            fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6, out_7])
W
wangchaochaohu 已提交
77 78 79

        assert np.array_equal(res_1, np.full([1, 2], 1.1, dtype="float32"))
        assert np.array_equal(res_2, np.full([1, 2], 1.1, dtype="float32"))
80
        assert np.array_equal(res_3, np.full([1, 2], 1.1, dtype="float32"))
W
wangchaochaohu 已提交
81 82 83
        assert np.array_equal(res_4, np.full([1, 2], 1.2, dtype="float32"))
        assert np.array_equal(res_5, np.full([1, 2], 1.1, dtype="float32"))
        assert np.array_equal(res_6, np.full([1, 2], 1.1, dtype="float32"))
84
        assert np.array_equal(res_7, np.full([1, 2], 1.1, dtype="float32"))
W
wangchaochaohu 已提交
85

86 87 88 89 90
    def test_api_eager(self):
        with fluid.dygraph.base.guard():
            with _test_eager_guard():
                positive_2_int32 = fluid.layers.fill_constant([1], "int32", 2)
                positive_2_int64 = fluid.layers.fill_constant([1], "int64", 2)
91 92 93
                positive_4_int64 = fluid.layers.fill_constant([1], "int64", 4,
                                                              True)

94 95 96
                out_1 = paddle.full(shape=[1, 2],
                                    dtype="float32",
                                    fill_value=1.1)
97

98 99 100
                out_2 = paddle.full(shape=[1, positive_2_int32.item()],
                                    dtype="float32",
                                    fill_value=1.1)
101

102 103 104
                out_3 = paddle.full(shape=[1, positive_2_int64.item()],
                                    dtype="float32",
                                    fill_value=1.1)
105

106 107 108
                out_4 = paddle.full(shape=[1, 2],
                                    dtype="float32",
                                    fill_value=1.2)
109

110 111 112
                out_5 = paddle.full(shape=[1, 2],
                                    dtype="float32",
                                    fill_value=1.1)
113

114 115 116
                out_6 = paddle.full(shape=[1, 2],
                                    dtype=np.float32,
                                    fill_value=1.1)
117

118 119 120 121 122 123
                val = fluid.layers.fill_constant(shape=[1],
                                                 dtype=np.float32,
                                                 value=1.1)
                out_7 = paddle.full(shape=[1, 2],
                                    dtype=np.float32,
                                    fill_value=val)
124

125 126 127
                out_8 = paddle.full(shape=positive_2_int32,
                                    dtype="float32",
                                    fill_value=1.1)
128

129 130 131 132 133
                out_9 = paddle.full(shape=[
                    positive_2_int32, positive_2_int64, positive_4_int64
                ],
                                    dtype="float32",
                                    fill_value=1.1)
134

135
                # test for numpy.float64 as fill_value
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                out_10 = paddle.full_like(out_7,
                                          dtype=np.float32,
                                          fill_value=np.abs(1.1))

                assert np.array_equal(out_1,
                                      np.full([1, 2], 1.1, dtype="float32"))
                assert np.array_equal(out_2,
                                      np.full([1, 2], 1.1, dtype="float32"))
                assert np.array_equal(out_3,
                                      np.full([1, 2], 1.1, dtype="float32"))
                assert np.array_equal(out_4,
                                      np.full([1, 2], 1.2, dtype="float32"))
                assert np.array_equal(out_5,
                                      np.full([1, 2], 1.1, dtype="float32"))
                assert np.array_equal(out_6,
                                      np.full([1, 2], 1.1, dtype="float32"))
                assert np.array_equal(out_7,
                                      np.full([1, 2], 1.1, dtype="float32"))
154
                assert np.array_equal(out_8, np.full([2], 1.1, dtype="float32"))
155 156 157 158
                assert np.array_equal(out_9,
                                      np.full([2, 2, 4], 1.1, dtype="float32"))
                assert np.array_equal(out_10,
                                      np.full([1, 2], 1.1, dtype="float32"))
159

W
wangchaochaohu 已提交
160 161

class TestFullOpError(unittest.TestCase):
162

W
wangchaochaohu 已提交
163 164 165
    def test_errors(self):
        with program_guard(Program(), Program()):
            #for ci coverage
166 167 168 169 170
            self.assertRaises(TypeError,
                              paddle.full,
                              shape=[1],
                              fill_value=5,
                              dtype='uint4')
W
wangchaochaohu 已提交
171 172

            # The argument dtype of full must be one of bool, float16,
173
            #float32, float64, uint8, int16, int32 or int64
W
wangchaochaohu 已提交
174 175 176

            # The argument shape's type of full_op  must be list, tuple or Variable.
            def test_shape_type():
177
                paddle.full(shape=1, dtype="float32", fill_value=1)
W
wangchaochaohu 已提交
178 179 180 181 182

            self.assertRaises(TypeError, test_shape_type)

            # The argument shape's size of full_op must not be 0.
            def test_shape_size():
183
                paddle.full(shape=[], dtype="float32", fill_value=1)
W
wangchaochaohu 已提交
184 185 186 187 188

            self.assertRaises(AssertionError, test_shape_size)

            # The shape dtype of full op must be int32 or int64.
            def test_shape_tensor_dtype():
189 190 191
                shape = fluid.data(name="shape_tensor",
                                   shape=[2],
                                   dtype="float32")
192
                paddle.full(shape=shape, dtype="float32", fill_value=1)
W
wangchaochaohu 已提交
193 194 195 196

            self.assertRaises(TypeError, test_shape_tensor_dtype)

            def test_shape_tensor_list_dtype():
197 198 199
                shape = fluid.data(name="shape_tensor_list",
                                   shape=[1],
                                   dtype="bool")
200
                paddle.full(shape=[shape, 2], dtype="float32", fill_value=1)
W
wangchaochaohu 已提交
201 202 203 204 205 206

            self.assertRaises(TypeError, test_shape_tensor_list_dtype)


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