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

20
import paddle
21
import paddle.framework.dtype as dtypes
22
from paddle.fluid import core
23 24 25 26 27 28 29 30


def fill_any_like_wrapper(x, value, out_dtype=None, name=None):
    if isinstance(out_dtype, int):
        tmp_dtype = dtypes.dtype(out_dtype)
    else:
        tmp_dtype = out_dtype
    return paddle.full_like(x, value, tmp_dtype, name)
31

Z
zhoukunsheng 已提交
32 33 34 35

class TestFillAnyLikeOp(OpTest):
    def setUp(self):
        self.op_type = "fill_any_like"
36 37
        self.prim_op_type = "comp"
        self.python_api = fill_any_like_wrapper
38
        self.public_python_api = fill_any_like_wrapper
Z
zhoukunsheng 已提交
39 40 41 42 43 44
        self.dtype = np.int32
        self.value = 0.0
        self.init()
        self.inputs = {'X': np.random.random((219, 232)).astype(self.dtype)}
        self.attrs = {'value': self.value}
        self.outputs = {'Out': self.value * np.ones_like(self.inputs["X"])}
45
        self.if_enable_cinn()
Z
zhoukunsheng 已提交
46 47 48 49 50

    def init(self):
        pass

    def test_check_output(self):
51 52
        self.check_output(check_prim=True)

53
    def if_enable_cinn(self):
54
        pass
Z
zhoukunsheng 已提交
55 56 57 58 59 60 61


class TestFillAnyLikeOpFloat32(TestFillAnyLikeOp):
    def init(self):
        self.dtype = np.float32
        self.value = 0.0

62
    def if_enable_cinn(self):
63
        pass
64

Z
zhoukunsheng 已提交
65

66 67 68
@unittest.skipIf(
    not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
69 70 71
class TestFillAnyLikeOpBfloat16(OpTest):
    def setUp(self):
        self.op_type = "fill_any_like"
72 73
        self.prim_op_type = "comp"
        self.python_api = fill_any_like_wrapper
74
        self.public_python_api = fill_any_like_wrapper
75 76 77 78 79
        self.dtype = np.uint16
        self.value = 0.0
        self.inputs = {'X': np.random.random((219, 232)).astype(np.float32)}
        self.attrs = {'value': self.value, 'dtype': core.VarDesc.VarType.BF16}
        self.outputs = {
80 81 82
            'Out': convert_float_to_uint16(
                self.value * np.ones_like(self.inputs["X"])
            )
83
        }
84
        self.if_enable_cinn()
85 86 87

    def test_check_output(self):
        place = core.CUDAPlace(0)
88 89
        self.check_output_with_place(place, check_prim=True)

90
    def if_enable_cinn(self):
91
        pass
92 93


Z
zhoukunsheng 已提交
94 95 96 97
class TestFillAnyLikeOpValue1(TestFillAnyLikeOp):
    def init(self):
        self.value = 1.0

98
    def if_enable_cinn(self):
99
        pass
100

Z
zhoukunsheng 已提交
101 102 103 104 105

class TestFillAnyLikeOpValue2(TestFillAnyLikeOp):
    def init(self):
        self.value = 1e-10

106
    def if_enable_cinn(self):
107
        pass
108

Z
zhoukunsheng 已提交
109 110 111 112 113

class TestFillAnyLikeOpValue3(TestFillAnyLikeOp):
    def init(self):
        self.value = 1e-100

114
    def if_enable_cinn(self):
115
        pass
116

Z
zhoukunsheng 已提交
117

118 119 120
class TestFillAnyLikeOpType(TestFillAnyLikeOp):
    def setUp(self):
        self.op_type = "fill_any_like"
121 122
        self.prim_op_type = "comp"
        self.python_api = fill_any_like_wrapper
123
        self.public_python_api = fill_any_like_wrapper
124 125 126 127 128 129
        self.dtype = np.int32
        self.value = 0.0
        self.init()
        self.inputs = {'X': np.random.random((219, 232)).astype(self.dtype)}
        self.attrs = {
            'value': self.value,
130
            'dtype': int(core.VarDesc.VarType.FP32),
131 132
        }
        self.outputs = {
133 134
            'Out': self.value
            * np.ones_like(self.inputs["X"]).astype(np.float32)
135 136
        }

137
        self.if_enable_cinn()
138

139
    def if_enable_cinn(self):
140
        pass
141

142

Z
zhoukunsheng 已提交
143 144 145 146
class TestFillAnyLikeOpFloat16(TestFillAnyLikeOp):
    def init(self):
        self.dtype = np.float16

147
    def if_enable_cinn(self):
148
        pass
149

Z
zhoukunsheng 已提交
150 151

if __name__ == "__main__":
152
    paddle.enable_static()
Z
zhoukunsheng 已提交
153
    unittest.main()