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
@unittest.skipIf(
67 68
    not core.is_compiled_with_cuda() or paddle.is_compiled_with_rocm(),
    "core is not compiled with CUDA",
69
)
70 71 72
class TestFillAnyLikeOpBfloat16(OpTest):
    def setUp(self):
        self.op_type = "fill_any_like"
73 74
        self.prim_op_type = "comp"
        self.python_api = fill_any_like_wrapper
75
        self.public_python_api = fill_any_like_wrapper
76 77 78 79 80
        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 = {
81 82 83
            'Out': convert_float_to_uint16(
                self.value * np.ones_like(self.inputs["X"])
            )
84
        }
85
        self.if_enable_cinn()
86 87 88

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

91
    def if_enable_cinn(self):
92
        pass
93 94


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

99
    def if_enable_cinn(self):
100
        pass
101

Z
zhoukunsheng 已提交
102 103 104 105 106

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

107
    def if_enable_cinn(self):
108
        pass
109

Z
zhoukunsheng 已提交
110 111 112 113 114

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

115
    def if_enable_cinn(self):
116
        pass
117

Z
zhoukunsheng 已提交
118

119 120 121
class TestFillAnyLikeOpType(TestFillAnyLikeOp):
    def setUp(self):
        self.op_type = "fill_any_like"
122 123
        self.prim_op_type = "comp"
        self.python_api = fill_any_like_wrapper
124
        self.public_python_api = fill_any_like_wrapper
125 126 127 128 129 130
        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,
131
            'dtype': int(core.VarDesc.VarType.FP32),
132 133
        }
        self.outputs = {
134 135
            'Out': self.value
            * np.ones_like(self.inputs["X"]).astype(np.float32)
136 137
        }

138
        self.if_enable_cinn()
139

140
    def if_enable_cinn(self):
141
        pass
142

143

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

148
    def if_enable_cinn(self):
149
        pass
150

Z
zhoukunsheng 已提交
151 152

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