|
| 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 | +/* |
| 22 | + * MulOp, IMatrixMultiplyLayer in TRT. This Layer doesn't has weights. |
| 23 | + */ |
| 24 | +class MulOpConverter : public OpConverter { |
| 25 | + public: |
| 26 | + void operator()(const framework::proto::OpDesc& op, |
| 27 | + const framework::Scope& scope, bool test_mode) override { |
| 28 | + VLOG(4) << "convert a fluid mul op to tensorrt mul layer without bias"; |
| 29 | + |
| 30 | + framework::OpDesc op_desc(op, nullptr); |
| 31 | + // Declare inputs |
| 32 | + auto* input1 = engine_->GetITensor(op_desc.Input("X")[0]); |
| 33 | + auto* input2 = engine_->GetITensor(op_desc.Input("Y")[0]); |
| 34 | + // Both the input1 and input2 do not need transpose. |
| 35 | + auto* layer = TRT_ENGINE_ADD_LAYER( |
| 36 | + engine_, MatrixMultiply, *const_cast<nvinfer1::ITensor*>(input1), false, |
| 37 | + *const_cast<nvinfer1::ITensor*>(input2), false); |
| 38 | + |
| 39 | + auto output_name = op_desc.Output("Out")[0]; |
| 40 | + engine_->SetITensor(output_name, layer->getOutput(0)); |
| 41 | + if (test_mode) { // the test framework can not determine which is the |
| 42 | + // output, so place the declaration inside. |
| 43 | + engine_->DeclareOutput(output_name); |
| 44 | + } |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +} // namespace tensorrt |
| 49 | +} // namespace inference |
| 50 | +} // namespace paddle |
| 51 | + |
| 52 | +USE_OP(mul); |
| 53 | +// TODO(xingzhaolong): change the name to mul then |
| 54 | +REGISTER_TRT_OP_CONVERTER(mul_temp, MulOpConverter); |
0 commit comments