-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
[Paddle Inference] Add add eye trt converter #48937
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3252fed
test with CI
DrRyanHuang 232d40b
codestyle check
DrRyanHuang 50c0645
fix some small bug
DrRyanHuang 9188665
fix const int error
DrRyanHuang 2b1f7f6
fix vector type error
DrRyanHuang 77abd76
phi::DataType => paddle::framework::proto::VarType
DrRyanHuang 035738f
fix cross init
DrRyanHuang d9e950b
del first typedef float T
DrRyanHuang 232ea8d
fix trt_type
DrRyanHuang 9438eb3
size_t => int
DrRyanHuang 5657708
fix unique_ptr array
DrRyanHuang 58553ce
fix not handle | test add dtype case
DrRyanHuang 4df68b8
switch => if
DrRyanHuang 25accfd
fix key_error
DrRyanHuang b778044
fix Nonetype don't have item
DrRyanHuang d7058af
fix only one params
DrRyanHuang def9b04
test case without -1
DrRyanHuang 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 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 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 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,110 @@ | ||
/* Copyright (c) 2018 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. */ | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <iterator> | ||
|
||
#include "paddle/fluid/inference/tensorrt/convert/op_converter.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
class Scope; | ||
|
||
namespace proto { | ||
class OpDesc; | ||
} // namespace proto | ||
} // namespace framework | ||
} // namespace paddle | ||
|
||
namespace paddle { | ||
namespace inference { | ||
namespace tensorrt { | ||
|
||
/* | ||
* EyeOp. | ||
*/ | ||
class EyeOpConverter : public OpConverter { | ||
public: | ||
void operator()(const framework::proto::OpDesc& op, | ||
const framework::Scope& scope, | ||
bool test_mode) override { | ||
VLOG(3) << "convert a fluid eye op with tensorrt Constant layer"; | ||
framework::OpDesc op_desc(op, nullptr); | ||
|
||
// Declare inputs attr | ||
const int num_rows = PADDLE_GET_CONST(int, op_desc.GetAttr("num_rows")); | ||
int num_columns = PADDLE_GET_CONST(int, op_desc.GetAttr("num_columns")); | ||
const int dtype = PADDLE_GET_CONST(int, op_desc.GetAttr("dtype")); | ||
|
||
// Set data dim | ||
nvinfer1::Dims input_shape; | ||
input_shape.nbDims = 2; | ||
if (-1 == num_columns) { | ||
num_columns = num_rows; | ||
} | ||
input_shape.d[0] = num_rows; | ||
input_shape.d[1] = num_columns; | ||
const int data_len = num_rows * num_columns; | ||
const int num_min = std::min(num_rows, num_columns); | ||
|
||
// Set data type | ||
void* trt_data = nullptr; | ||
nvinfer1::DataType nv_type = nvinfer1::DataType::kFLOAT; | ||
if (2 == dtype) { | ||
nv_type = nvinfer1::DataType::kINT32; | ||
std::unique_ptr<int32_t[]> data(new int32_t[data_len]()); | ||
for (int i = 0; i < num_min; i++) { | ||
data[i * num_columns + i] = 1; | ||
} | ||
trt_data = static_cast<void*>(data.get()); | ||
} else if (4 == dtype) { | ||
nv_type = nvinfer1::DataType::kHALF; | ||
std::unique_ptr<float16[]> data(new float16[data_len]()); | ||
for (int i = 0; i < num_min; i++) { | ||
data[i * num_columns + i] = 1; | ||
} | ||
trt_data = static_cast<void*>(data.get()); | ||
} else if (5 == dtype) { | ||
nv_type = nvinfer1::DataType::kFLOAT; | ||
std::unique_ptr<float[]> data(new float[data_len]()); | ||
for (int i = 0; i < num_min; i++) { | ||
data[i * num_columns + i] = 1; | ||
} | ||
trt_data = static_cast<void*>(data.get()); | ||
} else { | ||
paddle::platform::errors::InvalidArgument( | ||
"Paddle-TRT loads weighths failed, found not supported data type " | ||
"%s.", | ||
dtype); | ||
} | ||
|
||
auto* layer = | ||
TRT_ENGINE_ADD_LAYER(engine_, | ||
Constant, | ||
input_shape, | ||
nvinfer1::Weights{nv_type, trt_data, data_len}); | ||
|
||
std::string output_name = op_desc.Output("Out").front(); | ||
|
||
RreplenishLayerAndOutput(layer, "eye", {output_name}, test_mode); | ||
} | ||
}; | ||
|
||
} // namespace tensorrt | ||
} // namespace inference | ||
} // namespace paddle | ||
|
||
USE_OP_ITSELF(eye); | ||
REGISTER_TRT_OP_CONVERTER(eye, EyeOpConverter); |
This file contains 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 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
109 changes: 109 additions & 0 deletions
109
python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_eye.py
This file contains 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,109 @@ | ||
# Copyright (c) 2021 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 unittest | ||
from typing import List, Tuple | ||
|
||
import numpy as np | ||
from program_config import ProgramConfig | ||
from trt_layer_auto_scan_test import TrtLayerAutoScanTest | ||
|
||
import paddle.inference as paddle_infer | ||
|
||
|
||
class TrtConvertLeakyEyeTest(TrtLayerAutoScanTest): | ||
def is_program_valid(self, program_config: ProgramConfig) -> bool: | ||
return True | ||
|
||
def sample_program_configs(self): | ||
def generate_input_attr(): | ||
return np.random.randint(1, 320, size=2) | ||
# if np.random.random() > 0.5: | ||
# return np.random.randint(1, 320, size=2) | ||
# return np.random.randint(1, 320), -1 | ||
|
||
for dtype in [2, 4, 5]: | ||
for _ in range(6): | ||
|
||
num_rows, num_columns = generate_input_attr() | ||
attr_dic = { | ||
"num_rows": num_rows, | ||
"num_columns": num_columns, | ||
"dtype": dtype, | ||
} | ||
|
||
ops_config = [ | ||
{ | ||
"op_type": "eye", | ||
"op_outputs": { | ||
"Out": ["out_data"], | ||
}, | ||
"op_attrs": attr_dic, | ||
} | ||
] | ||
ops = self.generate_op_config(ops_config) | ||
program_config = ProgramConfig( | ||
ops=ops, | ||
weights={}, | ||
inputs={}, | ||
outputs=["out_data"], | ||
) | ||
|
||
yield program_config | ||
|
||
def sample_predictor_configs( | ||
self, program_config | ||
) -> Tuple[paddle_infer.Config, List[int], float]: | ||
def generate_dynamic_shape(attrs): | ||
pass | ||
|
||
def clear_dynamic_shape(): | ||
pass | ||
|
||
def generate_trt_nodes_num(attrs, dynamic_shape): | ||
return 1, 2 | ||
|
||
attrs = [ | ||
program_config.ops[i].attrs for i in range(len(program_config.ops)) | ||
] | ||
|
||
# for static_shape | ||
clear_dynamic_shape() | ||
self.trt_param.precision = paddle_infer.PrecisionType.Float32 | ||
yield self.create_inference_config(), generate_trt_nodes_num( | ||
attrs, False | ||
), 1e-5 | ||
self.trt_param.precision = paddle_infer.PrecisionType.Half | ||
yield self.create_inference_config(), generate_trt_nodes_num( | ||
attrs, False | ||
), 1e-3 | ||
|
||
# for dynamic_shape | ||
generate_dynamic_shape(attrs) | ||
self.trt_param.precision = paddle_infer.PrecisionType.Float32 | ||
yield self.create_inference_config(), generate_trt_nodes_num( | ||
attrs, True | ||
), 1e-5 | ||
self.trt_param.precision = paddle_infer.PrecisionType.Half | ||
yield self.create_inference_config(), generate_trt_nodes_num( | ||
attrs, True | ||
), 1e-3 | ||
|
||
def test(self): | ||
self.run_test() | ||
|
||
|
||
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.
可以改成