|
| 1 | +/* Copyright (c) 2018 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 | +#include "paddle/fluid/framework/data_layout.h" |
| 16 | +#include "paddle/fluid/inference/tensorrt/convert/op_converter.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace framework { |
| 20 | +class Scope; |
| 21 | + |
| 22 | +namespace proto { |
| 23 | +class OpDesc; |
| 24 | +} // namespace proto |
| 25 | +} // namespace framework |
| 26 | +} // namespace paddle |
| 27 | + |
| 28 | +namespace paddle { |
| 29 | +namespace inference { |
| 30 | +namespace tensorrt { |
| 31 | + |
| 32 | +/* |
| 33 | + * Affine Channel Op |
| 34 | + */ |
| 35 | +class AffineChannelOpConverter : public OpConverter { |
| 36 | + public: |
| 37 | + void operator()(const framework::proto::OpDesc& op, |
| 38 | + const framework::Scope& scope, bool test_mode) override { |
| 39 | + VLOG(3) << "convert a fluid affine_channel op to tensorrt scale nd layer"; |
| 40 | + |
| 41 | + framework::OpDesc op_desc(op, nullptr); |
| 42 | + std::string input_name = op_desc.Input("X").front(); |
| 43 | + std::string scale_name = op_desc.Input("Scale").front(); |
| 44 | + std::string bias_name = op_desc.Input("Bias").front(); |
| 45 | + std::string output_name = op_desc.Output("Out").front(); |
| 46 | + |
| 47 | + auto input_tensor = engine_->GetITensor(input_name); |
| 48 | + auto idim = input_tensor->getDimensions(); |
| 49 | + |
| 50 | + auto* scale_v = scope.FindVar(scale_name); |
| 51 | + auto* scale_t = scale_v->GetMutable<framework::LoDTensor>(); |
| 52 | + float* scale_ptr = engine_->GetWeightCPUData(scale_name, scale_t, false); |
| 53 | + |
| 54 | + auto* bias_v = scope.FindVar(bias_name); |
| 55 | + auto* bias_t = bias_v->GetMutable<framework::LoDTensor>(); |
| 56 | + float* bias_ptr = engine_->GetWeightCPUData(bias_name, bias_t, false); |
| 57 | + |
| 58 | + auto data_layout = framework::StringToDataLayout( |
| 59 | + BOOST_GET_CONST(std::string, op_desc.GetAttr("data_layout"))); |
| 60 | + |
| 61 | + PADDLE_ENFORCE_EQ( |
| 62 | + data_layout, framework::DataLayout::kNCHW, |
| 63 | + platform::errors::InvalidArgument( |
| 64 | + "TensorRT affine channel converter can only convert NCHW format. " |
| 65 | + "Other format should be run in fluid mode. Report a bug on github " |
| 66 | + "issue if you see this line.")); |
| 67 | + |
| 68 | + // tensorrt scalend layer only support spatial dims >= 2, |
| 69 | + // so nhwc is not availabe (spatial dims == 0) |
| 70 | + const int channel_axis = engine_->with_dynamic_shape(); |
| 71 | + |
| 72 | + TensorRTEngine::Weight scale_weights{nvinfer1::DataType::kFLOAT, |
| 73 | + static_cast<void*>(scale_ptr), |
| 74 | + (size_t)idim.d[channel_axis]}; |
| 75 | + TensorRTEngine::Weight bias_weights{nvinfer1::DataType::kFLOAT, |
| 76 | + static_cast<void*>(bias_ptr), |
| 77 | + (size_t)idim.d[channel_axis]}; |
| 78 | + TensorRTEngine::Weight power_weights{nvinfer1::DataType::kFLOAT, nullptr, |
| 79 | + 0}; |
| 80 | + |
| 81 | + auto layer = TRT_ENGINE_ADD_LAYER(engine_, ScaleNd, *input_tensor, |
| 82 | + nvinfer1::ScaleMode::kCHANNEL, |
| 83 | + bias_weights.get(), scale_weights.get(), |
| 84 | + power_weights.get(), channel_axis); |
| 85 | + |
| 86 | + RreplenishLayerAndOutput(layer, "affine_channel", {output_name}, test_mode); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace tensorrt |
| 91 | +} // namespace inference |
| 92 | +} // namespace paddle |
| 93 | + |
| 94 | +REGISTER_TRT_OP_CONVERTER(affine_channel, AffineChannelOpConverter); |
0 commit comments