|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import print_function |
| 16 | + |
| 17 | +import unittest |
| 18 | +import numpy as np |
| 19 | +import sys |
| 20 | +sys.path.append("..") |
| 21 | +from op_test import OpTest |
| 22 | +from op_test_xpu import XPUOpTest |
| 23 | +import paddle |
| 24 | +import paddle.fluid.core as core |
| 25 | + |
| 26 | +paddle.enable_static() |
| 27 | + |
| 28 | + |
| 29 | +class XPUBaseTestCase(XPUOpTest): |
| 30 | + def initTestCase(self): |
| 31 | + self.dims = (3, 4) |
| 32 | + self.dtype = 'float32' |
| 33 | + self.axis = 1 |
| 34 | + |
| 35 | + def setUp(self): |
| 36 | + self.initTestCase() |
| 37 | + self.__class__.op_type = 'arg_max' |
| 38 | + self.__class__.use_xpu = True |
| 39 | + np.random.seed(2021) |
| 40 | + self.x = (np.random.random(self.dims)).astype(self.dtype) |
| 41 | + self.inputs = {'X': self.x} |
| 42 | + self.attrs = {'axis': self.axis, 'use_xpu': True} |
| 43 | + if self.op_type == "arg_min": |
| 44 | + self.outputs = {'Out': np.argmin(self.x, axis=self.axis)} |
| 45 | + else: |
| 46 | + self.outputs = {'Out': np.argmax(self.x, axis=self.axis)} |
| 47 | + |
| 48 | + def test_check_output(self): |
| 49 | + if paddle.is_compiled_with_xpu(): |
| 50 | + place = paddle.XPUPlace(0) |
| 51 | + self.check_output_with_place(place) |
| 52 | + |
| 53 | + |
| 54 | +# test argmax, dtype: float32 |
| 55 | +class TestArgMaxFloat32Case1(XPUBaseTestCase): |
| 56 | + def initTestCase(self): |
| 57 | + self.op_type = 'arg_max' |
| 58 | + self.dims = (3, 4, 5) |
| 59 | + self.dtype = 'float32' |
| 60 | + self.axis = -1 |
| 61 | + |
| 62 | + |
| 63 | +class TestArgMaxFloat32Case2(XPUBaseTestCase): |
| 64 | + def initTestCase(self): |
| 65 | + self.op_type = 'arg_max' |
| 66 | + self.dims = (3, 4, 5) |
| 67 | + self.dtype = 'float32' |
| 68 | + self.axis = 0 |
| 69 | + |
| 70 | + |
| 71 | +class TestArgMaxFloat32Case3(XPUBaseTestCase): |
| 72 | + def initTestCase(self): |
| 73 | + self.op_type = 'arg_max' |
| 74 | + self.dims = (3, 4, 5) |
| 75 | + self.dtype = 'float32' |
| 76 | + self.axis = 1 |
| 77 | + |
| 78 | + |
| 79 | +class TestArgMaxFloat32Case4(XPUBaseTestCase): |
| 80 | + def initTestCase(self): |
| 81 | + self.op_type = 'arg_max' |
| 82 | + self.dims = (3, 4, 5) |
| 83 | + self.dtype = 'float32' |
| 84 | + self.axis = 2 |
| 85 | + |
| 86 | + |
| 87 | +class TestArgMaxFloat32Case5(XPUBaseTestCase): |
| 88 | + def initTestCase(self): |
| 89 | + self.op_type = 'arg_max' |
| 90 | + self.dims = (3, 4) |
| 91 | + self.dtype = 'float32' |
| 92 | + self.axis = -1 |
| 93 | + |
| 94 | + |
| 95 | +class TestArgMaxFloat32Case6(XPUBaseTestCase): |
| 96 | + def initTestCase(self): |
| 97 | + self.op_type = 'arg_max' |
| 98 | + self.dims = (3, 4) |
| 99 | + self.dtype = 'float32' |
| 100 | + self.axis = 0 |
| 101 | + |
| 102 | + |
| 103 | +class TestArgMaxFloat32Case7(XPUBaseTestCase): |
| 104 | + def initTestCase(self): |
| 105 | + self.op_type = 'arg_max' |
| 106 | + self.dims = (3, 4) |
| 107 | + self.dtype = 'float32' |
| 108 | + self.axis = 1 |
| 109 | + |
| 110 | + |
| 111 | +class TestArgMaxFloat32Case8(XPUBaseTestCase): |
| 112 | + def initTestCase(self): |
| 113 | + self.op_type = 'arg_max' |
| 114 | + self.dims = (1, ) |
| 115 | + self.dtype = 'float32' |
| 116 | + self.axis = 0 |
| 117 | + |
| 118 | + |
| 119 | +class TestArgMaxFloat32Case9(XPUBaseTestCase): |
| 120 | + def initTestCase(self): |
| 121 | + self.op_type = 'arg_max' |
| 122 | + self.dims = (2, ) |
| 123 | + self.dtype = 'float32' |
| 124 | + self.axis = 0 |
| 125 | + |
| 126 | + |
| 127 | +class TestArgMaxFloat32Case10(XPUBaseTestCase): |
| 128 | + def initTestCase(self): |
| 129 | + self.op_type = 'arg_max' |
| 130 | + self.dims = (3, ) |
| 131 | + self.dtype = 'float32' |
| 132 | + self.axis = 0 |
| 133 | + |
| 134 | + |
| 135 | +class TestArgMaxAPI(unittest.TestCase): |
| 136 | + def initTestCase(self): |
| 137 | + self.dims = (3, 4, 5) |
| 138 | + self.dtype = 'float32' |
| 139 | + self.axis = 0 |
| 140 | + |
| 141 | + def setUp(self): |
| 142 | + self.initTestCase() |
| 143 | + self.__class__.use_Xpu = True |
| 144 | + self.place = [paddle.XPUPlace(0)] |
| 145 | + |
| 146 | + def test_dygraph_api(self): |
| 147 | + def run(place): |
| 148 | + paddle.disable_static(place) |
| 149 | + np.random.seed(2021) |
| 150 | + numpy_input = (np.random.random(self.dims)).astype(self.dtype) |
| 151 | + tensor_input = paddle.to_tensor(numpy_input) |
| 152 | + numpy_output = np.argmax(numpy_input, axis=self.axis) |
| 153 | + paddle_output = paddle.argmax(tensor_input, axis=self.axis) |
| 154 | + self.assertEqual( |
| 155 | + np.allclose(numpy_output, paddle_output.numpy()), True) |
| 156 | + paddle.enable_static() |
| 157 | + |
| 158 | + for place in self.place: |
| 159 | + run(place) |
| 160 | + |
| 161 | + |
| 162 | +class TestArgMaxAPI_2(unittest.TestCase): |
| 163 | + def initTestCase(self): |
| 164 | + self.dims = (3, 4, 5) |
| 165 | + self.dtype = 'float32' |
| 166 | + self.axis = 0 |
| 167 | + self.keep_dims = True |
| 168 | + |
| 169 | + def setUp(self): |
| 170 | + self.initTestCase() |
| 171 | + self.__class__.use_xpu = True |
| 172 | + self.place = [paddle.XPUPlace(0)] |
| 173 | + |
| 174 | + def test_dygraph_api(self): |
| 175 | + def run(place): |
| 176 | + paddle.disable_static(place) |
| 177 | + np.random.seed(2021) |
| 178 | + numpy_input = (np.random.random(self.dims)).astype(self.dtype) |
| 179 | + tensor_input = paddle.to_tensor(numpy_input) |
| 180 | + numpy_output = np.argmax( |
| 181 | + numpy_input, axis=self.axis).reshape(1, 4, 5) |
| 182 | + paddle_output = paddle.argmax( |
| 183 | + tensor_input, axis=self.axis, keepdim=self.keep_dims) |
| 184 | + self.assertEqual( |
| 185 | + np.allclose(numpy_output, paddle_output.numpy()), True) |
| 186 | + self.assertEqual(numpy_output.shape, paddle_output.numpy().shape) |
| 187 | + paddle.enable_static() |
| 188 | + |
| 189 | + for place in self.place: |
| 190 | + run(place) |
| 191 | + |
| 192 | + |
| 193 | +if __name__ == '__main__': |
| 194 | + unittest.main() |
0 commit comments