From f541b596fb728ff15bdf1a32ec02a86eb4cce7cf Mon Sep 17 00:00:00 2001 From: MufanColin <76479709+MufanColin@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:54:28 +0800 Subject: [PATCH 01/10] Fixed 246 --- test/ir/inference/test_trt_convert_where.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/ir/inference/test_trt_convert_where.py b/test/ir/inference/test_trt_convert_where.py index fafa1a137fa9b..e4a27504aaf95 100644 --- a/test/ir/inference/test_trt_convert_where.py +++ b/test/ir/inference/test_trt_convert_where.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import unittest from functools import partial -from typing import List import numpy as np from program_config import ProgramConfig, TensorConfig @@ -110,7 +111,7 @@ def generate_input3(dims, batch): def sample_predictor_configs( self, program_config - ) -> (paddle_infer.Config, List[int], float): + ) -> tuple[paddle_infer.Config, list[int], float]: def generate_dynamic_shape(attrs): if self.dims == 1: self.dynamic_shape.min_input_shape = { From 461ccc46480f71a2e32d6cefb5f82e04ea8e133b Mon Sep 17 00:00:00 2001 From: MufanColin <76479709+MufanColin@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:56:11 +0800 Subject: [PATCH 02/10] Fixed 247 --- test/ir/inference/test_trt_convert_yolo_box.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/ir/inference/test_trt_convert_yolo_box.py b/test/ir/inference/test_trt_convert_yolo_box.py index 079db6e203901..9537154d0685a 100644 --- a/test/ir/inference/test_trt_convert_yolo_box.py +++ b/test/ir/inference/test_trt_convert_yolo_box.py @@ -18,7 +18,7 @@ import unittest from functools import partial from itertools import product -from typing import Any, Generator +from typing import TYPE_CHECKING, Any import numpy as np from program_config import ProgramConfig, TensorConfig @@ -26,6 +26,9 @@ import paddle.inference as paddle_infer +if TYPE_CHECKING: + from collections.abc import Generator + class TrtConvertYoloBoxTest(TrtLayerAutoScanTest): def is_program_valid(self, program_config: ProgramConfig) -> bool: From f070c67c8830399b0a456c2469e1cc2659739134 Mon Sep 17 00:00:00 2001 From: MufanColin <76479709+MufanColin@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:57:08 +0800 Subject: [PATCH 03/10] Fixed 248 --- test/ir/inference/test_trt_convert_yolo_box_head.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/ir/inference/test_trt_convert_yolo_box_head.py b/test/ir/inference/test_trt_convert_yolo_box_head.py index df53e2fec654f..f61b43821427a 100644 --- a/test/ir/inference/test_trt_convert_yolo_box_head.py +++ b/test/ir/inference/test_trt_convert_yolo_box_head.py @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import unittest from functools import partial -from typing import Any, Dict, List +from typing import Any import numpy as np from program_config import ProgramConfig, TensorConfig @@ -25,7 +27,7 @@ class TrtConvertYoloBoxHeadTest(TrtLayerAutoScanTest): def sample_program_configs(self): - def generate_input(attrs: List[Dict[str, Any]], batch, shape): + def generate_input(attrs: list[dict[str, Any]], batch, shape): gen_shape = shape.copy() gen_shape.insert(0, batch) return np.random.uniform(0, 1, gen_shape).astype("float32") @@ -76,7 +78,7 @@ def generate_input(attrs: List[Dict[str, Any]], batch, shape): def sample_predictor_configs( self, program_config - ) -> (paddle_infer.Config, List[int], float): + ) -> tuple[paddle_infer.Config, list[int], float]: # for static_shape self.trt_param.precision = paddle_infer.PrecisionType.Float32 program_config.set_input_type(np.float32) From 8c7a5e84650fe10426f7344245de98c8a905ffb5 Mon Sep 17 00:00:00 2001 From: MufanColin <76479709+MufanColin@users.noreply.github.com> Date: Mon, 5 Aug 2024 15:58:35 +0800 Subject: [PATCH 04/10] Fixed 249 --- test/ir/inference/test_trt_float64.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/ir/inference/test_trt_float64.py b/test/ir/inference/test_trt_float64.py index 9e707f31ba5ff..c65d7d578de86 100644 --- a/test/ir/inference/test_trt_float64.py +++ b/test/ir/inference/test_trt_float64.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import unittest from functools import partial -from typing import List import numpy as np from program_config import ProgramConfig, TensorConfig @@ -76,7 +77,7 @@ def generate_input(shape, op_type): def sample_predictor_configs( self, program_config - ) -> (paddle_infer.Config, List[int], float): + ) -> tuple[paddle_infer.Config, list[int], float]: def generate_dynamic_shape(attrs): self.dynamic_shape.min_input_shape = { "input_data1": [1, 4, 4, 4], From 4b0a76aca94a6faa1ea0d6f9ea9f7eb1da8e539b Mon Sep 17 00:00:00 2001 From: MufanColin <76479709+MufanColin@users.noreply.github.com> Date: Mon, 5 Aug 2024 16:09:46 +0800 Subject: [PATCH 05/10] Fixed 250 --- test/ir/inference/test_trt_int64.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/ir/inference/test_trt_int64.py b/test/ir/inference/test_trt_int64.py index 2ea5aef705bcd..a10faef5a73c7 100644 --- a/test/ir/inference/test_trt_int64.py +++ b/test/ir/inference/test_trt_int64.py @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import unittest from functools import partial -from typing import Any, Dict, List +from typing import Any import numpy as np from program_config import ProgramConfig, TensorConfig @@ -61,7 +63,7 @@ def is_program_valid(self, program_config: ProgramConfig) -> bool: return True def sample_program_configs(self): - def generate_input1(attrs: List[Dict[str, Any]]): + def generate_input1(attrs: list[dict[str, Any]]): return (10 * np.random.random([6, 6, 64, 64])).astype(np.int64) for axes in [[0, 1], [1, 3], [2, 3]]: @@ -108,7 +110,7 @@ def generate_input1(attrs: List[Dict[str, Any]]): def sample_predictor_configs( self, program_config - ) -> (paddle_infer.Config, List[int], float): + ) -> tuple[paddle_infer.Config, list[int], float]: def generate_dynamic_shape(attrs): self.dynamic_shape.min_input_shape = {"input_data": [1, 3, 32, 32]} self.dynamic_shape.max_input_shape = {"input_data": [8, 8, 64, 64]} @@ -187,7 +189,7 @@ def generate_input(shape, op_type): def sample_predictor_configs( self, program_config - ) -> (paddle_infer.Config, List[int], float): + ) -> tuple[paddle_infer.Config, list[int], float]: def generate_dynamic_shape(attrs): if self.dims == 3: self.dynamic_shape.min_input_shape = { From 12f49b2e69d5766f42a9ea14b5be2b198eef3db0 Mon Sep 17 00:00:00 2001 From: MufanColin <76479709+MufanColin@users.noreply.github.com> Date: Mon, 5 Aug 2024 16:12:34 +0800 Subject: [PATCH 06/10] Fixed 251 --- test/ir/inference/test_trt_ops_fp32_mix_precision.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/ir/inference/test_trt_ops_fp32_mix_precision.py b/test/ir/inference/test_trt_ops_fp32_mix_precision.py index 188ef1e10668d..b978491069b43 100644 --- a/test/ir/inference/test_trt_ops_fp32_mix_precision.py +++ b/test/ir/inference/test_trt_ops_fp32_mix_precision.py @@ -12,9 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import unittest from functools import partial -from typing import Any, Dict, List +from typing import Any import numpy as np from program_config import ProgramConfig, TensorConfig @@ -54,10 +56,10 @@ def generate_elementwise_weight(op_type): else: return np.random.randn(33, 1).astype(np.float32) - def generate_input1(attrs: List[Dict[str, Any]], shape_input): + def generate_input1(attrs: list[dict[str, Any]], shape_input): return np.random.random(shape_input).astype(np.float32) - def generate_input2(attrs: List[Dict[str, Any]], shape_input): + def generate_input2(attrs: list[dict[str, Any]], shape_input): begin = attrs[0]["begin_norm_axis"] sum = 1 for x in range(begin, len(shape_input)): @@ -176,7 +178,7 @@ def generate_input2(attrs: List[Dict[str, Any]], shape_input): def sample_predictor_configs( self, program_config - ) -> (paddle_infer.Config, List[int], float): + ) -> tuple[paddle_infer.Config, list[int], float]: def generate_dynamic_shape(attrs): self.dynamic_shape.min_input_shape = { "conv2d_input": [1, 3, 64, 64], From 1fbb63f783d432cb0293ff9192dca6ac470ee104 Mon Sep 17 00:00:00 2001 From: MufanColin <76479709+MufanColin@users.noreply.github.com> Date: Mon, 5 Aug 2024 16:21:22 +0800 Subject: [PATCH 07/10] Fixed 252 --- test/ir/pir/cinn/llama_test_model.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/test/ir/pir/cinn/llama_test_model.py b/test/ir/pir/cinn/llama_test_model.py index db52e07380394..4761aa6f64924 100644 --- a/test/ir/pir/cinn/llama_test_model.py +++ b/test/ir/pir/cinn/llama_test_model.py @@ -12,10 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import math import sys from os.path import dirname -from typing import Optional, Tuple import paddle import paddle.nn.functional as F @@ -313,13 +314,13 @@ def _init_rope(self): def forward( self, hidden_states, - position_ids: Optional[Tuple[paddle.Tensor]] = None, - past_key_value: Optional[Tuple[paddle.Tensor]] = None, - attention_mask: Optional[paddle.Tensor] = None, + position_ids: tuple[paddle.Tensor] | None = None, + past_key_value: tuple[paddle.Tensor] | None = None, + attention_mask: paddle.Tensor | None = None, output_attentions: bool = False, use_cache: bool = False, - ) -> Tuple[ - paddle.Tensor, Optional[paddle.Tensor], Optional[Tuple[paddle.Tensor]] + ) -> tuple[ + paddle.Tensor, paddle.Tensor | None, tuple[paddle.Tensor] | None ]: """Input shape: Batch x Time x Channel""" # [bs, seq_len, num_head * head_dim] -> [seq_len / n, bs, num_head * head_dim] (n is model parallelism) @@ -398,12 +399,12 @@ def __init__(self, config): def forward( self, hidden_states: paddle.Tensor, - position_ids: Optional[Tuple[paddle.Tensor]] = None, - attention_mask: Optional[paddle.Tensor] = None, - output_attentions: Optional[bool] = False, - past_key_value: Optional[Tuple[paddle.Tensor]] = None, - use_cache: Optional[bool] = False, - ) -> Tuple[paddle.Tensor, Optional[Tuple[paddle.Tensor, paddle.Tensor]]]: + position_ids: tuple[paddle.Tensor] | None = None, + attention_mask: paddle.Tensor | None = None, + output_attentions: bool | None = False, + past_key_value: tuple[paddle.Tensor] | None = None, + use_cache: bool | None = False, + ) -> tuple[paddle.Tensor, tuple[paddle.Tensor, paddle.Tensor] | None]: """ Args: hidden_states (`paddle.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` From 7815de63e81a7278262eef8a3d9438f638efafcb Mon Sep 17 00:00:00 2001 From: MufanColin <76479709+MufanColin@users.noreply.github.com> Date: Mon, 5 Aug 2024 16:24:50 +0800 Subject: [PATCH 08/10] Fixed 253 --- test/legacy_test/auto_parallel_op_test.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/test/legacy_test/auto_parallel_op_test.py b/test/legacy_test/auto_parallel_op_test.py index 3e4c56047d62b..cc93b1fa400b8 100644 --- a/test/legacy_test/auto_parallel_op_test.py +++ b/test/legacy_test/auto_parallel_op_test.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import os import pathlib import pickle @@ -20,7 +22,7 @@ import tempfile import uuid from collections import defaultdict -from typing import Dict, List, Tuple, cast +from typing import cast import numpy as np from prim_op_test import OpTestUtils, _as_list, convert_uint16_to_float, flatten @@ -297,7 +299,7 @@ def run_subprocess(start_command, env, timeout): ) -def convert_input_placements_to_dims_map(placements: Dict, inputs: Dict): +def convert_input_placements_to_dims_map(placements: dict, inputs: dict): all_dims_map = {} for name, item in inputs.items(): if name not in placements: @@ -322,7 +324,7 @@ def convert_input_placements_to_dims_map(placements: Dict, inputs: Dict): def convert_input_dims_map_to_placements( - dims_map: Dict, inputs: Dict, mesh_ndim: int + dims_map: dict, inputs: dict, mesh_ndim: int ): placements_map = {} for name, item in inputs.items(): @@ -348,7 +350,7 @@ def convert_input_dims_map_to_placements( # TODO: This method has been implementd in # paddle/phi/core/distributed/auto_parallel/placement_types.h, bind it # python and it's logic. -def placements_to_dims_map(placements: List, tensor_ndim: int) -> Tuple[int]: +def placements_to_dims_map(placements: list, tensor_ndim: int) -> tuple[int]: r = [-1] * tensor_ndim for i, placement in enumerate(placements): if placement.is_shard(): @@ -367,13 +369,13 @@ def placements_to_dims_map(placements: List, tensor_ndim: int) -> Tuple[int]: # paddle/phi/core/distributed/auto_parallel/placement_types.h, and bind it to # python def dims_map_to_placements( - dim_map: Tuple[int], mesh_ndim: int, sums: Tuple[int] = () -) -> Tuple[dist.Placement]: + dim_map: tuple[int], mesh_ndim: int, sums: tuple[int] = () +) -> tuple[dist.Placement]: """ Construct a placements from dim_map list and pending sum. Args: - dim_map (Tuple[int]): a list of integer that represents sharding on each + dim_map (tuple[int]): a list of integer that represents sharding on each tensor dimension, see `dim_map` property doc for details mesh_ndim (int): the ndim of Process mesh. sums (Tuple[int]): a list of integer that represents the dist tensor have @@ -383,7 +385,7 @@ def dims_map_to_placements( a placement sequence. """ # by default replicate on device mesh dims - placements: List[dist.Placement] = [ + placements: list[dist.Placement] = [ dist.Replicate() for _ in range(mesh_ndim) ] From c293fdb674d0aa928fa9156652a90e5c50870aef Mon Sep 17 00:00:00 2001 From: MufanColin <76479709+MufanColin@users.noreply.github.com> Date: Mon, 5 Aug 2024 16:30:13 +0800 Subject: [PATCH 09/10] Fixed 255 --- test/legacy_test/test_memory_efficient_attention.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/legacy_test/test_memory_efficient_attention.py b/test/legacy_test/test_memory_efficient_attention.py index 6298a3100a930..6a621b9f5f896 100644 --- a/test/legacy_test/test_memory_efficient_attention.py +++ b/test/legacy_test/test_memory_efficient_attention.py @@ -12,12 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import logging import os import random import re import unittest -from typing import List, Sequence, Tuple +from typing import TYPE_CHECKING import numpy as np @@ -29,6 +31,8 @@ memory_efficient_attention, ) +if TYPE_CHECKING: + from collections.abc import Sequence paddle.seed(2023) @@ -90,11 +94,11 @@ def create_attn_bias( def _rand_seqlens( r: random.Random, bs: int, q_len: int, kv_len: int -) -> Tuple[Sequence[int], Sequence[int]]: +) -> tuple[Sequence[int], Sequence[int]]: q_len *= bs kv_len *= bs - seqlens_q: List[int] = [] - seqlens_k: List[int] = [] + seqlens_q: list[int] = [] + seqlens_k: list[int] = [] step_q = [max(1, q_len // 10), max(2, q_len // 2)] step_k = [max(1, kv_len // 10), max(2, kv_len // 2)] From 765644548b28c00c835eb311a67f87cd0db98f9f Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Tue, 6 Aug 2024 00:08:02 +0800 Subject: [PATCH 10/10] Update test/legacy_test/test_memory_efficient_attention.py --- test/legacy_test/test_memory_efficient_attention.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/legacy_test/test_memory_efficient_attention.py b/test/legacy_test/test_memory_efficient_attention.py index 6a621b9f5f896..80526aa16cf8d 100644 --- a/test/legacy_test/test_memory_efficient_attention.py +++ b/test/legacy_test/test_memory_efficient_attention.py @@ -33,6 +33,7 @@ if TYPE_CHECKING: from collections.abc import Sequence + paddle.seed(2023)