Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TRT UT failures #47488

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def sample_program_configs(self):
def generate_input(shape):
return np.random.random(shape).astype(np.float32)

for shape in [[4], [4, 32], [2, 64, 32], [1, 8, 16, 32]]:
for shape in [[4], [4, 32], [2, 32, 16], [1, 8, 16, 32]]:
for op_type in [
"elementwise_add",
"elementwise_mul",
Expand Down Expand Up @@ -464,8 +464,8 @@ def generate_dynamic_shape(attrs):
"input_data2": [128, 128, 256],
}
self.dynamic_shape.opt_input_shape = {
"input_data1": [2, 64, 64],
"input_data2": [2, 64, 64],
"input_data1": [2, 32, 16],
"input_data2": [2, 32, 16],
}
elif self.dims == 4:
self.dynamic_shape.min_input_shape = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape):
self.trt_param.precision = paddle_infer.PrecisionType.Half
yield self.create_inference_config(), generate_trt_nodes_num(
attrs, False
), 1e-3
), (1e-3, 1e-3)

# for dynamic_shape
generate_dynamic_shape(attrs)
Expand All @@ -140,7 +140,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape):
self.trt_param.precision = paddle_infer.PrecisionType.Half
yield self.create_inference_config(), generate_trt_nodes_num(
attrs, True
), 1e-3
), (1e-3, 1e-3)

def add_skip_trt_case(self):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Any, Dict, List
import unittest
import itertools
import copy


class TrtConvertPool2dTest(TrtLayerAutoScanTest):
Expand Down Expand Up @@ -188,6 +189,39 @@ def teller(program_config, predictor_config):
"The results of some cases are Nan, but the results of TensorRT and GPU are the same.",
)

def assert_tensors_near(
self,
atol: float,
rtol: float,
tensor: Dict[str, np.array],
baseline: Dict[str, np.array],
):
for key, arr in tensor.items():
self.assertEqual(
baseline[key].shape,
arr.shape,
'The output shapes are not equal, the baseline shape is '
+ str(baseline[key].shape)
+ ', but got '
+ str(arr.shape),
)

# The result of Pool2d may have some elements that is the least value (-65504 for FP16),
# but for FP32 and FP16 precision, their least value are different.
# We set a threshold that is the least value of FP16,
# and make the values less than the threshold to be the threshold.
def align_less_threshold(arr, threshold):
return np.clip(arr, threshold, None)

fp16_min = np.finfo(np.float16).min
baseline_threshold = align_less_threshold(
copy.deepcopy(baseline[key]), fp16_min
)
arr_threshold = align_less_threshold(copy.deepcopy(arr), fp16_min)
np.testing.assert_allclose(
baseline_threshold, arr_threshold, rtol=rtol, atol=atol
)

def test(self):
self.add_skip_trt_case()
self.run_test()
Expand Down