diff --git a/paddle/fluid/pybind/pir.cc b/paddle/fluid/pybind/pir.cc index 720eeb8c637b9e..f036c571936140 100644 --- a/paddle/fluid/pybind/pir.cc +++ b/paddle/fluid/pybind/pir.cc @@ -984,6 +984,11 @@ void BindOperation(py::module *m) { attrs_dict[pair.first.c_str()] = pair.second.dyn_cast(); } else { + if (pair.second.isa()) { + VLOG(2) << "The value is stored with float32 precision, " + "which may cause precision issues for higher " + "precision requirements."; + } attrs_dict[pair.first.c_str()] = paddle::dialect::GetAttributeData(pair.second); } diff --git a/python/paddle/tensorrt/impls/manipulation.py b/python/paddle/tensorrt/impls/manipulation.py index 49d4433891d010..0c960faee92278 100644 --- a/python/paddle/tensorrt/impls/manipulation.py +++ b/python/paddle/tensorrt/impls/manipulation.py @@ -1049,11 +1049,17 @@ def tile_converter(network, paddle_op, inputs): repeat_rank = len(repeat_times) else: repeat_tensor = inputs[1] - repeat_tensor = resize_to_1d( - network, repeat_tensor, name=[paddle_op.name(), 'repeat_tensor'] - ) - repeat_shape = paddle_op.operands()[1].source().shape - repeat_rank = repeat_shape[0] + if isinstance(repeat_tensor, list): + repeat_rank = len(repeat_tensor) + repeat_tensor = trt_concat( + network, repeat_tensor, name=[paddle_op.name(), 'repeat_tensor'] + ) + else: + repeat_tensor = resize_to_1d( + network, repeat_tensor, name=[paddle_op.name(), 'repeat_tensor'] + ) + repeat_shape = paddle_op.operands()[1].source().shape + repeat_rank = repeat_shape[0] if rank > repeat_rank: one_rank_tensor = add_1D_constant_layer( diff --git a/python/paddle/tensorrt/impls/others.py b/python/paddle/tensorrt/impls/others.py index 219e89304dc67f..12668e675f07b2 100644 --- a/python/paddle/tensorrt/impls/others.py +++ b/python/paddle/tensorrt/impls/others.py @@ -323,7 +323,8 @@ def share_data_converter(network, paddle_op, inputs): @converter_registry.register("pd_op.temporal_shift", trt_version="8.x") def temporal_shift_converter(network, paddle_op, inputs): input_tensor = inputs[0] - shift_ratio = paddle_op.attrs()["shift_ratio"] + # Add a small bias to shift_ratio to mitigate floating point precision errors + shift_ratio = paddle_op.attrs()["shift_ratio"] + 1e-7 T = paddle_op.attrs()["seg_num"] data_format = paddle_op.attrs().get("data_format", "NCHW") diff --git a/test/ir/inference/CMakeLists.txt b/test/ir/inference/CMakeLists.txt index f53ae5c29c4dbb..2203b5aa5ae90e 100755 --- a/test/ir/inference/CMakeLists.txt +++ b/test/ir/inference/CMakeLists.txt @@ -169,7 +169,10 @@ if(WITH_GPU AND TENSORRT_FOUND) #set_tests_properties(test_trt_multiclass_nms_op PROPERTIES TIMEOUT 200) set_tests_properties(test_trt_ops_fp32_mix_precision PROPERTIES TIMEOUT 300) + set_tests_properties(test_trt_convert_expand_v2 PROPERTIES TIMEOUT 1000) set_tests_properties(test_trt_convert_unary PROPERTIES TIMEOUT 600) + set_tests_properties(test_trt_convert_temporal_shift_deprecated + PROPERTIES TIMEOUT 1000) set_tests_properties(test_trt_convert_pool2d PROPERTIES TIMEOUT 600) set_tests_properties(test_trt_convert_slice PROPERTIES TIMEOUT 600) diff --git a/test/ir/inference/test_trt_convert_expand_v2.py b/test/ir/inference/test_trt_convert_expand_v2.py index 19d9ca09b66853..093fafc3d07b2e 100644 --- a/test/ir/inference/test_trt_convert_expand_v2.py +++ b/test/ir/inference/test_trt_convert_expand_v2.py @@ -62,7 +62,7 @@ def generate_shapeT2_data(attrs: list[dict[str, Any]]): return np.array([24]).astype(np.int32) for dims in [4, 3, 2, 1]: - for shape in [[10, 12, -1, -1], [8, 64, -1, -1], [6, 8, -1]]: + for shape in [[10, 12, -1, -1]]: dics = [ { "shape": shape, @@ -93,44 +93,180 @@ def generate_shapeT2_data(attrs: list[dict[str, Any]]): yield program_config + def generate_dynamic_shape(self): + if self.dims == 4: + self.dynamic_shape.min_input_shape = { + "expand_v2_input": [1, 1, 4, 6] + } + self.dynamic_shape.max_input_shape = { + "expand_v2_input": [10, 1, 8, 6] + } + self.dynamic_shape.opt_input_shape = { + "expand_v2_input": [1, 1, 4, 6] + } + elif self.dims == 3: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [1, 8, 6]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [12, 8, 6]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [1, 8, 6]} + elif self.dims == 2: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [1, 48]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [4, 48]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [1, 48]} + elif self.dims == 1: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [48]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [48]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [48]} + return self.dynamic_shape + def sample_predictor_configs( - self, program_config + self, program_config, run_pir=False ) -> tuple[paddle_infer.Config, list[int], float]: - def generate_dynamic_shape(attrs): + + def clear_dynamic_shape(): + self.dynamic_shape.min_input_shape = {} + self.dynamic_shape.max_input_shape = {} + self.dynamic_shape.opt_input_shape = {} + + def generate_trt_nodes_num(attrs, dynamic_shape): + if dynamic_shape: + return 1, 2 + else: + return 0, 3 + + attrs = [ + program_config.ops[i].attrs for i in range(len(program_config.ops)) + ] + + clear_dynamic_shape() + if not run_pir: + self.trt_param.precision = paddle_infer.PrecisionType.Float32 + program_config.set_input_type(np.float32) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-5 + self.trt_param.precision = paddle_infer.PrecisionType.Half + program_config.set_input_type(np.float16) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-3 + + # for dynamic_shape + self.generate_dynamic_shape() + self.trt_param.precision = paddle_infer.PrecisionType.Float32 + program_config.set_input_type(np.float32) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, True + ), 1e-5 + self.trt_param.precision = paddle_infer.PrecisionType.Half + program_config.set_input_type(np.float16) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, True + ), 1e-3 + + def add_skip_trt_case(self): + pass + + def test(self): + self.add_skip_trt_case() + self.run_test(run_pir=True) + + +class TrtConvertExpandV2Case2Test(TrtLayerAutoScanTest): + def is_program_valid(self, program_config: ProgramConfig) -> bool: + attrs = [ + program_config.ops[i].attrs for i in range(len(program_config.ops)) + ] + if len(attrs[0]['shape']) < self.dims: + return False + if self.dims == 1: + if len(attrs[0]['shape']) == 4: + return False + return True + + def sample_program_configs(self): + def generate_input1(attrs: list[dict[str, Any]]): if self.dims == 4: - self.dynamic_shape.min_input_shape = { - "expand_v2_input": [1, 1, 4, 6] - } - self.dynamic_shape.max_input_shape = { - "expand_v2_input": [10, 1, 4, 6] - } - self.dynamic_shape.opt_input_shape = { - "expand_v2_input": [1, 1, 4, 6] - } + self.input_shape = [1, 1, 4, 6] + return np.random.random([1, 1, 4, 6]).astype(np.float32) elif self.dims == 3: - self.dynamic_shape.min_input_shape = { - "expand_v2_input": [1, 8, 6] - } - self.dynamic_shape.max_input_shape = { - "expand_v2_input": [4, 8, 6] - } - self.dynamic_shape.opt_input_shape = { - "expand_v2_input": [1, 8, 6] - } + self.input_shape = [1, 8, 6] + return np.random.random([1, 8, 6]).astype(np.float32) elif self.dims == 2: - self.dynamic_shape.min_input_shape = { - "expand_v2_input": [1, 48] - } - self.dynamic_shape.max_input_shape = { - "expand_v2_input": [4, 48] - } - self.dynamic_shape.opt_input_shape = { - "expand_v2_input": [1, 48] - } + self.input_shape = [1, 48] + return np.random.random([1, 48]).astype(np.float32) elif self.dims == 1: - self.dynamic_shape.min_input_shape = {"expand_v2_input": [48]} - self.dynamic_shape.max_input_shape = {"expand_v2_input": [48]} - self.dynamic_shape.opt_input_shape = {"expand_v2_input": [48]} + self.input_shape = [48] + return np.random.random([48]).astype(np.float32) + + def generate_weight1(attrs: list[dict[str, Any]]): + return np.array([1, 48]).astype(np.int32) + + def generate_shapeT1_data(attrs: list[dict[str, Any]]): + return np.array([2]).astype(np.int32) + + def generate_shapeT2_data(attrs: list[dict[str, Any]]): + return np.array([24]).astype(np.int32) + + for dims in [4, 3, 2, 1]: + for shape in [[10, 64, -1, -1]]: + dics = [ + { + "shape": shape, + }, + ] + self.dims = dims + dics_input = [{"X": ["expand_v2_input"]}] + + ops_config = [ + { + "op_type": "expand_v2", + "op_inputs": dics_input[0], + "op_outputs": {"Out": ["expand_v2_out"]}, + "op_attrs": dics[0], + } + ] + ops = self.generate_op_config(ops_config) + program_config = ProgramConfig( + ops=ops, + weights={}, + inputs={ + "expand_v2_input": TensorConfig( + data_gen=partial(generate_input1, dics) + ) + }, + outputs=["expand_v2_out"], + ) + + yield program_config + + def generate_dynamic_shape(self): + if self.dims == 4: + self.dynamic_shape.min_input_shape = { + "expand_v2_input": [1, 1, 4, 6] + } + self.dynamic_shape.max_input_shape = { + "expand_v2_input": [10, 1, 8, 6] + } + self.dynamic_shape.opt_input_shape = { + "expand_v2_input": [1, 1, 4, 6] + } + elif self.dims == 3: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [1, 8, 6]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [64, 8, 6]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [1, 8, 6]} + elif self.dims == 2: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [1, 48]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [4, 48]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [1, 48]} + elif self.dims == 1: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [48]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [48]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [48]} + return self.dynamic_shape + + def sample_predictor_configs( + self, program_config, run_pir=False + ) -> tuple[paddle_infer.Config, list[int], float]: def clear_dynamic_shape(): self.dynamic_shape.min_input_shape = {} @@ -148,19 +284,166 @@ def generate_trt_nodes_num(attrs, dynamic_shape): ] clear_dynamic_shape() + if not run_pir: + self.trt_param.precision = paddle_infer.PrecisionType.Float32 + program_config.set_input_type(np.float32) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-5 + self.trt_param.precision = paddle_infer.PrecisionType.Half + program_config.set_input_type(np.float16) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-3 + + # for dynamic_shape + self.generate_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) yield self.create_inference_config(), generate_trt_nodes_num( - attrs, False + attrs, True ), 1e-5 self.trt_param.precision = paddle_infer.PrecisionType.Half program_config.set_input_type(np.float16) yield self.create_inference_config(), generate_trt_nodes_num( - attrs, False + attrs, True ), 1e-3 + def add_skip_trt_case(self): + pass + + def test(self): + self.add_skip_trt_case() + self.run_test(run_pir=True) + + +class TrtConvertExpandV2Case3Test(TrtLayerAutoScanTest): + def is_program_valid(self, program_config: ProgramConfig) -> bool: + attrs = [ + program_config.ops[i].attrs for i in range(len(program_config.ops)) + ] + if len(attrs[0]['shape']) < self.dims: + return False + if self.dims == 1: + if len(attrs[0]['shape']) == 4: + return False + return True + + def sample_program_configs(self): + def generate_input1(attrs: list[dict[str, Any]]): + if self.dims == 4: + self.input_shape = [1, 1, 4, 6] + return np.random.random([1, 1, 4, 6]).astype(np.float32) + elif self.dims == 3: + self.input_shape = [1, 8, 6] + return np.random.random([1, 8, 6]).astype(np.float32) + elif self.dims == 2: + self.input_shape = [1, 48] + return np.random.random([1, 48]).astype(np.float32) + elif self.dims == 1: + self.input_shape = [48] + return np.random.random([48]).astype(np.float32) + + def generate_weight1(attrs: list[dict[str, Any]]): + return np.array([1, 48]).astype(np.int32) + + def generate_shapeT1_data(attrs: list[dict[str, Any]]): + return np.array([2]).astype(np.int32) + + def generate_shapeT2_data(attrs: list[dict[str, Any]]): + return np.array([24]).astype(np.int32) + + for dims in [4, 3, 2, 1]: + for shape in [[6, 8, -1]]: + dics = [ + { + "shape": shape, + }, + ] + self.dims = dims + dics_input = [{"X": ["expand_v2_input"]}] + + ops_config = [ + { + "op_type": "expand_v2", + "op_inputs": dics_input[0], + "op_outputs": {"Out": ["expand_v2_out"]}, + "op_attrs": dics[0], + } + ] + ops = self.generate_op_config(ops_config) + program_config = ProgramConfig( + ops=ops, + weights={}, + inputs={ + "expand_v2_input": TensorConfig( + data_gen=partial(generate_input1, dics) + ) + }, + outputs=["expand_v2_out"], + ) + + yield program_config + + def generate_dynamic_shape(self): + if self.dims == 4: + self.dynamic_shape.min_input_shape = { + "expand_v2_input": [1, 1, 4, 6] + } + self.dynamic_shape.max_input_shape = { + "expand_v2_input": [10, 1, 8, 6] + } + self.dynamic_shape.opt_input_shape = { + "expand_v2_input": [1, 1, 4, 6] + } + elif self.dims == 3: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [1, 8, 6]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [6, 8, 6]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [1, 8, 6]} + elif self.dims == 2: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [1, 48]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [8, 48]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [1, 48]} + elif self.dims == 1: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [48]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [48]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [48]} + return self.dynamic_shape + + def sample_predictor_configs( + self, program_config, run_pir=False + ) -> tuple[paddle_infer.Config, list[int], float]: + + def clear_dynamic_shape(): + self.dynamic_shape.min_input_shape = {} + self.dynamic_shape.max_input_shape = {} + self.dynamic_shape.opt_input_shape = {} + + def generate_trt_nodes_num(attrs, dynamic_shape): + if dynamic_shape: + return 1, 2 + else: + return 0, 3 + + attrs = [ + program_config.ops[i].attrs for i in range(len(program_config.ops)) + ] + + clear_dynamic_shape() + if not run_pir: + self.trt_param.precision = paddle_infer.PrecisionType.Float32 + program_config.set_input_type(np.float32) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-5 + self.trt_param.precision = paddle_infer.PrecisionType.Half + program_config.set_input_type(np.float16) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-3 + # for dynamic_shape - generate_dynamic_shape(attrs) + self.generate_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) yield self.create_inference_config(), generate_trt_nodes_num( @@ -177,7 +460,7 @@ def add_skip_trt_case(self): def test(self): self.add_skip_trt_case() - self.run_test() + self.run_test(run_pir=True) class TrtConvertExpandV2Test2(TrtLayerAutoScanTest): @@ -212,6 +495,7 @@ def generate_input1(attrs: list[dict[str, Any]]): "op_attrs": { "dtype": 2, "str_value": "10", + "value": 10, "shape": [1], }, }, @@ -236,14 +520,16 @@ def generate_input1(attrs: list[dict[str, Any]]): yield program_config + def generate_dynamic_shape(self): + if self.dims == 1: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [1]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [1]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [1]} + return self.dynamic_shape + def sample_predictor_configs( - self, program_config + self, program_config, run_pir=False ) -> tuple[paddle_infer.Config, list[int], float]: - def generate_dynamic_shape(): - if self.dims == 1: - self.dynamic_shape.min_input_shape = {"expand_v2_input": [1]} - self.dynamic_shape.max_input_shape = {"expand_v2_input": [1]} - self.dynamic_shape.opt_input_shape = {"expand_v2_input": [1]} def clear_dynamic_shape(): self.dynamic_shape.min_input_shape = {} @@ -252,7 +538,7 @@ def clear_dynamic_shape(): clear_dynamic_shape() # for dynamic_shape - generate_dynamic_shape() + self.generate_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) # fill_constant will be folded by constant folding pass! @@ -266,7 +552,7 @@ def add_skip_trt_case(self): def test(self): self.add_skip_trt_case() - self.run_test() + self.run_test(run_pir=True) class TrtConvertExpandV2Test3(TrtLayerAutoScanTest): @@ -312,6 +598,7 @@ def generate_input1(attrs: list[dict[str, Any]]): "op_attrs": { "dtype": 2, "str_value": "10", + "value": 10, "shape": [1], }, }, @@ -322,6 +609,7 @@ def generate_input1(attrs: list[dict[str, Any]]): "op_attrs": { "dtype": 2, "str_value": "12", + "value": 12, "shape": [1], }, }, @@ -332,6 +620,7 @@ def generate_input1(attrs: list[dict[str, Any]]): "op_attrs": { "dtype": 2, "str_value": "4", + "value": 4, "shape": [1], }, }, @@ -342,6 +631,7 @@ def generate_input1(attrs: list[dict[str, Any]]): "op_attrs": { "dtype": 2, "str_value": "6", + "value": 6, "shape": [1], }, }, @@ -366,30 +656,26 @@ def generate_input1(attrs: list[dict[str, Any]]): yield program_config + def generate_dynamic_shape(self): + if self.dims == 4: + self.dynamic_shape.min_input_shape = { + "expand_v2_input": [1, 1, 4, 6] + } + self.dynamic_shape.max_input_shape = { + "expand_v2_input": [10, 1, 4, 6] + } + self.dynamic_shape.opt_input_shape = { + "expand_v2_input": [1, 1, 4, 6] + } + elif self.dims == 3: + self.dynamic_shape.min_input_shape = {"expand_v2_input": [1, 4, 6]} + self.dynamic_shape.max_input_shape = {"expand_v2_input": [12, 4, 6]} + self.dynamic_shape.opt_input_shape = {"expand_v2_input": [1, 4, 6]} + return self.dynamic_shape + def sample_predictor_configs( - self, program_config + self, program_config, run_pir=False ) -> tuple[paddle_infer.Config, list[int], float]: - def generate_dynamic_shape(): - if self.dims == 4: - self.dynamic_shape.min_input_shape = { - "expand_v2_input": [1, 1, 4, 6] - } - self.dynamic_shape.max_input_shape = { - "expand_v2_input": [10, 1, 4, 6] - } - self.dynamic_shape.opt_input_shape = { - "expand_v2_input": [1, 1, 4, 6] - } - elif self.dims == 3: - self.dynamic_shape.min_input_shape = { - "expand_v2_input": [1, 4, 6] - } - self.dynamic_shape.max_input_shape = { - "expand_v2_input": [4, 4, 6] - } - self.dynamic_shape.opt_input_shape = { - "expand_v2_input": [1, 4, 6] - } def clear_dynamic_shape(): self.dynamic_shape.min_input_shape = {} @@ -398,7 +684,7 @@ def clear_dynamic_shape(): clear_dynamic_shape() # for dynamic_shape - generate_dynamic_shape() + self.generate_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) # fill_constant will be folded by constant folding pass! @@ -412,7 +698,7 @@ def add_skip_trt_case(self): def test(self): self.add_skip_trt_case() - self.run_test() + self.run_test(run_pir=True) if __name__ == "__main__": diff --git a/test/ir/inference/test_trt_convert_range.py b/test/ir/inference/test_trt_convert_range.py index 697c9ffb5f56c2..8e770265bc8936 100644 --- a/test/ir/inference/test_trt_convert_range.py +++ b/test/ir/inference/test_trt_convert_range.py @@ -43,6 +43,7 @@ def generate_input(): "op_attrs": { "dtype": self.in_dtype, "str_value": "7", + "value": 7, "shape": [1], }, }, @@ -53,6 +54,7 @@ def generate_input(): "op_attrs": { "dtype": self.in_dtype, "str_value": "256", + "value": 256, "shape": [1], }, }, @@ -63,6 +65,7 @@ def generate_input(): "op_attrs": { "dtype": self.in_dtype, "str_value": "1", + "value": 1, "shape": [1], }, }, @@ -96,26 +99,27 @@ def generate_input(): yield program_config + def generate_dynamic_shape(self): + self.dynamic_shape.min_input_shape = { + "start_data": [1], + "end_data": [1], + "step_data": [1], + } + self.dynamic_shape.max_input_shape = { + "start_data": [1], + "end_data": [1], + "step_data": [1], + } + self.dynamic_shape.opt_input_shape = { + "start_data": [1], + "end_data": [1], + "step_data": [1], + } + return self.dynamic_shape + def sample_predictor_configs( - self, program_config + self, program_config, run_pir=False ) -> tuple[paddle_infer.Config, list[int], float]: - def generate_dynamic_shape(attrs): - self.dynamic_shape.min_input_shape = { - "start_data": [1], - "end_data": [1], - "step_data": [1], - } - self.dynamic_shape.max_input_shape = { - "start_data": [1], - "end_data": [1], - "step_data": [1], - } - self.dynamic_shape.opt_input_shape = { - "start_data": [1], - "end_data": [1], - "step_data": [1], - } - def clear_dynamic_shape(): self.dynamic_shape.min_input_shape = {} self.dynamic_shape.max_input_shape = {} @@ -129,7 +133,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): ] # for dynamic_shape - generate_dynamic_shape(attrs) + self.generate_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 yield self.create_inference_config(), generate_trt_nodes_num( attrs, True @@ -140,7 +144,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): ), 1e-2 def test(self): - self.run_test() + self.run_test(run_pir=True) class TrtConvertRangeStaticTest(TrtLayerAutoScanTest): diff --git a/test/ir/inference/test_trt_convert_temporal_shift_deprecated.py b/test/ir/inference/test_trt_convert_temporal_shift_deprecated.py new file mode 100644 index 00000000000000..898938a71c8328 --- /dev/null +++ b/test/ir/inference/test_trt_convert_temporal_shift_deprecated.py @@ -0,0 +1,136 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import annotations + +import unittest +from functools import partial + +import numpy as np +from program_config import ProgramConfig, TensorConfig +from trt_layer_auto_scan_test import TrtLayerAutoScanTest + +import paddle.inference as paddle_infer + + +class TrtConvertTemporalShiftTest(TrtLayerAutoScanTest): + def is_program_valid(self, program_config: ProgramConfig) -> bool: + return True + + def sample_program_configs(self): + def generate_input1(attrs): + T = attrs[0]["seg_num"] + shape = [2 * T, 10, 64, 64] + return np.random.uniform(low=0.1, high=1.0, size=shape).astype( + np.float32 + ) + + for shift_value in [0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.49]: + for T in [2, 4]: + for data_format in ["NCHW", "NHWC"]: + dics = [ + { + "shift_ratio": shift_value, + "seg_num": T, + "data_format": data_format, + }, + {}, + ] + ops_config = [ + { + "op_type": "temporal_shift", + "op_inputs": {"X": ["input_data"]}, + "op_outputs": {"Out": ["output_data"]}, + "op_attrs": dics[0], + } + ] + + ops = self.generate_op_config(ops_config) + for i in range(10): + program_config = ProgramConfig( + ops=ops, + weights={}, + inputs={ + "input_data": TensorConfig( + data_gen=partial(generate_input1, dics) + ), + }, + outputs=["output_data"], + ) + + yield program_config + + def generate_dynamic_shape(self): + self.dynamic_shape.min_input_shape = {"input_data": [4, 10, 64, 64]} + self.dynamic_shape.max_input_shape = {"input_data": [20, 10, 64, 64]} + self.dynamic_shape.opt_input_shape = {"input_data": [12, 10, 64, 64]} + return self.dynamic_shape + + def sample_predictor_configs( + self, program_config, run_pir=False + ) -> tuple[paddle_infer.Config, list[int], float]: + + def clear_dynamic_shape(): + self.dynamic_shape.max_input_shape = {} + self.dynamic_shape.min_input_shape = {} + self.dynamic_shape.opt_input_shape = {} + + def generate_trt_nodes_num(attrs, is_dynamic_shape): + valid_version = (8, 2, 0) + compile_version = paddle_infer.get_trt_compile_version() + runtime_version = paddle_infer.get_trt_runtime_version() + self.assertTrue(compile_version == runtime_version) + if compile_version < valid_version: + return 0, 3 + if is_dynamic_shape: + return 1, 2 + return 0, 3 + + attrs = [ + program_config.ops[i].attrs for i in range(len(program_config.ops)) + ] + + # for static_shape + clear_dynamic_shape() + if not run_pir: + self.trt_param.precision = paddle_infer.PrecisionType.Float32 + program_config.set_input_type(np.float32) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-5 + self.trt_param.precision = paddle_infer.PrecisionType.Half + program_config.set_input_type(np.float16) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, False + ), 1e-3 + + # for dynamic_shape + self.generate_dynamic_shape() + self.trt_param.precision = paddle_infer.PrecisionType.Float32 + program_config.set_input_type(np.float32) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, True + ), 1e-5 + self.trt_param.precision = paddle_infer.PrecisionType.Half + program_config.set_input_type(np.float16) + yield self.create_inference_config(), generate_trt_nodes_num( + attrs, True + ), 1e-3 + + def test(self): + self.run_test(run_pir=True) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/ir/inference/test_trt_convert_tile.py b/test/ir/inference/test_trt_convert_tile.py index 8a3a5dea1431c4..b4697d05265b7d 100644 --- a/test/ir/inference/test_trt_convert_tile.py +++ b/test/ir/inference/test_trt_convert_tile.py @@ -43,7 +43,7 @@ def sample_program_configs(self, *args, **kwargs): def generate_input1(attrs: list[dict[str, Any]]): return np.ones([1, 2]).astype(np.float32) - dics = [{"repeat_times": kwargs['repeat_times']}] + dics = [{"repeat_times": kwargs.get('repeat_times', [1])}] ops_config = [ { @@ -68,13 +68,15 @@ def generate_input1(attrs: list[dict[str, Any]]): yield program_config + def generate_dynamic_shape(self): + self.dynamic_shape.min_input_shape = {"input_data": [1, 2]} + self.dynamic_shape.max_input_shape = {"input_data": [4, 3]} + self.dynamic_shape.opt_input_shape = {"input_data": [1, 3]} + return self.dynamic_shape + def sample_predictor_configs( - self, program_config + self, program_config, run_pir=False ) -> tuple[paddle_infer.Config, list[int], float]: - def generate_dynamic_shape(attrs): - self.dynamic_shape.min_input_shape = {"input_data": [1, 2]} - self.dynamic_shape.max_input_shape = {"input_data": [4, 3]} - self.dynamic_shape.opt_input_shape = {"input_data": [1, 3]} def clear_dynamic_shape(): self.dynamic_shape.min_input_shape = {} @@ -93,7 +95,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): ] # for dynamic_shape - generate_dynamic_shape(attrs) + self.generate_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) yield self.create_inference_config(), generate_trt_nodes_num( @@ -107,7 +109,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): @given(repeat_times=st.sampled_from([[1], [1, 2], [0, 3]])) def test(self, *args, **kwargs): - self.run_test(*args, **kwargs) + self.run_test(run_pir=True) class TrtConvertTileTest2(TrtLayerAutoScanTest): @@ -130,6 +132,7 @@ def generate_input1(attrs: list[dict[str, Any]]): "op_attrs": { "dtype": 2, "str_value": "1", + "value": 1, "shape": [1], }, }, @@ -154,13 +157,15 @@ def generate_input1(attrs: list[dict[str, Any]]): yield program_config + def generate_dynamic_shape(self): + self.dynamic_shape.min_input_shape = {"tile_input": [1, 2]} + self.dynamic_shape.max_input_shape = {"tile_input": [4, 3]} + self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2]} + return self.dynamic_shape + def sample_predictor_configs( - self, program_config + self, program_config, run_pir=False ) -> tuple[paddle_infer.Config, list[int], float]: - def generate_dynamic_shape(attrs): - self.dynamic_shape.min_input_shape = {"tile_input": [1, 2]} - self.dynamic_shape.max_input_shape = {"tile_input": [4, 3]} - self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2]} def clear_dynamic_shape(): self.dynamic_shape.min_input_shape = {} @@ -175,7 +180,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): ] # for dynamic_shape - generate_dynamic_shape(attrs) + self.generate_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) yield self.create_inference_config(), generate_trt_nodes_num( @@ -192,7 +197,7 @@ def add_skip_trt_case(self): def test(self): self.add_skip_trt_case() - self.run_test() + self.run_test(run_pir=True) class TrtConvertTileTest3(TrtLayerAutoScanTest): @@ -221,6 +226,7 @@ def generate_input1(attrs: list[dict[str, Any]]): "op_attrs": { "dtype": 2, "str_value": "10", + "value": 10, "shape": [1], }, }, @@ -231,6 +237,7 @@ def generate_input1(attrs: list[dict[str, Any]]): "op_attrs": { "dtype": 2, "str_value": "12", + "value": 12, "shape": [1], }, }, @@ -255,13 +262,15 @@ def generate_input1(attrs: list[dict[str, Any]]): yield program_config + def generate_dynamic_shape(self): + self.dynamic_shape.min_input_shape = {"tile_input": [1, 2]} + self.dynamic_shape.max_input_shape = {"tile_input": [4, 3]} + self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2]} + return self.dynamic_shape + def sample_predictor_configs( - self, program_config + self, program_config, run_pir=False ) -> tuple[paddle_infer.Config, list[int], float]: - def generate_dynamic_shape(attrs): - self.dynamic_shape.min_input_shape = {"tile_input": [1, 2]} - self.dynamic_shape.max_input_shape = {"tile_input": [4, 3]} - self.dynamic_shape.opt_input_shape = {"tile_input": [1, 2]} def clear_dynamic_shape(): self.dynamic_shape.min_input_shape = {} @@ -276,7 +285,7 @@ def generate_trt_nodes_num(attrs, dynamic_shape): ] # for dynamic_shape - generate_dynamic_shape(attrs) + self.generate_dynamic_shape() self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) yield self.create_inference_config(), generate_trt_nodes_num( @@ -293,7 +302,7 @@ def add_skip_trt_case(self): def test(self): self.add_skip_trt_case() - self.run_test() + self.run_test(run_pir=True) if __name__ == "__main__":