Skip to content

Commit d6dc542

Browse files
【Paddle-TensorRT】fix pd_op.any (#72192)
* support fp32 * fix pd_op.fused_conv2d * simplified code * fix pd_op.any * fix pd_op.fused_conv2d_add_act * fix --------- Co-authored-by: YuanRisheng <[email protected]>
1 parent aeb4df6 commit d6dc542

File tree

4 files changed

+55
-52
lines changed

4 files changed

+55
-52
lines changed

python/paddle/tensorrt/converter_utils.py

+30-47
Original file line numberDiff line numberDiff line change
@@ -669,38 +669,16 @@ def convert_conv2d(network, paddle_op, inputs):
669669
if paddle_op.name() == "pd_op.fused_conv2d_add_act":
670670
constant_manager = TensorRTConstantManager()
671671
bias_source_op = paddle_op.operands()[2].source().get_defining_op()
672-
673-
def get_bias_weights(current_op):
674-
if current_op.name() == "builtin.parameter":
675-
bias_name = current_op.attrs()["parameter_name"]
676-
elif current_op.name() == "builtin.constant":
677-
bias_name = current_op.attrs()["value"]
678-
else:
679-
raise ValueError(
680-
f"Unsupported bias source operation: {current_op.name()}"
681-
)
682-
683-
bias_np = constant_manager.get_constant_value(bias_name)
684-
return trt.Weights(bias_np)
685-
686-
if bias_source_op.name() in ["builtin.parameter", "builtin.constant"]:
687-
bias_weights = get_bias_weights(bias_source_op)
672+
if bias_source_op.name() == "builtin.parameter":
673+
bias_name = bias_source_op.attrs()['parameter_name']
674+
elif bias_source_op.name() == "builtin.constant":
675+
bias_np = bias_source_op.attrs()['value']
688676
else:
689-
while bias_source_op.name() == "pd_op.reshape":
690-
bias_source_op = (
691-
bias_source_op.operands()[0].source().get_defining_op()
692-
)
693-
if bias_source_op.name() in [
694-
"builtin.parameter",
695-
"builtin.constant",
696-
]:
697-
bias_weights = get_bias_weights(bias_source_op)
698-
break
699-
else:
700-
raise ValueError(
701-
f"Unsupported bias source operation: {bias_source_op.name()}"
702-
)
703-
677+
raise ValueError(
678+
f"Unsupported bias source op: {bias_source_op.name()}"
679+
)
680+
bias_np = constant_manager.get_constant_value(bias_name)
681+
bias_weights = trt.Weights(bias_np)
704682
layer = network.add_convolution_nd(
705683
input=input_tensor,
706684
num_output_maps=n_output,
@@ -897,27 +875,32 @@ def add_cast_reduce_layer(network, paddle_op, inputs, op_type):
897875

898876
axis = paddle_op.attrs().get("axis")
899877
input_shape = paddle_op.operands()[0].source().shape
900-
keepdim = paddle_op.attrs()["keepdim"]
901-
if network.has_implicit_batch_dimension:
902-
assert (
903-
axis != 0
904-
), "can't reduce on axis == 0 when network has implicit batch dimension"
905-
output_shape = []
878+
input_dims = len(input_shape)
879+
keepdim = paddle_op.attrs().get("keepdim")
880+
906881
if len(axis) == 0:
907-
axis = list(range(len(input_shape)))
908-
for i in range(len(axis)):
909-
if axis[i] < 0:
910-
axis[i] = len(input_shape) + axis[i]
911-
layer = network.add_reduce(
882+
axes = 0
883+
for i in range(input_dims):
884+
axes |= 1 << i
885+
else:
886+
for i in range(len(axis)):
887+
if axis[i] < 0:
888+
axis[i] += input_dims
889+
890+
axes = get_axes_for_reduce_op(axis)
891+
892+
reduce_layer = network.add_reduce(
912893
cast_layer.get_output(0),
913894
op_type,
914-
axes=get_axes_for_reduce_op(axis),
895+
axes=axes,
915896
keep_dims=keepdim,
916897
)
917-
set_layer_name(layer, paddle_op)
918-
layer.set_output_type(0, trt.bool)
919-
layer.get_output(0).dtype = cast_layer.get_output(0).dtype
920-
return layer.get_output(0)
898+
set_layer_name(reduce_layer, paddle_op)
899+
bool_layer = network.add_identity(reduce_layer.get_output(0))
900+
set_layer_name(bool_layer, paddle_op)
901+
bool_layer.set_output_type(0, trt.bool)
902+
bool_layer.get_output(0).dtype = trt.bool
903+
return bool_layer.get_output(0)
921904

922905

923906
def fix_negative_indices(network, input_shape, indices, name=None):

python/paddle/tensorrt/impls/activation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def gelu_converter(network, paddle_op, inputs):
150150
)
151151
constant_layer_sqrt = add_constant_layer(
152152
network,
153-
[0.7978845608028654],
153+
[0.79788456080286535587989211986876],
154154
const_shape,
155155
np.float32,
156156
name=[paddle_op.name(), "constant_layer_sqrt"],

test/tensorrt/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ if(NOT WIN32 AND TENSORRT_FOUND)
1616
set_tests_properties(test_converter_norm PROPERTIES TIMEOUT "300")
1717
set_tests_properties(test_converter_ops PROPERTIES TIMEOUT "600")
1818
set_tests_properties(test_converter_stat PROPERTIES TIMEOUT "300")
19-
set_tests_properties(test_converter_math PROPERTIES TIMEOUT "600")
20-
set_tests_properties(test_converter_activation PROPERTIES TIMEOUT "600")
19+
set_tests_properties(test_converter_math PROPERTIES TIMEOUT "900")
20+
set_tests_properties(test_converter_activation PROPERTIES TIMEOUT "900")
2121
set_tests_properties(test_converter_others PROPERTIES TIMEOUT "300")
2222
set_tests_properties(test_converter_manipulation PROPERTIES TIMEOUT "600")
2323
set_tests_properties(test_converter_creation PROPERTIES TIMEOUT "300")

test/tensorrt/test_converter_math.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def setUp(self):
260260
self.python_api = paddle.any
261261
self.api_args = {
262262
"x": np.random.randn(2, 3, 2).astype("bool"),
263-
"axis": [1, 1],
263+
"axis": [1],
264264
"keepdim": True,
265265
}
266266
self.program_config = {"feed_list": ["x"]}
@@ -277,7 +277,24 @@ def setUp(self):
277277
self.python_api = paddle.any
278278
self.api_args = {
279279
"x": np.random.randn(2, 3, 2).astype("bool"),
280-
"axis": [1, 1],
280+
"axis": [1],
281+
"keepdim": False,
282+
}
283+
self.program_config = {"feed_list": ["x"]}
284+
self.min_shape = {"x": [1, 3, 2]}
285+
self.opt_shape = {"x": [2, 3, 2]}
286+
self.max_shape = {"x": [5, 3, 2]}
287+
288+
def test_trt_result(self):
289+
self.check_trt_result()
290+
291+
292+
class TestAny2TRTPattern(TensorRTBaseTest):
293+
def setUp(self):
294+
self.python_api = paddle.any
295+
self.api_args = {
296+
"x": np.random.randn(2, 3, 2).astype("bool"),
297+
"axis": [-1],
281298
"keepdim": False,
282299
}
283300
self.program_config = {"feed_list": ["x"]}
@@ -288,6 +305,9 @@ def setUp(self):
288305
def test_trt_result(self):
289306
self.check_trt_result()
290307

308+
def test_trt_result_fp16(self):
309+
self.check_trt_result(precision_mode="fp16")
310+
291311

292312
class TestAllTRTPattern(TensorRTBaseTest):
293313
def setUp(self):

0 commit comments

Comments
 (0)