Skip to content

Commit 4646c0f

Browse files
authored
Merge pull request #10144 from luotao1/tr_convert_init
tensorrt convert init
2 parents 3bb99c4 + beb1245 commit 4646c0f

File tree

11 files changed

+372
-7
lines changed

11 files changed

+372
-7
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
if(WITH_TESTING)
2-
nv_test(test_tensorrt SRCS test_tensorrt.cc DEPS dynload_cuda device_context dynamic_loader)
3-
nv_test(test_tensorrt_engine SRCS test_engine.cc engine.cc DEPS dynload_cuda)
4-
endif()
1+
nv_test(test_tensorrt SRCS test_tensorrt.cc DEPS dynload_cuda device_context dynamic_loader)
2+
nv_test(test_tensorrt_engine SRCS test_engine.cc engine.cc DEPS dynload_cuda)
3+
set(ENGINE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/engine.cc)
4+
add_subdirectory(convert)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nv_test(test_tensorrt_op_converter SRCS test_op_converter.cc mul_op.cc conv2d_op.cc DEPS ${FLUID_CORE_MODULES})
2+
nv_test(test_tensorrt_activation_op SRCS test_activation_op.cc ${ENGINE_FILE} activation_op.cc
3+
DEPS ${FLUID_CORE_MODULES} activation_op)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
class ReluOpConverter : public OpConverter {
22+
public:
23+
ReluOpConverter() {}
24+
void operator()(const framework::OpDesc& op) override {
25+
LOG(INFO) << "convert a fluid relu op to tensorrt activation layer whose "
26+
"type is Relu";
27+
const nvinfer1::ITensor* input_tensor =
28+
engine_->GetITensor(op.Input("X")[0]);
29+
nvinfer1::IActivationLayer* layer = TRT_ENGINE_ADD_LAYER(
30+
engine_, Activation, *const_cast<nvinfer1::ITensor*>(input_tensor),
31+
nvinfer1::ActivationType::kRELU);
32+
engine_->SetITensor(op.Output("Out")[0], layer->getOutput(0));
33+
}
34+
};
35+
36+
REGISTER_TRT_OP_CONVERTER(relu, ReluOpConverter);
37+
38+
} // namespace tensorrt
39+
} // namespace inference
40+
} // namespace paddle
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
class Conv2dOpConverter : public OpConverter {
22+
public:
23+
Conv2dOpConverter() {}
24+
void operator()(const framework::OpDesc& op) override {
25+
LOG(INFO)
26+
<< "convert a fluid conv2d op to tensorrt conv layer without bias";
27+
}
28+
};
29+
30+
REGISTER_TRT_OP_CONVERTER(conv2d, Conv2dOpConverter);
31+
32+
} // namespace tensorrt
33+
} // namespace inference
34+
} // namespace paddle
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
class MulOpConverter : public OpConverter {
22+
public:
23+
MulOpConverter() {}
24+
void operator()(const framework::OpDesc& op) override {
25+
LOG(INFO) << "convert a fluid mul op to tensorrt fc layer without bias";
26+
}
27+
};
28+
29+
REGISTER_TRT_OP_CONVERTER(mul, MulOpConverter);
30+
31+
} // namespace tensorrt
32+
} // namespace inference
33+
} // namespace paddle
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
#pragma once
16+
17+
#include <string>
18+
#include <unordered_map>
19+
#include "paddle/fluid/framework/block_desc.h"
20+
#include "paddle/fluid/framework/scope.h"
21+
#include "paddle/fluid/inference/tensorrt/engine.h"
22+
23+
namespace paddle {
24+
namespace inference {
25+
namespace tensorrt {
26+
27+
/*
28+
* Convert Op from Fluid to TensorRT Engine.
29+
*/
30+
class OpConverter {
31+
public:
32+
OpConverter() {}
33+
virtual void operator()(const framework::OpDesc& op) {}
34+
35+
void Execute(const framework::OpDesc& op, TensorRTEngine* engine) {
36+
std::string type = op.Type();
37+
auto it = converters_.find(type);
38+
PADDLE_ENFORCE(it != converters_.end(), "no OpConverter for optype [%s]",
39+
type);
40+
it->second->SetEngine(engine);
41+
(*it->second)(op);
42+
}
43+
44+
static OpConverter& Global() {
45+
static auto* x = new OpConverter;
46+
return *x;
47+
}
48+
49+
template <typename T>
50+
void Register(const std::string& key) {
51+
converters_[key] = new T;
52+
}
53+
54+
// convert fluid op to tensorrt layer
55+
void ConvertOp(const framework::OpDesc& op, TensorRTEngine* engine) {
56+
OpConverter::Global().Execute(op, engine);
57+
}
58+
59+
// convert fluid block to tensorrt network
60+
void ConvertBlock(const framework::BlockDesc& block, TensorRTEngine* engine) {
61+
for (auto op : block.AllOps()) {
62+
OpConverter::Global().Execute(*op, engine);
63+
}
64+
}
65+
66+
void SetEngine(TensorRTEngine* engine) { engine_ = engine; }
67+
68+
virtual ~OpConverter() {}
69+
70+
// TensorRT engine
71+
TensorRTEngine* engine_{nullptr};
72+
73+
private:
74+
// registered op converter map, whose key is the fluid op type, and value is
75+
// the pointer position of corresponding OpConverter class.
76+
std::unordered_map<std::string, OpConverter*> converters_;
77+
// fluid inference scope
78+
framework::Scope* scope_{nullptr};
79+
};
80+
81+
#define REGISTER_TRT_OP_CONVERTER(op_type__, Converter__) \
82+
struct trt_##op_type__##_converter { \
83+
trt_##op_type__##_converter() { \
84+
OpConverter::Global().Register<Converter__>(#op_type__); \
85+
} \
86+
}; \
87+
trt_##op_type__##_converter trt_##op_type__##_converter__;
88+
89+
} // namespace tensorrt
90+
} // namespace inference
91+
} // namespace paddle
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 <gtest/gtest.h>
16+
#include "paddle/fluid/framework/lod_tensor.h"
17+
#include "paddle/fluid/framework/op_registry.h"
18+
#include "paddle/fluid/framework/program_desc.h"
19+
#include "paddle/fluid/inference/tensorrt/convert/op_converter.h"
20+
#include "paddle/fluid/platform/device_context.h"
21+
#include "paddle/fluid/platform/place.h"
22+
23+
USE_OP(relu);
24+
25+
namespace paddle {
26+
namespace inference {
27+
namespace tensorrt {
28+
29+
void compare(float input, float expect) {
30+
framework::Scope scope;
31+
platform::CUDAPlace place;
32+
platform::CUDADeviceContext ctx(place);
33+
34+
// init fluid op and variable
35+
auto x_var = scope.Var("X");
36+
auto x_tensor = x_var->GetMutable<framework::LoDTensor>();
37+
x_tensor->Resize({1, 1});
38+
std::vector<float> init;
39+
init.push_back(input);
40+
framework::TensorFromVector(init, ctx, x_tensor);
41+
42+
auto out_var = scope.Var("Out");
43+
auto out_tensor = out_var->GetMutable<framework::LoDTensor>();
44+
out_tensor->Resize({1, 1});
45+
out_tensor->mutable_data<float>(place);
46+
47+
framework::OpDesc op_desc;
48+
op_desc.SetType("relu");
49+
op_desc.SetInput("X", {"X"});
50+
op_desc.SetOutput("Out", {"Out"});
51+
52+
auto relu_op = framework::OpRegistry::CreateOp(op_desc);
53+
54+
// run fluid op
55+
relu_op->Run(scope, place);
56+
std::vector<float> out1;
57+
framework::TensorToVector(*out_tensor, ctx, &out1);
58+
59+
// init tensorrt op
60+
cudaStream_t stream;
61+
ASSERT_EQ(0, cudaStreamCreate(&stream));
62+
TensorRTEngine* engine = new TensorRTEngine(1, 1 << 10, &stream);
63+
engine->InitNetwork();
64+
engine->DeclareInput("X", nvinfer1::DataType::kFLOAT,
65+
nvinfer1::DimsCHW{1, 1, 1});
66+
67+
OpConverter op_converter;
68+
op_converter.ConvertOp(op_desc, engine);
69+
70+
engine->DeclareOutput("Out");
71+
engine->FreezeNetwork();
72+
engine->SetInputFromCPU("X", &input, 1 * sizeof(float));
73+
74+
// run tensorrt op
75+
engine->Execute(1);
76+
77+
float out2;
78+
engine->GetOutputInCPU("Out", &out2, 1 * sizeof(float));
79+
80+
ASSERT_EQ(out1[0], out2);
81+
ASSERT_EQ(out1[0], expect);
82+
83+
delete engine;
84+
cudaStreamDestroy(stream);
85+
}
86+
87+
TEST(OpConverter, ConvertRelu) {
88+
compare(1, 1); // relu(1) = 1
89+
compare(-5, 0); // relu(-5) = 0
90+
}
91+
92+
} // namespace tensorrt
93+
} // namespace inference
94+
} // namespace paddle
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 <gtest/gtest.h>
16+
#include "paddle/fluid/framework/program_desc.h"
17+
#include "paddle/fluid/inference/tensorrt/convert/op_converter.h"
18+
19+
namespace paddle {
20+
namespace inference {
21+
namespace tensorrt {
22+
23+
TEST(BlockConverter, ConvertBlock) {
24+
framework::ProgramDesc prog;
25+
auto* block = prog.MutableBlock(0);
26+
auto* mul_op = block->AppendOp();
27+
mul_op->SetType("mul");
28+
auto* conv2d_op = block->AppendOp();
29+
conv2d_op->SetType("conv2d");
30+
31+
OpConverter converter;
32+
converter.ConvertBlock(*block, nullptr /*TensorRTEngine*/);
33+
}
34+
35+
} // namespace tensorrt
36+
} // namespace inference
37+
} // namespace paddle

0 commit comments

Comments
 (0)