From 96e7d903f13a13761f3c7626a611bd84fbfdb377 Mon Sep 17 00:00:00 2001 From: Wilber Date: Tue, 31 Aug 2021 16:07:02 +0800 Subject: [PATCH] update infer trt ut. (#35261) --- .../unittests/ir/inference/CMakeLists.txt | 15 ++++++- .../unittests/ir/inference/auto_scan_test.py | 33 ++++++++++++--- .../unittests/ir/inference/program_config.py | 18 ++++---- .../ir/inference/test_trt_convert_conv2d.py | 4 +- .../ir/inference/trt_layer_auto_scan_test.py | 41 ++++++++++++++++--- 5 files changed, 89 insertions(+), 22 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt b/python/paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt index 7958b8be22..f73327f824 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt +++ b/python/paddle/fluid/tests/unittests/ir/inference/CMakeLists.txt @@ -3,15 +3,29 @@ string(REPLACE ".py" "" TEST_INFERENCE_IR_PASSES "${TEST_INFERENCE_IR_PASSES}") file(GLOB TEST_TRT_IR_PASSES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_trt_*.py") string(REPLACE ".py" "" TEST_TRT_IR_PASSES "${TEST_TRT_IR_PASSES}") + +file(GLOB TEST_TRT_CONVERTER RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_trt_convert_*.py") +string(REPLACE ".py" "" TEST_TRT_CONVERTER "${TEST_TRT_CONVERTER}") + foreach(TEST_INFERENCE_IR_PASS ${TEST_TRT_IR_PASSES}) list(REMOVE_ITEM TEST_INFERENCE_IR_PASSES ${TEST_INFERENCE_IR_PASS}) endforeach() if(WITH_GPU AND TENSORRT_FOUND) list(REMOVE_ITEM TEST_TRT_IR_PASSES test_trt_multiclass_nms_op) + + foreach(TRT_CONVERT ${TEST_TRT_CONVERTER}) + list(REMOVE_ITEM TEST_TRT_IR_PASSES ${TRT_CONVERT}) + endforeach() + foreach(target ${TEST_TRT_IR_PASSES}) py_test_modules(${target} MODULES ${target}) endforeach() + + foreach(target ${TEST_TRT_CONVERTER}) + py_test_modules(${target} MODULES ${target}) + set_tests_properties(${target} PROPERTIES TIMEOUT 100) + endforeach() endif() file(GLOB TEST_MKLDNN_IR_PASSES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "test_mkldnn_*.py") @@ -38,7 +52,6 @@ set_tests_properties(test_trt_dynamic_shape PROPERTIES TIMEOUT 120) set_tests_properties(test_trt_pool_op PROPERTIES ENVIRONMENT FLAGS_fraction_of_gpu_memory_to_use=0.1 TIMEOUT 45) set_tests_properties(test_trt_reduce_mean_op PROPERTIES TIMEOUT 60) set_tests_properties(test_trt_tile_op PROPERTIES TIMEOUT 60) -set_tests_properties(test_trt_convert_conv2d PROPERTIES TIMEOUT 100) set_tests_properties(test_trt_fc_fuse_quant_dequant_pass PROPERTIES TIMEOUT 100) set_tests_properties(test_trt_conv_quant_dequant_pass PROPERTIES TIMEOUT 100) set_tests_properties(test_trt_matmul_quant_dequant PROPERTIES TIMEOUT 100) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/auto_scan_test.py b/python/paddle/fluid/tests/unittests/ir/inference/auto_scan_test.py index 3a899a2e5e..59729e5637 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/auto_scan_test.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/auto_scan_test.py @@ -16,6 +16,7 @@ import numpy as np import unittest import abc import os +import logging import paddle import paddle.fluid as fluid from paddle.fluid.initializer import NumpyArrayInitializer @@ -25,6 +26,8 @@ import paddle.inference as paddle_infer from typing import Optional, List, Callable, Dict, Any, Set from program_config import TensorConfig, OpConfig, ProgramConfig, create_fake_model, create_quant_model +logging.basicConfig(level=logging.INFO, format="%(message)s") + class AutoScanTest(unittest.TestCase): def __init__(self, methodName='runTest'): @@ -53,7 +56,9 @@ class AutoScanTest(unittest.TestCase): for name, _ in prog_config.inputs.items(): input_tensor = predictor.get_input_handle(name) - input_tensor.copy_from_cpu(feed_data[name]) + input_tensor.copy_from_cpu(feed_data[name]['shape']) + if feed_data[name]['lod'] is not None: + input_tensor.set_lod(feed_data[name]['lod']) predictor.run() result = {} for out_name, o_name in zip(prog_config.outputs, @@ -97,22 +102,38 @@ class AutoScanTest(unittest.TestCase): trt_engine_num: int, paddle_op_num: int, threshold=1e-5, - quant=False): + quant=False, + error_msg=None): for prog_config in self.sample_program_configs(): model, params = create_fake_model(prog_config) if quant: model, params = create_quant_model(model, params) for batch_size in self.batch_size_set: feed_data = {} + log_str = ' -- Input tensor info: ' for name, tensor_config in prog_config.inputs.items(): tensor_shape = tensor_config.shape.copy() tensor_shape[0] = batch_size - feed_data[name] = np.random.random(tensor_shape).astype( - tensor_config.dtype) + feed_data[name] = { + 'shape': np.random.random(tensor_shape).astype( + tensor_config.dtype), + 'lod': tensor_config.lod + } + log_str += str({ + name: { + 'shape': tensor_shape, + 'lod': tensor_config.lod + } + }) + logging.info(log_str) results: List[Dict[str, Tensor]] = [] for pred_config in self.sample_predictor_configs(): results.append( self.run_test_config(model, params, prog_config, pred_config, feed_data)) - self.assert_tensors_near(threshold=threshold, tensors=results) - self.assert_op_size(trt_engine_num, paddle_op_num) + try: + self.assert_tensors_near( + threshold=threshold, tensors=results) + self.assert_op_size(trt_engine_num, paddle_op_num) + except: + logging.info('ERROR OCCURED: ' + error_msg) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/program_config.py b/python/paddle/fluid/tests/unittests/ir/inference/program_config.py index e570796c36..1343e96736 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/program_config.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/program_config.py @@ -37,7 +37,8 @@ class TensorConfig: def __init__(self, shape: [List[int]], dtype: [str]="float32", - data: Optional[np.array]=None): + data: Optional[np.array]=None, + lod: [List[List[int]]]=None): ''' shape: The shape of the tensor. dtype: The data type of the tensor. @@ -46,6 +47,7 @@ class TensorConfig: self.shape = shape self.dtype = dtype self.data = data + self.lod = lod class OpConfig: @@ -186,13 +188,6 @@ def create_quant_model(model, params_filename=params) graph = IrGraph(core.Graph(inference_program.desc), for_test=True) - transform_pass = QuantizationTransformPass( - scope=scope, - place=place, - activation_quantize_type=activation_quantize_type, - weight_quantize_type=weight_quantize_type) - transform_pass.apply(graph) - out_scale_op_list = [ "conv2d", "depthwise_conv2d", @@ -299,6 +294,13 @@ def create_quant_model(model, var_names.append(var_name) return var_names + transform_pass = QuantizationTransformPass( + scope=scope, + place=place, + activation_quantize_type=activation_quantize_type, + weight_quantize_type=weight_quantize_type) + transform_pass.apply(graph) + op_nodes = graph.all_op_nodes() for op_node in op_nodes: if op_node.name() in out_scale_op_list: diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d.py index d3f1eca2bf..109eef2038 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_conv2d.py @@ -61,12 +61,12 @@ class TrtConvertConv2dTest(TrtLayerAutoScanTest): self.program_outputs = ["relu_output_data"] def test_check_fp32_output(self): - self.trt_param.precision == paddle_infer.PrecisionType.Float32 + self.trt_param.precision = paddle_infer.PrecisionType.Float32 # the fused tensorrt engine num is 1, and paddle op num is 2(feed and fetch). self.run_test(trt_engine_num=1, paddle_op_num=2, threshold=1e-5) def test_check_fp16_output(self): - self.trt_param.precision == paddle_infer.PrecisionType.Half + self.trt_param.precision = paddle_infer.PrecisionType.Half self.run_test(trt_engine_num=1, paddle_op_num=2, threshold=1e-2) def test_dynamic_shape_fp32_check_output(self): diff --git a/python/paddle/fluid/tests/unittests/ir/inference/trt_layer_auto_scan_test.py b/python/paddle/fluid/tests/unittests/ir/inference/trt_layer_auto_scan_test.py index 90e69a9fce..7150067718 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/trt_layer_auto_scan_test.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/trt_layer_auto_scan_test.py @@ -27,8 +27,7 @@ from typing import * from program_config import TensorConfig, OpConfig, ProgramConfig from auto_scan_test import AutoScanTest -logging.basicConfig( - level=logging.INFO, format="%(asctime)s - %(filename)s - %(message)s") +logging.basicConfig(level=logging.INFO, format="%(message)s") class TrtLayerAutoScanTest(AutoScanTest): @@ -85,11 +84,19 @@ class TrtLayerAutoScanTest(AutoScanTest): op_attr_list = [] index = 0 ops = [] - for op_config in self.ops_config: + log_str = 'TEST_CASE: ' + for i in range(len(self.ops_config)): + op_config = self.ops_config[i] op_attr = dict( zip( list(op_config["op_attrs"].keys()), attrs_sample[ index:index + len(op_config["op_attrs"])])) + + if i != len(self.ops_config) - 1: + log_str += op_config['op_type'] + str(op_attr) + ' + ' + else: + log_str += op_config['op_type'] + str(op_attr) + op_attr_list.append(op_attr) index = index + len(op_config["op_attrs"]) ops.append( @@ -99,6 +106,7 @@ class TrtLayerAutoScanTest(AutoScanTest): outputs=op_config["op_outputs"], attrs=op_attr)) + logging.info(log_str) self.update_program_input_and_weight_with_attr(op_attr_list) # if no weight need to save, we create a place_holder to help seriazlie params. if not self.program_weights: @@ -117,6 +125,7 @@ class TrtLayerAutoScanTest(AutoScanTest): self, use_trt=True, precision_mode=paddle_infer.PrecisionType.Float32): config = paddle_infer.Config() + config.disable_glog_info() config.enable_use_gpu(100, 0) if use_trt: config.switch_ir_debug() @@ -141,8 +150,30 @@ class TrtLayerAutoScanTest(AutoScanTest): @abc.abstractmethod def sample_predictor_configs(self): - logging.info('--------- gpu inference ---------') + def precision_to_str(p): + if p == paddle_infer.PrecisionType.Float32: + return 'float32' + elif p == paddle_infer.PrecisionType.Half: + return 'half' + elif p == paddle_infer.PrecisionType.Int8: + return 'int8' + else: + raise NotImplementedError('not supported type.') + + trt_log_str = '' + if len(self.dynamic_shape.min_input_shape + ) != 0 and self.dynamic_shape.min_input_shape.keys( + ) == self.dynamic_shape.max_input_shape.keys( + ) and self.dynamic_shape.min_input_shape.keys( + ) == self.dynamic_shape.opt_input_shape.keys(): + trt_log_str += 'dynamic_shape ' + else: + trt_log_str += 'static_shape ' + trt_log_str += precision_to_str(self.trt_param.precision) + + logging.info(' --------- gpu inference ---------') yield self.create_program_config(use_trt=False) - logging.info('--------- trt inference ---------') + logging.info(' --------- trt ' + trt_log_str + + ' inference ---------') yield self.create_program_config( use_trt=True, precision_mode=self.trt_param.precision) -- GitLab