Skip to content

Commit 83c85f3

Browse files
authored
Merge pull request #12598 from NHZlX/add_tensorrt_softmax
Add tensorrt softmax
2 parents 1e1974c + 29ad979 commit 83c85f3

File tree

5 files changed

+120
-9
lines changed

5 files changed

+120
-9
lines changed

paddle/fluid/inference/analysis/analyzer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DfgPassManagerImpl final : public DfgPassManager {
4444
if (FLAGS_inference_analysis_enable_tensorrt_subgraph_engine) {
4545
auto trt_teller = [&](const Node* node) {
4646
std::unordered_set<std::string> teller_set(
47-
{"elementwise_add", "mul", "conv2d", "pool2d", "relu"});
47+
{"elementwise_add", "mul", "conv2d", "pool2d", "relu", "softmax"});
4848
if (!node->IsFunction()) return false;
4949

5050
const auto* func = static_cast<const Function*>(node);

paddle/fluid/inference/tensorrt/convert/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Add TRT tests
22
nv_library(tensorrt_converter
33
SRCS mul_op.cc conv2d_op.cc fc_op.cc pool2d_op.cc elementwise_op.cc
4-
activation_op.cc
4+
activation_op.cc softmax_op.cc
55
DEPS tensorrt_engine operator scope framework_proto op_registry)
66

77
nv_test(test_op_converter SRCS test_op_converter.cc DEPS
@@ -21,3 +21,6 @@ nv_test(test_trt_pool2d_op SRCS test_pool2d_op.cc pool2d_op.cc
2121

2222
nv_test(test_trt_elementwise_op SRCS test_elementwise_op.cc elementwise_op.cc
2323
DEPS ${FLUID_CORE_MODULES} tensorrt_engine elementwise_add_op SERIAL)
24+
25+
nv_test(test_trt_softmax_op SRCS test_softmax_op.cc softmax_op.cc
26+
DEPS ${FLUID_CORE_MODULES} tensorrt_engine softmax_op SERIAL)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
* SoftMaxOp, ISoftMaxLayer in TRT. This Layer doesn't has weights.
23+
*/
24+
class SoftMaxOpConverter : public OpConverter {
25+
public:
26+
void operator()(const framework::proto::OpDesc& op,
27+
const framework::Scope& scope, bool test_mode) override {
28+
VLOG(4)
29+
<< "convert a fluid softmax op to tensorrt softmax layer without bias";
30+
framework::OpDesc op_desc(op, nullptr);
31+
// Declare inputs
32+
auto* input1 = engine_->GetITensor(op_desc.Input("X")[0]);
33+
auto* layer = TRT_ENGINE_ADD_LAYER(engine_, SoftMax,
34+
*const_cast<nvinfer1::ITensor*>(input1));
35+
36+
auto output_name = op_desc.Output("Out")[0];
37+
engine_->SetITensor(output_name, layer->getOutput(0));
38+
if (test_mode) {
39+
engine_->DeclareOutput(output_name);
40+
}
41+
}
42+
};
43+
44+
} // namespace tensorrt
45+
} // namespace inference
46+
} // namespace paddle
47+
48+
USE_OP(softmax);
49+
REGISTER_TRT_OP_CONVERTER(softmax, SoftMaxOpConverter);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
#include <gtest/gtest.h>
15+
#include "paddle/fluid/framework/op_registry.h"
16+
#include "paddle/fluid/inference/tensorrt/convert/ut_helper.h"
17+
18+
namespace paddle {
19+
namespace inference {
20+
namespace tensorrt {
21+
22+
TEST(SoftMaxOpConverter, main) {
23+
framework::Scope scope;
24+
std::unordered_set<std::string> parameters;
25+
TRTConvertValidation validator(8, parameters, scope, 1000);
26+
27+
std::vector<int> tensor_shape{8, 10};
28+
validator.DeclInputVar("softmax-X", tensor_shape,
29+
nvinfer1::DimsCHW(10, 1, 1));
30+
validator.DeclOutputVar("softmax-Out", nvinfer1::DimsCHW(10, 1, 1));
31+
32+
// Prepare Op description
33+
framework::OpDesc desc;
34+
desc.SetType("softmax");
35+
desc.SetInput("X", {"softmax-X"});
36+
desc.SetOutput("Out", {"softmax-Out"});
37+
38+
LOG(INFO) << "set OP";
39+
validator.SetOp(*desc.Proto());
40+
LOG(INFO) << "execute";
41+
42+
validator.Execute(3);
43+
}
44+
45+
} // namespace tensorrt
46+
} // namespace inference
47+
} // namespace paddle
48+
49+
USE_OP(softmax);

paddle/fluid/inference/tensorrt/convert/ut_helper.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ class TRTConvertValidation {
7979
}
8080

8181
// Declare a Variable as input with random initialization.
82+
void DeclInputVar(const std::string& name, const std::vector<int> tensor_dims,
83+
const nvinfer1::Dims& trt_dims) {
84+
DeclVar(name, tensor_dims);
85+
engine_->DeclareInput(name, nvinfer1::DataType::kFLOAT, trt_dims);
86+
}
87+
8288
void DeclInputVar(const std::string& name, const nvinfer1::Dims& dims) {
8389
DeclVar(name, dims);
8490
// Declare TRT inputs.
@@ -94,23 +100,27 @@ class TRTConvertValidation {
94100
DeclVar(name, dims);
95101
}
96102

97-
// Declare a variable in a fluid Scope.
98-
void DeclVar(const std::string& name, const nvinfer1::Dims& dims,
99-
bool is_param = false) {
103+
void DeclVar(const std::string& name, const std::vector<int> dim_vec) {
100104
platform::CPUPlace place;
101105
platform::CPUDeviceContext ctx(place);
102106

107+
auto* x = scope_.Var(name);
108+
auto* x_tensor = x->GetMutable<framework::LoDTensor>();
109+
x_tensor->Resize(framework::make_ddim(dim_vec));
110+
RandomizeTensor(x_tensor, place, ctx);
111+
}
112+
// Declare a variable in a fluid Scope.
113+
void DeclVar(const std::string& name, const nvinfer1::Dims& dims,
114+
bool is_param = false) {
103115
// Init Fluid tensor.
104116
std::vector<int> dim_vec(dims.d, dims.d + dims.nbDims);
105117
// There is no batchsize in ITensor's shape, but We should add it to
106118
// tensor's shape of fluid. If the variable is not parameter and the
107119
// if_add_batch_ flag is true, add the max batchsize to dim_vec.
108120
if (is_param != true && if_add_batch_ == true)
109121
dim_vec.insert(dim_vec.begin(), max_batch_size_);
110-
auto* x = scope_.Var(name);
111-
auto* x_tensor = x->GetMutable<framework::LoDTensor>();
112-
x_tensor->Resize(framework::make_ddim(dim_vec));
113-
RandomizeTensor(x_tensor, place, ctx);
122+
123+
DeclVar(name, dim_vec);
114124
}
115125

116126
void SetOp(const framework::proto::OpDesc& desc) {

0 commit comments

Comments
 (0)