|
| 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/op_registry.h" |
| 16 | +#include "paddle/fluid/inference/tensorrt/convert/op_converter.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace inference { |
| 20 | +namespace tensorrt { |
| 21 | + |
| 22 | +class ElementwiseWeightOpConverter : public OpConverter { |
| 23 | + public: |
| 24 | + ElementwiseWeightOpConverter() {} |
| 25 | + void operator()(const framework::proto::OpDesc& op, |
| 26 | + const framework::Scope& scope, bool test_mode) override { |
| 27 | + // Here the two nullptr looks strange, that's because the |
| 28 | + // framework::OpDesc's constructor is strange. |
| 29 | + framework::OpDesc op_desc(op, nullptr); |
| 30 | + LOG(INFO) << "convert a fluid elementwise op to tensorrt IScaleLayer"; |
| 31 | + |
| 32 | + PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); |
| 33 | + PADDLE_ENFORCE_EQ(op_desc.Input("Y").size(), 1); // Y is a weight |
| 34 | + PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); |
| 35 | + |
| 36 | + auto* X = engine_->GetITensor(op_desc.Input("X").front()); |
| 37 | + nvinfer1::Dims dims_x = X->getDimensions(); |
| 38 | + PADDLE_ENFORCE(dims_x.nbDims >= 3); |
| 39 | + |
| 40 | + auto* Y_v = scope.FindVar(op_desc.Input("Y").front()); |
| 41 | + PADDLE_ENFORCE_NOT_NULL(Y_v); |
| 42 | + auto* Y_t = Y_v->GetMutable<framework::LoDTensor>(); |
| 43 | + auto* weight_data = Y_t->mutable_data<float>(platform::CPUPlace()); |
| 44 | + auto scale_mode = nvinfer1::ScaleMode::kELEMENTWISE; |
| 45 | + |
| 46 | + std::vector<int> dims_y = framework::vectorize2int(Y_t->dims()); |
| 47 | + if (static_cast<int>(dims_y.size()) == dims_x.nbDims + 1) { |
| 48 | + if (dims_y[0] == 1) dims_y.erase(dims_y.begin()); |
| 49 | + } |
| 50 | + |
| 51 | + if (static_cast<int>(dims_y.size()) == 1 && dims_y[0] == dims_x.d[0]) { |
| 52 | + scale_mode = nvinfer1::ScaleMode::kCHANNEL; |
| 53 | + } else if (static_cast<int>(dims_y.size()) == dims_x.nbDims && |
| 54 | + dims_y[0] == dims_x.d[0]) { |
| 55 | + scale_mode = nvinfer1::ScaleMode::kELEMENTWISE; |
| 56 | + for (int i = 1; i < dims_x.nbDims; i++) { |
| 57 | + if (dims_y[i] != dims_x.d[i]) { |
| 58 | + scale_mode = nvinfer1::ScaleMode::kCHANNEL; |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + if (scale_mode == nvinfer1::ScaleMode::kCHANNEL) { |
| 63 | + for (int i = 1; i < dims_x.nbDims; i++) { |
| 64 | + if (dims_y[i] != 1) |
| 65 | + PADDLE_THROW( |
| 66 | + "TensorRT unsupported weight shape for Elementwise op!"); |
| 67 | + } |
| 68 | + } |
| 69 | + } else { |
| 70 | + PADDLE_THROW("TensorRT unsupported weight Shape for Elementwise op!"); |
| 71 | + } |
| 72 | + |
| 73 | + TensorRTEngine::Weight shift_weights{nvinfer1::DataType::kFLOAT, |
| 74 | + static_cast<void*>(weight_data), |
| 75 | + Y_t->memory_size() / sizeof(float)}; |
| 76 | + TensorRTEngine::Weight scale_weights{nvinfer1::DataType::kFLOAT, nullptr, |
| 77 | + 0}; |
| 78 | + TensorRTEngine::Weight power_weights{nvinfer1::DataType::kFLOAT, nullptr, |
| 79 | + 0}; |
| 80 | + |
| 81 | + nvinfer1::IScaleLayer* layer = TRT_ENGINE_ADD_LAYER( |
| 82 | + engine_, Scale, *const_cast<nvinfer1::ITensor*>(X), scale_mode, |
| 83 | + shift_weights.get(), scale_weights.get(), power_weights.get()); |
| 84 | + auto output_name = op_desc.Output("Out")[0]; |
| 85 | + engine_->SetITensor(output_name, layer->getOutput(0)); |
| 86 | + if (test_mode) { // the test framework can not determine which is the |
| 87 | + // output, so place the declaration inside. |
| 88 | + engine_->DeclareOutput(output_name); |
| 89 | + } |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +class ElementwiseTensorOpConverter : public OpConverter { |
| 94 | + public: |
| 95 | + ElementwiseTensorOpConverter() {} |
| 96 | + void operator()(const framework::proto::OpDesc& op, |
| 97 | + const framework::Scope& scope, bool test_mode) override { |
| 98 | + // Here the two nullptr looks strange, that's because the |
| 99 | + // framework::OpDesc's constructor is strange. |
| 100 | + framework::OpDesc op_desc(op, nullptr); |
| 101 | + LOG(INFO) << "convert a fluid elementwise op to tensorrt IScaleLayer"; |
| 102 | + |
| 103 | + PADDLE_ENFORCE_EQ(op_desc.Input("X").size(), 1); |
| 104 | + PADDLE_ENFORCE_EQ(op_desc.Input("Y").size(), 1); // Y is a weight |
| 105 | + PADDLE_ENFORCE_EQ(op_desc.Output("Out").size(), 1); |
| 106 | + |
| 107 | + auto* X = engine_->GetITensor(op_desc.Input("X").front()); |
| 108 | + auto* Y = engine_->GetITensor(op_desc.Input("Y").front()); |
| 109 | + nvinfer1::Dims dims_x = X->getDimensions(); |
| 110 | + nvinfer1::Dims dims_y = Y->getDimensions(); |
| 111 | + |
| 112 | + // only support the C * H * W input format |
| 113 | + PADDLE_ENFORCE(dims_x.nbDims >= 3); |
| 114 | + if (dims_x.nbDims == dims_y.nbDims) { |
| 115 | + for (int i = 0; i < dims_x.nbDims; i++) { |
| 116 | + if (dims_x.d[i] != dims_y.d[i]) |
| 117 | + PADDLE_THROW("TensorRT unsupported tensor shape for Elementwise op!"); |
| 118 | + } |
| 119 | + } else { |
| 120 | + PADDLE_THROW("TensorRT unsupported tensor shape for Elementwise op!"); |
| 121 | + } |
| 122 | + |
| 123 | + auto op_pair = ops.find(op_type_); |
| 124 | + if (op_pair == ops.end()) { |
| 125 | + PADDLE_THROW("Wrong elementwise op type!"); |
| 126 | + } |
| 127 | + nvinfer1::IElementWiseLayer* layer = TRT_ENGINE_ADD_LAYER( |
| 128 | + engine_, ElementWise, *const_cast<nvinfer1::ITensor*>(X), |
| 129 | + *const_cast<nvinfer1::ITensor*>(Y), op_pair->second); |
| 130 | + |
| 131 | + auto output_name = op_desc.Output("Out")[0]; |
| 132 | + engine_->SetITensor(output_name, layer->getOutput(0)); |
| 133 | + if (test_mode) { // the test framework can not determine which is the |
| 134 | + // output, so place the declaration inside. |
| 135 | + engine_->DeclareOutput(output_name); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + protected: |
| 140 | + static const std::unordered_map<std::string, nvinfer1::ElementWiseOperation> |
| 141 | + ops; |
| 142 | + std::string op_type_; |
| 143 | +}; |
| 144 | + |
| 145 | +const std::unordered_map<std::string, nvinfer1::ElementWiseOperation> |
| 146 | + ElementwiseTensorOpConverter::ops = { |
| 147 | + {"add", nvinfer1::ElementWiseOperation::kSUM}, |
| 148 | + {"mul", nvinfer1::ElementWiseOperation::kPROD}, |
| 149 | + {"sub", nvinfer1::ElementWiseOperation::kSUB}, |
| 150 | + {"div", nvinfer1::ElementWiseOperation::kDIV}, |
| 151 | + {"min", nvinfer1::ElementWiseOperation::kMIN}, |
| 152 | + {"pow", nvinfer1::ElementWiseOperation::kPOW}, |
| 153 | + {"max", nvinfer1::ElementWiseOperation::kMAX}, |
| 154 | +}; |
| 155 | + |
| 156 | +class ElementwiseTensorAddOpConverter : public ElementwiseTensorOpConverter { |
| 157 | + public: |
| 158 | + ElementwiseTensorAddOpConverter() { op_type_ = "add"; } |
| 159 | +}; |
| 160 | + |
| 161 | +class ElementwiseTensorMulOpConverter : public ElementwiseTensorOpConverter { |
| 162 | + public: |
| 163 | + ElementwiseTensorMulOpConverter() { op_type_ = "mul"; } |
| 164 | +}; |
| 165 | + |
| 166 | +class ElementwiseTensorSubOpConverter : public ElementwiseTensorOpConverter { |
| 167 | + public: |
| 168 | + ElementwiseTensorSubOpConverter() { op_type_ = "sub"; } |
| 169 | +}; |
| 170 | + |
| 171 | +class ElementwiseTensorDivOpConverter : public ElementwiseTensorOpConverter { |
| 172 | + public: |
| 173 | + ElementwiseTensorDivOpConverter() { op_type_ = "div"; } |
| 174 | +}; |
| 175 | + |
| 176 | +class ElementwiseTensorMinOpConverter : public ElementwiseTensorOpConverter { |
| 177 | + public: |
| 178 | + ElementwiseTensorMinOpConverter() { op_type_ = "min"; } |
| 179 | +}; |
| 180 | + |
| 181 | +class ElementwiseTensorMaxOpConverter : public ElementwiseTensorOpConverter { |
| 182 | + public: |
| 183 | + ElementwiseTensorMaxOpConverter() { op_type_ = "max"; } |
| 184 | +}; |
| 185 | + |
| 186 | +class ElementwiseTensorPowOpConverter : public ElementwiseTensorOpConverter { |
| 187 | + public: |
| 188 | + ElementwiseTensorPowOpConverter() { op_type_ = "pow"; } |
| 189 | +}; |
| 190 | + |
| 191 | +} // namespace tensorrt |
| 192 | +} // namespace inference |
| 193 | +} // namespace paddle |
| 194 | + |
| 195 | +REGISTER_TRT_OP_CONVERTER(elementwise_add_weight, ElementwiseWeightOpConverter); |
| 196 | + |
| 197 | +REGISTER_TRT_OP_CONVERTER(elementwise_add_tensor, |
| 198 | + ElementwiseTensorAddOpConverter); |
| 199 | +REGISTER_TRT_OP_CONVERTER(elementwise_sub_tensor, |
| 200 | + ElementwiseTensorSubOpConverter); |
| 201 | +REGISTER_TRT_OP_CONVERTER(elementwise_div_tensor, |
| 202 | + ElementwiseTensorDivOpConverter); |
| 203 | +REGISTER_TRT_OP_CONVERTER(elementwise_mul_tensor, |
| 204 | + ElementwiseTensorMulOpConverter); |
| 205 | +REGISTER_TRT_OP_CONVERTER(elementwise_max_tensor, |
| 206 | + ElementwiseTensorMaxOpConverter); |
| 207 | +REGISTER_TRT_OP_CONVERTER(elementwise_min_tensor, |
| 208 | + ElementwiseTensorMinOpConverter); |
| 209 | +REGISTER_TRT_OP_CONVERTER(elementwise_pow_tensor, |
| 210 | + ElementwiseTensorPowOpConverter); |
0 commit comments