elementwise.py 3.6 KB
Newer Older
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
#  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 numpy as np
import paddle
import paddle.fluid as fluid
paddle.enable_static()


class TestXPUElementwiseOpBase(object):
    def setUp(self, op_type):
        self.op_type = op_type
        self.attrs = {'use_xpu': True}
        self.is_common_broadcast = False
        self.is_x_size_less_than_y = False
        self.grad_implemented = False
        self.y_grad_implemented = True
        self.dtype = np.float32
        self.__class__.op_type = self.op_type
        self.__class__.use_xpu = True
        self.__class__.dtype = self.dtype

    def net(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            x = fluid.layers.data(
                name='X', shape=self.inputs['X'].shape, dtype=self.dtype)
            y = fluid.layers.data(
                name='Y', shape=self.inputs['Y'].shape, dtype=self.dtype)
            op = getattr(fluid.layers, self.op_type)
            z = op(x, y)
            exe = fluid.Executor(place)
            z_value = exe.run(feed=self.inputs, fetch_list=[z.name])

    def test_check_output(self):
        if paddle.is_compiled_with_xpu():
            place = paddle.XPUPlace(0)
            if not self.is_common_broadcast and not self.is_x_size_less_than_y:
                self.check_output_with_place(place, atol=1e-3)
            else:
                with self.assertRaises(BaseException):
                    self.net(place)

    def _check_grad_xpu_helper(self,
                               inputs_to_check,
                               output_names,
                               no_grad_set=None,
57
                               max_relative_error=0.01):
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
        if self.grad_implemented and not self.is_common_broadcast   \
          and not self.is_x_size_less_than_y:
            if paddle.is_compiled_with_xpu():
                place = paddle.XPUPlace(0)
                self.check_grad_with_place(
                    place,
                    inputs_to_check,
                    output_names,
                    no_grad_set=no_grad_set,
                    max_relative_error=max_relative_error)

    def test_check_grad_normal(self):
        self._check_grad_xpu_helper(['X', 'Y'], 'Out')

    def test_check_grad_ingore_x(self):
        self._check_grad_xpu_helper(['Y'], 'Out', set("X"))

    def test_check_grad_ingore_y(self):
        if self.y_grad_implemented:
            self._check_grad_xpu_helper(['X'], 'Out', set("Y"))

    def init_axis(self):
        self.axis = -1

    def make_input(self, x_shape=[13, 17], y_shape=[13, 17]):
        self.inputs = {
            'X': np.random.uniform(0.1, 1, x_shape).astype(self.dtype),
            'Y': np.random.uniform(0.1, 1, y_shape).astype(self.dtype)
        }

    def reshape_input(self, x_shape=None, y_shape=None):
        if x_shape is None:
            x = self.inputs['X']
        else:
            x = self.inputs['X'].reshape(x_shape)
        if y_shape is None:
            y = self.inputs['Y']
        else:
            y = self.inputs['Y'].reshape(y_shape)
        return x, y

    def make_output(self, x_shape=None, y_shape=None):
        pass