|
| 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 common_import import * |
| 16 | + |
| 17 | + |
| 18 | +class ReduceAllAnyConfig(APIConfig): |
| 19 | + def __init__(self): |
| 20 | + super(ReduceAllAnyConfig, self).__init__('reduce_all_any') |
| 21 | + self.feed_spec = {"range": [-1, 1]} |
| 22 | + self.api_name = 'all' |
| 23 | + self.api_list = {'all': 'all', 'any': 'any'} |
| 24 | + |
| 25 | + def init_from_json(self, filename, config_id=3, unknown_dim=16): |
| 26 | + super(ReduceAllAnyConfig, self).init_from_json(filename, config_id, |
| 27 | + unknown_dim) |
| 28 | + if self.axis == None: |
| 29 | + self.axis = [] |
| 30 | + |
| 31 | + def squeeze_shape(self): |
| 32 | + if len(self.axis) == 1: |
| 33 | + return self.axis[0], self.x_shape |
| 34 | + elif len(self.axis) > 1: |
| 35 | + is_continuous = True |
| 36 | + for i in range(1, len(self.axis)): |
| 37 | + if self.axis[i] != self.axis[i - 1] + 1: |
| 38 | + is_continuous = False |
| 39 | + break |
| 40 | + if is_continuous: |
| 41 | + begin_axis = self.axis[0] |
| 42 | + end_axis = self.axis[-1] |
| 43 | + new_x_shape = [1] * (len(self.x_shape) + begin_axis - end_axis) |
| 44 | + for i in range(len(self.x_shape)): |
| 45 | + if i < begin_axis: |
| 46 | + new_x_shape[i] = self.x_shape[i] |
| 47 | + elif i < end_axis + 1: |
| 48 | + new_x_shape[begin_axis] = new_x_shape[ |
| 49 | + begin_axis] * self.x_shape[i] |
| 50 | + else: |
| 51 | + new_x_shape[i + begin_axis - end_axis] = self.x_shape[ |
| 52 | + i] |
| 53 | + return begin_axis, new_x_shape |
| 54 | + return self.axis, self.x_shape |
| 55 | + |
| 56 | + |
| 57 | +class PDReduceAllAny(PaddleDynamicAPIBenchmarkBase): |
| 58 | + def build_graph(self, config): |
| 59 | + x = self.variable(name='x', shape=config.x_shape, dtype=config.x_dtype) |
| 60 | + result = self.layers( |
| 61 | + config.api_name, x=x, axis=config.axis, keepdim=config.keepdim) |
| 62 | + |
| 63 | + self.feed_list = [x] |
| 64 | + self.fetch_list = [result] |
| 65 | + |
| 66 | + |
| 67 | +class TorchReduceAllAny(PytorchAPIBenchmarkBase): |
| 68 | + def build_graph(self, config): |
| 69 | + x = self.variable(name='x', shape=config.x_shape, dtype=config.x_dtype) |
| 70 | + axis, x_shape = config.squeeze_shape() |
| 71 | + if x_shape != config.x_shape: |
| 72 | + x = torch.reshape(x=x, shape=x_shape) |
| 73 | + if isinstance(axis, list) and len(axis) == 0: |
| 74 | + result = self.layers(config.api_name, input=x) |
| 75 | + else: |
| 76 | + result = self.layers( |
| 77 | + config.api_name, input=x, dim=axis, keepdim=config.keepdim) |
| 78 | + |
| 79 | + self.feed_list = [x] |
| 80 | + self.fetch_list = [result] |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + test_main( |
| 85 | + pd_dy_obj=PDReduceAllAny(), |
| 86 | + torch_obj=TorchReduceAllAny(), |
| 87 | + config=ReduceAllAnyConfig()) |
0 commit comments