-
Notifications
You must be signed in to change notification settings - Fork 5.9k
bugfix/trt op with kernel #11408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Superjomn
wants to merge
11
commits into
PaddlePaddle:develop
from
Superjomn:feature/trt_manul_benchmark
Closed
bugfix/trt op with kernel #11408
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
93d1a95
update
Superjomn 88db515
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Superjomn a64f888
Update
Superjomn 8178fc9
Update
Superjomn 8b237d5
Update
Superjomn 414c9c4
Update
Superjomn e7d2160
Update
6658c0b
Update engine.h
Superjomn 64eb697
Update tensorrt_engine_op.h
Superjomn a59bcf1
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Superjomn 8d65d52
Merge branch 'feature/trt_manul_benchmark' of https://github.com/Supe…
Superjomn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,14 @@ | |
| #include "paddle/fluid/framework/operator.h" | ||
| #include "paddle/fluid/inference/analysis/helper.h" | ||
| #include "paddle/fluid/inference/tensorrt/engine.h" | ||
| #include "paddle/fluid/inference/tensorrt/engine.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| using inference::Singleton; | ||
| using inference::tensorrt::TRT_EngineManager; | ||
|
|
||
| class TensorRTEngineOp : public framework::OperatorWithKernel { | ||
| public: | ||
| using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
|
@@ -47,37 +51,39 @@ template <typename DeviceContext, typename T> | |
| class TensorRTEngineKernel : public framework::OpKernel<T> { | ||
| public: | ||
| void Compute(const framework::ExecutionContext& context) const override { | ||
| if (!engine_) { | ||
| auto engine_name = context.Attr<std::string>("engine_uniq_key"); | ||
| if (!Singleton<TRT_EngineManager>::Global().HasEngine(engine_name)) { | ||
| Prepare(context); | ||
| } | ||
| auto* engine = Singleton<TRT_EngineManager>::Global().Get(engine_name); | ||
| auto input_names = context.op().Inputs("Xs"); | ||
| PADDLE_ENFORCE(!input_names.empty(), "should pass more than one inputs"); | ||
| // Try to determine a batch_size | ||
| auto& tensor0 = inference::analysis::GetFromScope<framework::LoDTensor>( | ||
| context.scope(), input_names.front()); | ||
| int batch_size = tensor0.dims()[0]; | ||
| PADDLE_ENFORCE_LE(batch_size, max_batch_); | ||
| PADDLE_ENFORCE_LE(batch_size, context.Attr<int>("max_batch")); | ||
|
|
||
| // Convert input tensor from fluid to engine. | ||
| for (const auto& x : context.Inputs("Xs")) { | ||
| // convert input and copy to TRT engine's buffer | ||
| auto& t = inference::analysis::GetFromScope<framework::LoDTensor>( | ||
| context.scope(), x); | ||
| if (platform::is_cpu_place(t.place())) { | ||
| engine_->SetInputFromCPU(x, static_cast<const void*>(t.data<void>()), | ||
| t.memory_size()); | ||
| engine->SetInputFromCPU(x, static_cast<const void*>(t.data<void>()), | ||
| t.memory_size()); | ||
| } else { | ||
| engine_->SetInputFromGPU(x, static_cast<const void*>(t.data<void>()), | ||
| t.memory_size()); | ||
| engine->SetInputFromGPU(x, static_cast<const void*>(t.data<void>()), | ||
| t.memory_size()); | ||
| } | ||
| } | ||
| // Execute the engine. | ||
| PADDLE_ENFORCE_GT(batch_size, 0); | ||
| engine_->Execute(batch_size); | ||
| engine->Execute(batch_size); | ||
| // Convert output tensor from engine to fluid | ||
| for (const auto& y : context.Outputs("Ys")) { | ||
| // convert output and copy to fluid. | ||
| nvinfer1::ITensor* trt_t = engine_->GetITensor(y); | ||
| nvinfer1::ITensor* trt_t = engine->GetITensor(y); | ||
| auto dims = trt_t->getDimensions(); | ||
| // Use the output ITensor's dims to reshape the Fluid Tensor. | ||
| std::vector<int> ddim(dims.d, dims.d + dims.nbDims); | ||
|
|
@@ -89,27 +95,26 @@ class TensorRTEngineKernel : public framework::OpKernel<T> { | |
| auto size = inference::analysis::AccuDims(dims.d, dims.nbDims); | ||
| if (platform::is_cpu_place(fluid_t->place())) { | ||
| // TODO(Superjomn) change this float to dtype size. | ||
| engine_->GetOutputInCPU( | ||
| engine->GetOutputInCPU( | ||
| y, fluid_t->mutable_data<float>(platform::CPUPlace()), | ||
| size * sizeof(float)); | ||
| } else { | ||
| engine_->GetOutputInGPU( | ||
| engine->GetOutputInGPU( | ||
| y, fluid_t->mutable_data<float>(platform::CUDAPlace()), | ||
| size * sizeof(float)); | ||
| } | ||
| } | ||
|
|
||
| cudaStreamSynchronize(stream_); | ||
| cudaStreamSynchronize(*engine->stream()); | ||
| } | ||
|
|
||
| protected: | ||
| // Build the engine. | ||
| void Prepare(const framework::ExecutionContext& context) const; | ||
|
|
||
| private: | ||
| mutable cudaStream_t stream_; | ||
| mutable inference::tensorrt::TensorRTEngine* engine_{nullptr}; | ||
| mutable int max_batch_{0}; | ||
| // TODO(Superjomn) replace this stream with context's stream. | ||
| // mutable cudaStream_t stream_; | ||
|
||
| }; | ||
|
|
||
| } // namespace operators | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create an engine called
name