test_scale_op_xpu.py 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2020 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

17
import numpy as np
R
RedContritio 已提交
18
from get_test_cover_info import (
19
    XPUOpTestWrapper,
20 21 22
    create_test_class,
    get_xpu_op_support_types,
)
R
RedContritio 已提交
23
from op_test_xpu import XPUOpTest
24

25 26 27
import paddle
from paddle.fluid import Program, program_guard

28 29 30 31 32 33 34 35 36 37 38 39 40 41

class XPUTestScaleOp(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'scale'
        self.use_dynamic_create_class = False

    class TestScaleOp(XPUOpTest):
        def setUp(self):
            self.init_dtype()
            self.set_xpu()
            self.op_type = "scale"
            self.place = paddle.XPUPlace(0)
            self.set_inputs()
            self.set_attrs()
42
            self.set_output()
43 44 45 46 47 48 49 50 51

        def set_xpu(self):
            self.__class__.use_xpu = True
            self.__class__.no_need_check_grad = True
            self.__class__.op_type = self.dtype

        def set_inputs(self):
            self.inputs = {'X': np.random.random((10, 10)).astype(self.dtype)}

52 53 54 55 56 57 58 59 60 61
        def set_output(self):
            if "float16" == self.in_type:
                output = self.inputs['X'] * np.float16(self.attrs['scale'])
            elif "int64" == self.in_type:
                output = self.inputs['X'] * np.int64(self.attrs['scale'])
            else:
                output = self.inputs['X'] * np.float32(self.attrs['scale'])

            self.outputs = {'Out': output}

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
        def init_dtype(self):
            if "float16" == self.in_type:
                self.dtype = np.float16
            if "float32" == self.in_type:
                self.dtype = np.float32
            if "int64" == self.in_type:
                self.dtype = np.int64

        def set_attrs(self):
            self.attrs = {'scale': -2.3}

        def test_check_output(self):
            if paddle.is_compiled_with_xpu():
                place = paddle.XPUPlace(0)
                self.check_output_with_place(place)

    class TestScaleOp1(TestScaleOp):
        def set_attrs(self):
            self.attrs = {'scale': 3.5}

    class TestScaleOp2(TestScaleOp):
        def set_attrs(self):
            self.attrs = {'scale': 6.77}

    class TestScaleOp3(TestScaleOp):
        def set_attrs(self):
            self.attrs = {'scale': -9.19}

    class TestScaleOp4(TestScaleOp):
        def set_attrs(self):
            self.attrs = {'scale': 0.0}

    class TestScaleOp5(TestScaleOp):
        def set_attrs(self):
            self.attrs = {'scale': -0.003}
T
taixiurong 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112


class TestScaleApiStatic(unittest.TestCase):
    def _executed_api(self, x, scale=1.0, bias=0.0):
        return paddle.scale(x, scale, bias)

    def test_api(self):
        paddle.enable_static()
        input = np.random.random([2, 25]).astype("float32")
        main_prog = Program()
        with program_guard(main_prog, Program()):
            x = paddle.static.data(name="x", shape=[2, 25], dtype="float32")
            out = self._executed_api(x, scale=2.0, bias=3.0)

        exe = paddle.static.Executor(place=paddle.CPUPlace())
        out = exe.run(main_prog, feed={"x": input}, fetch_list=[out])
113
        np.testing.assert_array_equal(out[0], input * 2.0 + 3.0)
T
taixiurong 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129


class TestScaleInplaceApiStatic(TestScaleApiStatic):
    def _executed_api(self, x, scale=1.0, bias=0.0):
        return x.scale_(scale, bias)


class TestScaleApiDygraph(unittest.TestCase):
    def _executed_api(self, x, scale=1.0, bias=0.0):
        return paddle.scale(x, scale, bias)

    def test_api(self):
        paddle.disable_static()
        input = np.random.random([2, 25]).astype("float32")
        x = paddle.to_tensor(input)
        out = self._executed_api(x, scale=2.0, bias=3.0)
130
        np.testing.assert_array_equal(out.numpy(), input * 2.0 + 3.0)
T
taixiurong 已提交
131 132 133 134 135 136 137 138
        paddle.enable_static()


class TestScaleInplaceApiDygraph(TestScaleApiDygraph):
    def _executed_api(self, x, scale=1.0, bias=0.0):
        return x.scale_(scale, bias)


139 140 141 142 143 144 145 146 147 148 149
class TestScaleOpZeroNumelVariable(unittest.TestCase):
    def test_check_zero_numel_xpu(self):
        if paddle.is_compiled_with_xpu():
            paddle.disable_static()
            paddle.set_device('xpu')
            data = paddle.ones([0, 1])
            out = paddle.scale(data, 2)
            self.assertEqual(out.shape, data.shape)
            paddle.enable_static()


150 151 152 153
support_types = get_xpu_op_support_types('scale')
for stype in support_types:
    create_test_class(globals(), XPUTestScaleOp, stype)

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