test_gather_op_xpu.py 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

15
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
import paddle

27
paddle.enable_static()
28 29 30 31 32 33 34 35 36


def gather_numpy(x, index, axis):
    x_transpose = np.swapaxes(x, 0, axis)
    tmp_gather = x_transpose[index, ...]
    gather = np.swapaxes(tmp_gather, 0, axis)
    return gather


37 38 39 40 41 42 43 44 45 46 47 48 49 50
class XPUTestGather(XPUOpTestWrapper):
    def __init__(self):
        self.op_name = 'gather'

    class TestXPUGatherOp(XPUOpTest):
        def setUp(self):
            self.op_type = "gather"
            self.place = paddle.XPUPlace(0)
            self.dtype = self.in_type

            self.init_config()
            xnp = np.random.random(self.x_shape).astype(self.dtype)
            self.inputs = {
                'X': xnp,
51
                'Index': np.array(self.index).astype(self.index_type),
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
            }
            self.outputs = {'Out': self.inputs["X"][self.inputs["Index"]]}

        def init_config(self):
            self.x_shape = (10, 20)
            self.index = [1, 3, 5]
            self.index_type = np.int32

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

        def test_check_grad(self):
            if paddle.is_compiled_with_xpu():
                self.check_grad_with_place(self.place, ['X'], 'Out')

    class TestCase1(TestXPUGatherOp):
        def init_config(self):
70
            self.x_shape = 100
71 72 73 74 75
            self.index = [1, 3, 5]
            self.index_type = np.int32

    class TestCase2(TestXPUGatherOp):
        def init_config(self):
76
            self.x_shape = 100
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            self.index = [1, 3, 5]
            self.index_type = np.int64

    class TestCase3(TestXPUGatherOp):
        def init_config(self):
            self.x_shape = (10, 20)
            self.index = [1, 3, 5]
            self.index_type = np.int32

    class TestCase4(TestXPUGatherOp):
        def init_config(self):
            self.x_shape = (10, 20)
            self.attrs = {'overwrite': False}
            self.index = [1, 1]
            self.index_type = np.int32

    class TestCase5(TestXPUGatherOp):
        def init_config(self):
            self.x_shape = (10, 20)
            self.attrs = {'overwrite': False}
            self.index = [1, 1, 3]
            self.index_type = np.int32

    class TestCase6(TestXPUGatherOp):
        def init_config(self):
            self.x_shape = (10, 20)
            self.attrs = {'overwrite': True}
            self.index = [1, 3]
            self.index_type = np.int32

    class TestCase7(TestXPUGatherOp):
        def init_config(self):
            self.x_shape = (10, 20)
            self.attrs = {'overwrite': True}
            self.index = [1, 3]
            self.index_type = np.int64


support_types = get_xpu_op_support_types('gather')
for stype in support_types:
    create_test_class(globals(), XPUTestGather, stype)
118

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