From 5ec82be03b47e728b876bb47f4962b039465b640 Mon Sep 17 00:00:00 2001 From: JeffWangYH <78253082+Jeff114514@users.noreply.github.com> Date: Sun, 11 Aug 2024 00:45:48 +0800 Subject: [PATCH] [CodeStyle][Ruff][BUAA][G-[181-190]] Fix Ruff `RUF005` diagnostic for 10 files in `test/ir/pir/fused_pass/onednn/` and `test/legacy_test/` (#67141) --------- Co-authored-by: Nyakku Shigure --- .../onednn/test_operator_unsqueeze_onednn_fuse_pass.py | 2 +- test/legacy_test/ctr_dataset_reader.py | 2 +- test/legacy_test/dist_allreduce_op.py | 2 +- test/legacy_test/dist_fleet_raw_program_optimizer.py | 2 +- .../dist_fleet_raw_program_optimizer_fuse_allreduce.py | 2 +- test/legacy_test/dist_mnist.py | 2 +- test/legacy_test/dist_mnist_dgc.py | 2 +- test/legacy_test/nets.py | 2 +- test/legacy_test/seresnext_net.py | 2 +- test/legacy_test/simple_nets.py | 7 ++++--- test/legacy_test/test_flash_attention.py | 6 +++--- test/sequence/test_sequence_conv.py | 4 +++- 12 files changed, 19 insertions(+), 16 deletions(-) diff --git a/test/ir/pir/fused_pass/onednn/test_operator_unsqueeze_onednn_fuse_pass.py b/test/ir/pir/fused_pass/onednn/test_operator_unsqueeze_onednn_fuse_pass.py index 5baf7dac489b5f..891677e97136c8 100644 --- a/test/ir/pir/fused_pass/onednn/test_operator_unsqueeze_onednn_fuse_pass.py +++ b/test/ir/pir/fused_pass/onednn/test_operator_unsqueeze_onednn_fuse_pass.py @@ -34,7 +34,7 @@ def build_ir_program(self): name='x', shape=[5, 5, 5, 5], dtype='float32' ) transpose = paddle.transpose( - x, [len(x.shape) - 1] + list(range(0, len(x.shape) - 1)) + x, [len(x.shape) - 1, *range(0, len(x.shape) - 1)] ) out = paddle.unsqueeze(transpose, [1]) out = paddle.assign(out) diff --git a/test/legacy_test/ctr_dataset_reader.py b/test/legacy_test/ctr_dataset_reader.py index 4fdc4430242f57..17358dfedcead4 100644 --- a/test/legacy_test/ctr_dataset_reader.py +++ b/test/legacy_test/ctr_dataset_reader.py @@ -77,7 +77,7 @@ def reader(): dnn_input = load_dnn_input_record(fs[0]) lr_input = load_lr_input_record(fs[1]) click = [int(fs[2])] - yield [dnn_input] + [lr_input] + [click] + yield [dnn_input, lr_input, click] return reader diff --git a/test/legacy_test/dist_allreduce_op.py b/test/legacy_test/dist_allreduce_op.py index 6604cf15f7a40e..7fece7b2343456 100644 --- a/test/legacy_test/dist_allreduce_op.py +++ b/test/legacy_test/dist_allreduce_op.py @@ -56,7 +56,7 @@ def cnn_model(data): SIZE = 10 input_shape = conv_pool_2.shape - param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1)] + [SIZE] + param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1), SIZE] scale = (2.0 / (param_shape[0] ** 2 * SIZE)) ** 0.5 predict = paddle.static.nn.fc( diff --git a/test/legacy_test/dist_fleet_raw_program_optimizer.py b/test/legacy_test/dist_fleet_raw_program_optimizer.py index 15314068e680d5..9b23116d9c0080 100644 --- a/test/legacy_test/dist_fleet_raw_program_optimizer.py +++ b/test/legacy_test/dist_fleet_raw_program_optimizer.py @@ -57,7 +57,7 @@ def cnn_model(data): SIZE = 10 input_shape = conv_pool_2.shape - param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1)] + [SIZE] + param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1), SIZE] scale = (2.0 / (param_shape[0] ** 2 * SIZE)) ** 0.5 predict = paddle.static.nn.fc( diff --git a/test/legacy_test/dist_fleet_raw_program_optimizer_fuse_allreduce.py b/test/legacy_test/dist_fleet_raw_program_optimizer_fuse_allreduce.py index 918a8819ee129a..fcf1c681151bbf 100644 --- a/test/legacy_test/dist_fleet_raw_program_optimizer_fuse_allreduce.py +++ b/test/legacy_test/dist_fleet_raw_program_optimizer_fuse_allreduce.py @@ -57,7 +57,7 @@ def cnn_model(data): SIZE = 10 input_shape = conv_pool_2.shape - param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1)] + [SIZE] + param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1), SIZE] scale = (2.0 / (param_shape[0] ** 2 * SIZE)) ** 0.5 predict = paddle.static.nn.fc( diff --git a/test/legacy_test/dist_mnist.py b/test/legacy_test/dist_mnist.py index 9bd37475242429..30b0564571dfff 100644 --- a/test/legacy_test/dist_mnist.py +++ b/test/legacy_test/dist_mnist.py @@ -57,7 +57,7 @@ def cnn_model(data): SIZE = 10 input_shape = conv_pool_2.shape - param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1)] + [SIZE] + param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1), SIZE] scale = (2.0 / (param_shape[0] ** 2 * SIZE)) ** 0.5 predict = paddle.static.nn.fc( diff --git a/test/legacy_test/dist_mnist_dgc.py b/test/legacy_test/dist_mnist_dgc.py index 4b98db2b4cc1b4..3e17c603ab957a 100644 --- a/test/legacy_test/dist_mnist_dgc.py +++ b/test/legacy_test/dist_mnist_dgc.py @@ -58,7 +58,7 @@ def cnn_model(data): SIZE = 10 input_shape = conv_pool_2.shape - param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1)] + [SIZE] + param_shape = [reduce(lambda a, b: a * b, input_shape[1:], 1), SIZE] scale = (2.0 / (param_shape[0] ** 2 * SIZE)) ** 0.5 predict = paddle.static.nn.fc( diff --git a/test/legacy_test/nets.py b/test/legacy_test/nets.py index 8847456681b817..1dbfb8546e0c1d 100644 --- a/test/legacy_test/nets.py +++ b/test/legacy_test/nets.py @@ -577,7 +577,7 @@ def __split_heads(x, num_heads): # [batch_size, max_sequence_length, num_heads, hidden_size_per_head]. reshaped = paddle.reshape( x=x, - shape=list(x.shape[:-1]) + [num_heads, hidden_size // num_heads], + shape=[*x.shape[:-1], num_heads, hidden_size // num_heads], ) # permute the dimensions into: diff --git a/test/legacy_test/seresnext_net.py b/test/legacy_test/seresnext_net.py index ef19deebba3789..7874e27f013e98 100644 --- a/test/legacy_test/seresnext_net.py +++ b/test/legacy_test/seresnext_net.py @@ -131,7 +131,7 @@ def bottleneck_block(input, num_filters, stride, cardinality, reduction_ratio): def SE_ResNeXt50Small(use_feed): img = paddle.static.data( - name='image', shape=[-1] + img_shape, dtype='float32' + name='image', shape=[-1, *img_shape], dtype='float32' ) label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') diff --git a/test/legacy_test/simple_nets.py b/test/legacy_test/simple_nets.py index a1d2bd5446ed58..945532f1eacc84 100644 --- a/test/legacy_test/simple_nets.py +++ b/test/legacy_test/simple_nets.py @@ -30,8 +30,9 @@ def pir_fc(hidden, size, activation, param_attr, bias_attr): input_shape = input.shape num_flatten_dims = len(input_shape) - 1 param_shape = [ - reduce(lambda a, b: a * b, input_shape[num_flatten_dims:], 1) - ] + [size] + reduce(lambda a, b: a * b, input_shape[num_flatten_dims:], 1), + size, + ] w = helper.create_parameter( attr=param_attr, shape=param_shape, dtype=input.dtype, is_bias=False @@ -216,7 +217,7 @@ def bow_net( def init_data(batch_size=32, img_shape=[784], label_range=9): np.random.seed(5) assert isinstance(img_shape, list) - input_shape = [batch_size] + img_shape + input_shape = [batch_size, *img_shape] img = np.random.random(size=input_shape).astype(np.float32) label = ( np.array([np.random.randint(0, label_range) for _ in range(batch_size)]) diff --git a/test/legacy_test/test_flash_attention.py b/test/legacy_test/test_flash_attention.py index cdfd375d74674c..b6a9cd5e044e98 100644 --- a/test/legacy_test/test_flash_attention.py +++ b/test/legacy_test/test_flash_attention.py @@ -567,10 +567,10 @@ def gen_unpadded_data(self, dtype): low=1, high=self.seq_len, size=[self.batch_size] ) cu_seqlen_q = paddle.to_tensor( - [0, *np.cumsum(seq_len_q)], dtype=paddle.int32 + [0, *np.cumsum(seq_len_q).tolist()], dtype=paddle.int32 ) cu_seqlen_k = paddle.to_tensor( - [0, *np.cumsum(seq_len_k)], dtype=paddle.int32 + [0, *np.cumsum(seq_len_k).tolist()], dtype=paddle.int32 ) qs, ks, vs = [], [], [] @@ -966,7 +966,7 @@ def gen_unpadded_data(self, dtype): ) seq_len_k = seq_len_q cu_seqlen_q = paddle.to_tensor( - [0, *np.cumsum(seq_len_q)], dtype=paddle.int32 + [0, *np.cumsum(seq_len_q).tolist()], dtype=paddle.int32 ) cu_seqlen_k = cu_seqlen_q diff --git a/test/sequence/test_sequence_conv.py b/test/sequence/test_sequence_conv.py index 100d4cad39d1bd..68432f9a601fbb 100644 --- a/test/sequence/test_sequence_conv.py +++ b/test/sequence/test_sequence_conv.py @@ -278,7 +278,9 @@ def init_test_case(self): self.input_size = [self.input_row, 25] idx = list(range(self.input_size[0])) del idx[0] - offset_lod = [[0, *np.sort(random.sample(idx, 8)), self.input_size[0]]] + offset_lod = [ + [0, *np.sort(random.sample(idx, 8)).tolist(), self.input_size[0]] + ] self.lod = [[]] # convert from offset-based lod to length-based lod for i in range(len(offset_lod[0]) - 1):