|
| 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/inference/tensorrt/convert/op_converter.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace inference { |
| 19 | +namespace tensorrt { |
| 20 | + |
| 21 | +// LeakyRelu converter from fluid to tensorRT |
| 22 | +class LeakyReluOpConverter : public OpConverter { |
| 23 | + public: |
| 24 | + void operator()(const framework::proto::OpDesc& op, |
| 25 | + const framework::Scope& scope, bool test_mode) override { |
| 26 | + VLOG(4) << "convert fluid leaky_relu op to tensorrt layer"; |
| 27 | + |
| 28 | + framework::OpDesc op_desc(op, nullptr); |
| 29 | + // Declare inputs |
| 30 | + int input_num = op_desc.Input("X").size(); |
| 31 | + PADDLE_ENFORCE(input_num == 1); |
| 32 | + auto* input = engine_->GetITensor(op_desc.Input("X")[0]); |
| 33 | + // Get output |
| 34 | + size_t output_num = op_desc.Output("Out").size(); |
| 35 | + PADDLE_ENFORCE(output_num == 1); |
| 36 | + // Get attrs |
| 37 | + float alpha = boost::get<float>(op_desc.GetAttr("alpha")); |
| 38 | + |
| 39 | + platform::CPUPlace place; |
| 40 | + std::unique_ptr<framework::LoDTensor> alpha_tensor( |
| 41 | + new framework::LoDTensor()); |
| 42 | + alpha_tensor->Resize(framework::make_ddim({2})); |
| 43 | + float* alpha_data = alpha_tensor->mutable_data<float>(place); |
| 44 | + alpha_data[0] = alpha; |
| 45 | + alpha_data[1] = 1.f - alpha; |
| 46 | + // the leaky relu formula y = (x > 0) ? x : alpha * x is equal to |
| 47 | + // y = alpha * x + (x > 0) ? (1 - alpha) * x : 0 |
| 48 | + TensorRTEngine::Weight scale{nvinfer1::DataType::kFLOAT, &alpha_data[0], 1}; |
| 49 | + TensorRTEngine::Weight shift{nvinfer1::DataType::kFLOAT, nullptr, 0}; |
| 50 | + TensorRTEngine::Weight power{nvinfer1::DataType::kFLOAT, nullptr, 0}; |
| 51 | + // y_scale = alpha * x |
| 52 | + auto* scale_layer = TRT_ENGINE_ADD_LAYER( |
| 53 | + engine_, Scale, *input, nvinfer1::ScaleMode::kUNIFORM, shift.get(), |
| 54 | + scale.get(), power.get()); |
| 55 | + PADDLE_ENFORCE(nullptr != scale_layer); |
| 56 | + // y_relu = (x > 0) : x : 0 |
| 57 | + auto* relu_layer = TRT_ENGINE_ADD_LAYER(engine_, Activation, *input, |
| 58 | + nvinfer1::ActivationType::kRELU); |
| 59 | + PADDLE_ENFORCE(nullptr != relu_layer); |
| 60 | + // |
| 61 | + TensorRTEngine::Weight sub_scale{nvinfer1::DataType::kFLOAT, &alpha_data[1], |
| 62 | + 1}; |
| 63 | + auto* scale_relu_layer = |
| 64 | + TRT_ENGINE_ADD_LAYER(engine_, Scale, *(relu_layer->getOutput(0)), |
| 65 | + nvinfer1::ScaleMode::kUNIFORM, shift.get(), |
| 66 | + sub_scale.get(), power.get()); |
| 67 | + PADDLE_ENFORCE(nullptr != scale_relu_layer); |
| 68 | + auto* output_layer = |
| 69 | + TRT_ENGINE_ADD_LAYER(engine_, ElementWise, *(scale_layer->getOutput(0)), |
| 70 | + *(scale_relu_layer->getOutput(0)), |
| 71 | + nvinfer1::ElementWiseOperation::kSUM); |
| 72 | + PADDLE_ENFORCE(nullptr != output_layer); |
| 73 | + // keep alpha tensor to avoid release it's memory |
| 74 | + std::string alpha_name = op_desc.Output("Out")[0] + "_alpha"; |
| 75 | + PADDLE_ENFORCE(engine_->weight_map.find(alpha_name) == |
| 76 | + engine_->weight_map.end()); |
| 77 | + engine_->weight_map[alpha_name] = std::move(alpha_tensor); |
| 78 | + |
| 79 | + std::string layer_name = "leaky_relu (Output: "; |
| 80 | + auto output_name = op_desc.Output("Out")[0]; |
| 81 | + output_layer->getOutput(0)->setName(output_name.c_str()); |
| 82 | + engine_->SetITensor(output_name, output_layer->getOutput(0)); |
| 83 | + layer_name += output_name; |
| 84 | + if (test_mode) { |
| 85 | + engine_->DeclareOutput(output_name); |
| 86 | + } |
| 87 | + output_layer->setName((layer_name + ")").c_str()); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +} // namespace tensorrt |
| 92 | +} // namespace inference |
| 93 | +} // namespace paddle |
| 94 | + |
| 95 | +REGISTER_TRT_OP_CONVERTER(leaky_relu, LeakyReluOpConverter); |
0 commit comments