test_elemetwise_x_op_ipu.py 6.2 KB
Newer Older
J
jianghaicheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#  Copyright (c) 2021 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

import numpy as np
import paddle
import paddle.static
20 21
from paddle.fluid.tests.unittests.ipu.op_test_ipu import (ExecutionMode,
                                                          IPUOpTest)
J
jianghaicheng 已提交
22 23 24 25 26 27 28 29


@unittest.skipIf(not paddle.is_compiled_with_ipu(),
                 "core is not compiled with IPU")
class TestMul(IPUOpTest):
    def setUp(self):
        self.set_atol()
        self.set_training()
30 31 32 33 34 35 36 37
        self.set_test_op()

    @property
    def fp16_enabled(self):
        if IPUOpTest.use_ipumodel():
            return False
        else:
            return True
J
jianghaicheng 已提交
38

39
    def set_test_op(self):
J
jianghaicheng 已提交
40 41 42
        self.op = paddle.fluid.layers.elementwise_mul

    def set_feed_attr(self):
43 44 45 46 47
        self.feed_shape = [x.shape for x in self.feed_fp32.values()]
        self.feed_list = list(self.feed_fp32.keys())

    def _test_base(self, exec_mode):
        scope = paddle.static.Scope()
J
jianghaicheng 已提交
48 49
        main_prog = paddle.static.Program()
        startup_prog = paddle.static.Program()
50 51
        main_prog.random_seed = self.SEED
        startup_prog.random_seed = self.SEED
J
jianghaicheng 已提交
52

53
        with paddle.static.scope_guard(scope):
J
jianghaicheng 已提交
54 55 56 57
            with paddle.static.program_guard(main_prog, startup_prog):
                x = paddle.static.data(
                    name=self.feed_list[0],
                    shape=self.feed_shape[0],
58
                    dtype='float32')
J
jianghaicheng 已提交
59 60 61
                y = paddle.static.data(
                    name=self.feed_list[1],
                    shape=self.feed_shape[1],
62 63
                    dtype='float32')

J
jianghaicheng 已提交
64 65
                out = self.op(x, y, **self.attrs)

66
            fetch_list = [out.name]
J
jianghaicheng 已提交
67

68
            if exec_mode == ExecutionMode.CPU_FP32:
J
jianghaicheng 已提交
69
                place = paddle.CPUPlace()
70 71 72
            else:
                place = paddle.IPUPlace()

J
jianghaicheng 已提交
73 74 75
            exe = paddle.static.Executor(place)
            exe.run(startup_prog)

76
            if exec_mode != ExecutionMode.CPU_FP32:
J
jianghaicheng 已提交
77
                feed_list = self.feed_list
Y
yaozhixin 已提交
78
                ipu_strategy = paddle.static.IpuStrategy()
79 80 81 82
                ipu_strategy.set_graph_config(is_training=self.is_training)
                if exec_mode == ExecutionMode.IPU_POPART_FP16:
                    ipu_strategy.set_precision_config(enable_fp16=True)
                program = paddle.static.IpuCompiledProgram(
J
jianghaicheng 已提交
83 84 85 86 87
                    main_prog,
                    ipu_strategy=ipu_strategy).compile(feed_list, fetch_list)
            else:
                program = main_prog

88 89 90 91 92
            feed = self.feed_fp32
            if exec_mode > ExecutionMode.IPU_FP32:
                feed = self.feed_fp16

            result = exe.run(program, feed=feed, fetch_list=fetch_list)
J
jianghaicheng 已提交
93 94 95
            return result[0]

    def run_test_base(self):
96 97 98 99 100
        output_dict = {}
        for mode in ExecutionMode:
            if mode > ExecutionMode.IPU_FP32 and not self.fp16_enabled:
                break
            output_dict[mode] = self._test_base(mode).flatten()
J
jianghaicheng 已提交
101

102
        self.check(output_dict)
J
jianghaicheng 已提交
103 104

    def test_case0(self):
105 106 107 108 109 110 111 112 113 114
        data_x = np.random.uniform(size=(2, 3, 4, 5))
        data_y = np.random.uniform(size=(2, 3, 4, 5))

        self.feed_fp32 = {
            "x": data_x.astype('float32'),
            "y": data_y.astype('float32'),
        }
        self.feed_fp16 = {
            "x": data_x.astype('float16'),
            "y": data_y.astype('float16'),
J
jianghaicheng 已提交
115 116 117 118 119 120
        }
        self.attrs = {}
        self.set_feed_attr()
        self.run_test_base()

    def test_case1(self):
121 122 123 124 125 126 127 128 129
        data_x = np.random.uniform(size=(2, 3, 4, 5))
        data_y = np.random.uniform(size=(3, 4))
        self.feed_fp32 = {
            "x": data_x.astype('float32'),
            "y": data_y.astype('float32'),
        }
        self.feed_fp16 = {
            "x": data_x.astype('float16'),
            "y": data_y.astype('float16'),
J
jianghaicheng 已提交
130 131 132 133 134 135
        }
        self.set_feed_attr()
        self.attrs = {"axis": 1}
        self.run_test_base()

    def test_case2(self):
136 137 138 139 140 141 142 143 144
        data_x = np.random.uniform(size=(2, 3, 4, 5))
        data_y = np.random.uniform(size=(5))
        self.feed_fp32 = {
            "x": data_x.astype('float32'),
            "y": data_y.astype('float32'),
        }
        self.feed_fp16 = {
            "x": data_x.astype('float16'),
            "y": data_y.astype('float16'),
J
jianghaicheng 已提交
145 146 147 148 149 150
        }
        self.set_feed_attr()
        self.attrs = {"axis": -1}
        self.run_test_base()

    def test_case3(self):
151 152 153 154 155 156 157 158 159
        data_x = np.random.uniform(size=(2, 3, 4, 5))
        data_y = np.random.uniform(size=(2))
        self.feed_fp32 = {
            "x": data_x.astype('float32'),
            "y": data_y.astype('float32'),
        }
        self.feed_fp16 = {
            "x": data_x.astype('float16'),
            "y": data_y.astype('float16'),
J
jianghaicheng 已提交
160 161 162 163 164 165 166
        }
        self.set_feed_attr()
        self.attrs = {"axis": 0}
        self.run_test_base()


class TestAdd(TestMul):
167
    def set_test_op(self):
J
jianghaicheng 已提交
168 169 170 171
        self.op = paddle.fluid.layers.elementwise_add


class TestSub(TestMul):
172
    def set_test_op(self):
J
jianghaicheng 已提交
173 174 175 176
        self.op = paddle.fluid.layers.elementwise_sub


class TestDiv(TestMul):
177
    def set_test_op(self):
J
jianghaicheng 已提交
178 179 180 181
        self.op = paddle.fluid.layers.elementwise_div


class TestMin(TestMul):
182
    def set_test_op(self):
J
jianghaicheng 已提交
183 184 185 186
        self.op = paddle.fluid.layers.elementwise_min


class TestMax(TestMul):
187
    def set_test_op(self):
J
jianghaicheng 已提交
188 189 190 191
        self.op = paddle.fluid.layers.elementwise_max


class TestPow(TestMul):
192
    def set_test_op(self):
J
jianghaicheng 已提交
193 194 195 196
        self.op = paddle.fluid.layers.elementwise_pow


class TestMod(TestMul):
197 198 199 200 201 202 203
    def set_atol(self):
        self.atol = 1e-7
        self.rtol = 1e-5
        self.atol_fp16 = 1e-2
        self.rtol_fp16 = 1e-3

    def set_test_op(self):
J
jianghaicheng 已提交
204 205 206 207 208
        self.op = paddle.fluid.layers.elementwise_mod


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