chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:40:42 +08:00
commit e25996e7db
15472 changed files with 3536181 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
if(NOT WIN32 AND TENSORRT_FOUND)
file(
GLOB TEST_OPS
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
"test_*.py")
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")
foreach(TEST_OP ${TEST_OPS})
py_test_modules(${TEST_OP} MODULES ${TEST_OP})
endforeach()
set_tests_properties(test_converter_model_bert PROPERTIES TIMEOUT "500")
set_tests_properties(test_converter_model_dummy PROPERTIES TIMEOUT "500")
set_tests_properties(test_converter_model_resnet50 PROPERTIES TIMEOUT "500")
set_tests_properties(test_converter_model_resnet50_move PROPERTIES TIMEOUT
"500")
set_tests_properties(test_converter_conv PROPERTIES TIMEOUT "300")
set_tests_properties(test_export PROPERTIES TIMEOUT "500")
set_tests_properties(test_converter_norm PROPERTIES TIMEOUT "300")
set_tests_properties(test_converter_ops PROPERTIES TIMEOUT "600")
set_tests_properties(test_converter_stat PROPERTIES TIMEOUT "300")
set_tests_properties(test_converter_math PROPERTIES TIMEOUT "900")
set_tests_properties(test_converter_activation PROPERTIES TIMEOUT "900")
set_tests_properties(test_converter_others PROPERTIES TIMEOUT "300")
set_tests_properties(test_converter_manipulation PROPERTIES TIMEOUT "600")
set_tests_properties(test_converter_creation PROPERTIES TIMEOUT "300")
set_tests_properties(test_converter_attribute PROPERTIES TIMEOUT "300")
set_tests_properties(test_converter_common PROPERTIES TIMEOUT "500")
set_tests_properties(test_converter_input PROPERTIES TIMEOUT "500")
set_tests_properties(test_converter_vision PROPERTIES TIMEOUT "500")
set_tests_properties(test_converter_linalg PROPERTIES TIMEOUT "100")
set_tests_properties(test_converter_einsum PROPERTIES TIMEOUT "200")
set_tests_properties(test_converter_search PROPERTIES TIMEOUT "300")
set_tests_properties(test_converter_logic PROPERTIES TIMEOUT "300")
set_tests_properties(test_converter_pooling PROPERTIES TIMEOUT "300")
endif()
+13
View File
@@ -0,0 +1,13 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
#
# 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.
+226
View File
@@ -0,0 +1,226 @@
# 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 numpy as np
import paddle
from paddle import nn, static
from paddle.nn import TransformerEncoder, TransformerEncoderLayer
def get_r50_program():
paddle.enable_static()
from paddle.vision.models import wide_resnet50_2
with paddle.pir_utils.IrGuard():
infer_program = paddle.static.Program()
startup_program = paddle.static.Program()
with static.program_guard(infer_program, startup_program):
scope = paddle.static.global_scope()
input_data = paddle.static.data(
shape=[-1, 3, 224, 224], dtype='float32', name='input'
)
model = wide_resnet50_2()
model.eval()
output = model(input_data)
place = paddle.CUDAPlace(0)
exe = static.Executor(place)
exe.run(startup_program)
params = infer_program.global_block().all_parameters()
param_dict = {}
for v in params:
name = v.get_defining_op().attrs()["parameter_name"]
param_dict.update({name: np.array(scope.var(name).get_tensor())})
return infer_program, scope, param_dict
def get_r50_refit_program(save_path):
paddle.enable_static()
from paddle.vision.models import wide_resnet50_2
infer_program = paddle.static.Program()
startup_program = paddle.static.Program()
with paddle.static.program_guard(infer_program, startup_program):
scope = paddle.static.global_scope()
input_data = paddle.static.data(
shape=[-1, 3, 224, 224], dtype='float32', name='input'
)
model = wide_resnet50_2()
model.eval()
output = model(input_data)
place = paddle.CUDAPlace(0)
exe = paddle.static.Executor(place)
exe.run(startup_program)
_ = exe.run(
infer_program,
feed={'input': np.random.randn(1, 3, 224, 224).astype(np.float32)},
fetch_list=[output],
)
paddle.static.save_inference_model(
path_prefix=save_path,
feed_vars=[input_data],
fetch_vars=[output],
executor=exe,
program=infer_program,
)
params = infer_program.global_block().all_parameters()
param_dict = {}
for v in params:
name = v.get_defining_op().attrs()["parameter_name"]
param_dict.update({name: np.array(scope.var(name).get_tensor())})
return infer_program, scope, param_dict
def get_dummy_program():
paddle.enable_static()
with paddle.pir_utils.IrGuard():
main_program = paddle.static.Program()
default_startup_program = paddle.static.Program()
with paddle.static.program_guard(main_program, default_startup_program):
scope = paddle.static.global_scope()
input = paddle.static.data(
shape=[-1, 64], dtype='float32', name='input'
)
weight_numpy = np.random.rand(64, 64).astype('float32')
weight = paddle.create_parameter(
name="w",
shape=[64, 64],
dtype='float32',
attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Assign(weight_numpy)
),
)
bias_numpy = np.random.rand(64).astype('float32')
bias = paddle.create_parameter(
name="b",
shape=[64],
dtype='float32',
attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Assign(bias_numpy)
),
)
x = paddle.matmul(input, weight)
x_1 = paddle.add(x, bias)
x_1 = paddle.unsqueeze(x_1, axis=0)
x_1 = paddle.squeeze(x_1, axis=0)
y = paddle.nn.functional.relu(x_1)
y_gelu_1 = paddle.nn.functional.gelu(y)
y_gelu_2 = paddle.nn.functional.gelu(x_1)
# Concatenate the outputs of the two GELU operations
concat_out = paddle.concat([y_gelu_1, y_gelu_2], axis=-1)
output = paddle.unsqueeze(concat_out, axis=0)
exe = paddle.static.Executor(paddle.CUDAPlace(0))
exe.run(default_startup_program)
params = main_program.global_block().all_parameters()
param_dict = {}
# save parameters
for v in params:
name = v.get_defining_op().attrs()["parameter_name"]
param_dict.update({name: np.array(scope.var(name).get_tensor())})
return main_program, scope, param_dict
class BertModel(nn.Layer):
def __init__(
self,
vocab_size,
hidden_size=768,
num_hidden_layers=12,
num_attention_heads=12,
):
super().__init__()
self.embeddings = nn.Embedding(vocab_size, hidden_size)
encoder_layer = TransformerEncoderLayer(
hidden_size, num_attention_heads, hidden_size * 4
)
self.encoder = TransformerEncoder(encoder_layer, num_hidden_layers)
def forward(self, input_ids):
embeddings = self.embeddings(input_ids)
encoded_output = self.encoder(embeddings)
return encoded_output
def get_bert_program():
paddle.enable_static()
vocab_size = 30522 # BERT base vocab size
hidden_size = 768
num_hidden_layers = 2
num_attention_heads = 12
seq_length = 128
with paddle.pir_utils.IrGuard():
main_program = static.default_main_program()
startup_program = static.default_startup_program()
with static.program_guard(main_program, startup_program):
scope = paddle.static.global_scope()
input_ids = static.data(
name='input_ids', shape=[-1, -1], dtype='int64'
)
bert_model = BertModel(
vocab_size, hidden_size, num_hidden_layers, num_attention_heads
)
bert_model.eval()
logits = bert_model(input_ids)
place = (
paddle.CUDAPlace(0)
if paddle.is_compiled_with_cuda()
else paddle.CPUPlace()
)
pir_program = main_program
with (
paddle.pir_utils.IrGuard(),
paddle.static.program_guard(pir_program, startup_program),
):
x = np.ones([1, seq_length]).astype('int64')
executor = paddle.static.Executor(place)
executor.run(startup_program)
fetches = executor.run(
pir_program,
feed={"input_ids": x},
fetch_list=pir_program.list_vars()[-3],
)
params = main_program.global_block().all_parameters()
param_dict = {}
# save parameters
for v in params:
name = v.get_defining_op().attrs()["parameter_name"]
param_dict.update({name: np.array(scope.var(name).get_tensor())})
return pir_program, scope, param_dict
class SimpleGatherNet(nn.Layer):
def __init__(self):
super().__init__()
self.linear = paddle.nn.Linear(149600, 1)
def forward(self, map_vector_features, polyline_mask):
map_vector_features = map_vector_features[polyline_mask]
return map_vector_features
+357
View File
@@ -0,0 +1,357 @@
# 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 copy
import unittest
import numpy as np
import paddle
from paddle.base import core
from paddle.tensorrt.converter import PaddleToTensorRTConverter
from paddle.tensorrt.export import (
Input,
PrecisionMode,
TensorRTConfig,
)
from paddle.tensorrt.util import (
mark_builtin_op,
run_pir_pass,
run_trt_partition,
warmup_shape_infer,
)
class TensorRTBaseTest(unittest.TestCase):
def __init__(self, methodName='runTest'):
super().__init__(methodName)
self.python_api = None
self.api_args = None
self.program_config = None
self.min_shape = None
self.opt_shape = None
self.max_shape = None
self.target_marker_op = ""
self.dynamic_shape_data = {}
self.disable_passes = [
"constant_folding_pass",
"dead_code_elimination_pass",
]
def create_fake_program(self):
if self.python_api is None:
raise ValueError(
"The unittest must specify a python api that will be used for building pir program."
)
main_program = paddle.static.Program()
startup_program = paddle.static.Program()
with paddle.static.program_guard(main_program, startup_program):
api_args = copy.deepcopy(self.api_args)
for feed_name in self.program_config["feed_list"]:
if self.api_args[feed_name] is None:
continue
if isinstance(self.api_args[feed_name], dict):
new_list_args = []
for sub_arg_name, sub_arg_value in self.api_args[
feed_name
].items():
if (
feed_name in self.min_shape.keys()
and feed_name in self.opt_shape.keys()
and feed_name in self.max_shape.keys()
):
input_shape_without_dynamic_dim = (
sub_arg_value.shape[1:]
)
input_dynamic_shape = [-1]
input_dynamic_shape.extend(
input_shape_without_dynamic_dim
)
input_shape = input_dynamic_shape
else:
input_shape = []
input_shape_without_dynamic_dim = (
sub_arg_value.shape[0:]
)
input_shape.extend(input_shape_without_dynamic_dim)
input_dtype = sub_arg_value.dtype
input_data = paddle.static.data(
name=sub_arg_name,
shape=input_shape,
dtype=input_dtype,
)
new_list_args.append(input_data)
api_args[feed_name] = new_list_args
else:
empty_min_max_shape = (
self.min_shape is None
or self.max_shape is None
or self.opt_shape is None
)
if (
not empty_min_max_shape
and feed_name in self.min_shape.keys()
and feed_name in self.opt_shape.keys()
and feed_name in self.max_shape.keys()
):
# dynamic shape condition
input_shape_without_dynamic_dim = self.api_args[
feed_name
].shape[1:]
input_shape = [-1]
input_shape.extend(input_shape_without_dynamic_dim)
else:
input_shape = self.api_args[feed_name].shape
input_dtype = self.api_args[feed_name].dtype
input_data = paddle.static.data(
name=feed_name,
shape=input_shape,
dtype=input_dtype,
)
api_args[feed_name] = input_data
actual_args = []
for name, value in api_args.items():
actual_args.append(value)
output = self.python_api(*actual_args)
fetch_list = []
if isinstance(output, tuple):
fetch_list = [out for out in list(output) if out is not None]
else:
fetch_list.append(output)
return main_program, startup_program, fetch_list
def run_program(self, main_program, fetch_list):
place = (
paddle.CUDAPlace(0)
if core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
feed_data = dict() # noqa: C408
for feed_name in self.program_config["feed_list"]:
if self.api_args[feed_name] is None:
continue
if isinstance(self.api_args[feed_name], dict):
for sub_arg_name, sub_arg_value in self.api_args[
feed_name
].items():
feed_data[sub_arg_name] = sub_arg_value
else:
feed_data[feed_name] = self.api_args[feed_name]
ret = exe.run(main_program, feed=feed_data, fetch_list=fetch_list)
return ret
def prepare_feed(self):
for arg_name, arg_value in self.api_args.items():
# deal with condition that input is a list tensor
if (
isinstance(self.api_args[arg_name], list)
and arg_name in self.program_config["feed_list"]
):
new_list_args = dict() # noqa: C408
for i in range(len(self.api_args[arg_name])):
sub_arg_name = arg_name + str(i)
new_list_args[sub_arg_name] = self.api_args[arg_name][i]
self.api_args[arg_name] = new_list_args
def check_trt_result(self, rtol=1e-5, atol=1e-5, precision_mode="fp32"):
paddle.framework.set_flags({"FLAGS_trt_min_group_size": 1})
with paddle.pir_utils.IrGuard():
self.prepare_feed()
main_program, startup_program, fetch_list = (
self.create_fake_program()
)
place = (
paddle.CUDAPlace(0)
if core.is_compiled_with_cuda()
else paddle.CPUPlace()
)
exe = paddle.static.Executor(place)
# init all parameter
exe.run(startup_program)
fetch_num = len(fetch_list)
if isinstance(fetch_list[0], list):
fetch_index = [i for i, v in enumerate(fetch_list)]
else:
fetch_index = [v.index() for v in fetch_list]
output_expected = self.run_program(main_program, fetch_list)
min_shape_data = dict() # noqa: C408
opt_shape_data = dict() # noqa: C408
max_shape_data = dict() # noqa: C408
for feed_name in self.program_config["feed_list"]:
if self.api_args[feed_name] is None:
continue
if isinstance(self.api_args[feed_name], dict):
# shape_tensor
if (
feed_name not in self.min_shape.keys()
and feed_name not in self.max_shape.keys()
and feed_name not in self.opt_shape.keys()
):
for sub_feed_name, sub_feed_value in self.api_args[
feed_name
].items():
min_shape_data[sub_feed_name] = sub_feed_value
opt_shape_data[sub_feed_name] = sub_feed_value
max_shape_data[sub_feed_name] = sub_feed_value
continue
else:
# not shape_tensor
for i in range(len(self.min_shape[feed_name])):
sub_feed_name = feed_name + str(i)
min_shape_data[sub_feed_name] = np.random.randn(
*self.min_shape[feed_name][i]
).astype(
self.api_args[feed_name][sub_feed_name].dtype
)
opt_shape_data[sub_feed_name] = np.random.randn(
*self.opt_shape[feed_name][i]
).astype(
self.api_args[feed_name][sub_feed_name].dtype
)
max_shape_data[sub_feed_name] = np.random.randn(
*self.max_shape[feed_name][i]
).astype(
self.api_args[feed_name][sub_feed_name].dtype
)
else:
# shape_tensor is list
if (
feed_name not in self.min_shape.keys()
and feed_name not in self.max_shape.keys()
and feed_name not in self.opt_shape.keys()
):
min_shape_data[feed_name] = self.api_args[feed_name]
opt_shape_data[feed_name] = self.api_args[feed_name]
max_shape_data[feed_name] = self.api_args[feed_name]
continue
else:
if self.dynamic_shape_data:
min_shape_data[feed_name] = self.dynamic_shape_data[
feed_name
](self.min_shape[feed_name])
opt_shape_data[feed_name] = self.dynamic_shape_data[
feed_name
](self.opt_shape[feed_name])
max_shape_data[feed_name] = self.dynamic_shape_data[
feed_name
](self.max_shape[feed_name])
else:
min_shape_data[feed_name] = np.random.randn(
*self.min_shape[feed_name]
).astype(self.api_args[feed_name].dtype)
opt_shape_data[feed_name] = np.random.randn(
*self.opt_shape[feed_name]
).astype(self.api_args[feed_name].dtype)
max_shape_data[feed_name] = np.random.randn(
*self.max_shape[feed_name]
).astype(self.api_args[feed_name].dtype)
# run pir pass(including some constant fold pass, dead code elimination pass, fusion pass and trt_op_marker_pass)
main_program = run_pir_pass(
main_program,
disable_passes=self.disable_passes,
)
# delete unused op
for op in main_program.global_block().ops:
if (
op.name() == "builtin.constant"
or op.name() == "builtin.parameter"
):
if op.results()[0].use_empty():
main_program.global_block().remove_op(op)
scope = paddle.static.global_scope()
main_program = warmup_shape_infer(
main_program,
feeds=[min_shape_data, opt_shape_data, max_shape_data],
scope=scope,
)
for op in main_program.global_block().ops[::-1]:
# Remove all invalid fetch op
if op.name() == "pd_op.fetch":
main_program.global_block().remove_op(op)
# Adding marker labels to builtin ops facilitates convert processing, but they ultimately do not enter the TensorRT subgraph.
mark_builtin_op(main_program)
# run trt_sub_graph_extract_pass()
program_with_trt = run_trt_partition(main_program)
# run TRTConverter(would lower group_op into tensorrt_engine_op)
trt_config = None
input = Input(
min_input_shape=self.min_shape,
optim_input_shape=self.opt_shape,
max_input_shape=self.max_shape,
)
trt_config = TensorRTConfig(inputs=[input])
trt_config.disable_logging = False
if precision_mode == "fp16":
trt_config.precision_mode = PrecisionMode.FP16
converter = PaddleToTensorRTConverter(
program_with_trt, scope, trt_config
)
converter.convert_program_to_trt()
# check whether has trt op
has_trt_op = False
for op in program_with_trt.global_block().ops:
if op.name() == "pd_op.tensorrt_engine":
has_trt_op = True
self.assertEqual(has_trt_op, True)
trt_fetch_list = []
split_op = program_with_trt.global_block().ops[-1]
if split_op.name() == "builtin.split":
trt_fetch_list = [
split_op.result(index) for index in fetch_index
]
else:
raise ValueError(
"The last op of convert pir Program in test must be split op that is the next op of pd_op.engine."
)
output_trt = self.run_program(program_with_trt, trt_fetch_list)
# Check that the results are close to each other within a tolerance of 1e-3
for i in range(fetch_num):
np.testing.assert_allclose(
output_expected[i],
output_trt[i],
rtol=rtol,
atol=atol,
)
def check_marker(self, expected_result):
paddle.framework.set_flags({"FLAGS_trt_min_group_size": 1})
with paddle.pir_utils.IrGuard():
main_program, startup_program, fetch_list = (
self.create_fake_program()
)
main_program = run_pir_pass(
main_program,
disable_passes=self.disable_passes,
)
marker_result = False
for op in main_program.global_block().ops:
if op.name() == self.target_marker_op:
marker_result = op.attrs().get("__l_trt__", False)
self.assertEqual(marker_result, expected_result)
+599
View File
@@ -0,0 +1,599 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
class TestEluTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.elu
self.api_args = {
"x": np.random.randn(3).astype("float32"),
"alpha": 1.0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.opt_shape = {"x": [1]}
self.max_shape = {"x": [5]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
def softmax_wrapper(x, axis=-1):
softmax = paddle.nn.Softmax(axis=axis)
return softmax(x)
class TestSoftmaxCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = softmax_wrapper
self.api_args = {
"x": np.random.randn(2, 3, 3).astype("float32"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 3]}
self.opt_shape = {"x": [2, 3, 3]}
self.max_shape = {"x": [5, 3, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestSoftmaxCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = softmax_wrapper
self.api_args = {
"x": np.random.randn(2).astype("float32"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.opt_shape = {"x": [2]}
self.max_shape = {"x": [5]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestSoftmaxCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = softmax_wrapper
self.api_args = {
"x": np.random.randn(2, 3, 3).astype("float32"),
"axis": 1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 3]}
self.opt_shape = {"x": [2, 3, 3]}
self.max_shape = {"x": [5, 3, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestHardSigmoidTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.hardsigmoid
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestHardSwishTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.hardswish
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestReluTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.relu
self.api_args = {"x": np.random.randn(3).astype("float32")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.opt_shape = {"x": [1]}
self.max_shape = {"x": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestRelu6TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.relu6
self.api_args = {"x": np.random.randn(3).astype("float32")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.opt_shape = {"x": [2]}
self.max_shape = {"x": [5]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestTanhTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.tanh
self.api_args = {"x": np.random.randn(3).astype("float32")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.opt_shape = {"x": [1]}
self.max_shape = {"x": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestSigmoidTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.sigmoid
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestSoftplusTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.Softplus()
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestGeluTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.GELU()
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestGeluCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.GELU(True)
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestSiluFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.silu
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestSwishFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.swish
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestTanhShrinkOpFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.tanh_shrink
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestStanhFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.stanh
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"scale_a": 0.67,
"scale_b": 1.7159,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestCeluTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.celu
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"alpha": 1.0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestThresholdedReluTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.thresholded_relu
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"threshold": 1.0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result(rtol=1e-3, atol=1e-3)
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
class TestMishCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.mish
self.api_args = {
"x": np.random.randn(2).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.opt_shape = {"x": [2]}
self.max_shape = {"x": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestMishCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.mish
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestMishCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.mish
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4]}
self.opt_shape = {"x": [2, 3, 4]}
self.max_shape = {"x": [5, 3, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestMishCase4TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.mish
self.api_args = {
"x": np.random.randn(2, 3, 4, 2).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4, 2]}
self.opt_shape = {"x": [2, 3, 4, 2]}
self.max_shape = {"x": [5, 3, 4, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestLogSigmoidTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.log_sigmoid
x = np.random.random([1, 3, 32, 32]).astype(np.float32)
self.api_args = {
"x": x,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {
"x": [1, 3, 32, 32],
}
self.opt_shape = {"x": [4, 3, 32, 32]}
self.max_shape = {"x": [4, 3, 32, 32]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestSeluTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.selu
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestLeakyReluCas1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.leaky_relu
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"negative_slope": 0.5,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestLeakyReluCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.leaky_relu
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"negative_slope": -0.5,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestLeakyRelu_Cas1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.leaky_relu_
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"negative_slope": 0.5,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestLeakyRelu_Case2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.leaky_relu_
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"negative_slope": -0.5,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
def prelu_wrapper(x, alpha_shape, data_format='NCHW'):
alpha = paddle.create_parameter(
shape=alpha_shape, dtype='float32', name="alpha"
)
return paddle.nn.functional.prelu(x, alpha, data_format)
class TestPReluCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = prelu_wrapper
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"alpha_shape": [3],
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestPReluCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = prelu_wrapper
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"alpha_shape": [3],
"data_format": "NHWC",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestPReluCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = prelu_wrapper
self.api_args = {
"x": np.random.randn(2, 3, 3).astype("float32"),
"alpha_shape": [3],
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 3]}
self.opt_shape = {"x": [2, 3, 3]}
self.max_shape = {"x": [5, 3, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestPReluCase4TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = prelu_wrapper
self.api_args = {
"x": np.random.randn(2, 3, 3).astype("float32"),
"alpha_shape": [3],
"data_format": "NHWC",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 3]}
self.opt_shape = {"x": [2, 3, 3]}
self.max_shape = {"x": [5, 3, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
if __name__ == '__main__':
unittest.main()
+54
View File
@@ -0,0 +1,54 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
class TestShapeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.shape
self.api_args = {
"x": np.random.randn(2, 16).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 16]}
self.opt_shape = {"x": [2, 16]}
self.max_shape = {"x": [5, 16]}
def test_trt_result(self):
self.check_trt_result()
class TestShapeTRTCase1Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.shape
self.api_args = {
"x": np.random.randn(2, 16).astype("int64"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 16]}
self.opt_shape = {"x": [2, 16]}
self.max_shape = {"x": [5, 16]}
def test_trt_result(self):
self.check_trt_result()
if __name__ == '__main__':
unittest.main()
+742
View File
@@ -0,0 +1,742 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
from paddle import _C_ops
def dropout_wrapper(x, p, mode):
out = _C_ops.dropout(
x,
None,
p,
True,
mode,
0,
True,
)
return out
class TestDropoutWithUpscaleModeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = dropout_wrapper
self.api_args = {
"x": np.random.random([1, 2, 3]).astype("float32"),
"p": 0,
"mode": "upscale_in_train",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 3]}
self.opt_shape = {"x": [1, 2, 3]}
self.max_shape = {"x": [10, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestDropoutWithDowngradeModeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = dropout_wrapper
self.api_args = {
"x": np.random.random([1, 2, 3]).astype("float32"),
"p": 0,
"mode": "downgrade_in_infer",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 3]}
self.opt_shape = {"x": [1, 2, 3]}
self.max_shape = {"x": [10, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
def upsample_bilinear(x):
upsample = paddle.nn.Upsample(size=[12, 12], mode="bilinear")
return upsample(x)
def bilinear_python_api(x, OutSize, SizeTensor, Scale, attrs):
return _C_ops.bilinear_interp(
x,
OutSize,
SizeTensor,
Scale,
attrs['data_layout'],
attrs['out_d'],
attrs['out_h'],
attrs['out_w'],
attrs['scale'] if 'scale' in attrs else [],
attrs['interp_method'],
attrs['align_corners'],
attrs['align_mode'],
)
def nearest_python_api(x, OutSize, SizeTensor, Scale, attrs):
return _C_ops.nearest_interp(
x,
OutSize,
SizeTensor,
Scale,
attrs['data_layout'],
attrs['out_d'],
attrs['out_h'],
attrs['out_w'],
attrs['scale'] if 'scale' in attrs else [],
attrs['interp_method'],
attrs['align_corners'],
attrs['align_mode'],
)
def embedding_python_api(x, weight, attrs):
return _C_ops.embedding(
x,
weight,
attrs['padding_idx'],
attrs['sparse'],
)
def unbind_python_api(x, attrs):
return _C_ops.unbind(
x,
attrs['axis'],
)
class TestBilinearScaleTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = bilinear_python_api
self.api_args = {
"x": np.random.random([2, 3, 6, 10]).astype("float32"),
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"attrs": {
"data_layout": "NCHW",
"scale": [2.0, 2.0],
"out_h": 12,
"out_w": 12,
"out_d": -1,
"interp_method": "bilinear",
"align_corners": True,
"align_mode": 1,
},
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 3, 6, 10]}
self.opt_shape = {"x": [2, 3, 6, 10]}
self.max_shape = {"x": [12, 3, 6, 10]}
def test_trt_result(self):
self.check_trt_result()
class TestBilinearNHWCTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = bilinear_python_api
x_nchw = np.random.random([2, 3, 6, 10]).astype("float32")
x_nhwc = np.transpose(x_nchw, (0, 2, 3, 1))
self.api_args = {
"x": x_nhwc,
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"attrs": {
"data_layout": "NHWC",
"scale": [],
"out_h": 12,
"out_w": 12,
"out_d": -1,
"interp_method": "bilinear",
"align_corners": False,
"align_mode": 0,
},
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 6, 10, 3]}
self.opt_shape = {"x": [2, 6, 10, 3]}
self.max_shape = {"x": [12, 6, 10, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestBilinearOutSizeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = bilinear_python_api
self.api_args = {
"x": np.random.random([2, 3, 6, 10]).astype("float32"),
"OutSize": np.array([12, 12], dtype="int32"),
"SizeTensor": None,
"Scale": None,
"attrs": {
"data_layout": "NCHW",
"scale": [],
"out_h": 12,
"out_w": 12,
"out_d": -1,
"interp_method": "bilinear",
"align_corners": False,
"align_mode": 0,
},
}
self.program_config = {"feed_list": ["x", "OutSize"]}
self.min_shape = {"x": [2, 3, 6, 10]}
self.opt_shape = {"x": [2, 3, 6, 10]}
self.max_shape = {"x": [12, 3, 6, 10]}
def test_trt_result(self):
self.check_trt_result()
def bilinear_python_size_tensor_api(x, OutSize, SizeTensor, Scale, attrs):
if SizeTensor is None:
if SizeTensor is None:
if not isinstance(x, paddle.Tensor):
x = paddle.to_tensor(x)
shape_tensor = paddle.shape(x)
SizeTensor = [shape_tensor[2:3], shape_tensor[3:4]]
return _C_ops.bilinear_interp(
x,
OutSize,
SizeTensor,
Scale,
attrs['data_layout'],
attrs['out_d'],
attrs['out_h'],
attrs['out_w'],
attrs['scale'] if 'scale' in attrs else [],
attrs['interp_method'],
attrs['align_corners'],
attrs['align_mode'],
)
class TestBilinearSizeTensorTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = bilinear_python_size_tensor_api
self.api_args = {
"x": np.random.random([2, 3, 6, 10]).astype("float32"),
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"attrs": {
"data_layout": "NCHW",
"scale": [],
"out_h": -1,
"out_w": -1,
"out_d": -1,
"interp_method": "bilinear",
"align_corners": False,
"align_mode": 0,
},
}
self.program_config = {
"feed_list": ["x", "OutSize", "SizeTensor", "Scale"]
}
self.min_shape = {"x": [2, 3, 6, 10]}
self.opt_shape = {"x": [2, 3, 6, 10]}
self.max_shape = {"x": [12, 3, 6, 10]}
def test_trt_result(self):
self.check_trt_result()
class TestNearestNHWCTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = nearest_python_api
x_nchw = np.random.random([2, 3, 6, 10]).astype("float32")
x_nhwc = np.transpose(x_nchw, (0, 2, 3, 1))
self.api_args = {
"x": x_nhwc,
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"attrs": {
"data_layout": "NHWC",
"scale": [],
"out_h": 12,
"out_w": 12,
"out_d": -1,
"interp_method": "nearest",
"align_corners": False,
"align_mode": 1,
},
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 6, 10, 3]}
self.opt_shape = {"x": [2, 6, 10, 3]}
self.max_shape = {"x": [12, 6, 10, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestNearestSizeTensorTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = nearest_python_api
x_nchw = np.random.random([2, 3, 6, 10]).astype("float32")
self.api_args = {
"x": x_nchw,
"OutSize": None,
"SizeTensor": [
np.array([12], dtype="int64"),
np.array([12], dtype="int64"),
],
"Scale": None,
"attrs": {
"data_layout": "NCHW",
"scale": [],
"out_h": 12,
"out_w": 12,
"out_d": -1,
"interp_method": "nearest",
"align_corners": False,
"align_mode": 0,
},
}
self.program_config = {"feed_list": ["x", "SizeTensor"]}
self.min_shape = {"x": [2, 3, 6, 10]}
self.opt_shape = {"x": [2, 3, 6, 10]}
self.max_shape = {"x": [12, 3, 6, 10]}
def test_trt_result(self):
self.check_trt_result()
class TestEmbeddingTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = embedding_python_api
x = np.array([[3, 16, 24], [6, 4, 47]]).astype(np.int64)
weight = np.random.uniform(-1, 1, [64, 4]).astype('float32')
self.api_args = {
"x": x,
"weight": weight,
"attrs": {
"padding_idx": -1,
"sparse": False,
},
}
self.dynamic_shape_data = {
"x": lambda shape: np.random.randint(1, 64, size=shape).astype(
"int64"
),
"weight": lambda shape: np.random.randint(-1, 1, size=shape).astype(
"float32"
),
}
self.program_config = {"feed_list": ["x", "weight"]}
self.min_shape = {"x": [1, 3], "weight": [64, 4]}
self.opt_shape = {"x": [2, 3], "weight": [64, 4]}
self.max_shape = {"x": [16, 3], "weight": [64, 4]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestUnbindTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = unbind_python_api
x = np.random.random([3, 400, 196, 80]).astype(np.float32)
self.api_args = {
"x": x,
"attrs": {
"axis": 1,
},
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {
"x": [1, 400, 196, 80],
}
self.opt_shape = {"x": [2, 400, 196, 80]}
self.max_shape = {"x": [3, 400, 196, 80]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestNearestOutAndScaleTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = nearest_python_api
x_nchw = np.random.random([2, 3, 6, 10]).astype("float32")
self.api_args = {
"x": x_nchw,
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"attrs": {
"data_layout": "NCHW",
"scale": [2, 2],
"out_h": 12,
"out_w": 12,
"out_d": -1,
"interp_method": "nearest",
"align_corners": True,
"align_mode": 1,
},
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 3, 6, 10]}
self.opt_shape = {"x": [2, 3, 6, 10]}
self.max_shape = {"x": [12, 3, 6, 10]}
def test_trt_result(self):
self.check_trt_result()
class TestBilinearTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = upsample_bilinear
self.api_args = {"x": np.random.random([2, 3, 6, 10]).astype("float32")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 3, 6, 10]}
self.opt_shape = {"x": [2, 3, 6, 10]}
self.max_shape = {"x": [12, 3, 6, 10]}
def test_trt_result(self):
self.check_trt_result()
def upsample_nearest(x):
upsample = paddle.nn.Upsample(size=[12, 12], mode="nearest")
return upsample(x)
class TestNearestInterpTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = upsample_nearest
self.api_args = {"x": np.random.random([2, 3, 6, 10]).astype("float32")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 3, 6, 10]}
self.opt_shape = {"x": [2, 3, 6, 10]}
self.max_shape = {"x": [12, 3, 6, 10]}
def test_trt_result(self):
self.check_trt_result()
def linear_interp_test(
x,
OutSize=None,
SizeTensor=None,
Scale=None,
data_layout='NCHW',
out_d=-1,
out_h=-1,
out_w=-1,
scale=[],
interp_method='linear',
align_corners=True,
align_mode=0,
):
return paddle._C_ops.linear_interp(
x,
OutSize,
SizeTensor,
Scale,
data_layout,
out_d,
out_h,
out_w,
scale,
interp_method,
align_corners,
align_mode,
)
class TestLinearInterpTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = linear_interp_test
self.api_args = {
"x": np.random.random([1, 18, 144]).astype("float32"),
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"data_layout": "NCHW",
"out_d": -1,
"out_h": -1,
"out_w": 288,
"scale": [],
"interp_method": "linear",
"align_corners": False,
"align_mode": 0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 18, 144]}
self.opt_shape = {"x": [2, 18, 144]}
self.max_shape = {"x": [3, 18, 144]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestLinearInterpCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = linear_interp_test
self.api_args = {
"x": np.random.random([1, 18, 144]).astype("float32"),
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"data_layout": "NHWC",
"out_d": -1,
"out_h": -1,
"out_w": 288,
"scale": [],
"interp_method": "linear",
"align_corners": False,
"align_mode": 0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 18, 144]}
self.opt_shape = {"x": [2, 18, 144]}
self.max_shape = {"x": [3, 18, 144]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestLinearInterpCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = linear_interp_test
self.api_args = {
"x": np.random.random([1, 18, 144]).astype("float32"),
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"data_layout": "NHWC",
"out_d": -1,
"out_h": -1,
"out_w": 288,
"scale": [],
"interp_method": "linear",
"align_corners": False,
"align_mode": 0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 18, 144]}
self.opt_shape = {"x": [2, 18, 144]}
self.max_shape = {"x": [3, 18, 144]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestLinearInterpCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = linear_interp_test
self.api_args = {
"x": np.random.random([1, 18, 144]).astype("float32"),
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"data_layout": "NHWC",
"out_d": -1,
"out_h": -1,
"out_w": 288,
"scale": [],
"interp_method": "linear",
"align_corners": True,
"align_mode": 0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 18, 144]}
self.opt_shape = {"x": [2, 18, 144]}
self.max_shape = {"x": [3, 18, 144]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestLinearInterpCase4TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = linear_interp_test
self.api_args = {
"x": np.random.random([1, 3, 64]).astype("float32"),
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"data_layout": "NCHW",
"out_d": -1,
"out_h": -1,
"out_w": -1,
"scale": [1.0],
"interp_method": "linear",
"align_corners": False,
"align_mode": 0,
}
self.program_config = {"feed_list": ["x", "Scale"]}
self.min_shape = {"x": [1, 3, 64]}
self.opt_shape = {"x": [2, 3, 64]}
self.max_shape = {"x": [4, 3, 64]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestLinearInterpCase5TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = linear_interp_test
self.api_args = {
"x": np.random.random([1, 3, 64]).astype("float32"),
"OutSize": None,
"SizeTensor": None,
"Scale": None,
"data_layout": "NHWC",
"out_d": -1,
"out_h": -1,
"out_w": -1,
"scale": [1.0],
"interp_method": "linear",
"align_corners": True,
"align_mode": 0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 64]}
self.opt_shape = {"x": [2, 3, 64]}
self.max_shape = {"x": [4, 3, 64]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestLinearInterpCase6TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = linear_interp_test
self.api_args = {
"x": np.random.random([1, 18, 144]).astype("float32"),
"OutSize": np.array([288], dtype="int32"),
"SizeTensor": [
np.array([288], dtype="int64"),
],
"Scale": None,
"data_layout": "NHWC",
"out_d": -1,
"out_h": -1,
"out_w": 288,
"scale": [],
"interp_method": "linear",
"align_corners": True,
"align_mode": 0,
}
self.program_config = {"feed_list": ["x", "OutSize", "SizeTensor"]}
self.min_shape = {"x": [1, 18, 144]}
self.opt_shape = {"x": [2, 18, 144]}
self.max_shape = {"x": [4, 18, 144]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestLinearInterpCase7TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = linear_interp_test
self.api_args = {
"x": np.random.random([1, 18, 144]).astype("float32"),
"OutSize": np.array([288], dtype="int32"),
"SizeTensor": [
np.array([288], dtype="int64"),
],
"Scale": None,
"data_layout": "NCHW",
"out_d": -1,
"out_h": -1,
"out_w": 288,
"scale": [],
"interp_method": "linear",
"align_corners": True,
"align_mode": 0,
}
self.program_config = {"feed_list": ["x", "OutSize", "SizeTensor"]}
self.min_shape = {"x": [1, 18, 144]}
self.opt_shape = {"x": [2, 18, 144]}
self.max_shape = {"x": [4, 18, 144]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestLinearInterpCase8TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = linear_interp_test
self.api_args = {
"x": np.random.random([1, 18, 144]).astype("float32"),
"OutSize": None,
"SizeTensor": [
np.array([288], dtype="int64"),
],
"Scale": None,
"data_layout": "NCHW",
"out_d": -1,
"out_h": -1,
"out_w": 288,
"scale": [],
"interp_method": "linear",
"align_corners": True,
"align_mode": 0,
}
self.program_config = {"feed_list": ["x", "SizeTensor"]}
self.min_shape = {"x": [1, 18, 144]}
self.opt_shape = {"x": [2, 18, 144]}
self.max_shape = {"x": [4, 18, 144]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
if __name__ == "__main__":
unittest.main()
+517
View File
@@ -0,0 +1,517 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
from paddle import _C_ops
def conv2d_wrapper(x):
conv = paddle.nn.Conv2D(3, 3, (3, 3))
return conv(x)
def conv2d_python_api(x, padding="SAME", stride=(1, 1)):
conv = paddle.nn.Conv2D(3, 3, (3, 3), padding=padding, stride=stride)
return conv(x)
class TestConv2dTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv2d_wrapper
self.api_args = {
"x": np.random.random([2, 3, 8, 8]).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 8, 8]}
self.opt_shape = {"x": [2, 3, 8, 8]}
self.max_shape = {"x": [10, 3, 8, 8]}
self.disable_passes = [
'constant_folding_pass',
'conv2d_add_fuse_pass',
'dead_code_elimination_pass',
]
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestConv2dPaddingAlgorithmTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv2d_python_api
self.api_args = {
"x": np.random.random([2, 3, 8, 8]).astype("float32"),
"padding": "SAME",
"stride": (1, 2),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 8, 8]}
self.opt_shape = {"x": [2, 3, 8, 8]}
self.max_shape = {"x": [10, 3, 8, 8]}
self.disable_passes = [
'constant_folding_pass',
'conv2d_add_fuse_pass',
'dead_code_elimination_pass',
]
def test_trt_result(self):
self.check_trt_result()
class TestConv2dPaddingTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv2d_python_api
self.api_args = {
"x": np.random.random([2, 3, 8, 8]).astype("float32"),
"padding": "VALID",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 8, 8]}
self.opt_shape = {"x": [2, 3, 8, 8]}
self.max_shape = {"x": [10, 3, 8, 8]}
self.disable_passes = [
'constant_folding_pass',
'conv2d_add_fuse_pass',
'dead_code_elimination_pass',
]
def test_trt_result(self):
self.check_trt_result()
def conv2dtranspose_wrapper(
x,
stride=1,
padding=0,
output_padding=[],
output_size=None,
padding_algorithm="EXPLICIT",
groups=1,
dilation=1,
data_format="NCDHW",
):
if data_format == "AnyLayout":
data_format = "NCDHW"
if padding_algorithm is None:
padding_algorithm = "EXPLICIT"
weight = paddle.static.create_parameter(
name="weight",
shape=[3, 6, 3, 3],
dtype="float32",
default_initializer=paddle.nn.initializer.Normal(mean=0.0, std=1.0),
)
return _C_ops.conv2d_transpose(
x,
weight,
stride,
padding,
output_padding,
output_size,
padding_algorithm,
groups,
dilation,
data_format,
)
class TestConv2dTransposeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv2dtranspose_wrapper
self.api_args = {
"x": np.random.random([2, 3, 5, 5]).astype("float32"),
"stride": [1, 1],
"padding": [1, 1],
"output_padding": [],
"output_size": [7, 7],
"padding_algorithm": "VALID",
"groups": 1,
"dilation": [1, 1],
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5]}
self.opt_shape = {"x": [2, 3, 5, 5]}
self.max_shape = {"x": [4, 3, 5, 5]}
def test_trt_result(self):
self.check_trt_result()
class TestConv2dTransposePaddingAlgorithmTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv2dtranspose_wrapper
self.api_args = {
"x": np.random.random([2, 3, 5, 5]).astype("float32"),
"stride": [1, 1],
"padding": [1, 0, 1, 2],
"output_padding": [],
"output_size": None,
"padding_algorithm": "SAME",
"groups": 1,
"dilation": [1, 1],
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5]}
self.opt_shape = {"x": [2, 3, 5, 5]}
self.max_shape = {"x": [4, 3, 5, 5]}
def test_trt_result(self):
self.check_trt_result()
class TestConv2dTransposeOutputPaddingTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv2dtranspose_wrapper
self.api_args = {
"x": np.random.random([2, 3, 5, 5]).astype("float32"),
"stride": [2, 2],
"padding": [2, 2],
"output_padding": [1, 1],
"output_size": None,
"padding_algorithm": "EXPLICIT",
"groups": 1,
"dilation": [1, 1],
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5]}
self.opt_shape = {"x": [2, 3, 5, 5]}
self.max_shape = {"x": [4, 3, 5, 5]}
def test_trt_result(self):
self.check_trt_result()
def depthwise_conv2d_wrapper(x):
conv = paddle.nn.Conv2D(2, 2, (3, 3), groups=2)
return conv(x)
def depthwise_conv2d_python_api(
x, padding="SAME", stride=(1, 1), dilation=(1, 1)
):
conv = paddle.nn.Conv2D(
2,
2,
(3, 3),
groups=2,
padding=padding,
stride=stride,
dilation=dilation,
)
return conv(x)
class TestDepthwiseConv2dTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = depthwise_conv2d_wrapper
self.api_args = {"x": np.random.random([3, 2, 8, 8]).astype("float32")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 8, 8]}
self.opt_shape = {"x": [3, 2, 8, 8]}
self.max_shape = {"x": [10, 2, 8, 8]}
def test_trt_result(self):
self.check_trt_result()
class TestDepthwiseConv2dPaddingTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = depthwise_conv2d_python_api
self.api_args = {
"x": np.random.random([3, 2, 8, 8]).astype("float32"),
"padding": "VALID",
"stride": (1, 2),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 8, 8]}
self.opt_shape = {"x": [3, 2, 8, 8]}
self.max_shape = {"x": [10, 2, 8, 8]}
def test_trt_result(self):
self.check_trt_result()
class TestDepthwiseConv2dSameTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = depthwise_conv2d_python_api
self.api_args = {
"x": np.random.random([3, 2, 8, 8]).astype("float32"),
"padding": "SAME",
"stride": (1, 2),
"dialation": (2, 2),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 8, 8]}
self.opt_shape = {"x": [3, 2, 8, 8]}
self.max_shape = {"x": [10, 2, 8, 8]}
def test_trt_result(self):
self.check_trt_result()
def depthwise_conv2d_transpose_wrapper(x):
conv = paddle.nn.Conv2DTranspose(2, 2, (3, 3), groups=2)
return conv(x)
def depthwise_conv2d_transpose_python_api(
x, padding="SAME", stride=(1, 1), dilation=(1, 1)
):
conv = paddle.nn.Conv2DTranspose(2, 2, (3, 3), groups=2)
return conv(x)
class TestDepthwiseConv2dTransposeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = depthwise_conv2d_transpose_wrapper
self.api_args = {"x": np.random.random([3, 2, 8, 8]).astype("float32")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 8, 8]}
self.opt_shape = {"x": [3, 2, 8, 8]}
self.max_shape = {"x": [10, 2, 8, 8]}
def test_trt_result(self):
self.check_trt_result()
class TestDepthwiseConv2dTransposeSameTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = depthwise_conv2d_transpose_python_api
self.api_args = {
"x": np.random.random([3, 2, 8, 8]).astype("float32"),
"padding": "SAME",
"stride": (1, 2),
"dialation": (2, 2),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 8, 8]}
self.opt_shape = {"x": [3, 2, 8, 8]}
self.max_shape = {"x": [10, 2, 8, 8]}
def test_trt_result(self):
self.check_trt_result()
class TestDepthwiseConv2dTransposeValidTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = depthwise_conv2d_transpose_python_api
self.api_args = {
"x": np.random.random([3, 2, 8, 8]).astype("float32"),
"padding": "VALID",
"stride": (1, 2),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 8, 8]}
self.opt_shape = {"x": [3, 2, 8, 8]}
self.max_shape = {"x": [10, 2, 8, 8]}
def test_trt_result(self):
self.check_trt_result()
def conv3d_wrapper(x):
conv = paddle.nn.Conv3D(3, 3, (3, 3, 3))
return conv(x)
def conv3d_python_api(x, padding="SAME", stride=(1, 1, 1)):
conv = paddle.nn.Conv3D(3, 3, (3, 3, 3), padding=padding, stride=stride)
return conv(x)
class TestConv3dTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv3d_wrapper
self.api_args = {
"x": np.random.random([2, 3, 8, 8, 8]).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 8, 8, 8]}
self.opt_shape = {"x": [1, 3, 8, 8, 8]}
self.max_shape = {"x": [10, 3, 8, 8, 8]}
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestConv3dPaddingAlgorithmTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv3d_python_api
self.api_args = {
"x": np.random.random([2, 3, 8, 8, 8]).astype("float32"),
"paddings": "SAME",
"stride": (1, 1, 1),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 8, 8, 8]}
self.opt_shape = {"x": [1, 3, 8, 8, 8]}
self.max_shape = {"x": [10, 3, 8, 8, 8]}
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
def depthwise_conv3d_transpose_wrapper(x):
conv = paddle.nn.Conv3DTranspose(
in_channels=2, out_channels=2, kernel_size=(3, 3, 3)
)
return conv(x)
def depthwise_conv3d_transpose_python_api(
x, padding="SAME", stride=(1, 1, 1), dilation=(1, 1, 1)
):
conv = paddle.nn.Conv3DTranspose(
in_channels=2,
out_channels=2,
kernel_size=(3, 3, 3),
stride=stride,
padding=padding,
dilation=dilation,
)
return conv(x)
def depthwise_conv3d_transpose_wrapper_outpadding(x, output_padding):
conv = paddle.nn.Conv3DTranspose(
in_channels=3,
out_channels=3,
kernel_size=(3, 3, 3),
stride=2,
output_padding=output_padding,
)
return conv(x)
def conv3d_transpose_with_algorithm(x, algorithm):
conv = paddle.nn.Conv3DTranspose(
in_channels=3,
out_channels=3,
kernel_size=(3, 3, 3),
padding=algorithm,
)
return conv(x)
class TestDepthwiseConv3dTransposeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = depthwise_conv3d_transpose_wrapper
self.api_args = {
"x": np.random.random([3, 2, 8, 8, 8]).astype("float32")
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 8, 8, 8]}
self.opt_shape = {"x": [1, 2, 8, 8, 8]}
self.max_shape = {"x": [10, 2, 8, 8, 8]}
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestDepthwiseConv3dTransposeSameTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv3d_transpose_with_algorithm
self.api_args = {
"x": np.random.random([2, 3, 8, 8, 8]).astype("float32"),
"padding_algorithm": "SAME",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 8, 8, 8]}
self.opt_shape = {"x": [1, 3, 8, 8, 8]}
self.max_shape = {"x": [10, 3, 8, 8, 8]}
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestDepthwiseConv3dTransposeOutputPaddingTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = depthwise_conv3d_transpose_wrapper_outpadding
self.api_args = {
"x": np.random.random([2, 3, 8, 8, 8]).astype("float32"),
"output_padding": [1, 1, 1],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 8, 8, 8]}
self.opt_shape = {"x": [1, 3, 8, 8, 8]}
self.max_shape = {"x": [10, 3, 8, 8, 8]}
def test_trt_result(self):
with self.assertRaises(ValueError) as context:
self.check_trt_result()
class TestDepthwiseConv3dTransposeOutputPadding2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = depthwise_conv3d_transpose_wrapper_outpadding
self.api_args = {
"x": np.random.random([2, 3, 8, 8, 8]).astype("float32"),
"output_padding": [0, 0, 0],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 8, 8, 8]}
self.opt_shape = {"x": [1, 3, 8, 8, 8]}
self.max_shape = {"x": [10, 3, 8, 8, 8]}
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestFusedConv2dAddActTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = conv2d_wrapper
self.api_args = {
"x": np.random.random([2, 3, 8, 8]).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 8, 8]}
self.opt_shape = {"x": [2, 3, 8, 8]}
self.max_shape = {"x": [10, 3, 8, 8]}
self.disable_passes = ['dead_code_elimination_pass']
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
if __name__ == '__main__':
unittest.main()
+273
View File
@@ -0,0 +1,273 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
from paddle import _C_ops
class TestFlattenTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.full
self.api_args = {"shape": [3, 2], "fill_value": 1.0}
self.program_config = {"feed_list": []}
self.min_shape = {}
self.opt_shape = {}
self.max_shape = {}
def test_trt_result(self):
self.check_trt_result()
class TestAssignTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.assign
self.api_args = {
"x": np.random.random([2, 2]).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2]}
self.opt_shape = {"x": [2, 2]}
self.max_shape = {"x": [3, 2]}
def test_trt_result(self):
self.check_trt_result()
def assign_value_api(input, dtype, values):
output = paddle.zeros_like(input)
return _C_ops.assign_value_(
output,
list(input.shape),
dtype,
values,
paddle.framework._current_expected_place(),
)
def assign_value_api_case2(input, dtype, values):
return _C_ops.assign_value(
list(input.shape),
dtype,
values,
paddle.framework._current_expected_place(),
)
class TestAssignValueInTRTPattern(TensorRTBaseTest):
def test_trt_result(self):
test_cases = [
# Test case 1
(
assign_value_api,
{
"x": np.random.random([2, 2]).astype("int32"),
"dtype": paddle.int32,
"values": [1, 1, 1, 1],
},
),
# Test case 2
(
assign_value_api_case2,
{
"x": np.random.random([2, 2]).astype("float32"),
"dtype": paddle.float32,
"values": [1.0, 1.0, 1.0, 1.0],
},
),
]
for python_api, api_args in test_cases:
with self.subTest(python_api=python_api, api_args=api_args):
self.python_api = python_api
self.api_args = api_args
self.program_config = {"feed_list": ["x"]}
self.min_shape = {}
self.opt_shape = {}
self.max_shape = {}
self.check_trt_result()
class TestArangeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.arange
self.api_args = {
"start": np.array([0]).astype("int64"),
"end": np.array([6]).astype("int64"),
"step": np.array([1]).astype("int64"),
}
self.program_config = {"feed_list": []}
self.min_shape = {}
self.opt_shape = {}
self.max_shape = {}
def test_trt_result(self):
self.check_trt_result()
class TestArangeTRTPatternCase1(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.arange
self.api_args = {
"start": np.array([0]).astype("float32"),
"end": np.array([6]).astype("float32"),
"step": np.array([1]).astype("float32"),
}
self.program_config = {"feed_list": []}
self.min_shape = {}
self.opt_shape = {}
self.max_shape = {}
def test_trt_result(self):
self.check_trt_result()
class TestAssignOutTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.assign
self.api_args = {
"x": np.random.random([2, 2]).astype("float32"),
"output": np.zeros((2, 2), dtype="float32"),
}
self.program_config = {"feed_list": ["x", "output"]}
self.min_shape = {"x": [1, 2], "output": [1, 2]}
self.opt_shape = {"x": [2, 2], "output": [2, 2]}
self.max_shape = {"x": [3, 2], "output": [3, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestFullLikeBoolTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.full_like
self.api_args = {
"input": np.random.randn(3, 2).astype("bool"),
"fill_value": True,
}
self.program_config = {"feed_list": ["input"]}
self.min_shape = {"input": [1, 2]}
self.opt_shape = {"input": [3, 2]}
self.max_shape = {"input": [5, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestFullLikeFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.full_like
self.api_args = {
"input": np.random.randn(3, 2).astype("float32"),
"fill_value": 5.0,
}
self.program_config = {"feed_list": ["input"]}
self.min_shape = {"input": [1, 2]}
self.opt_shape = {"input": [3, 2]}
self.max_shape = {"input": [5, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestFullLikeIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.full_like
self.api_args = {
"input": np.random.randn(3, 2).astype("int64"),
"fill_value": 5,
}
self.program_config = {"feed_list": ["input"]}
self.min_shape = {"input": [1, 2]}
self.opt_shape = {"input": [3, 2]}
self.max_shape = {"input": [5, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestFullLikeDynamicTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.full_like
self.api_args = {
"input": np.random.randn(3, 2).astype("float32"),
"fill_value": np.array([5.0]).astype("float32"),
}
self.program_config = {"feed_list": ["input", "fill_value"]}
self.min_shape = {"input": [1, 2]}
self.opt_shape = {"input": [3, 2]}
self.max_shape = {"input": [5, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestFullWithTensorTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.tensor.fill_constant
self.api_args = {
"shape": np.array([1]).astype("int64"),
"dtype": "float32",
"value": np.array([0.0]).astype("float32"),
}
self.program_config = {"feed_list": ["value", "shape"]}
self.min_shape = {}
self.opt_shape = {}
self.max_shape = {}
def test_trt_result(self):
self.check_trt_result()
class TestFullWithTensorCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.tensor.fill_constant
self.api_args = {
"shape": [1, 1],
"dtype": np.float32,
"value": np.array([1.0]).astype("float32"),
}
self.program_config = {"feed_list": ["value"]}
self.min_shape = {}
self.opt_shape = {}
self.max_shape = {}
def test_trt_result(self):
self.check_trt_result()
class TestMeshgridTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.meshgrid
self.api_args = {
"x": [
np.random.random([20]).astype("float32"),
np.random.random([30]).astype("float32"),
],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [[10], [20]]}
self.opt_shape = {"x": [[20], [30]]}
self.max_shape = {"x": [[30], [40]]}
def test_trt_result(self):
self.check_trt_result()
if __name__ == "__main__":
unittest.main()
+88
View File
@@ -0,0 +1,88 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
def einsum_wrapper(equation, x):
if not isinstance(x, list):
x = [x]
out = paddle.einsum(equation, *x)
return out[0]
class TestEinsumCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = einsum_wrapper
self.api_args = {
"equation": "ijk->ij",
"x": np.random.randn(2, 3, 4).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4]}
self.opt_shape = {"x": [2, 3, 4]}
self.max_shape = {"x": [4, 3, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestEinsumCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = einsum_wrapper
self.api_args = {
"equation": "abcd,bcd->a",
"operands": [
np.random.randn(2, 3, 4, 5).astype("float32"),
np.random.randn(3, 4, 5).astype("float32"),
],
}
self.program_config = {"feed_list": ["operands"]}
self.min_shape = {"operands_0": [1, 2, 3, 4], "operands_1": [2, 3, 4]}
self.opt_shape = {"operands_0": [2, 3, 4, 5], "operands_1": [3, 4, 5]}
self.max_shape = {"operands_0": [4, 6, 8, 9], "operands_1": [6, 8, 9]}
def test_trt_result(self):
self.check_trt_result()
class TestEinsumCaseTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = einsum_wrapper
self.api_args = {
"equation": "mij,jk->ki",
"operands": [
np.random.randn(2, 3, 4).astype("float16"),
np.random.randn(4, 3).astype("float16"),
],
}
self.program_config = {"feed_list": ["operands"]}
self.min_shape = {"operands_0": [1, 3, 4], "operands_1": [1, 3]}
self.opt_shape = {"operands_0": [2, 3, 4], "operands_1": [4, 3]}
self.max_shape = {"operands_0": [4, 3, 4], "operands_1": [6, 3]}
def test_trt_result(self):
self.check_trt_result(precision_mode="fp16")
if __name__ == '__main__':
unittest.main()
+70
View File
@@ -0,0 +1,70 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
class TestOneHotCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.one_hot
self.api_args = {
"x": np.random.randint(0, 2, size=(3, 1)).astype("int64"),
"num_classes": np.array([2], dtype="int64"),
}
self.dynamic_shape_data = {
"x": lambda shape: np.random.randint(0, 2, size=shape).astype(
"int64"
),
"num_classes": lambda shape: np.array([2], dtype="int64"),
}
self.program_config = {"feed_list": ["x", "num_classes"]}
self.min_shape = {"x": [1, 1]}
self.opt_shape = {"x": [3, 1]}
self.max_shape = {"x": [6, 1]}
def test_trt_result(self):
self.check_trt_result()
class TestOneHotCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.one_hot
self.num_classes = 2
self.api_args = {
"x": np.random.randint(0, 2, size=(3, 1)).astype(
"int64"
), # Random integers between 0 and num_classes
"num_classes": self.num_classes,
}
self.dynamic_shape_data = {
"x": lambda shape: np.random.randint(
0, self.num_classes, size=shape
)
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 1]}
self.opt_shape = {"x": [3, 1]}
self.max_shape = {"x": [6, 1]}
def test_trt_result(self):
self.check_trt_result()
if __name__ == '__main__':
unittest.main()
+174
View File
@@ -0,0 +1,174 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
class TestMatmulTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.matmul
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(3, 2).astype("float32"),
"transpose_x": False,
"transpose_y": False,
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3, 2]}
self.opt_shape = {"x": [1, 3], "y": [3, 2]}
self.max_shape = {"x": [5, 3], "y": [3, 2]}
def test_trt_result(self):
self.check_trt_result(rtol=1e-3, atol=1e-3)
class TestTransposeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.transpose
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
"perm": [1, 0, 2],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4]}
self.opt_shape = {"x": [1, 3, 4]}
self.max_shape = {"x": [5, 3, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestBmmTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bmm
self.api_args = {
"x": np.random.randn(2, 2, 3).astype("float32"),
"y": np.random.randn(2, 3, 2).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 2, 3], "y": [1, 3, 2]}
self.opt_shape = {"x": [1, 2, 3], "y": [1, 3, 2]}
self.max_shape = {"x": [5, 2, 3], "y": [5, 3, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestFlipTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.flip
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
"axis": [0, 2],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4]}
self.opt_shape = {"x": [1, 3, 4]}
self.max_shape = {"x": [5, 3, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestFlipNegAxisTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.flip
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
"axis": [-1, -3],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4]}
self.opt_shape = {"x": [1, 3, 4]}
self.max_shape = {"x": [5, 3, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestFlipIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.flip
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("int64"),
"axis": [0, 2],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4]}
self.opt_shape = {"x": [1, 3, 4]}
self.max_shape = {"x": [5, 3, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestFlipIntNegAxisTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.flip
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("int64"),
"axis": [-1, -3],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4]}
self.opt_shape = {"x": [1, 3, 4]}
self.max_shape = {"x": [5, 3, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestPNormTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.linalg.norm
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
"p": 2,
"axis": -1,
"keepdim": False,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4]}
self.opt_shape = {"x": [2, 3, 4]}
self.max_shape = {"x": [4, 3, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestPNormCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.linalg.norm
self.api_args = {
"x": np.random.randn(2, 3).astype("float16"),
"p": 2,
"axis": -1,
"keepdim": False,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [4, 3]}
def test_trt_result(self):
self.check_trt_result(precision_mode="fp16")
if __name__ == '__main__':
unittest.main()
+540
View File
@@ -0,0 +1,540 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
class TestGreaterThanFloat32TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.greater_than
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [5, 3], "y": [3]}
def test_trt_result(self):
self.check_trt_result()
class TestGreaterThanInt64TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.greater_than
self.api_args = {
"x": np.random.randn(3).astype("int64"),
"y": np.random.randn(3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestLessThanFloat32TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.less_than
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [5, 3], "y": [3]}
def test_trt_result(self):
self.check_trt_result()
class TestLessThanInt64TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.less_than
self.api_args = {
"x": np.random.randn(3).astype("int64"),
"y": np.random.randn(3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestEqualFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.equal
self.api_args = {
"x": np.random.randn(3).astype("float32"),
"y": np.random.randn(3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestEqualIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.equal
self.api_args = {
"x": np.random.randn(3).astype("int64"),
"y": np.random.randn(3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestNotEqualFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.not_equal
self.api_args = {
"x": np.random.randn(3).astype("float32"),
"y": np.random.randn(3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestNotEqualIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.not_equal
self.api_args = {
"x": np.random.randn(3).astype("int64"),
"y": np.random.randn(3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestAndRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_and
self.api_args = {
"x": np.random.randn(2, 3).astype("bool"),
"y": np.random.randn(3).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [5, 3], "y": [3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestAndRTPatternDifferentShapes(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_and
self.api_args = {
"x": np.random.randn(4, 5).astype("bool"),
"y": np.random.randn(1, 5).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 5], "y": [1, 5]}
self.opt_shape = {"x": [2, 5], "y": [1, 5]}
self.max_shape = {"x": [10, 5], "y": [1, 5]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestAndRTPatternDifferentShapes1(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_and
self.api_args = {
"x": np.random.randint(0, 2, (2, 3)).astype("bool"),
"y": np.random.randint(0, 2, (2, 3)).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestOrRTPatternBroadcast(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_or
self.api_args = {
"x": np.random.randn(2, 1).astype("bool"),
"y": np.random.randn(2, 3).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [2, 1], "y": [2, 3]}
self.opt_shape = {"x": [2, 1], "y": [2, 3]}
self.max_shape = {"x": [2, 1], "y": [2, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestOrRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_or
self.api_args = {
"x": np.random.randn(2, 3).astype("bool"),
"y": np.random.randn(3).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [5, 3], "y": [3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestNotRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_not
self.api_args = {
"x": np.random.randn(2, 3).astype("bool"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestNotRTPatternEdgeCase(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_not
self.api_args = {
"x": np.zeros((2, 3)).astype("bool"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestLogicalOrTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_or
def test_trt_result(self):
self.api_args = {
"x": np.random.choice([True, False], size=(3,)).astype("bool"),
"y": np.random.choice([True, False], size=(3,)).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
self.check_trt_result()
def test_trt_diff_shape_result(self):
self.api_args = {
"x": np.random.choice([True, False], size=(2, 3)).astype("bool"),
"y": np.random.choice([True, False], size=(3)).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [4, 3], "y": [3]}
self.check_trt_result()
class TestAndRTPatternErrorType(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_and
self.api_args = {
"x": np.random.randn(2, 3).astype("int32"),
"y": np.random.randn(3).astype("int32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [5, 3], "y": [3]}
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestOrRTPatternErrorType(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_or
self.api_args = {
"x": np.random.randn(2, 3).astype("int32"),
"y": np.random.randn(3).astype("int32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [5, 3], "y": [3]}
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestNotRTINT8(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_not
self.api_args = {
"x": np.random.randn(2, 3).astype("int8"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestNotRTINT64(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.bitwise_not
self.api_args = {
"x": np.random.randn(2, 3).astype("int64"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestLogicalOrMarker(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_or
self.api_args = {
"x": np.random.randn(3).astype("int64"),
"y": np.random.randn(3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.target_marker_op = "pd_op.logical_or"
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestLogicalAndTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_and
def test_trt_result(self):
self.api_args = {
"x": np.random.choice([True, False], size=(3,)).astype("bool"),
"y": np.random.choice([True, False], size=(3,)).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
self.check_trt_result()
def test_trt_diff_shape_result(self):
self.api_args = {
"x": np.random.choice([True, False], size=(2, 3)).astype("bool"),
"y": np.random.choice([True, False], size=(3)).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [4, 3], "y": [3]}
self.check_trt_result()
class TestLogicalAndMarker(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_and
self.api_args = {
"x": np.random.randn(3).astype("int64"),
"y": np.random.randn(3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.target_marker_op = "pd_op.logical_and"
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestLogicalOr_TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_or_
def test_trt_result(self):
self.api_args = {
"x": np.random.choice([True, False], size=(3,)).astype("bool"),
"y": np.random.choice([True, False], size=(3,)).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
self.check_trt_result()
def test_trt_diff_shape_result(self):
self.api_args = {
"x": np.random.choice([True, False], size=(2, 3)).astype("bool"),
"y": np.random.choice([True, False], size=(3)).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [4, 3], "y": [3]}
self.check_trt_result()
class TestLogicalOr_Marker(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_or_
self.api_args = {
"x": np.random.randn(3).astype("int64"),
"y": np.random.randn(3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.target_marker_op = "pd_op.logical_or_"
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestLogicalNotTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_not
self.api_args = {
"x": np.random.choice([True, False], size=(2, 3)).astype("bool"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [2, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestLogicalNotCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_not
self.api_args = {"x": np.random.random([2]).astype("bool")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2]}
self.opt_shape = {"x": [2]}
self.max_shape = {"x": [2]}
def test_trt_result(self):
self.check_trt_result()
class TestLogicalXorTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_xor
def test_trt_result(self):
self.api_args = {
"x": np.random.choice([True, False], size=(3,)).astype("bool"),
"y": np.random.choice([True, False], size=(3,)).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1], "y": [1]}
self.opt_shape = {"x": [2], "y": [2]}
self.max_shape = {"x": [5], "y": [5]}
self.check_trt_result()
def test_trt_diff_shape_result(self):
self.api_args = {
"x": np.random.choice([True, False], size=(2, 3)).astype("bool"),
"y": np.random.choice([True, False], size=(3)).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [3]}
self.opt_shape = {"x": [2, 3], "y": [3]}
self.max_shape = {"x": [4, 3], "y": [3]}
self.check_trt_result()
class TestLogicalXorMarker(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.logical_xor
self.api_args = {
"x": np.random.randn(3).astype("int64"),
"y": np.random.randn(3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.target_marker_op = "pd_op.logical_xor"
def test_trt_result(self):
self.check_marker(expected_result=False)
if __name__ == '__main__':
unittest.main()
File diff suppressed because it is too large Load Diff
+936
View File
@@ -0,0 +1,936 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
class TestMaxTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.max
self.api_args = {
"x": np.random.randn(2, 4).astype("float32"),
"axis": [0, 1],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 4]}
self.opt_shape = {"x": [2, 4]}
self.max_shape = {"x": [5, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestDivideTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.divide
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestMultiplyTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.multiply
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestSubtractTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.subtract
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestAddTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.add
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestElementwisePowTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.elementwise_pow
self.api_args = {
"x": np.random.randn(2, 3).astype(np.float32),
"y": np.random.randn(2, 3).astype(np.float32),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestPowCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.pow
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": 2.5,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp32(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestPowCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.pow
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": 2,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result_fp32(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestRemainderFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.remainder
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.uniform(low=0.1, high=1, size=(2, 3)).astype(
"float32"
), # Ensure y is non-zero
}
self.dynamic_shape_data = {
"x": lambda shape: np.random.randn(*shape).astype("float32"),
"y": lambda shape: np.random.uniform(
low=0.1, high=1, size=shape
).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestRemainderIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.remainder
self.api_args = {
"x": np.random.randint(1, 10, size=(2, 3)).astype("int64"),
"y": np.random.randint(1, 10, size=(2, 3)).astype(
"int64"
), # Ensure y is non-zero
}
self.dynamic_shape_data = {
"x": lambda shape: np.random.randint(1, 10, size=shape).astype(
"int64"
),
"y": lambda shape: np.random.randint(1, 10, size=shape).astype(
"int64"
),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestMinTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.min
self.api_args = {
"x": np.random.randn(2, 4).astype("float32"),
"axis": [0, 1],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 4]}
self.opt_shape = {"x": [2, 4]}
self.max_shape = {"x": [5, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestSumTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.sum
self.api_args = {
"x": np.random.randn(2, 4, 6).astype("int64"),
"axis": [1, 1],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 4, 6]}
self.opt_shape = {"x": [2, 4, 6]}
self.max_shape = {"x": [5, 4, 6]}
def test_trt_result(self):
self.check_trt_result()
class TestSum1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.sum
self.api_args = {
"x": np.random.randn(2, 4, 6).astype("float32"),
"axis": [1, 1],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 4, 6]}
self.opt_shape = {"x": [2, 4, 6]}
self.max_shape = {"x": [5, 4, 6]}
def test_trt_result(self):
self.check_trt_result()
class TestAnyTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.any
self.api_args = {
"x": np.random.randn(2, 3, 2).astype("bool"),
"axis": [1],
"keepdim": True,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 2]}
self.opt_shape = {"x": [2, 3, 2]}
self.max_shape = {"x": [5, 3, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestAny1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.any
self.api_args = {
"x": np.random.randn(2, 3, 2).astype("bool"),
"axis": [1],
"keepdim": False,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 2]}
self.opt_shape = {"x": [2, 3, 2]}
self.max_shape = {"x": [5, 3, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestAny2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.any
self.api_args = {
"x": np.random.randn(2, 3, 2).astype("bool"),
"axis": [-1],
"keepdim": False,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 2]}
self.opt_shape = {"x": [2, 3, 2]}
self.max_shape = {"x": [5, 3, 2]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestAllTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.all
self.api_args = {
"x": np.random.randn(2, 3, 2).astype("bool"),
"axis": [1, 1],
"keepdim": True,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 2]}
self.opt_shape = {"x": [2, 3, 2]}
self.max_shape = {"x": [5, 3, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestAll1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.all
self.api_args = {
"x": np.random.randn(2, 3, 2).astype("bool"),
"axis": [1, 1],
"keepdim": False,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 2]}
self.opt_shape = {"x": [2, 3, 2]}
self.max_shape = {"x": [5, 3, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestCumsumCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.cumsum
self.api_args = {
"x": np.random.randn(2, 2, 3).astype("float32"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 3]}
self.opt_shape = {"x": [2, 2, 3]}
self.max_shape = {"x": [5, 2, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestCumsumCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.cumsum
self.api_args = {
"x": np.random.randn(2, 2, 3).astype("float32"),
"axis": 1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 3]}
self.opt_shape = {"x": [2, 2, 3]}
self.max_shape = {"x": [5, 2, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestCumsumCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.cumsum
self.api_args = {
"x": np.random.randn(2, 2, 3).astype("float32"),
"axis": 0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 3]}
self.opt_shape = {"x": [2, 2, 3]}
self.max_shape = {"x": [5, 2, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestCumsumCase4TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.cumsum
self.api_args = {
"x": np.random.randn(2, 2, 3).astype("int64"),
"axis": 0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 3]}
self.opt_shape = {"x": [2, 2, 3]}
self.max_shape = {"x": [5, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestFloorDivideFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.floor_divide
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestFloorDivideIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.floor_divide
self.api_args = {
"x": np.random.randint(low=1, high=100, size=(2, 3), dtype="int64"),
"y": np.random.randint(low=1, high=100, size=(2, 3), dtype="int64"),
}
self.dynamic_shape_data = {
"x": lambda shape: np.random.randint(
1, 100, size=shape, dtype="int64"
),
"y": lambda shape: np.random.randint(
1, 100, size=shape, dtype="int64"
),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [2, 3], "y": [2, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestLogFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.log
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestLogIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.log
self.api_args = {
"x": np.random.randn(2, 3).astype("int32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestClipTRTPatternCase1(TensorRTBaseTest):
'''min/max is attr, and x/min/max is float'''
def setUp(self):
self.python_api = paddle.clip
self.api_args = {
"x": np.array([[2, 0.3, 0.5, 0.9], [0.1, 0.2, 6, 7]]).astype(
"float32"
),
"min": 2.2,
"max": 5.5,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 4]}
self.opt_shape = {"x": [2, 4]}
self.max_shape = {"x": [5, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestClipTRTPatternCase2(TensorRTBaseTest):
def setUp(self):
'''min/max is attr, and x is int, min/max is float'''
self.python_api = paddle.clip
self.api_args = {
"x": np.array([[2, 3, 5, 9], [1, 2, 6, 7]]).astype("int64"),
"min": 2.2,
"max": 5.5,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 4]}
self.opt_shape = {"x": [2, 4]}
self.max_shape = {"x": [5, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestClipTRTPatternCase3(TensorRTBaseTest):
'''min/max is input, and x/min/max is float'''
def setUp(self):
self.python_api = paddle.clip
self.api_args = {
"x": np.array([[2, 0.3, 0.5, 0.9], [0.1, 0.2, 6, 7]]).astype(
"float32"
),
"min": np.array([2.2]).astype("float32"),
"max": np.array([5.2]).astype("float32"),
}
self.program_config = {"feed_list": ["x", "min", "max"]}
self.min_shape = {"x": [1, 4]}
self.opt_shape = {"x": [2, 4]}
self.max_shape = {"x": [5, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestClipTRTPatternCase4(TensorRTBaseTest):
'''min/max is input, and x is int, min/max is float'''
def setUp(self):
self.python_api = paddle.clip
self.api_args = {
"x": np.array([[2, 3, 5, 9], [1, 2, 6, 7]]).astype("int64"),
"min": np.array([2]).astype("float32"),
"max": np.array([5]).astype("float32"),
}
self.program_config = {"feed_list": ["x", "min", "max"]}
self.min_shape = {"x": [1, 4]}
self.opt_shape = {"x": [2, 4]}
self.max_shape = {"x": [5, 4]}
def test_trt_result(self):
self.check_trt_result()
class TestIsnanFP32TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.isnan
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestIsnanFP16TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.isnan
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestIsnanIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.isnan
self.api_args = {
"x": np.random.randn(2, 3).astype("int64"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestMaximumTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.maximum
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
"y": np.random.randn(2, 3, 4).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3, 4], "y": [1, 3, 4]}
self.opt_shape = {"x": [2, 3, 4], "y": [2, 3, 4]}
self.max_shape = {"x": [5, 3, 4], "y": [5, 3, 4]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestMaximumBroadcastTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.maximum
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
"y": np.random.randn(4).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3, 4], "y": [4]}
self.opt_shape = {"x": [2, 3, 4], "y": [4]}
self.max_shape = {"x": [5, 3, 4], "y": [4]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestMaximumIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.maximum
self.api_args = {
"x": np.random.randint(
low=1, high=100, size=(2, 3, 4), dtype="int64"
),
"y": np.random.randint(
low=1, high=100, size=(2, 3, 4), dtype="int64"
),
}
self.dynamic_shape_data = {
"x": lambda shape: np.random.randint(
1, 100, size=shape, dtype="int64"
),
"y": lambda shape: np.random.randint(
1, 100, size=shape, dtype="int64"
),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3, 4], "y": [1, 3, 4]}
self.opt_shape = {"x": [2, 3, 4], "y": [2, 3, 4]}
self.max_shape = {"x": [5, 3, 4], "y": [5, 3, 4]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestMinimumTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.minimum
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
"y": np.random.randn(2, 3, 4).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3, 4], "y": [1, 3, 4]}
self.opt_shape = {"x": [2, 3, 4], "y": [2, 3, 4]}
self.max_shape = {"x": [5, 3, 4], "y": [5, 3, 4]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestMinimumBroadcastTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.minimum
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
"y": np.random.randn(4).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3, 4], "y": [4]}
self.opt_shape = {"x": [2, 3, 4], "y": [4]}
self.max_shape = {"x": [5, 3, 4], "y": [4]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestMinimumIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.minimum
self.api_args = {
"x": np.random.randint(
low=1, high=100, size=(2, 3, 4), dtype="int64"
),
"y": np.random.randint(
low=1, high=100, size=(2, 3, 4), dtype="int64"
),
}
self.dynamic_shape_data = {
"x": lambda shape: np.random.randint(
1, 100, size=shape, dtype="int64"
),
"y": lambda shape: np.random.randint(
1, 100, size=shape, dtype="int64"
),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3, 4], "y": [1, 3, 4]}
self.opt_shape = {"x": [2, 3, 4], "y": [2, 3, 4]}
self.max_shape = {"x": [5, 3, 4], "y": [5, 3, 4]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestGreaterEqualTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.greater_equal
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestGreaterEqual_TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.greater_equal_
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestGreaterEqualINTTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.greater_equal
self.api_args = {
"x": np.random.randn(2, 3).astype("int64"),
"y": np.random.randn(2, 3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestGreaterEqual_INTTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.greater_equal_
self.api_args = {
"x": np.random.randn(2, 3).astype("int64"),
"y": np.random.randn(2, 3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestGreaterEqualErrorTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.greater_equal
self.api_args = {
"x": np.random.randn(2, 3).astype("bool"),
"y": np.random.randn(2, 3).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestLessEqualTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.less_equal
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestLessEqual_TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.less_equal_
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestLessEqualINTTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.less_equal
self.api_args = {
"x": np.random.randn(2, 3).astype("int64"),
"y": np.random.randn(2, 3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestLessEqual_INTTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.less_equal_
self.api_args = {
"x": np.random.randn(2, 3).astype("int64"),
"y": np.random.randn(2, 3).astype("int64"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestLessEqualErrorTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle._C_ops.less_equal
self.api_args = {
"x": np.random.randn(2, 3).astype("bool"),
"y": np.random.randn(2, 3).astype("bool"),
}
self.program_config = {"feed_list": ["x", "y"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.opt_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_marker(expected_result=False)
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,82 @@
# 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 unittest
import numpy as np
from get_program import (
get_bert_program,
)
from paddle.tensorrt.export import (
Input,
TensorRTConfig,
convert_to_trt,
)
from paddle.tensorrt.util import (
predict_program,
)
class TestConverterBert(unittest.TestCase):
def test_paddle_to_tensorrt_conversion_bert(self):
# Step1: get program and init fake inputs
program, scope, param_dict = get_bert_program()
# Set input
input_config = Input(
min_input_shape=(1, 100),
optim_input_shape=(4, 1000),
max_input_shape=(8, 1000),
)
input_config.input_data_type = 'int64'
input_min_data, _, input_max_data = input_config.generate_input_data()
# Create a TensorRTConfig with inputs as a required field.
trt_config = TensorRTConfig(inputs=[input_config])
trt_config.disable_ops = "pd_op.dropout"
trt_config.disable_passes = [
'constant_folding_pass',
'dead_code_elimination_pass',
]
# Step1.1: get original results(for tests only)
output_var = program.global_block().ops[-1].result(0)
output_expected = predict_program(
program, {"input_ids": input_min_data}, [output_var]
)
# get tensorrt_engine_op(converted_program)
program_with_trt = convert_to_trt(program, trt_config, scope)
output_var = program_with_trt.global_block().ops[-1].result(0)
# run inference(converted_program)
output_converted = predict_program(
program_with_trt,
{"input_ids": input_min_data},
[output_var],
)
# # Check that the results are close to each other within a tolerance of 1e-2
np.testing.assert_allclose(
output_expected[0],
output_converted[0],
rtol=1e-2,
atol=1e-2,
err_msg="Outputs are not within the 1e-2 tolerance",
)
if __name__ == "__main__":
unittest.main()
+121
View File
@@ -0,0 +1,121 @@
# 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 unittest
import numpy as np
from get_program import (
get_dummy_program,
)
from paddle.tensorrt.export import (
Input,
PrecisionMode,
TensorRTConfig,
convert_to_trt,
)
from paddle.tensorrt.util import (
predict_program,
)
class TestConverterDummy(unittest.TestCase):
def test_paddle_to_tensorrt_conversion_dummy(self):
program, scope, param_dict = get_dummy_program()
# Set input
input_config = Input(
min_input_shape=(1, 64),
optim_input_shape=(4, 64),
max_input_shape=(8, 64),
input_data_type='float32',
)
_, input_optim_data, _ = input_config.generate_input_data()
# Create a TensorRTConfig with inputs as a required field.
trt_config = TensorRTConfig(inputs=[input_config])
trt_config.precision_mode = PrecisionMode.FP16
trt_config.ops_run_float = "pd_op.add"
trt_config.optimization_level = 5
trt_config.disable_passes = ['dead_code_elimination_pass']
output_var = program.list_vars()[-1]
# get original results(for tests only)
output_expected = predict_program(
program, {"input": input_optim_data}, [output_var]
)
# get tensorrt_engine_op(converted_program)
program_with_trt = convert_to_trt(program, trt_config, scope)
output_var = program_with_trt.list_vars()[-1]
# run inference(converted_program)
output_converted = predict_program(
program_with_trt, {"input": input_optim_data}, [output_var]
)
# Check that the results are close to each other within a tolerance of 1e-2
np.testing.assert_allclose(
output_expected[0],
output_converted[0],
rtol=1e-2,
atol=1e-2,
err_msg="Outputs are not within the 1e-2 tolerance",
)
def test_paddle_to_tensorrt_collect_shape(self):
program, scope, param_dict = get_dummy_program()
# Set input
input_data = tuple(
np.random.rand(n, 64).astype(np.float32) for n in (1, 4, 8)
)
input_optim_data = input_data[1]
input_config = Input(warmup_data=input_data)
# Create a TensorRTConfig with inputs as a required field.
trt_config = TensorRTConfig(inputs=[input_config])
trt_config.precision_mode = PrecisionMode.FP16
trt_config.ops_run_float = "pd_op.add"
trt_config.optimization_level = 5
trt_config.disable_passes = ['dead_code_elimination_pass']
# get tensorrt_engine_op(converted_program)
program_with_trt = convert_to_trt(program, trt_config, scope)
output_var = program.list_vars()[-1]
# get original results(for tests only)
output_expected = predict_program(
program, {"input": input_optim_data}, [output_var]
)
output_var = program_with_trt.list_vars()[-1]
# run inference(converted_program)
output_converted = predict_program(
program_with_trt, {"input": input_optim_data}, [output_var]
)
# Check that the results are close to each other within a tolerance of 1e-2
np.testing.assert_allclose(
output_expected[0],
output_converted[0],
rtol=1e-2,
atol=1e-2,
err_msg="Auto shape collection outputs mismatch",
)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,306 @@
# 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 tempfile
import unittest
import numpy as np
from get_program import (
get_r50_program,
get_r50_refit_program,
)
import paddle
import paddle.inference as paddle_infer
from paddle.quantization import PTQ, QuantConfig
from paddle.quantization.observers import AbsmaxObserver
from paddle.tensorrt.export import (
Input,
PrecisionMode,
TensorRTConfig,
convert,
convert_to_trt,
)
from paddle.tensorrt.util import (
predict_program,
)
from paddle.vision.models import resnet18
# NOTE(Pan Zhaowu): using legacy linear to fulfill promise of tensorrt graph capturing
# and converting.
paddle.set_flags({"FLAGS_use_legacy_linear": True})
def standardize(array):
mean_val = np.mean(array)
std_val = np.std(array)
standardized_array = (array - mean_val) / std_val
return standardized_array
class TestConverterResNet50(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.path = os.path.join(self.temp_dir.name, 'pir-trt')
def test_paddle_to_tensorrt_conversion_r50(self):
# Step1: get program and init fake inputs
program, scope, param_dict = get_r50_program()
# Set input
input_config = Input(
min_input_shape=(1, 3, 224, 224),
optim_input_shape=(1, 3, 224, 224),
max_input_shape=(4, 3, 224, 224),
input_data_type='float32',
name='input',
)
_, input_optim_data, _ = input_config.generate_input_data()
# Create a TensorRTConfig with inputs as a required field.
trt_config = TensorRTConfig(inputs=[input_config])
trt_config.disable_passes = ['dead_code_elimination_pass']
output_var = program.list_vars()[-1]
# get original results(for tests only)
output_expected = predict_program(
program, {"input": input_optim_data}, [output_var]
)
program_with_trt = convert_to_trt(program, trt_config, scope)
output_var = program_with_trt.list_vars()[-1]
# Step6: run inference(converted_program)
output_converted = predict_program(
program_with_trt, {"input": input_optim_data}, [output_var]
)
output_expected = standardize(output_expected[0])
output_trt = standardize(output_converted[0])
# Check that the results are close to each other within a tolerance of 1e-3
np.testing.assert_allclose(
output_expected,
output_trt,
rtol=1e-3,
atol=1e-3,
err_msg="Outputs are not within the 1e-3 tolerance",
)
def test_refit(self):
# Step1: get program and init fake inputs
paddle.enable_static()
save_path = os.path.join(self.temp_dir.name, 'resnet50')
program, scope, param_dict = get_r50_refit_program(save_path)
# Set input
input_config = Input(
min_input_shape=(1, 3, 224, 224),
optim_input_shape=(1, 3, 224, 224),
max_input_shape=(4, 3, 224, 224),
input_data_type='float32',
)
_, input_optim_data, _ = input_config.generate_input_data()
# Create a TensorRTConfig with inputs as a required field.
trt_config = TensorRTConfig(inputs=[input_config])
output_var = program.list_vars()[-1]
# get original results(for tests only)
output_expected = predict_program(
program, {"input": input_optim_data}, [output_var]
)
trt_save_path = os.path.join(self.temp_dir.name, 'resnet50trt')
trt_config.save_model_dir = trt_save_path
trt_config.refit_params_path = save_path + '.pdiparams'
model_dir = save_path
program_with_trt = paddle.tensorrt.convert(model_dir, trt_config)
config = paddle_infer.Config(
trt_config.save_model_dir + '.json',
trt_config.save_model_dir + '.pdiparams',
)
config.switch_ir_debug(True)
if paddle.is_compiled_with_cuda():
config.enable_use_gpu(100, 0)
else:
config.disable_gpu()
predictor = paddle_infer.create_predictor(config)
paddle.disable_static()
for i, input_instance in enumerate(trt_config.inputs):
min_data, _, max_data = input_instance.generate_input_data()
model_inputs = paddle.to_tensor(min_data)
output_converted = predictor.run([model_inputs])
output_expected = standardize(output_expected[0])
output_trt = standardize(output_converted[0].numpy())
np.testing.assert_allclose(
output_expected,
output_trt,
rtol=1e-1,
atol=1e-1,
err_msg="Outputs are not within the 1e-1 tolerance",
)
def test_paddle_to_tensorrt_conversion_r50_collect_shape(self):
# Step1: get program and init fake inputs
program, scope, param_dict = get_r50_program()
# Set input
input_data = tuple(
np.random.rand(n, 3, 224, 224).astype(np.float32) for n in (1, 2, 4)
)
input_optim_data = input_data[1]
input_config = Input(warmup_data=input_data)
# Create a TensorRTConfig with inputs as a required field.
trt_config = TensorRTConfig(inputs=[input_config])
trt_config.disable_passes = ['dead_code_elimination_pass']
output_var = program.list_vars()[-1]
# get original results(for tests only)
output_expected = predict_program(
program, {"input": input_optim_data}, [output_var]
)
program_with_trt = convert_to_trt(program, trt_config, scope)
output_var = program_with_trt.list_vars()[-1]
# Step6: run inference(converted_program)
output_converted = predict_program(
program_with_trt, {"input": input_optim_data}, [output_var]
)
output_expected = standardize(output_expected[0])
output_trt = standardize(output_converted[0])
# Check that the results are close to each other within a tolerance of 1e-3
np.testing.assert_allclose(
output_expected,
output_trt,
rtol=1e-2,
atol=1e-2,
err_msg="Outputs are not within the 1e-2 tolerance",
)
def test_convert_quant_model(self):
paddle.disable_static()
image = paddle.ones([1, 3, 224, 224], dtype="float32")
model = resnet18()
model.eval()
output_fp32 = model(image)
observer = AbsmaxObserver(quant_bits=8)
q_config = QuantConfig(activation=observer, weight=observer)
ptq = PTQ(q_config)
quant_model = ptq.quantize(model)
out = quant_model(image)
converted_model = ptq.convert(quant_model)
save_path = os.path.join(self.temp_dir.name, 'int8_infer')
paddle.jit.save(converted_model, save_path, input_spec=[image])
paddle.enable_static()
trt_save_path = os.path.join(self.temp_dir.name, 'int8_trt_infer')
# Set input
input_config = Input(
min_input_shape=(1, 3, 224, 224),
optim_input_shape=(1, 3, 224, 224),
max_input_shape=(1, 3, 224, 224),
input_data_type='float32',
)
trt_config = TensorRTConfig(inputs=[input_config])
trt_config.disable_passes = ['dead_code_elimination_pass']
trt_config.save_model_dir = trt_save_path
trt_config.precision_mode = PrecisionMode.INT8
convert(save_path, trt_config)
config = paddle_infer.Config(
trt_config.save_model_dir + '.json',
trt_config.save_model_dir + '.pdiparams',
)
config.enable_use_gpu(100, 0)
predictor = paddle_infer.create_predictor(config)
output_trt_int8 = predictor.run([image])
# Check that the results are close to each other within a tolerance of 0.9
np.testing.assert_allclose(
output_fp32,
output_trt_int8[0],
rtol=0.9,
atol=0.9,
err_msg="Outputs are not within the 0.9 tolerance",
)
def test_paddle_to_tensorrt_conversion_r50_use_cuda_graph(self):
# Step1: get program and init fake inputs
program, scope, param_dict = get_r50_program()
# Set input
input_config = Input(
min_input_shape=(1, 3, 224, 224),
optim_input_shape=(1, 3, 224, 224),
max_input_shape=(4, 3, 224, 224),
input_data_type='float32',
name='input',
)
_, input_optim_data, _ = input_config.generate_input_data()
# Create a TensorRTConfig with inputs as a required field.
trt_config = TensorRTConfig(inputs=[input_config])
trt_config.disable_passes = ['dead_code_elimination_pass']
# use_cuda_graph: True
trt_config.use_cuda_graph = True
output_var = program.list_vars()[-1]
# get original results(for tests only)
output_expected = predict_program(
program, {"input": input_optim_data}, [output_var]
)
program_with_trt = convert_to_trt(program, trt_config, scope)
output_var = program_with_trt.list_vars()[-1]
# Step6: run inference(converted_program)
output_converted = predict_program(
program_with_trt, {"input": input_optim_data}, [output_var]
)
output_expected = standardize(output_expected[0])
output_trt = standardize(output_converted[0])
# Check that the results are close to each other within a tolerance of 1e-3
np.testing.assert_allclose(
output_expected,
output_trt,
rtol=1e-3,
atol=1e-3,
err_msg="Outputs are not within the 1e-3 tolerance",
)
if __name__ == "__main__":
unittest.main()
@@ -0,0 +1,165 @@
# 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 tempfile
import unittest
import numpy as np
from get_program import (
get_r50_program,
get_r50_refit_program,
)
import paddle
import paddle.inference as paddle_infer
from paddle.tensorrt.export import (
Input,
TensorRTConfig,
convert_to_trt,
)
from paddle.tensorrt.util import (
predict_program,
)
# NOTE(Pan Zhaowu): using legacy linear to fulfill promise of tensorrt graph capturing
# and converting.
paddle.set_flags({"FLAGS_use_legacy_linear": True})
def standardize(array):
mean_val = np.mean(array)
std_val = np.std(array)
standardized_array = (array - mean_val) / std_val
return standardized_array
class TestConverterResNet50Move(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.path = os.path.join(self.temp_dir.name, 'pir-trt')
def test_paddle_to_tensorrt_conversion_r50(self):
# Step1: get program and init fake inputs
program, scope, param_dict = get_r50_program()
# Set input
input_config = Input(
min_input_shape=(1, 3, 224, 224),
optim_input_shape=(1, 3, 224, 224),
max_input_shape=(4, 3, 224, 224),
input_data_type='float32',
name='input',
)
_, input_optim_data, _ = input_config.generate_input_data()
# Create a TensorRTConfig with inputs as a required field.
trt_config = TensorRTConfig(inputs=[input_config])
trt_config.disable_passes = ['dead_code_elimination_pass']
output_var = program.list_vars()[-1]
# get original results(for tests only)
output_expected = predict_program(
program, {"input": input_optim_data}, [output_var]
)
program_with_trt = convert_to_trt(program, trt_config, scope)
output_var = program_with_trt.list_vars()[-1]
# Step6: run inference(converted_program)
output_converted = predict_program(
program_with_trt, {"input": input_optim_data}, [output_var]
)
output_expected = standardize(output_expected[0])
output_trt = standardize(output_converted[0])
# Check that the results are close to each other within a tolerance of 1e-3
np.testing.assert_allclose(
output_expected,
output_trt,
rtol=1e-3,
atol=1e-3,
err_msg="Outputs are not within the 1e-3 tolerance",
)
def test_engine_serialized_path_move(self):
paddle.enable_static()
save_path = os.path.join(self.temp_dir.name, 'resnet50')
program, scope, param_dict = get_r50_refit_program(save_path)
input_config = Input(
min_input_shape=(1, 3, 224, 224),
optim_input_shape=(1, 3, 224, 224),
max_input_shape=(4, 3, 224, 224),
input_data_type='float32',
)
_, input_optim_data, _ = input_config.generate_input_data()
trt_config = TensorRTConfig(inputs=[input_config])
output_var = program.list_vars()[-1]
output_expected = predict_program(
program, {"input": input_optim_data}, [output_var]
)
trt_save_path = os.path.join(self.temp_dir.name, 'resnet50trt')
trt_config.save_model_dir = trt_save_path
cache_path = trt_config.save_model_dir
model_dir = save_path
program_with_trt = paddle.tensorrt.convert(model_dir, trt_config)
config_json = cache_path + '.json'
params_file = cache_path + '.pdiparams'
import shutil
cache_path_new = '/root/.pp_trt_cache_test'
config_json_new = cache_path_new + '.json'
params_file_new = cache_path_new + '.pdiparams'
if os.path.exists(cache_path_new):
shutil.rmtree(cache_path_new)
shutil.copytree(cache_path, cache_path_new)
shutil.copy2(config_json, config_json_new)
shutil.rmtree(cache_path)
config = paddle_infer.Config(config_json_new, params_file_new)
config.switch_ir_debug(True)
if paddle.is_compiled_with_cuda():
config.enable_use_gpu(100, 0)
else:
config.disable_gpu()
predictor = paddle_infer.create_predictor(config)
paddle.disable_static()
for i, input_instance in enumerate(trt_config.inputs):
min_data, _, max_data = input_instance.generate_input_data()
model_inputs = paddle.to_tensor(min_data)
output_converted = predictor.run([model_inputs])
output_expected = standardize(output_expected[0])
output_trt = standardize(output_converted[0].numpy())
np.testing.assert_allclose(
output_expected,
output_trt,
rtol=1e-1,
atol=1e-1,
err_msg="Outputs are not within the 1e-1 tolerance",
)
if __name__ == "__main__":
unittest.main()
+292
View File
@@ -0,0 +1,292 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
def batch_norm_wrapper(x):
batch_norm = paddle.nn.BatchNorm(num_channels=1, is_test=True)
return batch_norm(x)
class TestBatchNormTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = batch_norm_wrapper
self.api_args = {
"x": np.arange(12).reshape([2, 1, 2, 3]).astype("float32")
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 1, 2, 3]}
self.opt_shape = {"x": [2, 1, 2, 3]}
self.max_shape = {"x": [5, 1, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
def instance_norm_wrapper(x, weight, bias):
return paddle.nn.functional.instance_norm(x, None, None, weight, bias)
class TestInstanceNormTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = instance_norm_wrapper
self.api_args = {
"x": np.arange(12).reshape([2, 2, 1, 3]).astype("float32"),
"weight": np.random.random([2]).astype("float32"),
"bias": np.random.random([2]).astype("float32"),
}
self.program_config = {"feed_list": ["x", "weight", "bias"]}
self.min_shape = {"x": [1, 2, 1, 3]}
self.opt_shape = {"x": [2, 2, 1, 3]}
self.max_shape = {"x": [5, 2, 1, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestInstanceNormWith3DInputTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = instance_norm_wrapper
self.api_args = {
"x": np.arange(4).reshape([2, 2, 1]).astype("float32"),
"weight": np.random.random([2]).astype("float32"),
"bias": np.random.random([2]).astype("float32"),
}
self.program_config = {"feed_list": ["x", "weight", "bias"]}
self.min_shape = {"x": [1, 2, 1]}
self.opt_shape = {"x": [2, 2, 1]}
self.max_shape = {"x": [5, 2, 1]}
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestInstanceNormWithNoneInputTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = instance_norm_wrapper
self.api_args = {
"x": np.arange(12).reshape([2, 2, 1, 3]).astype("float32"),
"weight": None,
"bias": None,
}
self.program_config = {"feed_list": ["x", "weight", "bias"]}
self.min_shape = {"x": [1, 2, 1, 3]}
self.opt_shape = {"x": [2, 2, 1, 3]}
self.max_shape = {"x": [5, 2, 1, 3]}
def test_trt_result(self):
self.check_marker(expected_result=False)
def fused_bias_dropout_residual_layer_norm(
x,
residual,
bias_shape,
ln_scale_shape,
ln_bias_shape,
dropout_rate,
ln_epsilon,
):
bias = paddle.create_parameter(
shape=bias_shape, dtype='float32', name="bias"
)
ln_scale = paddle.create_parameter(
shape=ln_scale_shape, dtype='float32', name="ln_scale"
)
ln_bias = paddle.create_parameter(
shape=ln_bias_shape, dtype='float32', name="ln_bias"
)
return paddle.incubate.nn.functional.fused_bias_dropout_residual_layer_norm(
x,
residual,
bias,
ln_scale,
ln_bias,
dropout_rate=dropout_rate,
ln_epsilon=ln_epsilon,
)
class TestFusedBiasDropoutResidualLayerNormTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = fused_bias_dropout_residual_layer_norm
self.api_args = {
"x": np.random.rand(2, 4, 128).astype("float32"),
"residual": np.random.rand(2, 4, 128).astype("float32"),
"bias_shape": [128],
"ln_scale_shape": [128],
"ln_bias_shape": [128],
"dropout_rate": 0.0,
"ln_epsilon": 1e-5,
}
self.program_config = {"feed_list": ["x", "residual"]}
self.min_shape = {"x": [2, 4, 128]}
self.opt_shape = {"x": [4, 4, 128]}
self.max_shape = {"x": [8, 4, 128]}
# TODO(bukejiyu): FusedBiasDropoutResidualLayerNorm will support FP16 UT in the future.
def test_fp16_trt_result(self):
with self.assertRaises(NotImplementedError) as context:
self.check_trt_result(rtol=1e-2, atol=1e-2, precision_mode="fp16")
class TestFusedBiasDropoutResidualLayerNormCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = fused_bias_dropout_residual_layer_norm
self.api_args = {
"x": np.random.rand(2, 4, 128).astype("float32"),
"residual": np.random.rand(2, 4, 128).astype("float32"),
"bias_shape": [128],
"ln_scale_shape": [128],
"ln_bias_shape": [128],
"dropout_rate": 0.0,
"ln_epsilon": 1e-5,
}
self.program_config = {"feed_list": ["x", "residual"]}
self.min_shape = {"x": [2, 4, 128]}
self.opt_shape = {"x": [4, 4, 128]}
self.max_shape = {"x": [8, 4, 128]}
def test_fp32_trt_result(self):
self.check_trt_result()
class TestFusedBiasDropoutResidualLayerNormErrorTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = fused_bias_dropout_residual_layer_norm
self.api_args = {
"x": np.random.rand(2, 4, 128).astype("float32"),
"residual": np.random.rand(2, 4, 128).astype("float32"),
"bias_shape": [128],
"ln_scale_shape": [128],
"ln_bias_shape": [128],
"dropout_rate": 1.0,
"ln_epsilon": 1e-5,
}
self.program_config = {"feed_list": ["x", "residual"]}
self.min_shape = {"x": [2, 4, 128]}
self.opt_shape = {"x": [4, 4, 128]}
self.max_shape = {"x": [8, 4, 128]}
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestGroupNormNCHWFP32TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.group_norm
self.api_args = {
"x": np.random.random([4, 32, 64, 64]).astype(np.float32),
"num_groups": 2,
"epsilon": 1e-05,
"weight": np.random.randn(32).astype(np.float32),
"bias": np.random.randn(32).astype(np.float32),
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x", "weight", "bias"]}
self.min_shape = {"x": [1, 32, 64, 64]}
self.opt_shape = {"x": [4, 32, 64, 64]}
self.max_shape = {"x": [6, 32, 64, 64]}
def test_trt_result(self):
self.check_trt_result()
class TestGroupNormNCHWFP16TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.group_norm
self.api_args = {
"x": np.random.random([4, 32, 64, 64]).astype(np.float32),
"num_groups": 2,
"epsilon": 1e-05,
"weight": np.random.randn(32).astype(np.float32),
"bias": np.random.randn(32).astype(np.float32),
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x", "weight", "bias"]}
self.min_shape = {"x": [1, 32, 64, 64]}
self.opt_shape = {"x": [4, 32, 64, 64]}
self.max_shape = {"x": [6, 32, 64, 64]}
def test_trt_result(self):
self.check_trt_result(precision_mode="fp16")
def layer_norm_wrapper(x, weight, bias):
normalized_shape = x.shape[1:]
begin_norm_axis = 1
epsilon = 1e-5
return paddle._C_ops.layer_norm(x, weight, bias, epsilon, begin_norm_axis)
def layer_norm_wrapper_1(x, weight, bias):
weight = paddle.to_tensor(weight)
bias = paddle.to_tensor(bias)
normalized_shape = x.shape[1:]
begin_norm_axis = 1
epsilon = 1e-5
return paddle._C_ops.layer_norm(x, weight, bias, epsilon, begin_norm_axis)
class TestLayerNormTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = layer_norm_wrapper
normalized_shape = [3, 4, 5]
normalized_size = np.prod(normalized_shape)
self.api_args = {
"x": np.random.random([2, 3, 4, 5]).astype("float32"),
"weight": np.random.random([normalized_size]).astype("float32"),
"bias": np.random.random([normalized_size]).astype("float32"),
}
self.program_config = {"feed_list": ["x", "weight", "bias"]}
self.min_shape = {"x": [1, 3, 4, 5]}
self.opt_shape = {"x": [2, 3, 4, 5]}
self.max_shape = {"x": [4, 3, 4, 5]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestLayerNorm2DTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = layer_norm_wrapper_1
normalized_size = 128
self.api_args = {
"x": np.random.random([2, 128]).astype("float32"),
"weight": np.random.random([normalized_size]).astype("float32"),
"bias": np.random.random([normalized_size]).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 128]}
self.opt_shape = {"x": [2, 128]}
self.max_shape = {"x": [4, 128]}
def test_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
if __name__ == '__main__':
unittest.main()
+566
View File
@@ -0,0 +1,566 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
class TestSqrtTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.sqrt
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestFloorFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.floor
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestExpFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.exp
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestAbsFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.abs
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestAbsIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.abs
self.api_args = {
"x": np.random.randn(7, 3).astype("int64"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestSinFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.sin
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestCosFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.cos
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestSinhFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.sinh
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestCoshFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.cosh
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestAsinhFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.asinh
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestAcoshFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.acosh
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestCeilFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.ceil
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestRsqrtFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.rsqrt
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestReciprocalFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.reciprocal
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestErfFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.erf
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestSignFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.sign
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestSignIntTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.sign
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestRoundFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.round
self.api_args = {
"x": np.random.randn(7, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [3, 3]}
self.opt_shape = {"x": [7, 3]}
self.max_shape = {"x": [10, 3]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def roi_align(
x, boxes, boxes_num, output_size, spatial_scale, sampling_ratio, aligned
):
x = paddle.to_tensor(x)
boxes = paddle.to_tensor(boxes)
boxes_num = paddle.to_tensor(boxes_num)
roi_align_out = paddle.vision.ops.roi_align(
x,
boxes,
boxes_num,
output_size,
spatial_scale,
sampling_ratio,
aligned,
)
return roi_align_out
class TestRoiAlignPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = roi_align
boxes = np.random.random([3, 4]).astype(np.float32)
boxes[:, 2] += boxes[:, 0] + 3
boxes[:, 3] += boxes[:, 1] + 4
self.api_args = {
"x": np.random.random((1, 256, 32, 32)).astype("float32"),
"boxes": boxes,
"boxes_num": np.array([3]).astype(np.int32),
"output_size": (3, 3),
"spatial_scale": 1.0,
"sampling_ratio": -1,
"aligned": True,
}
self.program_config = {"feed_list": ["x", "boxes", "boxes_num"]}
self.min_shape = {"x": [1, 256, 32, 32], "boxes": [3, 4]}
self.opt_shape = {"x": [1, 256, 32, 32], "boxes": [3, 4]}
self.max_shape = {"x": [1, 256, 32, 32], "boxes": [3, 4]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_fp16_result(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
def yolo_box(x, img_size):
x = paddle.to_tensor(x)
img_size = paddle.to_tensor(img_size)
boxes, scores = paddle.vision.ops.yolo_box(
x,
img_size=img_size,
anchors=[10, 13, 16, 30],
class_num=2,
conf_thresh=0.01,
downsample_ratio=8,
clip_bbox=True,
scale_x_y=1.0,
)
return boxes, scores
class TestYoloBoxPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = yolo_box
self.api_args = {
"x": np.random.randn(2, 14, 8, 8).astype("float32"),
"img_size": np.ones([2, 2]).astype("int32"),
}
self.program_config = {"feed_list": []}
self.min_shape = {}
self.opt_shape = {}
self.max_shape = {}
def test_trt_result(self):
self.check_trt_result()
def test_trt_fp16_result(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
class TestYoloBoxDynamicShapePattern(TensorRTBaseTest):
def setUp(self):
self.python_api = yolo_box
self.api_args = {
"x": np.random.randn(2, 14, 8, 8).astype("float32"),
"img_size": np.ones([2, 2]).astype("int32"),
}
self.program_config = {"feed_list": ["x", "img_size"]}
self.min_shape = {"x": [1, 14, 8, 8], "img_size": [1, 2]}
self.opt_shape = {"x": [2, 14, 8, 8], "img_size": [2, 2]}
self.max_shape = {"x": [3, 14, 8, 8], "img_size": [3, 2]}
def test_trt_result_dynamic(self):
self.check_trt_result()
def test_trt_fp16_result_dynamic(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
class TestTanTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.tan
self.api_args = {
"x": np.random.randn(1, 2, 32, 32).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 32, 32]}
self.opt_shape = {"x": [1, 2, 32, 32]}
self.max_shape = {"x": [1, 2, 32, 32]}
def test_trt_result(self):
self.check_trt_result(rtol=1e-3, atol=1e-3)
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
class TestAsinTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.asin
self.api_args = {
"x": np.random.randn(1, 2, 32, 32).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 32, 32]}
self.opt_shape = {"x": [1, 2, 32, 32]}
self.max_shape = {"x": [2, 2, 32, 32]}
def test_trt_result(self):
self.check_trt_result(rtol=1e-3, atol=1e-3)
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
def deform_conv2d_wrapper(
input_data,
offset,
weight_shape,
mask=None,
stride=1,
padding=0,
dilation=1,
deformable_groups=1,
groups=1,
im2col_step=1,
):
weights = paddle.create_parameter(
shape=weight_shape, dtype='float32', name="weights"
)
return paddle.vision.ops.deform_conv2d(
input_data,
offset,
weights,
None,
stride,
padding,
dilation,
deformable_groups,
groups,
mask,
)
class TestDeformableConvTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = deform_conv2d_wrapper
self.api_args = {
"input_data": np.random.random([8, 1, 28, 28]).astype(np.float32),
"offset": np.random.random([8, 2 * 3 * 3, 26, 26]).astype(
np.float32
),
"weight_shape": [16, 1, 3, 3],
"mask": np.random.random([8, 3 * 3, 26, 26]).astype(np.float32),
}
self.program_config = {"feed_list": ["input_data", "offset", "mask"]}
self.min_shape = {"input_data": [1, 1, 28, 28]}
self.opt_shape = {"input_data": [8, 1, 28, 28]}
self.max_shape = {"input_data": [10, 1, 28, 28]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestAcosTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.acos
self.api_args = {
"x": np.random.randn(1, 2, 32, 32).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 32, 32]}
self.opt_shape = {"x": [1, 2, 32, 32]}
self.max_shape = {"x": [2, 2, 32, 32]}
def test_trt_result(self):
self.check_trt_result(rtol=1e-3, atol=1e-3)
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
class TestAtanTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.atan
self.api_args = {
"x": np.random.randn(1, 2, 32, 32).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2, 32, 32]}
self.opt_shape = {"x": [1, 2, 32, 32]}
self.max_shape = {"x": [2, 2, 32, 32]}
def test_trt_result(self):
self.check_trt_result(rtol=1e-3, atol=1e-3)
def test_trt_result_fp16(self):
self.check_trt_result(rtol=1e-3, atol=1e-3, precision_mode="fp16")
if __name__ == '__main__':
unittest.main()
+711
View File
@@ -0,0 +1,711 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
from paddle import _C_ops
def api_wrapper(x):
return paddle._C_ops.share_data(x)
def multiclass_nms3(
bboxes,
scores,
rois_num=None,
score_threshold=0.3,
nms_top_k=4,
keep_top_k=1,
nms_threshold=0.3,
normalized=True,
nms_eta=1.5,
background_label=-1,
return_index=False,
return_rois_num=True,
name=None,
):
attrs = (
score_threshold,
nms_top_k,
keep_top_k,
nms_threshold,
normalized,
nms_eta,
background_label,
)
output, index, nms_rois_num = _C_ops.multiclass_nms3(
bboxes, scores, rois_num, *attrs
)
if not return_index:
index = None
return output, nms_rois_num, index
class TestMulticlassNMS3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = multiclass_nms3
self.api_args = {
"bboxes": np.random.randn(2, 5, 4).astype("float32"),
"scores": np.random.randn(2, 4, 5).astype("float32"),
}
self.program_config = {"feed_list": ["bboxes", "scores"]}
self.min_shape = {"bboxes": [1, 5, 4], "scores": [1, 4, 5]}
self.opt_shape = {"bboxes": [2, 5, 4], "scores": [2, 4, 5]}
self.max_shape = {"bboxes": [3, 5, 4], "scores": [3, 4, 5]}
def test_trt_result(self):
self.check_trt_result()
class TestMulticlassNMS3Marker(TensorRTBaseTest):
def setUp(self):
self.python_api = multiclass_nms3
self.api_args = {
"bboxes": np.random.randn(2, 5, 4, 1).astype("float32"),
"scores": np.random.randn(2, 4, 5, 1).astype("float32"),
}
self.program_config = {"feed_list": ["bboxes", "scores"]}
self.target_marker_op = "pd_op.multiclass_nms3"
def test_trt_result(self):
self.check_marker(expected_result=False)
def set_value(
x, starts, ends, steps, axes, decrease_axes, none_axes, shape, values
):
output = _C_ops.set_value(
x,
starts,
ends,
steps,
axes,
decrease_axes,
none_axes,
shape,
values,
)
return output
def set_value_(
x, starts, ends, steps, axes, decrease_axes, none_axes, shape, values
):
output = _C_ops.set_value_(
x,
starts,
ends,
steps,
axes,
decrease_axes,
none_axes,
shape,
values,
)
return output
def set_value_with_tensor(
x, values, starts, ends, steps, axes, decrease_axes, none_axes, shape
):
output = _C_ops.set_value_with_tensor(
x,
values,
starts,
ends,
steps,
axes,
decrease_axes,
none_axes,
shape,
)
return output
def set_value_with_tensor_(
x, values, starts, ends, steps, axes, decrease_axes, none_axes, shape
):
output = _C_ops.set_value_with_tensor_(
x,
values,
starts,
ends,
steps,
axes,
decrease_axes,
none_axes,
shape,
)
return output
class TestSetValueTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value
self.api_args = {
"x": np.ones([10, 2]).astype("float32"),
"starts": [0],
"ends": [1],
"steps": [1],
"axes": [1],
"decrease_axes": [],
"none_axes": [],
"shape": [],
"values": [10.0],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2]}
self.opt_shape = {"x": [2, 2]}
self.max_shape = {"x": [20, 2]}
def test_trt_result(self):
self.check_trt_result()
# starts/ends/steps is not one element
class TestSetValueMarkerCase1(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value
self.api_args = {
"x": np.ones([10, 2]).astype("float32"),
"starts": [0, 0],
"ends": [1, 1],
"steps": [1, 1],
"axes": [1],
"decrease_axes": [],
"none_axes": [],
"shape": [],
"values": [10.0],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2]}
self.opt_shape = {"x": [2, 2]}
self.max_shape = {"x": [5, 2]}
def test_trt_result(self):
self.check_marker(expected_result=False)
# decrease_axes has element
class TestSetValueMarkerCase2(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value
self.api_args = {
"x": np.ones([10, 2]).astype("float32"),
"starts": [0],
"ends": [1],
"steps": [1],
"axes": [1],
"decrease_axes": [1],
"none_axes": [],
"shape": [],
"values": [10.0],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2]}
self.opt_shape = {"x": [2, 2]}
self.max_shape = {"x": [20, 2]}
def test_trt_result(self):
self.check_marker(expected_result=False)
# values has more than one element
class TestSetValueMarkerCase3(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value
self.api_args = {
"x": np.ones([10, 2]).astype("float32"),
"starts": [0],
"ends": [1],
"steps": [1],
"axes": [1],
"decrease_axes": [],
"none_axes": [],
"shape": [],
"values": [10.0, 0],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2]}
self.opt_shape = {"x": [2, 2]}
self.max_shape = {"x": [20, 2]}
def test_trt_result(self):
self.check_marker(expected_result=False)
# values has int element
class TestSetValueMarkerCase4(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value
self.api_args = {
"x": np.ones([10, 2]).astype("float32"),
"starts": [0],
"ends": [1],
"steps": [1],
"axes": [1],
"decrease_axes": [],
"none_axes": [],
"shape": [],
"values": [10],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2]}
self.opt_shape = {"x": [2, 2]}
self.max_shape = {"x": [20, 2]}
def test_trt_result(self):
self.check_marker(expected_result=False)
# starts is not constant value
class TestSetValueMarkerCase5(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value
self.api_args = {
"x": np.ones([10, 2]).astype("float32"),
"starts": np.zeros([1]).astype("int64"),
"ends": [1],
"steps": [1],
"axes": [1],
"decrease_axes": [],
"none_axes": [],
"shape": [],
"values": [10.0],
}
self.program_config = {"feed_list": ["x", "starts"]}
self.min_shape = {"x": [1, 2]}
self.opt_shape = {"x": [2, 2]}
self.max_shape = {"x": [20, 2]}
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestSetValue_TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value_
self.api_args = {
"x": np.ones([10, 2]).astype("float32"),
"starts": [0],
"ends": [1],
"steps": [1],
"axes": [1],
"decrease_axes": [],
"none_axes": [],
"shape": [],
"values": [10.0],
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 2]}
self.opt_shape = {"x": [2, 2]}
self.max_shape = {"x": [20, 2]}
def test_trt_result(self):
self.check_trt_result()
class TestSetValueWithTensorTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value_with_tensor
self.api_args = {
"x": np.ones([2, 3, 3]).astype("float32"),
"values": np.random.randn(2, 2, 3).astype("float32"),
"starts": [0],
"ends": [2],
"steps": [1],
"axes": [1],
"decrease_axes": [],
"none_axes": [],
"shape": [],
}
self.program_config = {"feed_list": ["x", "values"]}
self.min_shape = {"x": [1, 3, 3], "values": [1, 2, 3]}
self.opt_shape = {"x": [2, 3, 3], "values": [2, 2, 3]}
self.max_shape = {"x": [4, 3, 3], "values": [4, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
# values is int type
class TestSetValueWithTensorMarkerCase1(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value_with_tensor
self.api_args = {
"x": np.ones([2, 3, 3]).astype("float32"),
"values": np.random.randn(2, 2, 3).astype("int32"),
"starts": [0],
"ends": [2],
"steps": [1],
"axes": [1],
"decrease_axes": [],
"none_axes": [],
"shape": [],
}
self.program_config = {"feed_list": ["x", "values"]}
self.min_shape = {"x": [1, 3, 3], "values": [1, 2, 3]}
self.opt_shape = {"x": [2, 3, 3], "values": [2, 2, 3]}
self.max_shape = {"x": [4, 3, 3], "values": [4, 2, 3]}
def test_trt_result(self):
self.check_marker(expected_result=False)
class TestSetValueWithTensor_TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = set_value_with_tensor_
self.api_args = {
"x": np.ones([2, 3, 3]).astype("float32"),
"values": np.random.randn(2, 2, 3).astype("float32"),
"starts": [0],
"ends": [2],
"steps": [1],
"axes": [1],
"decrease_axes": [],
"none_axes": [],
"shape": [],
}
self.program_config = {"feed_list": ["x", "values"]}
self.min_shape = {"x": [1, 3, 3], "values": [1, 2, 3]}
self.opt_shape = {"x": [2, 3, 3], "values": [2, 2, 3]}
self.max_shape = {"x": [4, 3, 3], "values": [4, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestShareDataTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = api_wrapper
self.api_args = {
"x": np.random.rand(4, 3, 5).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [4, 3, 5]}
self.opt_shape = {"x": [5, 3, 5]}
self.max_shape = {"x": [6, 3, 5]}
def test_trt_result(self):
self.check_trt_result()
class TestTemporalShiftTRTPatternBasic(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.temporal_shift
self.api_args = {
"x": np.random.random([4, 9, 7, 7]).astype(np.float32),
"seg_num": 2,
"shift_ratio": 0.2,
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 9, 7, 7]}
self.opt_shape = {"x": [2, 9, 7, 7]}
self.max_shape = {"x": [8, 9, 7, 7]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestTemporalShiftTRTPatternZeroSlice(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.temporal_shift
self.api_args = {
"x": np.random.random([4, 2, 7, 7]).astype(np.float32),
"seg_num": 2,
"shift_ratio": 0.2,
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 2, 7, 7]}
self.opt_shape = {"x": [2, 2, 7, 7]}
self.max_shape = {"x": [8, 2, 7, 7]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestTemporalShiftTRTPatternDifferentSegNum(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.temporal_shift
self.api_args = {
"x": np.random.random([4, 9, 7, 7]).astype(np.float32),
"seg_num": 4,
"shift_ratio": 0.2,
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [4, 9, 7, 7]}
self.opt_shape = {"x": [4, 9, 7, 7]}
self.max_shape = {"x": [8, 9, 7, 7]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestTemporalShiftTRTPatternDifferentShiftRatio(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.temporal_shift
self.api_args = {
"x": np.random.random([4, 9, 7, 7]).astype(np.float32),
"seg_num": 2,
"shift_ratio": 0.4,
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 9, 7, 7]}
self.opt_shape = {"x": [2, 9, 7, 7]}
self.max_shape = {"x": [8, 9, 7, 7]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestTemporalShiftTRTPatternDifferentDataFormat(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.temporal_shift
self.api_args = {
"x": np.random.random([4, 9, 7, 7]).astype(np.float32),
"seg_num": 2,
"shift_ratio": 0.2,
"name": None,
"data_format": "NHWC",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 9, 7, 7]}
self.opt_shape = {"x": [2, 9, 7, 7]}
self.max_shape = {"x": [8, 9, 7, 7]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestTemporalShiftTRTPatternMinMaxShape(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.temporal_shift
self.api_args = {
"x": np.random.random([4, 9, 7, 7]).astype(np.float32),
"seg_num": 2,
"shift_ratio": 0.2,
"data_format": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 9, 7, 7]}
self.opt_shape = {"x": [2, 9, 7, 7]}
self.max_shape = {"x": [10, 9, 7, 7]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
def wrapper_temporal_shift(x):
return paddle.nn.functional.temporal_shift(x=x, seg_num=2, shift_ratio=0.2)
class TestTemporalShiftTRTPatternError1(TensorRTBaseTest):
def setUp(self):
self.python_api = wrapper_temporal_shift
self.api_args = {
"x": np.random.random([4, 9, 7, 7]).astype(np.float32),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 9, 7, 7]}
self.opt_shape = {"x": [2, 9, 7, 7]}
self.max_shape = {"x": [10, 9, 7, 7]}
def test_trt_result(self):
self.check_marker(expected_result=False)
def affine_channel(x, scale_shape, bias_shape, layout):
scale = paddle.static.create_parameter(
shape=scale_shape, dtype='float32', name="scale"
)
bias = paddle.static.create_parameter(
shape=bias_shape, dtype='float32', name="bias"
)
return _C_ops.affine_channel(x, scale, bias, layout)
class TestAffineChannelTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = affine_channel
self.api_args = {
"x": np.random.random((2, 100, 3, 3)).astype("float32"),
"scale_shape": [100],
"bias_shape": [100],
"layout": "NCHW",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 100, 3, 3]}
self.opt_shape = {"x": [2, 100, 3, 3]}
self.max_shape = {"x": [3, 100, 3, 3]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestAffineChannelCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = affine_channel
self.api_args = {
"x": np.random.random((2, 3, 3, 100)).astype("float32"),
"scale_shape": [100],
"bias_shape": [100],
"layout": "NHWC",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 3, 100]}
self.opt_shape = {"x": [2, 3, 3, 100]}
self.max_shape = {"x": [3, 3, 3, 100]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
def anchor_generator(x, anchor_sizes, aspect_ratios, variances, stride, offset):
return _C_ops.anchor_generator(
x, anchor_sizes, aspect_ratios, variances, stride, offset
)
class TestAnchorGeneratorTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = anchor_generator
self.api_args = {
"x": np.random.random((2, 3, 3, 100)).astype("float32"),
"anchor_sizes": [64.0, 128.0, 256.0],
"aspect_ratios": [0.5, 1, 2],
"variances": [1.0, 1.0, 1.0, 1.0],
"stride": [16.0, 16.0],
"offset": 0.5,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 3, 100]}
self.opt_shape = {"x": [2, 3, 3, 100]}
self.max_shape = {"x": [3, 3, 3, 100]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestAnchorGeneratorCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = anchor_generator
self.api_args = {
"x": np.random.random((2, 3, 64, 64)).astype("float32"),
"anchor_sizes": [64.0, 128.0, 256.0],
"aspect_ratios": [0.4, 1.2, 3],
"variances": [0.5, 1.0, 0.5, 1.0],
"stride": [16.0, 32.0],
"offset": 0.8,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 3, 64, 64]}
self.opt_shape = {"x": [2, 3, 64, 64]}
self.max_shape = {"x": [3, 3, 64, 64]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
def shuffle_channel_wrapper(x, group=1):
return _C_ops.shuffle_channel(x, group)
class TestShuffleChannelTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = shuffle_channel_wrapper
self.api_args = {
"x": np.random.random((10, 16, 4, 4)).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [10, 16, 4, 4]}
self.opt_shape = {"x": [10, 16, 4, 4]}
self.max_shape = {"x": [10, 16, 4, 4]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
def full_batch_size_like_wrapper(x, dtype, value, batch_dim):
place = paddle.CPUPlace()
out_shape = [-1, 5, 1]
return _C_ops.full_batch_size_like(
x, out_shape, dtype, value, batch_dim, batch_dim, place
)
class TestFullBatchSizeLikeTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = full_batch_size_like_wrapper
self.api_args = {
"x": np.random.random((2, 3, 4)).astype("float32"),
"dtype": paddle.float32,
"value": 2.0,
"batch_dim": 0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [2, 3, 4]}
self.opt_shape = {"x": [3, 3, 4]}
self.max_shape = {"x": [4, 3, 4]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
if __name__ == '__main__':
unittest.main()
+486
View File
@@ -0,0 +1,486 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
def pool2d_api(
x,
ksize=[],
strides=[],
paddings=[],
ceil_mode=False,
exclusive=True,
data_format="NCHW",
pooling_type="max",
global_pooling=False,
adaptive=False,
padding_algorithm="EXPLICIT",
):
return paddle._C_ops.pool2d(
x,
ksize,
strides,
paddings,
ceil_mode,
exclusive,
data_format,
pooling_type,
global_pooling,
adaptive,
padding_algorithm,
)
class TestPoolingTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.AvgPool2D(kernel_size=2, stride=1)
self.api_args = {
"x": np.random.randn(1, 1, 2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 1, 2, 3]}
self.opt_shape = {"x": [1, 1, 2, 3]}
self.max_shape = {"x": [5, 1, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestPoolingTRTCase1Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool2d_api
self.api_args = {
"x": np.random.randn(1, 1, 2, 3).astype("float32"),
"ksize": [2, 3],
"strides": [1, 2],
"paddings": [0, 0],
"ceil_mode": False,
"exclusive": False,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": False,
"adaptive": False,
"padding_algorithm": "VALID",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 1, 2, 3]}
self.opt_shape = {"x": [1, 1, 2, 3]}
self.max_shape = {"x": [5, 1, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestPoolingTRTCase2Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool2d_api
self.api_args = {
"x": np.random.randn(1, 1, 2, 3).astype("float32"),
"ksize": [2, 3],
"strides": [1, 2],
"paddings": [0, 0],
"ceil_mode": True,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "max",
"global_pooling": False,
"adaptive": False,
"padding_algorithm": "SAME",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 1, 2, 3]}
self.opt_shape = {"x": [1, 1, 2, 3]}
self.max_shape = {"x": [5, 1, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestPoolingTRTCase3Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool2d_api
self.api_args = {
"x": np.random.randn(1, 1, 2, 3).astype("float32"),
"ksize": [2, 3],
"strides": [1, 2],
"paddings": [0, 0],
"ceil_mode": True,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "max",
"global_pooling": True,
"adaptive": False,
"padding_algorithm": "SAME",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 1, 2, 3]}
self.opt_shape = {"x": [1, 1, 2, 3]}
self.max_shape = {"x": [5, 1, 2, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestPoolingTRTCase4Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool2d_api
self.api_args = {
"x": np.random.randn(1, 1, 5, 5).astype("float32"),
"ksize": [3, 3],
"strides": [1, 1],
"paddings": [0, 0],
"ceil_mode": False,
"exclusive": False,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": True,
"adaptive": False,
"padding_algorithm": "SAME",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 1, 5, 5]}
self.opt_shape = {"x": [1, 1, 5, 5]}
self.max_shape = {"x": [5, 1, 5, 5]}
def test_trt_result(self):
self.check_trt_result()
class TestPoolingTRTCase5Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool2d_api
self.api_args = {
"x": np.random.randn(1, 16, 56, 56).astype("float32"),
"ksize": [2, 2],
"strides": [1, 1],
"paddings": [0, 0],
"ceil_mode": False,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": False,
"adaptive": True,
"padding_algorithm": "EXPLICIT",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 16, 56, 56]}
self.opt_shape = {"x": [1, 16, 56, 56]}
self.max_shape = {"x": [5, 16, 56, 56]}
def test_trt_result(self):
self.check_trt_result()
class TestPoolingTRTCase6Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool2d_api
self.api_args = {
"x": np.random.randn(1, 3, 5, 5).astype("float32"),
"ksize": [1, 1],
"strides": [1, 1],
"paddings": [0, 0],
"ceil_mode": False,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": False,
"adaptive": True,
"padding_algorithm": "EXPLICIT",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5]}
self.opt_shape = {"x": [1, 3, 5, 5]}
self.max_shape = {"x": [2, 3, 5, 5]}
def test_trt_result(self):
self.check_trt_result()
class TestPoolingTRTCase7Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool2d_api
self.api_args = {
"x": np.random.randn(1, 3, 32, 32).astype("float32"),
"ksize": [2, 3],
"strides": [1, 2],
"paddings": [0, 2],
"ceil_mode": True,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "max",
"global_pooling": False,
"adaptive": False,
"padding_algorithm": "EXPLICIT",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 32, 32]}
self.opt_shape = {"x": [1, 3, 32, 32]}
self.max_shape = {"x": [2, 3, 32, 32]}
def test_trt_result(self):
self.check_trt_result()
class TestPoolingTRTCase8Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool2d_api
self.api_args = {
"x": np.random.randn(1, 3, 32, 32).astype("float32"),
"ksize": [2, 3],
"strides": [1, 2],
"paddings": [0, 2],
"ceil_mode": False,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "max",
"global_pooling": False,
"adaptive": True,
"padding_algorithm": "EXPLICIT",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 32, 32]}
self.opt_shape = {"x": [1, 3, 32, 32]}
self.max_shape = {"x": [2, 3, 32, 32]}
def test_trt_result(self):
self.check_trt_result()
class TestPoolingTRTMarker(TensorRTBaseTest):
def setUp(self):
self.python_api = pool2d_api
self.api_args = {
"x": np.random.randn(1, 3, 5, 5).astype("float32"),
"ksize": [6, 6],
"strides": [2, 2],
"paddings": [0, 0],
"ceil_mode": False,
"exclusive": False,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": False,
"adaptive": False,
"padding_algorithm": "EXPLICIT",
}
self.program_config = {"feed_list": ["x"]}
self.target_marker_op = "pd_op.pool2d"
def test_trt_result(self):
self.check_marker(expected_result=False)
def pool3d_api(
x,
ksize=[],
strides=[],
paddings=[],
ceil_mode=False,
exclusive=True,
data_format="NCHW",
pooling_type="max",
global_pooling=False,
adaptive=False,
padding_algorithm="EXPLICIT",
):
return paddle._C_ops.pool3d(
x,
ksize,
strides,
paddings,
ceil_mode,
exclusive,
data_format,
pooling_type,
global_pooling,
adaptive,
padding_algorithm,
)
class TestPooling3dTRTCase1Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool3d_api
self.api_args = {
"x": np.random.randn(1, 3, 5, 5, 5).astype("float32"),
"ksize": [1, 1, 1],
"strides": [1, 1, 1],
"paddings": [0, 0, 0],
"ceil_mode": False,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": False,
"adaptive": False,
"padding_algorithm": "EXPLICIT",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5, 5]}
self.opt_shape = {"x": [1, 3, 5, 5, 5]}
self.max_shape = {"x": [2, 3, 5, 5, 5]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestPooling3dTRTCase2Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool3d_api
self.api_args = {
"x": np.ones([1, 3, 5, 5, 5]).astype("float32"),
"ksize": [1, 1, 1],
"strides": [1, 1, 1],
"paddings": [0, 0, 0],
"ceil_mode": False,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": False,
"adaptive": True,
"padding_algorithm": "EXPLICIT",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5, 5]}
self.opt_shape = {"x": [1, 3, 5, 5, 5]}
self.max_shape = {"x": [2, 3, 5, 5, 5]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestPooling3dTRTCase3Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool3d_api
self.api_args = {
"x": np.ones([1, 3, 5, 5, 5]).astype("float32"),
"ksize": [1, 1, 1],
"strides": [1, 1, 1],
"paddings": [0, 0, 0],
"ceil_mode": False,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": True,
"adaptive": False,
"padding_algorithm": "EXPLICIT",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5, 5]}
self.opt_shape = {"x": [1, 3, 5, 5, 5]}
self.max_shape = {"x": [2, 3, 5, 5, 5]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestPooling3dTRTCase4Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool3d_api
self.api_args = {
"x": np.random.randn(1, 3, 5, 5, 5).astype("float32"),
"ksize": [1, 1, 1],
"strides": [1, 1, 1],
"paddings": [0, 0, 0],
"ceil_mode": False,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": False,
"adaptive": False,
"padding_algorithm": "SAME",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5, 5]}
self.opt_shape = {"x": [1, 3, 5, 5, 5]}
self.max_shape = {"x": [2, 3, 5, 5, 5]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestPooling3dTRTCase5Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool3d_api
self.api_args = {
"x": np.random.randn(1, 3, 5, 5, 5).astype("float32"),
"ksize": [1, 1, 1],
"strides": [1, 1, 1],
"paddings": [0, 0, 0],
"ceil_mode": False,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "avg",
"global_pooling": False,
"adaptive": False,
"padding_algorithm": "VALID",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5, 5]}
self.opt_shape = {"x": [1, 3, 5, 5, 5]}
self.max_shape = {"x": [2, 3, 5, 5, 5]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
class TestPooling3dTRTCase6Pattern(TensorRTBaseTest):
def setUp(self):
self.python_api = pool3d_api
self.api_args = {
"x": np.ones([1, 3, 5, 5, 5]).astype("float32"),
"ksize": [1, 1, 1],
"strides": [1, 1, 1],
"paddings": [0, 0, 0],
"ceil_mode": False,
"exclusive": True,
"data_format": "NCHW",
"pooling_type": "max",
"global_pooling": True,
"adaptive": False,
"padding_algorithm": "EXPLICIT",
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 5, 5, 5]}
self.opt_shape = {"x": [1, 3, 5, 5, 5]}
self.max_shape = {"x": [2, 3, 5, 5, 5]}
def test_fp32_trt_result(self):
self.check_trt_result()
def test_fp16_trt_result(self):
self.check_trt_result(precision_mode="fp16")
if __name__ == '__main__':
unittest.main()
+333
View File
@@ -0,0 +1,333 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
class TestArgmaxCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argmax
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestArgmaxCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argmax
self.api_args = {
"x": np.random.randn(2, 3).astype("int64"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.target_marker_op = "pd_op.argmax"
def test_trt_result(self):
# test input's dtype
self.check_marker(expected_result=False)
class TestArgmaxCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argmax
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"axis": 0,
}
self.program_config = {"feed_list": ["x"]}
self.target_marker_op = "pd_op.argmax"
def test_trt_result(self):
# test axis
self.check_marker(expected_result=False)
class TestArgmaxCase4TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argmin
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"axis": np.random.randn(1).astype("int64"),
}
self.program_config = {"feed_list": ["x", "axis"]}
self.target_marker_op = "pd_op.argmax"
def test_trt_result(self):
# test axis Value
self.check_marker(expected_result=False)
class TestArgminCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argmin
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestArgminCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argmin
self.api_args = {
"x": np.random.randn(2, 3).astype("int64"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.target_marker_op = "pd_op.argmin"
def test_trt_result(self):
# test input's dtype
self.check_marker(expected_result=False)
class TestArgminCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argmin
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"axis": 0,
}
self.program_config = {"feed_list": ["x"]}
self.target_marker_op = "pd_op.argmin"
def test_trt_result(self):
# test axis
self.check_marker(expected_result=False)
class TestArgminCase4TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argmin
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"axis": np.random.randn(1).astype("int64"),
}
self.program_config = {"feed_list": ["x", "axis"]}
self.target_marker_op = "pd_op.argmin"
def test_trt_result(self):
# test axis Value
self.check_marker(expected_result=False)
class TestWhereTRTPatternCase1(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.where
self.api_args = {
"condition": np.random.choice([True, False], size=(2, 3)),
"x": np.random.randn(2, 3).astype("float32"),
"y": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["condition", "x", "y"]}
self.min_shape = {"condition": [1, 3], "x": [1, 3], "y": [1, 3]}
self.opt_shape = {"condition": [2, 3], "x": [2, 3], "y": [2, 3]}
self.max_shape = {"condition": [5, 3], "x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestArgsortCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argsort
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestArgsortCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argsort
self.api_args = {
"x": np.random.randn(2).astype("float32"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.opt_shape = {"x": [2]}
self.max_shape = {"x": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestArgsortCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argsort
self.api_args = {
"x": np.random.randn(2, 3).astype("int64"),
"axis": -1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestArgsortCase4TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.argsort
self.api_args = {
"x": np.random.randn(2, 4000).astype("float32"),
"axis": 1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 4000]}
self.opt_shape = {"x": [2, 4000]}
self.max_shape = {"x": [3, 4000]}
def test_trt_result(self):
self.check_trt_result()
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
class TestWhereTRTPatternCase2(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.where
self.api_args = {
"condition": np.random.choice([True, False], size=(2, 3)),
"x": np.random.randn(2, 3).astype("int64"),
"y": np.random.randn(2, 3).astype("int64"),
}
self.program_config = {"feed_list": ["condition", "x", "y"]}
self.min_shape = {"condition": [1, 3], "x": [1, 3], "y": [1, 3]}
self.opt_shape = {"condition": [2, 3], "x": [2, 3], "y": [2, 3]}
self.max_shape = {"condition": [5, 3], "x": [5, 3], "y": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestTopkCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.topk
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"k": 1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestTopkCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.topk
self.api_args = {
"x": np.random.randn(2).astype("int64"),
"k": 1,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.opt_shape = {"x": [2]}
self.max_shape = {"x": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestTopkCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.topk
self.api_args = {
"x": np.random.randn(2).astype("int64"),
"k": 1,
"axis": 0,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.opt_shape = {"x": [2]}
self.max_shape = {"x": [5]}
def test_trt_result(self):
self.check_trt_result()
class TestIndexSelectCase1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.index_select
self.api_args = {
"x": np.random.randn(2, 3, 3).astype("float32"),
"index": np.array([0, 2], dtype="int64"),
"axis": 1,
}
self.program_config = {"feed_list": ["x", "index"]}
self.min_shape = {"x": [1, 3, 3], "index": [1]}
self.opt_shape = {"x": [2, 3, 3], "index": [2]}
self.max_shape = {"x": [5, 3, 3], "index": [5]}
def test_trt_result_fp16(self):
self.check_trt_result(precision_mode="fp16")
def test_trt_result_fp32(self):
self.check_trt_result()
class TestIndexSelectCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.index_select
self.api_args = {
"x": np.random.randn(2, 3, 3).astype("int64"),
"index": np.array([0, 1], dtype="int64"),
"axis": 0,
}
self.program_config = {"feed_list": ["x", "index"]}
self.min_shape = {"x": [1, 3, 3], "index": [1]}
self.opt_shape = {"x": [2, 3, 3], "index": [2]}
self.max_shape = {"x": [5, 3, 3], "index": [5]}
def test_trt_result(self):
self.check_trt_result()
if __name__ == '__main__':
unittest.main()
+58
View File
@@ -0,0 +1,58 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle
class TestMean0TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.mean
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"axis": [1],
"keepdim": False,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.opt_shape = {"x": [2, 3]}
self.max_shape = {"x": [5, 3]}
def test_trt_result(self):
self.check_trt_result()
class TestMean1TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.mean
self.api_args = {
"x": np.random.randn(2, 3, 2).astype("float32"),
"axis": [1, 1],
"keepdim": True,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 2]}
self.opt_shape = {"x": [2, 3, 2]}
self.max_shape = {"x": [5, 3, 2]}
def test_trt_result(self):
self.check_trt_result()
if __name__ == "__main__":
unittest.main()
+100
View File
@@ -0,0 +1,100 @@
# 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 unittest
import numpy as np
from tensorrt_test_base import TensorRTBaseTest
import paddle.nn.functional as F
class TestGridSampleTRTPatternBase(TensorRTBaseTest):
def setUp(self):
self.python_api = F.grid_sample
self.api_args = {
"x": np.array(
[[[[-0.6, 0.8, -0.5], [-0.5, 0.2, 1.2], [1.4, 0.3, -0.2]]]]
).astype("float32"),
"grid": np.array(
[
[
[[0.2, 0.3], [-0.4, -0.3], [-0.9, 0.3], [-0.9, -0.6]],
[[0.4, 0.1], [0.9, -0.8], [0.4, 0.5], [0.5, -0.2]],
[[0.1, -0.8], [-0.3, -1.0], [0.7, 0.4], [0.2, 0.8]],
]
],
dtype='float32',
),
}
self.program_config = {"feed_list": ["x", "grid"]}
self.min_shape = {"x": [1, 1, 3, 3], "grid": [1, 3, 4, 2]}
self.opt_shape = {"x": [1, 1, 3, 3], "grid": [1, 3, 4, 2]}
self.max_shape = {"x": [5, 1, 3, 3], "grid": [5, 3, 4, 2]}
class TestGridSampleTRTPatternCase1(TestGridSampleTRTPatternBase):
"""default:mode='bilinear', padding_mode='zeros', align_corners=True"""
def test_trt_result(self):
self.check_trt_result()
class TestGridSampleTRTPatternCase2(TestGridSampleTRTPatternBase):
"""default:mode='nearest', padding_mode='reflection', align_corners=False"""
def setUp(self):
super().setUp()
self.api_args.update(
{
"mode": "nearest",
"padding_mode": "reflection",
"align_corner": False,
}
)
def test_trt_result(self):
self.check_trt_result()
class TestGridSampleTRTPatternCase3(TestGridSampleTRTPatternBase):
"""default:mode='nearest', padding_mode='border', align_corners=True"""
def setUp(self):
super().setUp()
self.api_args.update({"mode": "nearest", "padding_mode": "border"})
def test_trt_result(self):
self.check_trt_result()
class TestGridSampleTRTPatternCase4(TestGridSampleTRTPatternBase):
"""default:mode='bilinear', padding_mode='border', align_corners=False"""
def setUp(self):
super().setUp()
self.api_args.update(
{
"mode": "bilinear",
"padding_mode": "border",
"align_corner": False,
},
)
def test_trt_result(self):
self.check_trt_result()
if __name__ == '__main__':
unittest.main()
+364
View File
@@ -0,0 +1,364 @@
# 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 tempfile
import unittest
import numpy as np
import paddle
import paddle.inference as paddle_infer
import paddle.nn.functional as F
from paddle import Tensor, nn
from paddle.static import InputSpec
from paddle.tensorrt.export import (
Input,
TensorRTConfig,
_convert_,
)
from paddle.tensorrt.util import (
predict_program,
)
class LeNetMultiInput(nn.Layer):
"""LeNet model modified to accept two inputs."""
def __init__(self, num_classes: int = 10) -> None:
super().__init__()
self.num_classes = num_classes
# Convolution layers for the first input
self.features1 = nn.Sequential(
nn.Conv2D(1, 6, 3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2D(2, 2),
nn.Conv2D(6, 16, 5, stride=1, padding=0),
nn.ReLU(),
nn.MaxPool2D(2, 2),
)
# Convolution layers for the second input
self.features2 = nn.Sequential(
nn.Conv2D(1, 6, 3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2D(2, 2),
nn.Conv2D(6, 16, 5, stride=1, padding=0),
nn.ReLU(),
nn.MaxPool2D(2, 2),
)
# Fully connected layers
if num_classes > 0:
self.fc = nn.Sequential(
nn.Linear(400 * 2, 120), # Adjusted for two inputs
nn.Linear(120, 84),
nn.Linear(84, num_classes),
)
def forward(self, input1: Tensor, input2: Tensor) -> Tensor:
# Apply feature extraction on both inputs
x1 = self.features1(input1)
x2 = self.features2(input2)
# Flatten both feature maps
x1 = paddle.flatten(x1, 1)
x2 = paddle.flatten(x2, 1)
# Concatenate the features from both inputs
x = paddle.concat([x1, x2], axis=1)
if self.num_classes > 0:
x = self.fc(x)
return x
class CumsumModel(nn.Layer):
def __init__(self, input_dim):
super().__init__()
self.linear = nn.Linear(input_dim, input_dim)
def forward(self, x):
linear_out = self.linear(x)
relu_out = F.relu(linear_out)
axis = paddle.full([1], 2, dtype='int64')
out = paddle.cumsum(relu_out, axis=axis)
return out
class TestConvert(unittest.TestCase):
def setUp(self):
paddle.seed(2024)
self.temp_dir = tempfile.TemporaryDirectory()
self.save_path = os.path.join(self.temp_dir.name, 'tensor_axis_cumsum')
self.place = (
paddle.CUDAPlace(0)
if paddle.is_compiled_with_cuda()
else paddle.CPUPlace()
)
def test_paddle_to_tensorrt_conversion_cumsum(self):
paddle.enable_static()
np_x = np.random.randn(9, 10, 11).astype('float32')
with paddle.pir_utils.IrGuard():
main_prog = paddle.static.Program()
startup_prog = paddle.static.Program()
with paddle.static.program_guard(main_prog, startup_prog):
x = paddle.static.data(
shape=np_x.shape, name='x', dtype=np_x.dtype
)
model = CumsumModel(input_dim=np_x.shape[-1])
out = model(x)
loss = paddle.mean(out)
sgd = paddle.optimizer.SGD(learning_rate=0.0)
sgd.minimize(paddle.mean(out))
exe = paddle.static.Executor(self.place)
exe.run(startup_prog)
static_out = exe.run(feed={'x': np_x}, fetch_list=[out])
# run infer
paddle.static.save_inference_model(
self.save_path, [x], [out], exe
)
config = paddle_infer.Config(
self.save_path + '.json', self.save_path + '.pdiparams'
)
config.enable_new_ir()
config.enable_new_executor()
config.use_optimized_model(True)
# Set input
input_config = Input(
min_input_shape=(9, 10, 11),
optim_input_shape=(9, 10, 11),
max_input_shape=(9, 10, 11),
)
# Create a TensorRTConfig with inputs as a required field.
trt_config = TensorRTConfig(inputs=[input_config])
trt_save_path = os.path.join(self.temp_dir.name, 'trt')
trt_config.save_model_dir = trt_save_path
trt_config.refit_params_path = self.save_path + '.pdiparams'
model_dir = self.save_path
# Obtain tensorrt_engine_op by passing the model path and trt_config.(converted_program)
program_with_trt = paddle.tensorrt.convert(model_dir, trt_config)
# Create a config for inference.
config = paddle_infer.Config(
trt_config.save_model_dir + '.json',
trt_config.save_model_dir + '.pdiparams',
)
if paddle.is_compiled_with_cuda():
config.enable_use_gpu(100, 0)
else:
config.disable_gpu()
predictor = paddle_infer.create_predictor(config)
paddle.disable_static()
for i, input_instance in enumerate(trt_config.inputs):
min_data, _, max_data = input_instance.generate_input_data()
model_inputs = paddle.to_tensor(min_data)
output_converted = predictor.run([model_inputs])
class TestConvert_(unittest.TestCase):
def test_run(self):
with paddle.pir_utils.IrGuard():
input_config = Input(
min_input_shape=(9, 10, 11),
optim_input_shape=(9, 10, 11),
max_input_shape=(10, 10, 11),
)
trt_config = TensorRTConfig(inputs=[input_config])
for i, input_instance in enumerate(trt_config.inputs):
min_data, _, max_data = input_instance.generate_input_data()
paddle.disable_static()
x = paddle.to_tensor(min_data)
net = CumsumModel(input_dim=min_data.shape[-1])
out = net(x)
input_spec = [
InputSpec(shape=[None, 10, 11], dtype='float32', name='x')
]
program_with_trt, scope = _convert_(
net,
input_spec=input_spec,
config=trt_config,
)
output_var = program_with_trt.list_vars()[-1]
output_converted = predict_program(
program_with_trt,
{"x": min_data},
[output_var],
scope=scope,
)
output_expected = out.numpy()
output_converted_np = output_converted[0]
# Check that the results are close to each other within a tolerance of 1e-2
np.testing.assert_allclose(
output_expected,
output_converted_np,
rtol=1e-2,
atol=1e-2,
err_msg="Outputs are not within the 1e-2 tolerance",
)
class TestConvertMultipleInputs(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.save_path = os.path.join(
self.temp_dir.name, 'tensor_axis_cumsum_multiple'
)
self.place = (
paddle.CUDAPlace(0)
if paddle.is_compiled_with_cuda()
else paddle.CPUPlace()
)
def test_run(self):
with paddle.pir_utils.IrGuard():
input_config = Input(
min_input_shape=(1, 1, 28, 28),
optim_input_shape=(1, 1, 28, 28),
max_input_shape=(1, 1, 28, 28),
)
input_config2 = Input(
min_input_shape=(1, 1, 28, 28),
optim_input_shape=(1, 1, 28, 28),
max_input_shape=(1, 1, 28, 28),
)
trt_config = TensorRTConfig(inputs=[input_config, input_config2])
trt_config.save_model_dir = os.path.join(self.temp_dir.name, 'trt')
min_data_list = []
max_data_list = []
for i, input_instance in enumerate(trt_config.inputs):
min_data, _, max_data = input_instance.generate_input_data()
min_data_list.append(min_data)
max_data_list.append(max_data)
paddle.disable_static()
x = [paddle.to_tensor(md) for md in min_data_list]
net = LeNetMultiInput()
out = net(*x)
input_spec = [
InputSpec(
shape=min_data_list[0].shape, dtype='float32', name='input1'
),
InputSpec(
shape=min_data_list[1].shape, dtype='float32', name='input2'
),
]
program_with_trt, scope = _convert_(
net,
input_spec=input_spec,
config=trt_config,
full_graph=True,
)
config = paddle_infer.Config(
trt_config.save_model_dir + '.json',
trt_config.save_model_dir + '.pdiparams',
)
if paddle.is_compiled_with_cuda():
config.enable_use_gpu(100, 0)
else:
config.disable_gpu()
predictor = paddle_infer.create_predictor(config)
output_converted = predictor.run(x)
output_converted_np = output_converted[0]
output_expected = out.numpy()
np.testing.assert_allclose(
output_expected,
output_converted_np,
rtol=1e-2,
atol=1e-2,
err_msg="Outputs are not within the 1e-2 tolerance",
)
class TestConvertPredictor(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.save_path = os.path.join(self.temp_dir.name, 'tensor_axis_cumsum')
self.place = (
paddle.CUDAPlace(0)
if paddle.is_compiled_with_cuda()
else paddle.CPUPlace()
)
def test_run(self):
input_config = Input(
min_input_shape=(9, 10, 11),
optim_input_shape=(9, 10, 11),
max_input_shape=(10, 10, 11),
)
trt_config = TensorRTConfig(inputs=[input_config])
trt_config.save_model_dir = os.path.join(self.temp_dir.name, 'trt')
min_data, _, max_data = input_config.generate_input_data()
net = CumsumModel(input_dim=min_data.shape[-1])
x = paddle.to_tensor(min_data)
out = net(x).numpy()
input_spec = [
InputSpec(shape=[None, 10, 11], dtype='float32', name='x')
]
program_with_trt, scope = _convert_(
net,
input_spec=input_spec,
config=trt_config,
)
config = paddle_infer.Config(
trt_config.save_model_dir + '.json',
trt_config.save_model_dir + '.pdiparams',
)
if paddle.is_compiled_with_cuda():
config.enable_use_gpu(100, 0)
else:
config.disable_gpu()
predictor = paddle_infer.create_predictor(config)
output_converted = predictor.run([x])
output_converted_np = output_converted[0]
np.testing.assert_allclose(
out,
output_converted_np,
rtol=1e-2,
atol=1e-2,
err_msg="Outputs are not within the 1e-2 tolerance",
)
if __name__ == "__main__":
unittest.main()