test_full_op.py 5.0 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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 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 139
#   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.

from __future__ import print_function

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
import paddle.tensor as tensor
from paddle.fluid import compiler, Program, program_guard


# Test python API
class TestFullAPI(unittest.TestCase):
    def test_api(self):
        positive_2_int32 = fluid.layers.fill_constant([1], "int32", 2)

        positive_2_int64 = fluid.layers.fill_constant([1], "int64", 2)
        shape_tensor_int32 = fluid.data(
            name="shape_tensor_int32", shape=[2], dtype="int32")

        shape_tensor_int64 = fluid.data(
            name="shape_tensor_int64", shape=[2], dtype="int64")

        out_1 = tensor.full(
            shape=[1, 2], dtype="float32", fill_value=1.1, device='gpu')

        out_2 = tensor.full(
            shape=[1, positive_2_int32],
            dtype="float32",
            fill_value=1.1,
            device='cpu')

        out_3 = tensor.full(
            shape=[1, positive_2_int64],
            dtype="float32",
            fill_value=1.1,
            device='gpu')

        out_4 = tensor.full(
            shape=shape_tensor_int32,
            dtype="float32",
            fill_value=1.2,
            out=out_3)

        out_5 = tensor.full(
            shape=shape_tensor_int64,
            dtype="float32",
            fill_value=1.1,
            device='gpu',
            stop_gradient=False)

        out_6 = tensor.full(
            shape=shape_tensor_int64, dtype=np.float32, fill_value=1.1)

        exe = fluid.Executor(place=fluid.CPUPlace())
        res_1, res_2, res_3, res_4, res_5, res_6 = exe.run(
            fluid.default_main_program(),
            feed={
                "shape_tensor_int32": np.array([1, 2]).astype("int32"),
                "shape_tensor_int64": np.array([1, 2]).astype("int64"),
            },
            fetch_list=[out_1, out_2, out_3, out_4, out_5, out_6])

        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"))
        assert np.array_equal(res_3, np.full([1, 2], 1.2, dtype="float32"))
        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"))


class TestFullOpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):
            #for ci coverage
            x1 = fluid.layers.data(name='x1', shape=[1], dtype="int16")
            self.assertRaises(
                ValueError, tensor.full, shape=[1], fill_value=5, dtype='uint4')
            self.assertRaises(
                TypeError,
                tensor.full,
                shape=[1],
                fill_value=5,
                dtype='int16',
                out=x1)

            # The argument dtype of full must be one of bool, float16,
            #float32, float64, int32 or int64
            x2 = fluid.layers.data(name='x2', shape=[1], dtype="int32")

            self.assertRaises(
                TypeError, tensor.full, shape=[1], fill_value=5, dtype='uint8')

            # The argument shape's type of full_op  must be list, tuple or Variable.
            def test_shape_type():
                tensor.full(shape=1, dtype="float32", fill_value=1)

            self.assertRaises(TypeError, test_shape_type)

            # The argument shape's size of full_op must not be 0.
            def test_shape_size():
                tensor.full(shape=[], dtype="float32", fill_value=1)

            self.assertRaises(AssertionError, test_shape_size)

            # The shape dtype of full op must be int32 or int64.
            def test_shape_tensor_dtype():
                shape = fluid.data(
                    name="shape_tensor", shape=[2], dtype="float32")
                tensor.full(shape=shape, dtype="float32", fill_value=1)

            self.assertRaises(TypeError, test_shape_tensor_dtype)

            def test_shape_tensor_list_dtype():
                shape = fluid.data(
                    name="shape_tensor_list", shape=[1], dtype="bool")
                tensor.full(shape=[shape, 2], dtype="float32", fill_value=1)

            self.assertRaises(TypeError, test_shape_tensor_list_dtype)


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