-
Notifications
You must be signed in to change notification settings - Fork 5.9k
add inference api:exp_specify_tensorrt_subgraph_precision #62402
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c41f15b
la
lizexu123 1364824
la
lizexu123 18ac0f2
单测
lizexu123 0a9a6ab
添加了bf16的支持
lizexu123 3af79fd
只需要一个参数即可
lizexu123 911d6c8
kg
lizexu123 57d4c86
kf
lizexu123 75d0a33
解决冲突
lizexu123 fbc6c37
ll
lizexu123 f56c99f
修改意见
lizexu123 339871f
解释api含义
lizexu123 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
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 |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| import numpy as np | ||
|
|
||
| import paddle | ||
| from paddle import nn, static | ||
| from paddle.inference import Config, PrecisionType, create_predictor | ||
|
|
||
| paddle.enable_static() | ||
|
|
||
|
|
||
| class SimpleNet(nn.Layer): | ||
| def __init__(self): | ||
| super().__init__() | ||
| self.conv1 = nn.Conv2D( | ||
| in_channels=4, | ||
| out_channels=4, | ||
| kernel_size=3, | ||
| stride=2, | ||
| padding=0, | ||
| ) | ||
| self.relu1 = nn.ReLU() | ||
| self.conv2 = nn.Conv2D( | ||
| in_channels=4, | ||
| out_channels=2, | ||
| kernel_size=3, | ||
| stride=2, | ||
| padding=0, | ||
| ) | ||
| self.relu2 = nn.ReLU() | ||
| self.conv3 = nn.Conv2D( | ||
| in_channels=2, | ||
| out_channels=1, | ||
| kernel_size=3, | ||
| stride=2, | ||
| padding=0, | ||
| ) | ||
| self.relu3 = nn.ReLU() | ||
| self.flatten = nn.Flatten() | ||
| self.fc = nn.Linear(729, 10) | ||
| self.softmax = nn.Softmax() | ||
|
|
||
| def forward(self, x): | ||
| x = self.conv1(x) | ||
| x = self.relu1(x) | ||
| x = self.conv2(x) | ||
| x = self.relu2(x) | ||
| x = self.conv3(x) | ||
| x = self.relu3(x) | ||
| x = self.flatten(x) | ||
| x = self.fc(x) | ||
| x = self.softmax(x) | ||
| return x | ||
|
|
||
|
|
||
| class TestTRTOptimizationLevel(unittest.TestCase): | ||
| def setUp(self): | ||
| self.place = paddle.CUDAPlace(0) | ||
| self.temp_dir = tempfile.TemporaryDirectory() | ||
| self.path = os.path.join(self.temp_dir.name, 'optimization_level', '') | ||
| self.model_prefix = self.path + 'infer_model' | ||
|
|
||
| def tearDown(self): | ||
| shutil.rmtree(self.path) | ||
|
|
||
| def build_model(self): | ||
| image = static.data( | ||
| name='img', shape=[None, 4, 224, 224], dtype='float32' | ||
| ) | ||
| predict = SimpleNet()(image) | ||
| exe = paddle.static.Executor(self.place) | ||
| exe.run(paddle.static.default_startup_program()) | ||
| paddle.static.save_inference_model( | ||
| self.model_prefix, [image], [predict], exe | ||
| ) | ||
|
|
||
| def init_predictor(self): | ||
| config = Config( | ||
| self.model_prefix + '.pdmodel', self.model_prefix + '.pdiparams' | ||
| ) | ||
| config.enable_use_gpu(256, 0, PrecisionType.Float32) | ||
| config.exp_disable_tensorrt_ops(["relu_1.tmp_0"]) | ||
| config.enable_tensorrt_engine( | ||
| workspace_size=1 << 30, | ||
| max_batch_size=1, | ||
| min_subgraph_size=3, | ||
| precision_mode=PrecisionType.Float32, | ||
| use_static=False, | ||
| use_calib_mode=False, | ||
| ) | ||
|
|
||
| config.exp_specify_tensorrt_subgraph_precision( | ||
| ["conv2d_1.w_0"], [""], ["conv2d_2.w_0"] | ||
| ) | ||
|
|
||
| config.enable_memory_optim() | ||
| # config.disable_glog_info() | ||
| config.set_tensorrt_optimization_level(0) | ||
| self.assertEqual(config.tensorrt_optimization_level(), 0) | ||
| predictor = create_predictor(config) | ||
| return predictor | ||
|
|
||
| def infer(self, predictor, img): | ||
| input_names = predictor.get_input_names() | ||
| for i, name in enumerate(input_names): | ||
| input_tensor = predictor.get_input_handle(name) | ||
| input_tensor.reshape(img[i].shape) | ||
| input_tensor.copy_from_cpu(img[i].copy()) | ||
|
|
||
| predictor.run() | ||
| results = [] | ||
| output_names = predictor.get_output_names() | ||
| for i, name in enumerate(output_names): | ||
| output_tensor = predictor.get_output_handle(name) | ||
| output_data = output_tensor.copy_to_cpu() | ||
| results.append(output_data) | ||
| return results | ||
|
|
||
| def test_optimization_level(self): | ||
| self.build_model() | ||
| predictor = self.init_predictor() | ||
| img = np.ones((1, 4, 224, 224), dtype=np.float32) | ||
| results = self.infer(predictor, img=[img]) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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.
下个PR把这几个api都加上注释
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.
这最后一个PR了,我改下吧