|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software |
| 7 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +See the License for the specific language governing permissions and |
| 10 | +limitations under the License. */ |
| 11 | + |
| 12 | +#include "paddle/fluid/inference/tensorrt/convert/op_converter.h" |
| 13 | + |
| 14 | +namespace paddle { |
| 15 | +namespace framework { |
| 16 | +class Scope; |
| 17 | +namespace proto { |
| 18 | +class OpDesc; |
| 19 | +} // namespace proto |
| 20 | +} // namespace framework |
| 21 | +} // namespace paddle |
| 22 | + |
| 23 | +namespace paddle { |
| 24 | +namespace inference { |
| 25 | +namespace tensorrt { |
| 26 | + |
| 27 | +/* |
| 28 | + * ReshapeOp |
| 29 | + */ |
| 30 | +class TileOpConverter : public OpConverter { |
| 31 | + public: |
| 32 | + void operator()(const framework::proto::OpDesc& op, |
| 33 | + const framework::Scope& scope, bool test_mode) override { |
| 34 | +#if IS_TRT_VERSION_GE(7000) |
| 35 | + VLOG(4) << "convert a fluid tile op to tensorrt tile layer"; |
| 36 | + |
| 37 | + framework::OpDesc op_desc(op, nullptr); |
| 38 | + // Declare inputs |
| 39 | + auto* input = engine_->GetITensor(op_desc.Input("X")[0]); |
| 40 | + nvinfer1::Dims input_shape = input->getDimensions(); |
| 41 | + std::vector<int> repeat_times = |
| 42 | + BOOST_GET_CONST(std::vector<int>, op_desc.GetAttr("repeat_times")); |
| 43 | + |
| 44 | + nvinfer1::Dims output_dim = input_shape; |
| 45 | + nvinfer1::Dims output_stride; |
| 46 | + // If input_dims.nbDims + 1 < repeat_times.size() means we |
| 47 | + // should expand 1 on batchsize. trt doesn't support this behavior. |
| 48 | + PADDLE_ENFORCE_GE(input_shape.nbDims + 1, repeat_times.size(), |
| 49 | + platform::errors::InvalidArgument( |
| 50 | + "Can't change batchsize, please check repeat_times")); |
| 51 | + int diff = input_shape.nbDims + 1 - repeat_times.size(); |
| 52 | + if (diff > 0) repeat_times.insert(repeat_times.begin(), diff, 1); |
| 53 | + |
| 54 | + // Can't expand on batchsize |
| 55 | + PADDLE_ENFORCE_EQ( |
| 56 | + repeat_times[0], 1, |
| 57 | + platform::errors::InvalidArgument( |
| 58 | + "Can't expand on batchsize, please check repeat_times")); |
| 59 | + output_stride.nbDims = input_shape.nbDims; |
| 60 | + for (int i = 0; i < input_shape.nbDims; i++) { |
| 61 | + output_dim.d[i] = output_dim.d[i] * repeat_times[i + 1]; |
| 62 | + output_stride.d[i] = 1; |
| 63 | + } |
| 64 | + |
| 65 | + auto* layer = TRT_ENGINE_ADD_LAYER(engine_, Slice, *input, input_shape, |
| 66 | + output_dim, output_stride); |
| 67 | + layer->setMode(nvinfer1::SliceMode::kWRAP); |
| 68 | + auto output_name = op_desc.Output("Out")[0]; |
| 69 | + RreplenishLayerAndOutput(layer, "tile", {output_name}, test_mode); |
| 70 | +#endif |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +} // namespace tensorrt |
| 75 | +} // namespace inference |
| 76 | +} // namespace paddle |
| 77 | + |
| 78 | +REGISTER_TRT_OP_CONVERTER(tile, TileOpConverter); |
0 commit comments