未验证 提交 e26a2504 编写于 作者: X xiaoxiaohehe001 提交者: GitHub

[Paddle Inference]Add split op TRT converter unittest. (#35127)

* add_split_op

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller

* add_split_teller
上级 09f920a2
......@@ -49,43 +49,21 @@ class SplitOpConverter : public OpConverter {
} else {
axis += (axis < 0) ? input_dims.nbDims : -1;
}
PADDLE_ENFORCE_NE(input_dims.d[axis], -1,
platform::errors::InvalidArgument(
"The (%d) dim of input should not be -1", axis));
if (num > 0) {
int64_t in_axis_dim = input_dims.d[axis];
PADDLE_ENFORCE_EQ(
in_axis_dim % num, 0,
platform::errors::InvalidArgument(
"Invalid number to split. Tensor split does not result"
" in an equal division of dimensions. Axis dim = %d %% num = %d "
"!= 0",
in_axis_dim, num));
size_t out_axis_dim = in_axis_dim / num;
for (int i = 0; i < num; ++i) {
output_lengths.push_back(out_axis_dim);
}
}
PADDLE_ENFORCE_EQ(
output_lengths.size(), output_num,
platform::errors::InvalidArgument(
"The output_length should be equal to the output size."));
nvinfer1::ILayer* layer = nullptr;
if (engine_->with_dynamic_shape()) {
#if IS_TRT_VERSION_GE(6000)
bool with_fp16 =
engine_->WithFp16() && !engine_->disable_trt_plugin_fp16();
plugin::SplitPluginDynamic* plugin =
new plugin::SplitPluginDynamic(axis, output_lengths, with_fp16);
layer = engine_->AddDynamicPlugin(&input, input_num, plugin);
#else
PADDLE_THROW(platform::errors::Fatal(
"You are running the TRT Dynamic Shape mode, need to confirm that "
"your TRT version is no less than 6.0"));
#endif
} else {
bool with_fp16 =
engine_->WithFp16() && !engine_->disable_trt_plugin_fp16();
......
......@@ -577,16 +577,78 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
<< desc.Input("X").size() << ".";
return false;
}
auto split_inputs = desc.Inputs();
if (split_inputs.find("AxisTensor") != split_inputs.end()) {
if (desc.Input("AxisTensor").size() >= 1) {
return false;
}
}
if (split_inputs.find("SectionsTensorList") != split_inputs.end()) {
if (desc.Input("SectionsTensorList").size() >= 1) {
return false;
}
}
if (!desc.HasAttr("axis")) {
return false;
} else {
int axis = BOOST_GET_CONST(int, desc.GetAttr("axis"));
if (axis == 0) {
VLOG(3) << "Invalid split axis. Split on batch is not supported in "
"TensorRT";
return false;
}
int axis = BOOST_GET_CONST(int, desc.GetAttr("axis"));
if (axis == 0) {
VLOG(3) << "Invalid split axis. Split on batch is not supported in "
"TensorRT";
return false;
}
auto* block = desc.Block();
auto x_var_name = desc.Input("X")[0];
auto* x_var_desc = block->FindVar(x_var_name);
const auto x_shape = x_var_desc->GetShape();
size_t output_num = desc.Output("Out").size();
std::vector<int> output_lengths;
int num = 0;
if (desc.HasAttr("num")) {
num = BOOST_GET_CONST(int, desc.GetAttr("num"));
}
if (desc.HasAttr("sections")) {
output_lengths =
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("sections"));
}
if (output_lengths.size() == 0 && num == 0) {
VLOG(3) << "sections and num cannot be equal to 0 at the same time";
return false;
}
if (with_dynamic_shape) {
#if IS_TRT_VERSION_GE(6000)
#else
VLOG(3) << "You are running the TRT Dynamic Shape mode, need to "
"confirm that "
"your TRT version is no less than 6.0";
return false;
#endif
}
axis += (axis < 0) ? x_shape.size() : 0;
if (x_shape[axis] == -1) {
VLOG(3) << "The (" << axis << ") dim of input should not be -1";
return false;
}
if (output_lengths.size() == 0) {
if (num > 0) {
int64_t in_axis_dim = x_shape[axis];
if (in_axis_dim % num != 0) {
VLOG(3) << "Invalid number to split. Tensor split does not result"
" in an equal division of dimensions. Axis dim = "
<< in_axis_dim << " num = " << num << "!= 0";
return false;
}
size_t out_axis_dim = in_axis_dim / num;
for (int i = 0; i < num; ++i) {
output_lengths.push_back(out_axis_dim);
}
}
}
if (output_lengths.size() != output_num) {
VLOG(3) << "The output_length should be equal to the output size.";
return false;
}
}
if (op_type == "scale") {
auto scale_inputs = desc.Inputs();
......
# 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.
from trt_layer_auto_scan_test import TrtLayerAutoScanTest, SkipReasons
from program_config import TensorConfig, ProgramConfig
import numpy as np
import paddle.inference as paddle_infer
from functools import partial
from typing import Optional, List, Callable, Dict, Any, Set
class TrtConvertSplitTest(TrtLayerAutoScanTest):
def is_program_valid(self, program_config: ProgramConfig) -> bool:
inputs = program_config.inputs
weights = program_config.weights
outputs = program_config.outputs
attrs = [
program_config.ops[i].attrs
for i in range(len(program_config.ops))
]
# the dimensions of input and axis match
if len(inputs['split_input'].shape) <= attrs[0]['axis']:
return False
#Sections and num cannot both be equal to 0.
if len(attrs[0]['sections']) == 0:
if attrs[0]['num'] == 0:
return False
#When sections and num are not both equal to 0, sections has higher priority.
#The sum of sections should be equal to the input size.
if len(attrs[0]['sections']) != 0:
if attrs[0]['num'] != 0:
return False
if len(outputs) != len(attrs[0]['sections']):
return False
sum = 0
for num in attrs[0]['sections']:
sum += num
if sum != inputs['split_input'].shape[attrs[0]['axis']]:
return False
#The size of num should be equal to the input dimension.
if attrs[0]['num'] != 0:
if len(outputs) != attrs[0]['num']:
return False
#Test AxisTensor and SectionsTensorList
if self.num_input == 0:
if self.dims == 2 and attrs[0]['sections'] == [10, 14] and len(
outputs) == 2:
return True
else:
return False
return True
def sample_program_configs(self):
def generate_input1(attrs: List[Dict[str, Any]], batch):
if self.dims == 4:
return np.ones([batch, 3, 3, 24]).astype(np.float32)
elif self.dims == 3:
return np.ones([batch, 3, 24]).astype(np.float32)
elif self.dims == 2:
return np.ones([batch, 24]).astype(np.float32)
elif self.dims == 1:
return np.ones([24]).astype(np.float32)
def generate_AxisTensor(attrs: List[Dict[str, Any]]):
return np.ones([1]).astype(np.int32)
def generate_SectionsTensorList1(attrs: List[Dict[str, Any]]):
return np.array([10]).astype(np.int32)
def generate_SectionsTensorList2(attrs: List[Dict[str, Any]]):
return np.array([14]).astype(np.int32)
for num_input in [0, 1]:
for dims in [1, 2, 3, 4]:
for batch in [3, 6, 9]:
for Out in [["output_var0", "output_var1"],
["output_var0", "output_var1", "output_var2"]]:
for sections in [[], [1, 2], [2, 1], [10, 14],
[1, 1, 1], [2, 2, 2], [3, 3, 3],
[3, 7, 14]]:
for num in [0, 3]:
for axis in [0, 1, 2, 3]:
self.batch = batch
self.num_input = num_input
self.dims = dims
dics = [{
"sections": sections,
"num": num,
"axis": axis
}, {}]
dics_intput = [{
"X": ["split_input"],
"AxisTensor": ["AxisTensor"],
"SectionsTensorList": [
"SectionsTensorList1",
"SectionsTensorList2"
]
}, {
"X": ["split_input"]
}]
dics_intputs = [{
"AxisTensor":
TensorConfig(data_gen=partial(
generate_AxisTensor, dics)),
"SectionsTensorList1": TensorConfig(
data_gen=partial(
generate_SectionsTensorList1,
dics)),
"SectionsTensorList2":
TensorConfig(data_gen=partial(
generate_SectionsTensorList2, dics))
}, {}]
ops_config = [{
"op_type": "split",
"op_inputs": dics_intput[num_input],
"op_outputs": {
"Out": Out
},
"op_attrs": dics[0]
}]
ops = self.generate_op_config(ops_config)
program_config = ProgramConfig(
ops=ops,
weights=dics_intputs[num_input],
inputs={
"split_input":
TensorConfig(data_gen=partial(
generate_input1, dics, batch))
},
outputs=Out)
yield program_config
def sample_predictor_configs(
self, program_config) -> (paddle_infer.Config, List[int], float):
def generate_dynamic_shape(attrs):
if self.dims == 4:
self.dynamic_shape.min_input_shape = {
"split_input": [1, 3, 3, 24]
}
self.dynamic_shape.max_input_shape = {
"split_input": [9, 3, 3, 24]
}
self.dynamic_shape.opt_input_shape = {
"split_input": [1, 3, 3, 24]
}
elif self.dims == 3:
self.dynamic_shape.min_input_shape = {"split_input": [1, 3, 24]}
self.dynamic_shape.max_input_shape = {"split_input": [9, 3, 24]}
self.dynamic_shape.opt_input_shape = {"split_input": [1, 3, 24]}
elif self.dims == 2:
self.dynamic_shape.min_input_shape = {"split_input": [1, 24]}
self.dynamic_shape.max_input_shape = {"split_input": [9, 24]}
self.dynamic_shape.opt_input_shape = {"split_input": [1, 24]}
elif self.dims == 1:
self.dynamic_shape.min_input_shape = {"split_input": [24]}
self.dynamic_shape.max_input_shape = {"split_input": [24]}
self.dynamic_shape.opt_input_shape = {"split_input": [24]}
def clear_dynamic_shape():
self.dynamic_shape.min_input_shape = {}
self.dynamic_shape.max_input_shape = {}
self.dynamic_shape.opt_input_shape = {}
def generate_trt_nodes_num(attrs, dynamic_shape):
if len(program_config.outputs) == 2:
if attrs[0]['axis'] != 0:
return 1, 3
else:
return 0, 4
else:
if attrs[0]['axis'] != 0:
return 1, 4
else:
return 0, 5
attrs = [
program_config.ops[i].attrs
for i in range(len(program_config.ops))
]
self.trt_param.max_batch_size = 9
# 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-5
# 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-5
def add_skip_trt_case(self):
def teller1(program_config, predictor_config):
if len(program_config.weights) == 3:
return True
return False
self.add_skip_case(
teller1, SkipReasons.TRT_NOT_SUPPORT,
"INPUT AxisTensor AND SectionsTensorList NOT SUPPORT.")
def test(self):
self.add_skip_trt_case()
self.run_test()
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册