From 12e95aa4ee4924c0f97a6ed876bc96c43f68c6f9 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Wed, 20 Nov 2024 17:52:07 +0000 Subject: [PATCH] [BE]: Apply PERF401 autofixes from ruff (#140980) * Automatically applies ruff rule 401. Turns loops into equivalent list comprehensions which are faster and do not leak the scope of the loop variables. * list comprehensions not only often have better typing, but are 50+% faster than for loops on overhead. They also preserve length information etc and are better for the interpreter to optimize. * Manually went back and made mypy happy after the change. * Also fixed style lints in files covered by flake8 but not by pyfmt Pull Request resolved: https://github.com/pytorch/pytorch/pull/140980 Approved by: https://github.com/justinchuby, https://github.com/malfet --- torch/_dynamo/backends/distributed.py | 3 +- torch/_dynamo/bytecode_transformation.py | 11 ++-- torch/_dynamo/compiled_autograd.py | 14 ++--- torch/_dynamo/guards.py | 7 +-- torch/_dynamo/output_graph.py | 4 +- torch/_dynamo/resume_execution.py | 3 +- torch/_dynamo/testing.py | 4 +- torch/_dynamo/utils.py | 7 ++- torch/_dynamo/variables/builtin.py | 7 +-- torch/_dynamo/variables/ctx_manager.py | 19 +++--- torch/_dynamo/variables/iter.py | 6 +- torch/_dynamo/variables/user_defined.py | 11 ++-- torch/_export/converter.py | 4 +- torch/_export/db/examples/static_for_loop.py | 5 +- torch/_export/serde/serialize.py | 8 +-- .../_aot_autograd/runtime_wrappers.py | 10 +-- torch/_higher_order_ops/map.py | 4 +- torch/_higher_order_ops/triton_kernel_wrap.py | 3 +- torch/_higher_order_ops/utils.py | 4 +- torch/_inductor/codecache.py | 9 +-- torch/_inductor/codegen/cpp.py | 5 +- torch/_inductor/codegen/debug_utils.py | 18 +++--- torch/_inductor/codegen/halide.py | 24 ++++---- torch/_inductor/codegen/multi_kernel.py | 9 +-- .../_inductor/codegen/triton_combo_kernel.py | 11 ++-- torch/_inductor/cpp_builder.py | 10 ++- torch/_inductor/cpu_vec_isa.py | 8 ++- torch/_inductor/cudagraph_trees.py | 23 +++---- torch/_inductor/dependencies.py | 8 +-- torch/_inductor/fx_passes/split_cat.py | 11 ++-- torch/_inductor/graph.py | 17 +++--- torch/_inductor/ir.py | 7 ++- torch/_inductor/lowering.py | 4 +- torch/_inductor/mkldnn_ir.py | 3 +- torch/_inductor/pattern_matcher.py | 11 ++-- torch/_inductor/runtime/triton_heuristics.py | 20 +++--- torch/_inductor/scheduler.py | 19 +++--- torch/_library/infer_schema.py | 10 ++- torch/_library/utils.py | 20 +++--- torch/_logging/structured.py | 21 +++---- torch/_prims/__init__.py | 9 +-- torch/_refs/__init__.py | 4 +- torch/_subclasses/fake_impls.py | 15 +++-- torch/_subclasses/fake_tensor.py | 33 +++++----- torch/ao/nn/quantizable/modules/rnn.py | 20 +++--- torch/ao/nn/quantized/modules/conv.py | 3 +- torch/ao/ns/fx/n_shadows_utils.py | 6 +- torch/ao/ns/fx/utils.py | 5 +- torch/ao/quantization/_equalize.py | 3 +- .../_common_operator_config_utils.py | 31 +++++----- .../backend_config/_qnnpack_pt2e.py | 14 ++--- .../quantization/backend_config/executorch.py | 27 ++++---- torch/ao/quantization/backend_config/utils.py | 18 +++--- torch/ao/quantization/fuse_modules.py | 4 +- .../ao/quantization/fuser_method_mappings.py | 4 +- .../_model_report/model_report_visualizer.py | 5 +- torch/ao/quantization/fx/convert.py | 7 +-- torch/ao/quantization/fx/fuse_handler.py | 7 +-- torch/ao/quantization/fx/prepare.py | 20 +++--- torch/ao/quantization/pt2e/graph_utils.py | 13 ++-- .../quantizer/xnnpack_quantizer.py | 8 +-- torch/autograd/gradcheck.py | 24 ++++---- torch/autograd/graph.py | 4 +- torch/autograd/profiler_util.py | 12 ++-- torch/cuda/__init__.py | 4 +- torch/cuda/random.py | 4 +- torch/distributed/_shard/sharded_optim/api.py | 5 +- .../_shard/sharded_tensor/_ops/tensor_ops.py | 7 ++- .../_shard/sharded_tensor/reshard.py | 6 +- .../benchmarks/benchmark_ddp_rpc.py | 3 +- .../distributed/checkpoint/planner_helpers.py | 6 +- torch/distributed/distributed_c10d.py | 10 ++- torch/distributed/fsdp/_runtime_utils.py | 9 +-- torch/distributed/nn/api/remote_module.py | 6 +- torch/distributed/optim/named_optimizer.py | 4 +- torch/distributed/optim/optimizer.py | 15 +++-- .../optim/zero_redundancy_optimizer.py | 16 ++--- torch/distributed/pipelining/microbatch.py | 11 ++-- torch/distributed/pipelining/schedules.py | 24 +++----- torch/distributed/tensor/_collective_utils.py | 3 +- .../tensor/_ops/_einsum_strategy.py | 4 +- torch/distributed/tensor/_ops/_tensor_ops.py | 19 +++--- torch/distributed/tensor/_ops/utils.py | 7 ++- torch/export/unflatten.py | 15 +++-- .../experimental/accelerator_partitioner.py | 9 +-- .../constraint_transformation.py | 20 ++---- torch/fx/experimental/partitioner_utils.py | 9 ++- torch/fx/experimental/symbolic_shapes.py | 7 +-- torch/fx/passes/pass_manager.py | 5 +- torch/jit/_recursive.py | 11 ++-- torch/jit/_script.py | 9 ++- torch/mtia/__init__.py | 4 +- torch/nn/parallel/_functions.py | 3 +- .../nn/utils/_expanded_weights/conv_utils.py | 4 +- torch/onnx/symbolic_helper.py | 8 ++- torch/onnx/symbolic_opset18.py | 5 +- torch/onnx/verification.py | 8 +-- torch/package/_directory_reader.py | 9 +-- torch/package/find_file_dependencies.py | 10 +-- torch/profiler/_pattern_matcher.py | 7 +-- torch/sparse/_triton_ops_meta.py | 3 +- .../_internal/common_methods_invocations.py | 8 +-- .../testing/_internal/common_quantization.py | 4 +- torch/testing/_internal/common_quantized.py | 4 +- torch/testing/_internal/common_utils.py | 7 +-- .../distributed/_tensor/common_dtensor.py | 11 ++-- .../_internal/distributed/distributed_test.py | 8 +-- .../distributed/multi_threaded_pg.py | 4 +- .../distributed/rpc/dist_autograd_test.py | 24 ++++---- .../rpc/examples/parameter_server_test.py | 6 +- .../reinforcement_learning_rpc_test.py | 18 +++--- .../_internal/distributed/rpc/jit/rpc_test.py | 14 ++--- .../_internal/distributed/rpc/rpc_test.py | 61 ++++++++----------- torch/testing/_internal/hypothesis_utils.py | 8 +-- .../_internal/opinfo/definitions/nested.py | 20 +++--- torch/utils/_device.py | 4 +- torch/utils/_pytree.py | 6 +- torch/utils/benchmark/utils/compare.py | 7 +-- torch/utils/data/_utils/worker.py | 4 +- .../data/datapipes/dataframe/datapipes.py | 8 ++- torch/utils/data/datapipes/iter/sharding.py | 9 +-- torch/utils/data/datapipes/map/grouping.py | 3 +- torch/utils/hipify/hipify_python.py | 14 ++--- torch/utils/hooks.py | 4 +- torch/utils/mobile_optimizer.py | 5 +- torch/utils/tensorboard/_onnx_graph.py | 4 +- torch/utils/tensorboard/_pytorch_graph.py | 29 +++++---- torch/xpu/__init__.py | 4 +- torch/xpu/random.py | 4 +- torchgen/dest/register_dispatch_key.py | 12 ++-- torchgen/gen.py | 8 ++- torchgen/model.py | 7 +-- .../operator_versions/gen_mobile_upgraders.py | 36 ++++++----- 133 files changed, 611 insertions(+), 761 deletions(-) diff --git a/torch/_dynamo/backends/distributed.py b/torch/_dynamo/backends/distributed.py index bb35a9117da..de833d24b44 100644 --- a/torch/_dynamo/backends/distributed.py +++ b/torch/_dynamo/backends/distributed.py @@ -67,8 +67,7 @@ def pretty_print_buckets(buckets: List[Bucket], bucket_bytes_cap: int): for idx, bucket in enumerate(reversed(buckets)): if len(bucket.params) > 0: rows.append((idx, bucket.size, bucket.params[0])) - for param in bucket.params[1:]: - rows.append((None, None, param)) + rows.extend((None, None, param) for param in bucket.params[1:]) if bucket.opcount_increased_to_capture_external_output > 0: extended_buckets.append( ( diff --git a/torch/_dynamo/bytecode_transformation.py b/torch/_dynamo/bytecode_transformation.py index 73054dfb740..868c30acf5a 100644 --- a/torch/_dynamo/bytecode_transformation.py +++ b/torch/_dynamo/bytecode_transformation.py @@ -1140,11 +1140,12 @@ def update_offsets(instructions) -> None: def debug_bytes(*args) -> str: index = range(max(map(len, args))) - result = [] - for arg in ( - [index] + list(args) + [[int(a != b) for a, b in zip(args[-1], args[-2])]] - ): - result.append(" ".join(f"{x:03}" for x in arg)) + result = [ + " ".join(f"{x:03}" for x in arg) + for arg in [index] + + list(args) + + [[int(a != b) for a, b in zip(args[-1], args[-2])]] + ] return "bytes mismatch\n" + "\n".join(result) diff --git a/torch/_dynamo/compiled_autograd.py b/torch/_dynamo/compiled_autograd.py index ced34311af6..24b9556f49c 100644 --- a/torch/_dynamo/compiled_autograd.py +++ b/torch/_dynamo/compiled_autograd.py @@ -478,10 +478,8 @@ class AutogradCompilerInstance: @staticmethod def get_all_nodes(args): - nodes = [] - for n in args: - if type(n) is torch.fx.Node: # filter out non-Node args, like None - nodes.append(n) + # filter out non-Node args, like None + nodes = [n for n in args if type(n) is torch.fx.Node] return nodes @staticmethod @@ -671,13 +669,15 @@ class AutogradCompilerInstance: input_nodes_and_users = [] input_nodes_and_users.extend(list(input_nodes)) for input_node in input_nodes: - for user in list(input_node.users.keys()): + input_nodes_and_users.extend( + user + for user in list(input_node.users.keys()) if not ( user.op == "call_function" and user.target == call_hook and node.kwargs.get("hook_type", None) == "post_hook" - ): - input_nodes_and_users.append(user) + ) + ) arg = max(input_nodes_and_users) # last input users if ( diff --git a/torch/_dynamo/guards.py b/torch/_dynamo/guards.py index f96aa4d0293..39b015e6df6 100644 --- a/torch/_dynamo/guards.py +++ b/torch/_dynamo/guards.py @@ -2526,7 +2526,6 @@ def make_torch_function_mode_stack_guard(intial_stack): def recompilation_reason_for_no_tensor_aliasing_guard(guard_manager, scope): - duplicate_tensors = [] global_scope = dict(guard_manager.global_scope) ids_to_source = collections.defaultdict(list) for tensor_source in guard_manager.no_tensor_aliasing_sources: # type: ignore[attr-defined] @@ -2534,9 +2533,9 @@ def recompilation_reason_for_no_tensor_aliasing_guard(guard_manager, scope): tensor_id = id(eval(tensor_source, global_scope, scope)) ids_to_source[tensor_id].append(tensor_source) - for key in ids_to_source: - if len(ids_to_source[key]) > 1: - duplicate_tensors.append(f"{ids_to_source[key]}") + duplicate_tensors = [ + f"{ids_to_source[key]}" for key in ids_to_source if len(ids_to_source[key]) > 1 + ] reason = ", ".join(duplicate_tensors) return [f"Duplicate tensors found: {reason}"] diff --git a/torch/_dynamo/output_graph.py b/torch/_dynamo/output_graph.py index 48e226d6b28..b2ba414461d 100644 --- a/torch/_dynamo/output_graph.py +++ b/torch/_dynamo/output_graph.py @@ -1463,9 +1463,7 @@ class OutputGraph: return compiled_fn def example_inputs(self) -> List[torch.Tensor]: - result = [] - for arg in self.graphargs: - result.append(arg.example) + result = [arg.example for arg in self.graphargs] return result def remove_unused_graphargs(self) -> None: diff --git a/torch/_dynamo/resume_execution.py b/torch/_dynamo/resume_execution.py index 3dc308dcbb9..65fa1174433 100644 --- a/torch/_dynamo/resume_execution.py +++ b/torch/_dynamo/resume_execution.py @@ -252,8 +252,7 @@ def _filter_iter(l1, l2, cond): def _load_tuple_and_call(tup): insts: List[Instruction] = [] _initial_push_null(insts) - for val in tup: - insts.append(create_instruction("LOAD_CONST", argval=val)) + insts.extend(create_instruction("LOAD_CONST", argval=val) for val in tup) insts.extend(create_call_function(len(tup), False)) return insts diff --git a/torch/_dynamo/testing.py b/torch/_dynamo/testing.py index ee7fb48d2ab..fc4cc36036e 100644 --- a/torch/_dynamo/testing.py +++ b/torch/_dynamo/testing.py @@ -95,9 +95,7 @@ def collect_results( results.append(buffers) for example in example_inputs: if isinstance(example, (tuple, list)): - for inp in example: - if isinstance(inp, torch.Tensor): - results.append(inp.grad) + results.extend(inp.grad for inp in example if isinstance(inp, torch.Tensor)) else: if isinstance(example, torch.Tensor): results.append(example.grad) diff --git a/torch/_dynamo/utils.py b/torch/_dynamo/utils.py index 345abcd76ff..9f2c7b0c9cf 100644 --- a/torch/_dynamo/utils.py +++ b/torch/_dynamo/utils.py @@ -1535,9 +1535,10 @@ def checkpoint_params(gm): rng_state = torch.clone(torch.random.get_rng_state()) if torch.cuda.is_available(): cuda_rng_state = torch.clone(torch.cuda.get_rng_state()) - saved_state = [] - for param in itertools.chain(gm.parameters(), gm.buffers()): - saved_state.append((param, param._version, torch.clone(param))) + saved_state = [ + (param, param._version, torch.clone(param)) + for param in itertools.chain(gm.parameters(), gm.buffers()) + ] def restore(): with torch.no_grad(): diff --git a/torch/_dynamo/variables/builtin.py b/torch/_dynamo/variables/builtin.py index ce0e4cf6a0a..b8ea2fdd7c4 100644 --- a/torch/_dynamo/variables/builtin.py +++ b/torch/_dynamo/variables/builtin.py @@ -1765,10 +1765,9 @@ class BuiltinVariable(VariableTracker): # tracked fakes to produce incorrect guards. This is sound because the TensorVariable # coming out of set_() below will be a new one, and get # installed in tracked fakes. - to_remove = [] - for tf in tx.output.tracked_fakes: - if tf.source == obj.source: - to_remove.append(tf) + to_remove = [ + tf for tf in tx.output.tracked_fakes if tf.source == obj.source + ] for tf in to_remove: tx.output.tracked_fakes.remove(tf) diff --git a/torch/_dynamo/variables/ctx_manager.py b/torch/_dynamo/variables/ctx_manager.py index b2978ab94e5..f87df341830 100644 --- a/torch/_dynamo/variables/ctx_manager.py +++ b/torch/_dynamo/variables/ctx_manager.py @@ -1026,17 +1026,16 @@ class SDPAKernelVariable(ContextWrappingVariable): @staticmethod def _backends_to_nodes(tx, backends): - nodes = [] - for backend in backends: - # convert to/from string in order to bake the backend into FX graph - nodes.append( - tx.output.create_node( - "call_function", - torch.nn.attention._backend_from_string, - (backend.name,), - {}, - ) + # convert to/from string in order to bake the backend into FX graph + nodes = [ + tx.output.create_node( + "call_function", + torch.nn.attention._backend_from_string, + (backend.name,), + {}, ) + for backend in backends + ] return nodes def enter(self, tx): diff --git a/torch/_dynamo/variables/iter.py b/torch/_dynamo/variables/iter.py index 694bfcc6c17..64449463003 100644 --- a/torch/_dynamo/variables/iter.py +++ b/torch/_dynamo/variables/iter.py @@ -48,9 +48,9 @@ class ItertoolsVariable(VariableTracker): and all(arg.has_unpack_var_sequence(tx) for arg in args) ): seqs = [arg.unpack_var_sequence(tx) for arg in args] - items = [] - for item in itertools.product(*seqs): - items.append(variables.TupleVariable(list(item))) + items = [ + variables.TupleVariable(list(item)) for item in itertools.product(*seqs) + ] return variables.ListIteratorVariable( items, mutation_type=ValueMutationNew() ) diff --git a/torch/_dynamo/variables/user_defined.py b/torch/_dynamo/variables/user_defined.py index a65d421cd86..985f7ff8c6f 100644 --- a/torch/_dynamo/variables/user_defined.py +++ b/torch/_dynamo/variables/user_defined.py @@ -776,14 +776,13 @@ class UserDefinedObjectVariable(UserDefinedVariable): ): assert self.source # OrderedDict, dict subtypes must always have source assert not (args or kwargs) - items = [] keys = self.call_method(tx, "keys", [], {}) - for key in keys.force_unpack_var_sequence(tx): - items.append( - TupleVariable( - [key, self.odict_getitem(tx, key)], - ) + items = [ + TupleVariable( + [key, self.odict_getitem(tx, key)], ) + for key in keys.force_unpack_var_sequence(tx) + ] tx.output.guard_on_key_order.add(self.source.name()) return TupleVariable(items) diff --git a/torch/_export/converter.py b/torch/_export/converter.py index 2d11f47b637..5d50f3e9208 100644 --- a/torch/_export/converter.py +++ b/torch/_export/converter.py @@ -833,9 +833,7 @@ class TS2FXGraphConverter: self._convert_prim_iterator(node) def _convert_prim_iterator(self, node: torch._C.Node): - output_list = [] - for inp in node.inputs(): - output_list.append(self.get_fx_value_by_ir_value(inp)) + output_list = [self.get_fx_value_by_ir_value(inp) for inp in node.inputs()] output_name = node.output().debugName() self.name_to_node[output_name] = output_list diff --git a/torch/_export/db/examples/static_for_loop.py b/torch/_export/db/examples/static_for_loop.py index 3924643bd94..aa62b86d16d 100644 --- a/torch/_export/db/examples/static_for_loop.py +++ b/torch/_export/db/examples/static_for_loop.py @@ -7,9 +7,8 @@ class StaticForLoop(torch.nn.Module): """ def forward(self, x): - ret = [] - for i in range(10): # constant - ret.append(i + x) + # constant + ret = [i + x for i in range(10)] return ret example_args = (torch.randn(3, 2),) diff --git a/torch/_export/serde/serialize.py b/torch/_export/serde/serialize.py index 6fa755f679c..68408ea19a5 100644 --- a/torch/_export/serde/serialize.py +++ b/torch/_export/serde/serialize.py @@ -1780,9 +1780,7 @@ class GraphModuleDeserializer(metaclass=Final): ) from e # Outputs: convert to a single `output` node. - outputs = [] - for output in serialized_graph.outputs: - outputs.append(self.deserialize_graph_output(output)) + outputs = [self.deserialize_graph_output(output) for output in serialized_graph.outputs] if serialized_graph.is_single_tensor_return: assert len(outputs) == 1 @@ -2149,9 +2147,7 @@ class GraphModuleDeserializer(metaclass=Final): if len(value) == 0: return [] elif typ_ == "as_tensors": - result = [] - for arg in value: - result.append(self.serialized_name_to_node[arg.name]) + result = [self.serialized_name_to_node[arg.name] for arg in value] return result elif typ_ in ("as_ints", "as_floats", "as_bools", "as_strings"): # convert from serialized.python.types.List to python list diff --git a/torch/_functorch/_aot_autograd/runtime_wrappers.py b/torch/_functorch/_aot_autograd/runtime_wrappers.py index d2d6691a12c..6756e62b536 100644 --- a/torch/_functorch/_aot_autograd/runtime_wrappers.py +++ b/torch/_functorch/_aot_autograd/runtime_wrappers.py @@ -1306,8 +1306,9 @@ def merge_view_inputs( mutated_input_info[inpt_idx].mutates_data for inpt_idx in aliased_input_indices ): - for curr_idx in aliased_input_indices: - other_args.append(fwd_inputs[curr_idx]) + other_args.extend( + fwd_inputs[curr_idx] for curr_idx in aliased_input_indices + ) continue # Here, we attempt to do a more complicated check to detect false aliasing @@ -1320,8 +1321,9 @@ def merge_view_inputs( fwd_inputs, aliased_input_indices ) if len(aliased_input_indices_no_false_sharing) <= 1: - for curr_idx in aliased_input_indices: - other_args.append(fwd_inputs[curr_idx]) + other_args.extend( + fwd_inputs[curr_idx] for curr_idx in aliased_input_indices + ) continue # We detected an input that was mutated, AND aliases with another input. diff --git a/torch/_higher_order_ops/map.py b/torch/_higher_order_ops/map.py index dbf07b24964..cbd7f9c45fd 100644 --- a/torch/_higher_order_ops/map.py +++ b/torch/_higher_order_ops/map.py @@ -217,9 +217,7 @@ def trace_map(proxy_mode, func_overload, f, xs, pos_args): @map_impl.py_impl(DispatchKey.CompositeExplicitAutograd) def map_dense(f, xs, pos_args): - pytrees = [] - for inp in _unstack_pytree(xs): - pytrees.append(f(*inp, *pos_args)) + pytrees = [f(*inp, *pos_args) for inp in _unstack_pytree(xs)] return _stack_pytree(pytrees) diff --git a/torch/_higher_order_ops/triton_kernel_wrap.py b/torch/_higher_order_ops/triton_kernel_wrap.py index 65f62ce7d9d..b4c9f80cf9f 100644 --- a/torch/_higher_order_ops/triton_kernel_wrap.py +++ b/torch/_higher_order_ops/triton_kernel_wrap.py @@ -543,8 +543,7 @@ def analyze_kernel_mutations( ) stack.extend(arg for arg, mutated in zip(op.args, mutations) if mutated) else: - for idx in MUTATION_OPS.get(op.name, []): - stack.append(op.args[idx]) + stack.extend(op.args[idx] for idx in MUTATION_OPS.get(op.name, [])) # The following is an iterative DFS algorithm mutated = [False] * num_args diff --git a/torch/_higher_order_ops/utils.py b/torch/_higher_order_ops/utils.py index 04068d6337a..86c552b5a39 100644 --- a/torch/_higher_order_ops/utils.py +++ b/torch/_higher_order_ops/utils.py @@ -388,9 +388,7 @@ def _unstack_pytree(xs): a = zip(*flat_xs) - pytrees = [] - for tuple in a: - pytrees.append(pytree.tree_unflatten(tuple, inspec)) + pytrees = [pytree.tree_unflatten(tuple, inspec) for tuple in a] return pytrees diff --git a/torch/_inductor/codecache.py b/torch/_inductor/codecache.py index 01e2eb4da9c..808246483e1 100644 --- a/torch/_inductor/codecache.py +++ b/torch/_inductor/codecache.py @@ -2102,13 +2102,14 @@ class AotCodeCompiler: aot_constants = struct.pack("qq", consts_size + 8, magic_number) consts_o = _compile_consts(aot_constants, sys.platform) - kernels_o = [] gpu_codecache: Union[ROCmCodeCache, CUDACodeCache] = ( ROCmCodeCache() if torch.version.hip else CUDACodeCache() ) - for entry in gpu_codecache.cache.values(): - if entry.output_path.endswith(".o"): - kernels_o.append(entry.output_path) + kernels_o = [ + entry.output_path + for entry in gpu_codecache.cache.values() + if entry.output_path.endswith(".o") + ] kernels_o = " ".join(kernels_o) output_name, output_dir = get_name_and_dir_from_output_file_path(output_so) diff --git a/torch/_inductor/codegen/cpp.py b/torch/_inductor/codegen/cpp.py index 0dbf21bcdcd..7de9a43dc92 100644 --- a/torch/_inductor/codegen/cpp.py +++ b/torch/_inductor/codegen/cpp.py @@ -4800,10 +4800,9 @@ class LoopLevel: def split_with_tiling(self, depth, factor): def clone_inner(): - inner = [] + inner: List[LoopLevel] = [] if self.inner: - for loop in self.inner: - inner.append(loop.clone()) + inner.extend(loop.clone() for loop in self.inner) return inner def do_split_with_tiling(): diff --git a/torch/_inductor/codegen/debug_utils.py b/torch/_inductor/codegen/debug_utils.py index f256a09bc6a..52b2ef55e3d 100644 --- a/torch/_inductor/codegen/debug_utils.py +++ b/torch/_inductor/codegen/debug_utils.py @@ -150,18 +150,16 @@ class DebugPrinterManager: # get the list of args_to_print_or_save # TODO: Find a more reliable way to detect kernel args types to print for extern kernel calls if kernel_type == "extern": - args_to_print_or_save_extern = [] - for arg in args_to_print_or_save: - if arg.startswith(("buf", "arg")): - args_to_print_or_save_extern.append(arg) + args_to_print_or_save_extern = [ + arg for arg in args_to_print_or_save if arg.startswith(("buf", "arg")) + ] self.args_to_print_or_save = args_to_print_or_save_extern elif kernel_type == "cpp": - args_to_print_or_save_cpp = [] - for arg in args_to_print_or_save: - if arg.startswith(("buf", "arg")): - args_to_print_or_save_cpp.append( - f"convert_arrayref_tensor_to_tensor({arg})" - ) + args_to_print_or_save_cpp = [ + f"convert_arrayref_tensor_to_tensor({arg})" + for arg in args_to_print_or_save + if arg.startswith(("buf", "arg")) + ] self.args_to_print_or_save = args_to_print_or_save_cpp else: self.args_to_print_or_save = args_to_print_or_save diff --git a/torch/_inductor/codegen/halide.py b/torch/_inductor/codegen/halide.py index 584c5a5393a..5efb93902da 100644 --- a/torch/_inductor/codegen/halide.py +++ b/torch/_inductor/codegen/halide.py @@ -1392,19 +1392,19 @@ class HalideKernel(SIMDKernel): result.append((call_str, arg)) if isinstance(arg, TensorArg): assert arg.offset == 0 and arg.alias_of is None - for alias in self.buffer_aliases.get(arg.name, ()): - result.append( - ( - None, - TensorArg( - alias, - arg.buffer, - arg.dtype, - arg.offset, - alias_of=arg.name, - ), - ) + result.extend( + ( + None, + TensorArg( + alias, + arg.buffer, + arg.dtype, + arg.offset, + alias_of=arg.name, + ), ) + for alias in self.buffer_aliases.get(arg.name, ()) + ) return result def halide_kernel_meta(self) -> HalideMeta: diff --git a/torch/_inductor/codegen/multi_kernel.py b/torch/_inductor/codegen/multi_kernel.py index b67bf3b59e1..639b6266795 100644 --- a/torch/_inductor/codegen/multi_kernel.py +++ b/torch/_inductor/codegen/multi_kernel.py @@ -72,10 +72,11 @@ def get_all_call_args(call_args_list, arg_types_list): def get_numel_argdefs(kernel): - numel_argdefs = [] - for tree in kernel.range_trees: - if tree.prefix != "r" or kernel.inside_reduction: - numel_argdefs.append(f"{tree.prefix}numel") + numel_argdefs = [ + f"{tree.prefix}numel" + for tree in kernel.range_trees + if tree.prefix != "r" or kernel.inside_reduction + ] return numel_argdefs diff --git a/torch/_inductor/codegen/triton_combo_kernel.py b/torch/_inductor/codegen/triton_combo_kernel.py index 684d115bad9..4c354506715 100644 --- a/torch/_inductor/codegen/triton_combo_kernel.py +++ b/torch/_inductor/codegen/triton_combo_kernel.py @@ -76,7 +76,7 @@ def _default_custom_combo_kernel_horizontal_partition( tilings = [node_info_map[n][1] for n in nodes] max_dims = max(len(t) for t in tilings) - nodes_per_ndim = [] + nodes_per_ndim: List[List[BaseSchedulerNode]] = [] for i in range(2, max_dims + 1): group_per_dim = [n for n, t in zip(nodes, tilings) if len(t) == i] reduction = [ @@ -111,12 +111,11 @@ def _default_custom_combo_kernel_horizontal_partition( len(large_pointwise), ) not_reduction = [n for n in not_reduction if n not in large_pointwise] - for node in large_pointwise: - nodes_per_ndim.append([node]) + nodes_per_ndim.extend([node] for node in large_pointwise) - for g in (not_reduction, short_reduction, long_reduction): - if g: - nodes_per_ndim.append(g) + nodes_per_ndim.extend( + g for g in (not_reduction, short_reduction, long_reduction) if g + ) assert sum(len(p) for p in nodes_per_ndim) == len(nodes) return nodes_per_ndim diff --git a/torch/_inductor/cpp_builder.py b/torch/_inductor/cpp_builder.py index a54571cf4ab..28c8440b811 100644 --- a/torch/_inductor/cpp_builder.py +++ b/torch/_inductor/cpp_builder.py @@ -288,8 +288,7 @@ def get_compiler_version_info(compiler: str) -> str: # =============================== cpp builder =============================== def _append_list(dest_list: List[str], src_list: List[str]) -> None: - for item in src_list: - dest_list.append(copy.deepcopy(item)) + dest_list.extend(copy.deepcopy(item) for item in src_list) def _remove_duplication_in_list(orig_list: List[str]) -> List[str]: @@ -742,12 +741,11 @@ def _setup_standard_sys_libs( def _get_build_args_of_chosen_isa(vec_isa: VecISA) -> Tuple[List[str], List[str]]: - macros = [] - build_flags = [] + macros: List[str] = [] + build_flags: List[str] = [] if vec_isa != invalid_vec_isa: # Add Windows support later. - for x in vec_isa.build_macro(): - macros.append(copy.deepcopy(x)) + macros.extend(copy.deepcopy(x) for x in vec_isa.build_macro()) build_flags = [vec_isa.build_arch_flags()] diff --git a/torch/_inductor/cpu_vec_isa.py b/torch/_inductor/cpu_vec_isa.py index c249c6311b7..1a55de75c18 100644 --- a/torch/_inductor/cpu_vec_isa.py +++ b/torch/_inductor/cpu_vec_isa.py @@ -398,9 +398,11 @@ def valid_vec_isa_list() -> List[VecISA]: arch value is x86_64 on Linux, and the value is AMD64 on Windows. """ _cpu_supported_x86_isa = x86_isa_checker() - for isa in supported_vec_isa_list: - if all(flag in _cpu_supported_x86_isa for flag in str(isa).split()) and isa: - isa_list.append(isa) + isa_list.extend( + isa + for isa in supported_vec_isa_list + if all(flag in _cpu_supported_x86_isa for flag in str(isa).split()) and isa + ) return isa_list diff --git a/torch/_inductor/cudagraph_trees.py b/torch/_inductor/cudagraph_trees.py index f1ab20fd6e0..fed24b5d69e 100644 --- a/torch/_inductor/cudagraph_trees.py +++ b/torch/_inductor/cudagraph_trees.py @@ -621,13 +621,12 @@ class CUDAWarmupNode: } def get_non_cudagraph_inps() -> List[weakref.ReferenceType[UntypedStorage]]: - non_cudagraph_inps = [] - for t in itertools.chain(new_inputs, self.wrapped_function.constants): - if ( - isinstance(t, torch.Tensor) - and t.untyped_storage().data_ptr() not in existing_path_data_ptrs - ): - non_cudagraph_inps.append(weakref.ref(t.untyped_storage())) + non_cudagraph_inps = [ + weakref.ref(t.untyped_storage()) + for t in itertools.chain(new_inputs, self.wrapped_function.constants) + if isinstance(t, torch.Tensor) + and t.untyped_storage().data_ptr() not in existing_path_data_ptrs + ] return non_cudagraph_inps non_cudagraph_inps_storages = get_non_cudagraph_inps() @@ -1709,12 +1708,10 @@ def get_block_addrs(pool_id: Tuple[int, int], live_only: bool = True) -> List[in def format_tb(frames: List[Any]) -> str: - formatted_traceback = [] - - for entry in frames: - formatted_traceback.append( - traceback.FrameSummary(entry["filename"], entry["line"], entry["name"]) - ) + formatted_traceback = [ + traceback.FrameSummary(entry["filename"], entry["line"], entry["name"]) + for entry in frames + ] return "".join(traceback.format_list(formatted_traceback)) diff --git a/torch/_inductor/dependencies.py b/torch/_inductor/dependencies.py index bcbeb8dfc73..c592d5b7348 100644 --- a/torch/_inductor/dependencies.py +++ b/torch/_inductor/dependencies.py @@ -127,9 +127,7 @@ class MemoryDep(Dep): return None stride_to_index = {s: i for i, s in enumerate(self_strides)} - order = [] - for s in other_strides: - order.append(stride_to_index[s]) + order = [stride_to_index[s] for s in other_strides] assert set(order) == set(range(0, self.num_vars)) return order @@ -547,9 +545,7 @@ def var_builder(prefix: str) -> Tuple[VarRanges, Callable[[sympy.Expr], sympy.Sy def index_vars_no_squeeze(*argsizes: Tuple[sympy.Expr, ...], prefix: str): var_ranges, add_var = var_builder(prefix) - args: List[List[sympy.Symbol]] = [] - for size in argsizes: - args.append(list(map(add_var, size))) + args: List[List[sympy.Symbol]] = [list(map(add_var, size)) for size in argsizes] return args, var_ranges diff --git a/torch/_inductor/fx_passes/split_cat.py b/torch/_inductor/fx_passes/split_cat.py index d7805732df0..5b84e445694 100644 --- a/torch/_inductor/fx_passes/split_cat.py +++ b/torch/_inductor/fx_passes/split_cat.py @@ -1120,9 +1120,9 @@ class UnbindCatRemover(SplitCatSimplifier): return # we need to check if the getitem indices from unbind are consecutive and all go to the same cat node # before we do the unbind remove, otherwise it will hit the error when we unbind part of them - getitem_indices = [] - for getitem_node in unbind_node.users.keys(): - getitem_indices.append(getitem_node.args[1]) + getitem_indices = [ + getitem_node.args[1] for getitem_node in unbind_node.users.keys() + ] if not is_sorted_and_consecutive(getitem_indices) or len( # type: ignore[arg-type] getitem_indices ) != len( @@ -1497,9 +1497,8 @@ def merge_getitem_cat(match: Match, split_sections: List[int], dim: int): ): continue # find the index of getitems to be cated/stacked - indices = [] - for arg in cat_user.args[0]: # type: ignore[union-attr] - indices.append(arg.args[1]) # type: ignore[union-attr] + # type: ignore[union-attr] + indices = [arg.args[1] for arg in cat_user.args[0]] # type: ignore[union-attr] # the gettitems to be merged must be consecutive, otherwise # returned sliced tensor could be wrong if not is_sorted_and_consecutive(indices): diff --git a/torch/_inductor/graph.py b/torch/_inductor/graph.py index 792ffa5c0f1..139c9f26eb2 100644 --- a/torch/_inductor/graph.py +++ b/torch/_inductor/graph.py @@ -1657,15 +1657,14 @@ class GraphLowering(torch.fx.Interpreter): new_unbacked_defs |= op.get_unbacked_symbol_defs() def format_new_defs() -> str: - r = [] - for buf in self.buffers[buffer_watermark:]: - r.append( - f"unbacked_symbol_defs={buf.get_unbacked_symbol_defs()} in:\n{buf}\n" - ) - for op in self.operations[operation_watermark:]: - r.append( - f"unbacked_symbol_defs={op.get_unbacked_symbol_defs()} in:\n{op}\n" - ) + r = [ + f"unbacked_symbol_defs={buf.get_unbacked_symbol_defs()} in:\n{buf}\n" + for buf in self.buffers[buffer_watermark:] + ] + r.extend( + f"unbacked_symbol_defs={op.get_unbacked_symbol_defs()} in:\n{op}\n" + for op in self.operations[operation_watermark:] + ) return "***\n".join(r) if n.op != "placeholder": diff --git a/torch/_inductor/ir.py b/torch/_inductor/ir.py index a5b45192ca0..e82c28f2449 100644 --- a/torch/_inductor/ir.py +++ b/torch/_inductor/ir.py @@ -5484,13 +5484,14 @@ class UserDefinedTritonKernel(ExternKernel): kernel = kernel_side_table.get_kernel(self.kernel_idx) configs = [] - restore_value_args = [] + restore_value_args: List[str] = [] if isinstance(kernel, Autotuner): # https://github.com/triton-lang/triton/pull/5083 # changes kernel.restore_idx to kernel.restore_value if hasattr(kernel, "restore_idx"): - for i in kernel.restore_idx: - restore_value_args.append(kernel.fn.arg_names[i]) + restore_value_args.extend( + kernel.fn.arg_names[i] for i in kernel.restore_idx + ) else: assert hasattr(kernel, "restore_value") restore_value_args.extend(kernel.restore_value) diff --git a/torch/_inductor/lowering.py b/torch/_inductor/lowering.py index 4ecbcff73e2..12cf8521fc1 100644 --- a/torch/_inductor/lowering.py +++ b/torch/_inductor/lowering.py @@ -1691,9 +1691,7 @@ def split_with_sizes(x, sizes, dim=0): def unbind(x, dim=0): dim = _validate_dim(x, dim, 0) x_size = V.graph.sizevars.evaluate_static_shape(x.get_size()[dim]) - result = [] - for i in range(x_size): - result.append(select(x, dim, i)) + result = [select(x, dim, i) for i in range(x_size)] return result diff --git a/torch/_inductor/mkldnn_ir.py b/torch/_inductor/mkldnn_ir.py index 1c50c7472bf..3c4807de424 100644 --- a/torch/_inductor/mkldnn_ir.py +++ b/torch/_inductor/mkldnn_ir.py @@ -87,8 +87,7 @@ def _prepare_convolution_fusion_create( weight_size = [] weight_size.append(prepacked_weight_size[1] * groups) weight_size.append(prepacked_weight_size[0] / groups) - for d in range(2, dim): - weight_size.append(prepacked_weight_size[d]) + weight_size.extend(prepacked_weight_size[d] for d in range(2, dim)) else: weight_size = prepacked_weight.transpose(0, 1).size() return weight_size diff --git a/torch/_inductor/pattern_matcher.py b/torch/_inductor/pattern_matcher.py index 436ac963c2e..f4426b6bc8e 100644 --- a/torch/_inductor/pattern_matcher.py +++ b/torch/_inductor/pattern_matcher.py @@ -952,9 +952,10 @@ class PatternPrettyPrinter: assert hasattr(obj, "pretty_print") out_str = obj.pretty_print(pp=pp) - output = [] - for key in pp.memoized_objs_names: - output.append(f"{pp.memoized_objs_names[key]} = {pp.memoized_objs_pp[key]}") + output = [ + f"{pp.memoized_objs_names[key]} = {pp.memoized_objs_pp[key]}" + for key in pp.memoized_objs_names + ] output.append(f"{output_name} = {out_str}") @@ -1361,9 +1362,7 @@ def register_replacement( return False def normalize_args(**kwargs: Any) -> List[Any]: - args = [] - for name in argnames_static: - args.append(kwargs.pop(name)) + args = [kwargs.pop(name) for name in argnames_static] for i in range(1, len(kwargs) + 1): if f"tangents_{i}" not in kwargs: break diff --git a/torch/_inductor/runtime/triton_heuristics.py b/torch/_inductor/runtime/triton_heuristics.py index 5face85e397..4b8a73118bb 100644 --- a/torch/_inductor/runtime/triton_heuristics.py +++ b/torch/_inductor/runtime/triton_heuristics.py @@ -120,7 +120,7 @@ def autotune_hints_to_configs( configs to try. """ xyz_options: Tuple[Tuple[int, Optional[int], Optional[int]], ...] - configs = [] + configs: List[Config] = [] warp_size = device_props.warp_size # CPU target has no concept of "warp" if warp_size is None: @@ -138,16 +138,16 @@ def autotune_hints_to_configs( (1, block_size // 4, 1), (1, 1, block_size // 4), ) - for xyz in xyz_options: - configs.append( - triton_config( - size_hints, - *xyz, - num_elements_per_warp=( - device_props.warp_size if device_props.warp_size else 32 - ), - ) + configs.extend( + triton_config( + size_hints, + *xyz, + num_elements_per_warp=( + device_props.warp_size if device_props.warp_size else 32 + ), ) + for xyz in xyz_options + ) return configs diff --git a/torch/_inductor/scheduler.py b/torch/_inductor/scheduler.py index 9669c152acb..9cb29462801 100644 --- a/torch/_inductor/scheduler.py +++ b/torch/_inductor/scheduler.py @@ -308,9 +308,13 @@ class BaseSchedulerNode: dep = deps.pop() used_names.add(dep) if V.graph.name_to_buffer.get(dep): - for alias in V.graph.name_to_buffer[dep].get_inputs_that_alias_output(): - if alias not in used_names: - deps.append(alias) + deps.extend( + alias + for alias in V.graph.name_to_buffer[ + dep + ].get_inputs_that_alias_output() + if alias not in used_names + ) return used_names def prune_deps(self) -> None: @@ -3324,10 +3328,11 @@ class Scheduler: node1 = node2 node2 = tmp - deps = [] - for dep in node1.read_writes.reads | node1.read_writes.writes: - if dep in node2.read_writes.reads or dep in node2.read_writes.writes: - deps.append(dep) + deps = [ + dep + for dep in node1.read_writes.reads | node1.read_writes.writes + if dep in node2.read_writes.reads or dep in node2.read_writes.writes + ] return sum(self.dep_size_hint(dep) for dep in deps) diff --git a/torch/_library/infer_schema.py b/torch/_library/infer_schema.py index 9845a648748..412e7ebe6f5 100644 --- a/torch/_library/infer_schema.py +++ b/torch/_library/infer_schema.py @@ -176,14 +176,12 @@ def derived_types( ] if list_base: - for seq_typ in derived_seq_types(base_type): - result.append((seq_typ, f"{cpp_type}[]")) # type: ignore[valid-type] + result.extend((seq_typ, f"{cpp_type}[]") for seq_typ in derived_seq_types(base_type)) # type: ignore[valid-type] if optional_base_list: - for seq_typ in derived_seq_types(typing.Optional[base_type]): - result.append((seq_typ, f"{cpp_type}?[]")) # type: ignore[valid-type] + result.extend((seq_typ, f"{cpp_type}?[]") for seq_typ in derived_seq_types(typing.Optional[base_type])) # type: ignore[valid-type] if optional_list_base: - for seq_typ in derived_seq_types(base_type): # type: ignore[valid-type] - result.append((typing.Optional[seq_typ], f"{cpp_type}[]?")) # type: ignore[valid-type] + # type: ignore[valid-type] + result.extend((typing.Optional[seq_typ], f"{cpp_type}[]?") for seq_typ in derived_seq_types(base_type)) # type: ignore[valid-type] return result diff --git a/torch/_library/utils.py b/torch/_library/utils.py index 9c12ec9ebb0..45c8208d9e3 100644 --- a/torch/_library/utils.py +++ b/torch/_library/utils.py @@ -279,17 +279,17 @@ def requires_set_python_module() -> bool: def handle_dispatch_mode(curr_mode, op_overload, *args, **kwargs): assert isinstance(curr_mode, torch.utils._python_dispatch.TorchDispatchMode) - overload_types = [] args_flattened, _ = torch.utils._pytree.tree_flatten((args, kwargs.values())) - for a in args_flattened: - # TODO: need to double check the semantics of the "types" argument to torch_dispatch. - # It's generated in PyInterpreter.cpp, but seems to be generated in two places, - # where in one case we only include tensors with the python key, and in another - # we include **all** tensors. - if isinstance(a, torch.Tensor) and torch._C._dispatch_keys(a).has( - torch._C.DispatchKey.Python - ): - overload_types.append(type(a)) + # TODO: need to double check the semantics of the "types" argument to torch_dispatch. + # It's generated in PyInterpreter.cpp, but seems to be generated in two places, + # where in one case we only include tensors with the python key, and in another + # we include **all** tensors. + overload_types = [ + type(a) + for a in args_flattened + if isinstance(a, torch.Tensor) + and torch._C._dispatch_keys(a).has(torch._C.DispatchKey.Python) + ] # TODO: check that I got these args correct (in C++, we pass in "0000"??) return curr_mode.__torch_dispatch__(op_overload, overload_types, args, kwargs) diff --git a/torch/_logging/structured.py b/torch/_logging/structured.py index 2a99a7a8496..40ff76f9cac 100644 --- a/torch/_logging/structured.py +++ b/torch/_logging/structured.py @@ -43,15 +43,14 @@ def dump_file(filename: str) -> None: def from_traceback(tb: Sequence[traceback.FrameSummary]) -> List[Dict[str, Any]]: - r = [] - for frame in tb: - # dict naming convention here coincides with - # python/combined_traceback.cpp - r.append( - { - "line": frame.lineno, - "name": frame.name, - "filename": intern_string(frame.filename), - } - ) + # dict naming convention here coincides with + # python/combined_traceback.cpp + r = [ + { + "line": frame.lineno, + "name": frame.name, + "filename": intern_string(frame.filename), + } + for frame in tb + ] return r diff --git a/torch/_prims/__init__.py b/torch/_prims/__init__.py index db1569a0477..9f0434c4d6c 100644 --- a/torch/_prims/__init__.py +++ b/torch/_prims/__init__.py @@ -312,10 +312,11 @@ def _make_prim( prim_autograd_impl.impl(name, _autograd_impl) prim_meta_impl.impl(name, meta) else: - mutates_args = [] - for arg in cpp_schema.arguments: - if arg.alias_info is not None and arg.alias_info.is_write: - mutates_args.append(arg.name) + mutates_args = [ + arg.name + for arg in cpp_schema.arguments + if arg.alias_info is not None and arg.alias_info.is_write + ] prim_def = torch.library.custom_op( "prims::" + name, _prim_impl, diff --git a/torch/_refs/__init__.py b/torch/_refs/__init__.py index 117d4acccd4..c71e9a483bc 100644 --- a/torch/_refs/__init__.py +++ b/torch/_refs/__init__.py @@ -3055,9 +3055,7 @@ def chunk(a: TensorLikeType, chunks: int, dim: int = 0) -> Tuple[TensorLikeType, full_chunks = math.floor(length / chunk_size) tail_chunk_size = length % chunk_size - result = [] - for i in range(full_chunks): - result.append(narrow(a, dim, i * chunk_size, chunk_size)) + result = [narrow(a, dim, i * chunk_size, chunk_size) for i in range(full_chunks)] if tail_chunk_size != 0: result.append(narrow(a, dim, full_chunks * chunk_size, tail_chunk_size)) diff --git a/torch/_subclasses/fake_impls.py b/torch/_subclasses/fake_impls.py index 358ff2419f6..9772493b364 100644 --- a/torch/_subclasses/fake_impls.py +++ b/torch/_subclasses/fake_impls.py @@ -585,14 +585,13 @@ def has_meta(func): lambda func: is_builtin(func) and "foreach" in func.name() and has_meta(func) ) def foreach_run_and_map_input_device(fake_mode, func, *args, **kwargs): - tensor_lists = [] - for arg in itertools.chain(args, kwargs.values()): - if ( - isinstance(arg, (list, tuple)) - and len(arg) - and isinstance(arg[0], torch.Tensor) - ): - tensor_lists.append(arg) + tensor_lists = [ + arg + for arg in itertools.chain(args, kwargs.values()) + if isinstance(arg, (list, tuple)) + and len(arg) + and isinstance(arg[0], torch.Tensor) + ] try: with in_kernel_invocation_manager(fake_mode): diff --git a/torch/_subclasses/fake_tensor.py b/torch/_subclasses/fake_tensor.py index 985b274cf2b..4618cc0cb52 100644 --- a/torch/_subclasses/fake_tensor.py +++ b/torch/_subclasses/fake_tensor.py @@ -171,8 +171,7 @@ def get_plain_tensors( continue inner_keys, _ = curr.__tensor_flatten__() - for key in reversed(inner_keys): - todo.append(getattr(curr, key)) + todo.extend(getattr(curr, key) for key in reversed(inner_keys)) return out @@ -1629,13 +1628,12 @@ class FakeTensorMode(TorchDispatchMode): ) if isinstance(output, tuple): - output_infos = [] - for out_elem in output: - output_infos.append( - self._get_output_info_for_cache_entry( - state, key, func, args, kwargs, out_elem - ) + output_infos = [ + self._get_output_info_for_cache_entry( + state, key, func, args, kwargs, out_elem ) + for out_elem in output + ] return _DispatchCacheEntry( output_infos=tuple(output_infos), is_output_tuple=True ) @@ -1727,17 +1725,16 @@ class FakeTensorMode(TorchDispatchMode): """ if entry.is_output_tuple: - outputs = [] - for output_info in entry.output_infos: - outputs.append( - self._get_output_tensor_from_cache_entry( - state, - output_info, - key, - func, - args, - ) + outputs = [ + self._get_output_tensor_from_cache_entry( + state, + output_info, + key, + func, + args, ) + for output_info in entry.output_infos + ] return tuple(outputs) else: return self._get_output_tensor_from_cache_entry( diff --git a/torch/ao/nn/quantizable/modules/rnn.py b/torch/ao/nn/quantizable/modules/rnn.py index 7a520f5b250..9eeff0e8e9e 100644 --- a/torch/ao/nn/quantizable/modules/rnn.py +++ b/torch/ao/nn/quantizable/modules/rnn.py @@ -389,17 +389,17 @@ class LSTM(torch.nn.Module): **factory_kwargs, ) ] - for layer in range(1, num_layers): - layers.append( - _LSTMLayer( - self.hidden_size, - self.hidden_size, - self.bias, - batch_first=False, - bidirectional=self.bidirectional, - **factory_kwargs, - ) + layers.extend( + _LSTMLayer( + self.hidden_size, + self.hidden_size, + self.bias, + batch_first=False, + bidirectional=self.bidirectional, + **factory_kwargs, ) + for layer in range(1, num_layers) + ) self.layers = torch.nn.ModuleList(layers) def forward(self, x: Tensor, hidden: Optional[Tuple[Tensor, Tensor]] = None): diff --git a/torch/ao/nn/quantized/modules/conv.py b/torch/ao/nn/quantized/modules/conv.py index 0ef2f3af1dc..53bd316450b 100644 --- a/torch/ao/nn/quantized/modules/conv.py +++ b/torch/ao/nn/quantized/modules/conv.py @@ -32,8 +32,7 @@ def _reverse_repeat_padding(padding: List[int]) -> List[int]: _reversed_padding_repeated_twice: List[int] = [] N = len(padding) for idx in range(N): - for _ in range(2): - _reversed_padding_repeated_twice.append(padding[N - idx - 1]) + _reversed_padding_repeated_twice.extend(padding[N - idx - 1] for _ in range(2)) return _reversed_padding_repeated_twice diff --git a/torch/ao/ns/fx/n_shadows_utils.py b/torch/ao/ns/fx/n_shadows_utils.py index 9909da2a3af..d5b92e0e361 100644 --- a/torch/ao/ns/fx/n_shadows_utils.py +++ b/torch/ao/ns/fx/n_shadows_utils.py @@ -568,9 +568,9 @@ def create_one_transformed_and_logged_copy_of_subgraph( and len(arg) and isinstance(arg[0], Node) ): - for inner_arg in arg: - if isinstance(inner_arg, Node): - new_args.append(inner_arg) + new_args.extend( + inner_arg for inner_arg in arg if isinstance(inner_arg, Node) + ) new_kwargs = {} for name, old_kwarg in first_node.kwargs.items(): diff --git a/torch/ao/ns/fx/utils.py b/torch/ao/ns/fx/utils.py index 03d9a63560d..7348b66dfaa 100644 --- a/torch/ao/ns/fx/utils.py +++ b/torch/ao/ns/fx/utils.py @@ -312,10 +312,7 @@ def get_arg_indices_of_inputs_to_log(node: Node) -> List[int]: node.target in (torch.add, torch.ops.quantized.add, operator.add) or node.target in (torch.mul, torch.ops.quantized.mul, operator.mul) ): - result = [] - for i in range(2): - if type(node.args[i]) == Node: - result.append(i) + result = [i for i in range(2) if type(node.args[i]) == Node] return result return [0] diff --git a/torch/ao/quantization/_equalize.py b/torch/ao/quantization/_equalize.py index 57a4cfdead2..a06078b1dcb 100644 --- a/torch/ao/quantization/_equalize.py +++ b/torch/ao/quantization/_equalize.py @@ -191,8 +191,7 @@ def expand_groups_in_paired_modules_list(paired_modules_list): elif len(group) == 2: new_list.append(group) elif len(group) > 2: - for i in range(len(group) - 1): - new_list.append([group[i], group[i + 1]]) + new_list.extend([group[i], group[i + 1]] for i in range(len(group) - 1)) return new_list diff --git a/torch/ao/quantization/backend_config/_common_operator_config_utils.py b/torch/ao/quantization/backend_config/_common_operator_config_utils.py index c28c8795667..ceafcde0193 100644 --- a/torch/ao/quantization/backend_config/_common_operator_config_utils.py +++ b/torch/ao/quantization/backend_config/_common_operator_config_utils.py @@ -155,14 +155,14 @@ def _get_binary_op_configs( (op_with_quantized_bop_scalar_variant, torch.relu), op_with_quantized_bop_scalar_variant, ] - for bop_pattern in bop_patterns: - binary_op_configs.append( - BackendPatternConfig(bop_pattern) - .set_dtype_configs(dtype_configs) # noqa: E131 - ._set_num_tensor_args_to_observation_type( - num_tensor_args_to_observation_type_mapping - ) + binary_op_configs.extend( + BackendPatternConfig(bop_pattern) + .set_dtype_configs(dtype_configs) # noqa: E131 + ._set_num_tensor_args_to_observation_type( + num_tensor_args_to_observation_type_mapping ) + for bop_pattern in bop_patterns + ) # matmul binary_op_configs.append( BackendPatternConfig(torch.matmul).set_dtype_configs( @@ -502,7 +502,6 @@ def _get_ln_configs(dtype_configs: List[DTypeConfig]) -> List[BackendPatternConf def _get_default_op_configs( dtype_configs: List[DTypeConfig], ) -> List[BackendPatternConfig]: - configs = [] default_ops = [ torch.nn.ELU, torch.nn.LeakyReLU, @@ -517,14 +516,14 @@ def _get_default_op_configs( torch.nn.functional.leaky_relu, torch.nn.functional.dropout, ] - for op in default_ops: - configs.append( - BackendPatternConfig(op) - .set_observation_type( - ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT - ) # noqa: E131 - .set_dtype_configs(dtype_configs) - ) + configs = [ + BackendPatternConfig(op) + .set_observation_type( + ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT + ) # noqa: E131 + .set_dtype_configs(dtype_configs) + for op in default_ops + ] configs.append( BackendPatternConfig(torch.nn.functional.group_norm) diff --git a/torch/ao/quantization/backend_config/_qnnpack_pt2e.py b/torch/ao/quantization/backend_config/_qnnpack_pt2e.py index fdfeb4ab36e..1dc7954b1c0 100644 --- a/torch/ao/quantization/backend_config/_qnnpack_pt2e.py +++ b/torch/ao/quantization/backend_config/_qnnpack_pt2e.py @@ -159,14 +159,14 @@ def get_binary_op_configs(): # TODO: remove when functionalization is supported in pt2_mode (op_with_quantized_bop_scalar_variant, torch.ops.aten.relu_.default), ] - for bop_pattern in bop_patterns: - binary_op_configs.append( - BackendPatternConfig(bop_pattern) - .set_dtype_configs(dtype_configs) # noqa: E131 - ._set_num_tensor_args_to_observation_type( - num_tensor_args_to_observation_type_mapping - ) + binary_op_configs.extend( + BackendPatternConfig(bop_pattern) + .set_dtype_configs(dtype_configs) # noqa: E131 + ._set_num_tensor_args_to_observation_type( + num_tensor_args_to_observation_type_mapping ) + for bop_pattern in bop_patterns + ) return binary_op_configs diff --git a/torch/ao/quantization/backend_config/executorch.py b/torch/ao/quantization/backend_config/executorch.py index 5ad423c6d26..4cb97eb56bb 100644 --- a/torch/ao/quantization/backend_config/executorch.py +++ b/torch/ao/quantization/backend_config/executorch.py @@ -325,14 +325,14 @@ def _get_binary_ops_configs() -> List[BackendPatternConfig]: (op, torch.relu), op, ] - for bop_pattern in bop_patterns: - binary_op_configs.append( - BackendPatternConfig(bop_pattern) - .set_dtype_configs(dtype_configs) # noqa: E131 - ._set_num_tensor_args_to_observation_type( - num_tensor_args_to_observation_type_mapping - ) + binary_op_configs.extend( + BackendPatternConfig(bop_pattern) + .set_dtype_configs(dtype_configs) # noqa: E131 + ._set_num_tensor_args_to_observation_type( + num_tensor_args_to_observation_type_mapping ) + for bop_pattern in bop_patterns + ) return binary_op_configs @@ -385,13 +385,12 @@ def _get_share_qparams_ops_configs() -> List[BackendPatternConfig]: "squeeze_", "leaky_relu", ] - share_qparams_op_configs: List[BackendPatternConfig] = [] - for op in share_qparams_ops: - share_qparams_op_configs.append( - BackendPatternConfig(op) - .set_observation_type(observation_type) # noqa: E131 - .set_dtype_configs(dtype_configs) - ) + share_qparams_op_configs: List[BackendPatternConfig] = [ + BackendPatternConfig(op) + .set_observation_type(observation_type) # noqa: E131 + .set_dtype_configs(dtype_configs) + for op in share_qparams_ops + ] return share_qparams_op_configs diff --git a/torch/ao/quantization/backend_config/utils.py b/torch/ao/quantization/backend_config/utils.py index 957816ab590..2cca4f5aaad 100644 --- a/torch/ao/quantization/backend_config/utils.py +++ b/torch/ao/quantization/backend_config/utils.py @@ -36,18 +36,20 @@ def get_pattern_to_dtype_configs( def get_qat_module_classes(backend_config: BackendConfig) -> Tuple[type, ...]: - qat_module_classes = [] - for config in backend_config.configs: - if config.qat_module is not None: - qat_module_classes.append(config.qat_module) + qat_module_classes = [ + config.qat_module + for config in backend_config.configs + if config.qat_module is not None + ] return tuple(set(qat_module_classes)) def get_fused_module_classes(backend_config: BackendConfig) -> Tuple[type, ...]: - fused_module_classes = [] - for config in backend_config.configs: - if config.fused_module is not None: - fused_module_classes.append(config.fused_module) + fused_module_classes = [ + config.fused_module + for config in backend_config.configs + if config.fused_module is not None + ] return tuple(set(fused_module_classes)) diff --git a/torch/ao/quantization/fuse_modules.py b/torch/ao/quantization/fuse_modules.py index c16e82b1e0a..f3b1da3858b 100644 --- a/torch/ao/quantization/fuse_modules.py +++ b/torch/ao/quantization/fuse_modules.py @@ -92,9 +92,7 @@ def _fuse_modules_helper( additional_fuser_method_mapping = fuse_custom_config_dict.get( "additional_fuser_method_mapping", {} ) - mod_list = [] - for item in modules_to_fuse: - mod_list.append(_get_module(model, item)) + mod_list = [_get_module(model, item) for item in modules_to_fuse] # Fuse list of modules new_mod_list = fuser_func(mod_list, is_qat, additional_fuser_method_mapping) diff --git a/torch/ao/quantization/fuser_method_mappings.py b/torch/ao/quantization/fuser_method_mappings.py index 40f305919fb..08279aa5694 100644 --- a/torch/ao/quantization/fuser_method_mappings.py +++ b/torch/ao/quantization/fuser_method_mappings.py @@ -266,9 +266,7 @@ def _get_valid_patterns(op_pattern): """ result: List[Any] if isinstance(op_pattern, (tuple, list)): - sub_combs = [] - for sub_pattern in op_pattern: - sub_combs.append(_get_valid_patterns(sub_pattern)) + sub_combs = [_get_valid_patterns(sub_pattern) for sub_pattern in op_pattern] result = list(itertools.product(*sub_combs)) else: result = [op_pattern, MatchAllNode] diff --git a/torch/ao/quantization/fx/_model_report/model_report_visualizer.py b/torch/ao/quantization/fx/_model_report/model_report_visualizer.py index 712b0669ee3..b0589c05a0e 100644 --- a/torch/ao/quantization/fx/_model_report/model_report_visualizer.py +++ b/torch/ao/quantization/fx/_model_report/model_report_visualizer.py @@ -527,8 +527,9 @@ class ModelReportVisualizer: # gather the x_data and multiple y_data # calculate the number of channels num_channels: int = max(row[self.CHANNEL_NUM_INDEX] for row in table) + 1 - for channel in range(num_channels): - y_data.append([]) # separate data list per channel + y_data.extend( + [] for channel in range(num_channels) + ) # separate data list per channel for table_row_num, row in enumerate(table): # get x_value to append diff --git a/torch/ao/quantization/fx/convert.py b/torch/ao/quantization/fx/convert.py index 454e316df6d..d062e69156f 100644 --- a/torch/ao/quantization/fx/convert.py +++ b/torch/ao/quantization/fx/convert.py @@ -1116,10 +1116,9 @@ def convert( # for dynamic quant ops or weight only quant ops _run_weight_observers(model, backend_config) - graph_inputs: List[str] = [] - for node in model.graph.nodes: - if node.op == "placeholder": - graph_inputs.append(node.name) + graph_inputs: List[str] = [ + node.name for node in model.graph.nodes if node.op == "placeholder" + ] # additional state to override inputs to be quantized, if specified # by the user diff --git a/torch/ao/quantization/fx/fuse_handler.py b/torch/ao/quantization/fx/fuse_handler.py index 75e7f3cb9df..78e556779b9 100644 --- a/torch/ao/quantization/fx/fuse_handler.py +++ b/torch/ao/quantization/fx/fuse_handler.py @@ -78,8 +78,7 @@ class DefaultFuseHandler(FuseHandler): n, *args = pattern modules: List[torch.nn.Module] = [] modules.append(get_modules(n)) - for a in args: - modules.append(get_modules(a)) + modules.extend(get_modules(a) for a in args) return tuple(modules) else: n = pattern @@ -111,9 +110,7 @@ class DefaultFuseHandler(FuseHandler): # as input fused_module = fuser_method(is_qat, *matched_modules) setattr(named_modules[module_parent_name], module_name, fused_module) - extra_args = [] - for input in extra_inputs: - extra_args.append(load_arg(input)) + extra_args = [load_arg(input) for input in extra_inputs] node = fused_graph.node_copy(root_node, load_arg) args = list(node.args) args.extend(extra_args) diff --git a/torch/ao/quantization/fx/prepare.py b/torch/ao/quantization/fx/prepare.py index 8b3073f5e31..cef69de8290 100644 --- a/torch/ao/quantization/fx/prepare.py +++ b/torch/ao/quantization/fx/prepare.py @@ -1219,13 +1219,12 @@ def _maybe_insert_observers_before_graph_output( else: return maybe_node elif isinstance(maybe_node, (list, tuple)): - results = [] - for inner_node in maybe_node: - results.append( - _recursive_maybe_replace_node_with_obs( - inner_node, model, named_modules, graph - ) + results = [ + _recursive_maybe_replace_node_with_obs( + inner_node, model, named_modules, graph ) + for inner_node in maybe_node + ] if isinstance(maybe_node, list): return results else: @@ -1244,11 +1243,10 @@ def _maybe_insert_observers_before_graph_output( "Unhandled type for returned node:", maybe_node ) - new_args = [] - for old_arg in graph_output_node.args: - new_args.append( - _recursive_maybe_replace_node_with_obs(old_arg, model, named_modules, graph) - ) + new_args = [ + _recursive_maybe_replace_node_with_obs(old_arg, model, named_modules, graph) + for old_arg in graph_output_node.args + ] graph_output_node.args = tuple(new_args) # type: ignore[assignment] diff --git a/torch/ao/quantization/pt2e/graph_utils.py b/torch/ao/quantization/pt2e/graph_utils.py index 34561416151..b37d853f378 100644 --- a/torch/ao/quantization/pt2e/graph_utils.py +++ b/torch/ao/quantization/pt2e/graph_utils.py @@ -1,7 +1,7 @@ # mypy: allow-untyped-defs import itertools import operator -from typing import Any, Callable, List, Optional, OrderedDict, Set +from typing import Any, Callable, List, Optional, OrderedDict, Sequence, Set import torch from torch.fx import Node @@ -58,7 +58,7 @@ def update_equivalent_types_dict(customized_equivalent_types=None): _EQUIVALENT_TYPES_DICT = _create_equivalent_types_dict() -def _partitions_sequential(partitions: List[SourcePartition]): +def _partitions_sequential(partitions: Sequence[SourcePartition]): prev_partition = None for partition in partitions: if prev_partition is not None and not check_subgraphs_connected( @@ -108,8 +108,9 @@ def find_sequential_partitions( typed_partitions_list = list(typed_partitions.values()) fusion_candidates = itertools.product(*typed_partitions_list) - fused_partitions = [] - for candidate in fusion_candidates: - if _partitions_sequential(candidate): # type: ignore[arg-type] - fused_partitions.append(candidate) + fused_partitions = [ + candidate + for candidate in fusion_candidates + if _partitions_sequential(candidate) + ] return fused_partitions diff --git a/torch/ao/quantization/quantizer/xnnpack_quantizer.py b/torch/ao/quantization/quantizer/xnnpack_quantizer.py index 078e48e42e2..88fc5555f31 100644 --- a/torch/ao/quantization/quantizer/xnnpack_quantizer.py +++ b/torch/ao/quantization/quantizer/xnnpack_quantizer.py @@ -93,10 +93,10 @@ def _get_supported_symmetric_config_and_operators() -> List[OperatorConfig]: get_symmetric_quantization_config(is_per_channel=True, is_qat=True), ]: ops = _supported_symmetric_quantized_operators() - for pattern_list in ops.values(): - supported_config_and_operators.append( - OperatorConfig(quantization_config, pattern_list) - ) + supported_config_and_operators.extend( + OperatorConfig(quantization_config, pattern_list) + for pattern_list in ops.values() + ) return copy.deepcopy(supported_config_and_operators) diff --git a/torch/autograd/gradcheck.py b/torch/autograd/gradcheck.py index 78e5d4207bb..c7ec4eba0ce 100644 --- a/torch/autograd/gradcheck.py +++ b/torch/autograd/gradcheck.py @@ -55,10 +55,11 @@ def _allocate_jacobians_with_inputs( # of `t.numel` and width of `numel_output`. Otherwise, for each tensor, returns # a 1-d tensor with size `(t.numel,)`. Each new tensor will be strided and have # the same dtype and device as those of the corresponding input. - out: List[torch.Tensor] = [] - for t in input_tensors: - if _is_float_or_complex_tensor(t) and t.requires_grad: - out.append(t.new_zeros((t.numel(), numel_output), layout=torch.strided)) + out: List[torch.Tensor] = [ + t.new_zeros((t.numel(), numel_output), layout=torch.strided) + for t in input_tensors + if _is_float_or_complex_tensor(t) and t.requires_grad + ] return tuple(out) @@ -69,11 +70,12 @@ def _allocate_jacobians_with_outputs( # in `output_tensors`, returns a new zero-filled tensor with height of `dim` and # width of `t.numel`. Otherwise, for each tensor, returns a 1-d tensor with size # (t.numel,). - out: List[torch.Tensor] = [] options = {"dtype": dtype, "device": device, "layout": torch.strided} - for t in output_tensors: - if _is_float_or_complex_tensor(t): - out.append(t.new_zeros((numel_input, t.numel()), **options)) + out: List[torch.Tensor] = [ + t.new_zeros((numel_input, t.numel()), **options) + for t in output_tensors + if _is_float_or_complex_tensor(t) + ] return tuple(out) @@ -904,10 +906,10 @@ def _compute_analytical_jacobian_rows( def _get_analytical_vjps_wrt_specific_output( vjp_fn, sample_output, v ) -> List[List[Optional[torch.Tensor]]]: - vjps: List[List[Optional[torch.Tensor]]] = [] grad_inputs = vjp_fn(v.reshape(sample_output.shape)) - for vjp in grad_inputs: - vjps.append([vjp.clone() if isinstance(vjp, torch.Tensor) else None]) + vjps: List[List[Optional[torch.Tensor]]] = [ + [vjp.clone() if isinstance(vjp, torch.Tensor) else None] for vjp in grad_inputs + ] return vjps diff --git a/torch/autograd/graph.py b/torch/autograd/graph.py index 4792ced32b2..1bf4191e291 100644 --- a/torch/autograd/graph.py +++ b/torch/autograd/graph.py @@ -802,9 +802,7 @@ def _register_logging_hooks_on_whole_graph( log_str = f"Executing: {node} with grad_outputs: {grad_outputs_str}" log.debug(log_str) - handles = [] - for node in iter_graph(grad_fns): - handles.append(node.register_prehook(prehook)) + handles = [node.register_prehook(prehook) for node in iter_graph(grad_fns)] def unregister_hooks() -> None: for handle in handles: diff --git a/torch/autograd/profiler_util.py b/torch/autograd/profiler_util.py index 3957c044cbb..8803b1ec5fc 100644 --- a/torch/autograd/profiler_util.py +++ b/torch/autograd/profiler_util.py @@ -856,10 +856,9 @@ def _build_table( flops_column_width = DEFAULT_COLUMN_WIDTH src_column_width = None - stacks = [] - for evt in events: - if evt.stack is not None and len(evt.stack) > 0: - stacks.append(evt.stack) + stacks = [ + evt.stack for evt in events if evt.stack is not None and len(evt.stack) > 0 + ] has_stack = len(stacks) > 0 if has_stack: src_column_width = ( @@ -947,10 +946,7 @@ def _build_table( if with_flops: # Auto-scaling of flops header - raw_flops = [] - for evt in events: - if evt.flops > 0: - raw_flops.append(evt.flops) + raw_flops = [evt.flops for evt in events if evt.flops > 0] if len(raw_flops) != 0: (flops_scale, flops_header) = auto_scale_flops(min(raw_flops)) headers.append(f"Total {flops_header}") diff --git a/torch/cuda/__init__.py b/torch/cuda/__init__.py index 95d5f1244a8..324174ed2d4 100644 --- a/torch/cuda/__init__.py +++ b/torch/cuda/__init__.py @@ -322,9 +322,7 @@ def _lazy_init(): # However, we must not let any *other* threads in! _tls.is_initializing = True - for calls in _lazy_seed_tracker.get_calls(): - if calls: - _queued_calls.append(calls) + _queued_calls.extend(calls for calls in _lazy_seed_tracker.get_calls() if calls) try: for queued_call, orig_traceback in _queued_calls: diff --git a/torch/cuda/random.py b/torch/cuda/random.py index 088e8398f64..97b2d80e247 100644 --- a/torch/cuda/random.py +++ b/torch/cuda/random.py @@ -44,9 +44,7 @@ def get_rng_state(device: Union[int, str, torch.device] = "cuda") -> Tensor: def get_rng_state_all() -> List[Tensor]: r"""Return a list of ByteTensor representing the random number states of all devices.""" - results = [] - for i in range(device_count()): - results.append(get_rng_state(i)) + results = [get_rng_state(i) for i in range(device_count())] return results diff --git a/torch/distributed/_shard/sharded_optim/api.py b/torch/distributed/_shard/sharded_optim/api.py index 1c7c632f22b..c0ddbece766 100644 --- a/torch/distributed/_shard/sharded_optim/api.py +++ b/torch/distributed/_shard/sharded_optim/api.py @@ -31,8 +31,9 @@ class ShardedOptimizer(optim.Optimizer): tensors: List[Tensor] = [] for value in named_params.values(): if isinstance(value, ShardedTensor): - for local_shard in value.local_shards(): - tensors.append(local_shard.tensor) + tensors.extend( + local_shard.tensor for local_shard in value.local_shards() + ) else: tensors.append(value) diff --git a/torch/distributed/_shard/sharded_tensor/_ops/tensor_ops.py b/torch/distributed/_shard/sharded_tensor/_ops/tensor_ops.py index 93902d6f314..19c475fe817 100644 --- a/torch/distributed/_shard/sharded_tensor/_ops/tensor_ops.py +++ b/torch/distributed/_shard/sharded_tensor/_ops/tensor_ops.py @@ -99,9 +99,10 @@ def sharded_type_as(args, kwargs, pg): tensor = args[1] if isinstance(tensor, ShardedTensor): tensor = tensor.local_tensor() - new_local_shards = [] - for shard in st.local_shards(): - new_local_shards.append(Shard(shard.tensor.type_as(tensor), shard.metadata)) + new_local_shards = [ + Shard(shard.tensor.type_as(tensor), shard.metadata) + for shard in st.local_shards() + ] st_meta = copy.deepcopy(st._metadata) st_meta.tensor_properties.dtype = tensor.dtype return new_local_shards, st_meta diff --git a/torch/distributed/_shard/sharded_tensor/reshard.py b/torch/distributed/_shard/sharded_tensor/reshard.py index 9a82012d59c..07c09029498 100644 --- a/torch/distributed/_shard/sharded_tensor/reshard.py +++ b/torch/distributed/_shard/sharded_tensor/reshard.py @@ -191,9 +191,9 @@ def reshard_local_shard( ) # Compute expected size - input_split_sizes = [] - for metadata in shards_metadata: - input_split_sizes.append(metadata.shard_sizes[reshard_dim]) + input_split_sizes = [ + metadata.shard_sizes[reshard_dim] for metadata in shards_metadata + ] rearrange_input = any(ranks[i] > ranks[i + 1] for i in range(len(ranks) - 1)) if rearrange_input: diff --git a/torch/distributed/benchmarks/benchmark_ddp_rpc.py b/torch/distributed/benchmarks/benchmark_ddp_rpc.py index 5943051419a..4d9a4ab9075 100644 --- a/torch/distributed/benchmarks/benchmark_ddp_rpc.py +++ b/torch/distributed/benchmarks/benchmark_ddp_rpc.py @@ -142,8 +142,7 @@ def _run_trainer(emb_rref_list, rank): ) # model.parameters() only includes local parameters. - for param in model.parameters(): - model_parameter_rrefs.append(RRef(param)) + model_parameter_rrefs.extend(RRef(param) for param in model.parameters()) # Setup distributed optimizer opt = DistributedOptimizer(optim.SGD, model_parameter_rrefs, lr=0.05) diff --git a/torch/distributed/checkpoint/planner_helpers.py b/torch/distributed/checkpoint/planner_helpers.py index e4b47ef659c..06b2482d5d5 100644 --- a/torch/distributed/checkpoint/planner_helpers.py +++ b/torch/distributed/checkpoint/planner_helpers.py @@ -207,8 +207,10 @@ def _create_default_metadata_only_plan(state_dict: STATE_DICT_TYPE) -> SavePlan: if isinstance(obj, DTensor): requests.append(_create_write_items_for_dtensor(fqn, obj)) elif isinstance(obj, ShardedTensor): - for shard_md in obj.metadata().shards_metadata: - requests.append(_create_write_item_for_shard(fqn, obj, shard_md)) + requests.extend( + _create_write_item_for_shard(fqn, obj, shard_md) + for shard_md in obj.metadata().shards_metadata + ) elif isinstance(obj, torch.Tensor): requests.append(_create_write_item_for_tensor(fqn, obj)) else: diff --git a/torch/distributed/distributed_c10d.py b/torch/distributed/distributed_c10d.py index 15becc08323..611fca987ad 100644 --- a/torch/distributed/distributed_c10d.py +++ b/torch/distributed/distributed_c10d.py @@ -1371,9 +1371,9 @@ def _get_all_pg_configs() -> List[Dict[str, Any]]: Return the pg configuration of all the process groups. """ - config_info: List[Dict[str, Any]] = [] - for pg in _world.pg_map.keys(): - config_info.append(_get_pg_config(pg)) + config_info: List[Dict[str, Any]] = [ + _get_pg_config(pg) for pg in _world.pg_map.keys() + ] return config_info @@ -2508,9 +2508,7 @@ def _coalescing_manager( # - coalesced `reduce_scatter_tensor` op0 = op_list[0].op if op0 == all_reduce: - tensors = [] - for op in op_list: - tensors.append(op.tensor) + tensors = [op.tensor for op in op_list] all_reduce_opts = AllreduceCoalescedOptions() all_reduce_opts.reduceOp = not_none(op_list[0].redop) work = group.allreduce_coalesced(tensors, all_reduce_opts) diff --git a/torch/distributed/fsdp/_runtime_utils.py b/torch/distributed/fsdp/_runtime_utils.py index 1dcea4f325e..95cb491a968 100644 --- a/torch/distributed/fsdp/_runtime_utils.py +++ b/torch/distributed/fsdp/_runtime_utils.py @@ -568,10 +568,11 @@ def _root_pre_forward( state._needs_buffer_dtype_restore_check = False if state.forward_prefetch: - handles = [] - for fsdp_state in state._all_fsdp_states: - if fsdp_state._handle: - handles.append(fsdp_state._handle) + handles = [ + fsdp_state._handle + for fsdp_state in state._all_fsdp_states + if fsdp_state._handle + ] for handle in handles: handle._needs_pre_forward_unshard = True handle._prefetched = False diff --git a/torch/distributed/nn/api/remote_module.py b/torch/distributed/nn/api/remote_module.py index 4e18fe3245e..6ce293c883c 100644 --- a/torch/distributed/nn/api/remote_module.py +++ b/torch/distributed/nn/api/remote_module.py @@ -110,9 +110,9 @@ def _create_module_with_interface( def _param_rrefs(module_rref, recurse) -> List[rpc.RRef[Parameter]]: - ret: List[rpc.RRef[Parameter]] = [] - for param in module_rref.local_value().parameters(recurse): - ret.append(rpc.RRef(param)) + ret: List[rpc.RRef[Parameter]] = [ + rpc.RRef(param) for param in module_rref.local_value().parameters(recurse) + ] return ret diff --git a/torch/distributed/optim/named_optimizer.py b/torch/distributed/optim/named_optimizer.py index 8e0b539b148..4130189967a 100644 --- a/torch/distributed/optim/named_optimizer.py +++ b/torch/distributed/optim/named_optimizer.py @@ -146,9 +146,7 @@ class _NamedOptimizer(optim.Optimizer): ret_groups = [] for group in param_groups: - param_keys = [] - for param in group["params"]: - param_keys.append(self.ordered_param_keys[param]) + param_keys = [self.ordered_param_keys[param] for param in group["params"]] ret_group = {"params": sorted(param_keys)} for k, v in group.items(): if k != "params": diff --git a/torch/distributed/optim/optimizer.py b/torch/distributed/optim/optimizer.py index f52fe488c46..a32e5505ffe 100644 --- a/torch/distributed/optim/optimizer.py +++ b/torch/distributed/optim/optimizer.py @@ -245,13 +245,12 @@ class DistributedOptimizer: else _local_optimizer_step ) - rpc_futs = [] - for optimizer in self.remote_optimizers: - rpc_futs.append( - rpc.rpc_async( - optimizer.owner(), - optimizer_step_func, - args=(optimizer, context_id), - ) + rpc_futs = [ + rpc.rpc_async( + optimizer.owner(), + optimizer_step_func, + args=(optimizer, context_id), ) + for optimizer in self.remote_optimizers + ] _wait_for_all(rpc_futs) diff --git a/torch/distributed/optim/zero_redundancy_optimizer.py b/torch/distributed/optim/zero_redundancy_optimizer.py index 0493b97f27f..7eed2dc3876 100644 --- a/torch/distributed/optim/zero_redundancy_optimizer.py +++ b/torch/distributed/optim/zero_redundancy_optimizer.py @@ -781,15 +781,15 @@ class ZeroRedundancyOptimizer(Optimizer, Joinable): self.process_group, rank ) for param_group in param_groups: - for param in param_group["params"]: - handles.append( - dist.broadcast( - tensor=param.data, - src=global_rank, - group=self.process_group, - async_op=True, - ) + handles.extend( + dist.broadcast( + tensor=param.data, + src=global_rank, + group=self.process_group, + async_op=True, ) + for param in param_group["params"] + ) return handles def _sync_params(self): diff --git a/torch/distributed/pipelining/microbatch.py b/torch/distributed/pipelining/microbatch.py index 2c276b7d6a5..5fb974e2d64 100644 --- a/torch/distributed/pipelining/microbatch.py +++ b/torch/distributed/pipelining/microbatch.py @@ -224,9 +224,7 @@ def _shard_dict_of_args( for chunk_idx in range(real_num_chunks): chunk_args = {} for key, arg in args_sharded_replicated.items(): - arg_single_chunk = [] - for v_flat in arg: - arg_single_chunk.append(v_flat[chunk_idx]) + arg_single_chunk = [v_flat[chunk_idx] for v_flat in arg] chunk_args[key] = arg_single_chunk chunks_flat.append(chunk_args) @@ -340,9 +338,10 @@ def split_args_kwargs_into_chunks( f"{len(args_split_dict)}, {len(kwargs_split)}" ) - args_split = [] - for chunk_args in args_split_dict: - args_split.append(tuple(chunk_args[i] for i in range(len(chunk_args)))) + args_split = [ + tuple(chunk_args[i] for i in range(len(chunk_args))) + for chunk_args in args_split_dict + ] return args_split, kwargs_split diff --git a/torch/distributed/pipelining/schedules.py b/torch/distributed/pipelining/schedules.py index be6bbb7a3f4..3c17655474c 100644 --- a/torch/distributed/pipelining/schedules.py +++ b/torch/distributed/pipelining/schedules.py @@ -1702,16 +1702,14 @@ class ScheduleLoopedBFS(PipelineScheduleMulti): ) # Store the list of operations used for that rank - rank_ops: List[Optional[_Action]] = [] # Pre-padding, rank starts with no-ops based on the warmup. - for _ in range(rank): - rank_ops.append(None) + rank_ops: List[Optional[_Action]] = [None for _ in range(rank)] for stage_index in stage_indices: - for mb_index in range(self._n_microbatches): - rank_ops.append( - _Action(stage_index, _ComputationType.FORWARD, mb_index) - ) + rank_ops.extend( + _Action(stage_index, _ComputationType.FORWARD, mb_index) + for mb_index in range(self._n_microbatches) + ) # wait for the first backward to trickle up # which is 2 for every hop away @@ -1719,10 +1717,10 @@ class ScheduleLoopedBFS(PipelineScheduleMulti): rank_ops.extend([None] * post_warmup_ops) for stage_index in reversed(stage_indices): - for mb_index in reversed(range(self._n_microbatches)): - rank_ops.append( - _Action(stage_index, _ComputationType.FULL_BACKWARD, mb_index) - ) + rank_ops.extend( + _Action(stage_index, _ComputationType.FULL_BACKWARD, mb_index) + for mb_index in reversed(range(self._n_microbatches)) + ) return rank_ops @@ -1744,10 +1742,8 @@ def _get_1f1b_rank_ops( weight_stage_mb_index: Dict[int, int] = defaultdict(int) # Store the list of operations used for that rank - rank_ops: List[Optional[_Action]] = [] # Pre-padding, rank starts with no-ops based on the warmup. - for _ in range(rank): - rank_ops.append(None) + rank_ops: List[Optional[_Action]] = [None for _ in range(rank)] # These are used to calculate the number of slots to fill with no-ops, to account for the delay in warmup # when we want to wait for the backward to trickle back up and start 1f1b to align all ranks. # Formula: diff --git a/torch/distributed/tensor/_collective_utils.py b/torch/distributed/tensor/_collective_utils.py index 34795858356..b8ebae2b2a4 100644 --- a/torch/distributed/tensor/_collective_utils.py +++ b/torch/distributed/tensor/_collective_utils.py @@ -195,8 +195,7 @@ def fill_empty_tensor_to_shards( size if idx != shard_dim else 0 for idx, size in enumerate(tensor_size) ] tensor = shards[0].new_zeros(tensor_size) - for _ in range(num_empty_tensors): - shards.append(tensor) + shards.extend(tensor for _ in range(num_empty_tensors)) return shards diff --git a/torch/distributed/tensor/_ops/_einsum_strategy.py b/torch/distributed/tensor/_ops/_einsum_strategy.py index 6753aa33f9b..83545bbdf70 100644 --- a/torch/distributed/tensor/_ops/_einsum_strategy.py +++ b/torch/distributed/tensor/_ops/_einsum_strategy.py @@ -167,9 +167,7 @@ def gen_einsum_strategies( # (i.e. for Shard, tensor dim size must > mesh size) all_strategies = [] for strategy_comb in strategy_combs: - spec_list = [] - for specs in zip(*strategy_comb): - spec_list.append(DTensorSpec(mesh, tuple(specs))) + spec_list = [DTensorSpec(mesh, tuple(specs)) for specs in zip(*strategy_comb)] strat = PlacementStrategy(output_specs=spec_list[0], input_specs=spec_list[1:]) all_strategies.append(strat) diff --git a/torch/distributed/tensor/_ops/_tensor_ops.py b/torch/distributed/tensor/_ops/_tensor_ops.py index 76f7d730c37..ef048b643e0 100644 --- a/torch/distributed/tensor/_ops/_tensor_ops.py +++ b/torch/distributed/tensor/_ops/_tensor_ops.py @@ -43,18 +43,17 @@ def default_strategy(mesh: DeviceMesh, op_schema: OpSchema) -> StrategyType: # Default strategy by default just propagate the first input strategy select_strategy = op_schema.args_schema[0] assert isinstance(select_strategy, OpStrategy) - default_strategy = [] - for strategy in select_strategy.strategies: - # we create new DTensorSpecs even for default strategy to assure that - # the tensor metas are distinct between the arguments and outputs - default_strategy.append( - PlacementStrategy( - output_specs=DTensorSpec( - mesh=strategy.output_spec.mesh, - placements=strategy.output_spec.placements, - ) + # we create new DTensorSpecs even for default strategy to assure that + # the tensor metas are distinct between the arguments and outputs + default_strategy = [ + PlacementStrategy( + output_specs=DTensorSpec( + mesh=strategy.output_spec.mesh, + placements=strategy.output_spec.placements, ) ) + for strategy in select_strategy.strategies + ] return OpStrategy(default_strategy) diff --git a/torch/distributed/tensor/_ops/utils.py b/torch/distributed/tensor/_ops/utils.py index 334bcc4a37f..cfbdab9271d 100644 --- a/torch/distributed/tensor/_ops/utils.py +++ b/torch/distributed/tensor/_ops/utils.py @@ -209,9 +209,10 @@ def map_placements_after_broadcast( def generate_redistribute_costs( src_strategy: OpStrategy, dst_spec: DTensorSpec ) -> List[float]: - redistribute_costs: List[float] = [] - for strat in src_strategy.strategies: - redistribute_costs.append(redistribute_cost(strat.output_spec, dst_spec)) + redistribute_costs: List[float] = [ + redistribute_cost(strat.output_spec, dst_spec) + for strat in src_strategy.strategies + ] return redistribute_costs diff --git a/torch/export/unflatten.py b/torch/export/unflatten.py index 57c18119c35..9a2296bdb21 100644 --- a/torch/export/unflatten.py +++ b/torch/export/unflatten.py @@ -152,9 +152,11 @@ class InterpreterModule(torch.nn.Module): # the keys in the kwarg dict. arg_list = list(args) kwarg_names = self.arg_names[len(arg_list) :] - for kwarg_name in kwarg_names: - if kwarg_name in kwargs: - arg_list.append(kwargs[kwarg_name]) + arg_list.extend( + kwargs[kwarg_name] + for kwarg_name in kwarg_names + if kwarg_name in kwargs + ) # Assert that the kwargs passed in exactly match the positional # arguments specified by the GraphModule. This should be @@ -933,9 +935,10 @@ class _ModuleFrame: assert kwargs_spec.context is not None with self.graph.inserting_after(None): - arg_nodes = [] - for idx in range(args_spec.num_children): - arg_nodes.append(self.graph.placeholder(f"_positional_arg_{idx}")) + arg_nodes = [ + self.graph.placeholder(f"_positional_arg_{idx}") + for idx in range(args_spec.num_children) + ] kwarg_nodes = {} for name in kwargs_spec.context: kwarg_nodes[name] = self.graph.placeholder(name) diff --git a/torch/fx/experimental/accelerator_partitioner.py b/torch/fx/experimental/accelerator_partitioner.py index 4f9fe0f9a14..d2907ad3d08 100644 --- a/torch/fx/experimental/accelerator_partitioner.py +++ b/torch/fx/experimental/accelerator_partitioner.py @@ -1015,10 +1015,11 @@ class Partitioner: # Keep tracking the partition pair of node pair partition_pair: List[Partition] = [] # Collect all the op nodes from the graph - op_nodes = [] - for n in self.graph_module.graph.nodes: - if n.op not in {"placeholder", "get_attr", "output"}: - op_nodes.append(n) + op_nodes = [ + n + for n in self.graph_module.graph.nodes + if n.op not in {"placeholder", "get_attr", "output"} + ] for node in op_nodes: # Find which partition the current node belongs p0_index = self.node_to_partition[node] diff --git a/torch/fx/experimental/migrate_gradual_types/constraint_transformation.py b/torch/fx/experimental/migrate_gradual_types/constraint_transformation.py index 7a854b1dabe..263ac5de560 100644 --- a/torch/fx/experimental/migrate_gradual_types/constraint_transformation.py +++ b/torch/fx/experimental/migrate_gradual_types/constraint_transformation.py @@ -648,11 +648,7 @@ def generate_reshape(constraint, counter): # then there must be exactly one occurrence of dyn else: - new_target = [] - - for n in target: - if n != Dyn: - new_target.append(n) + new_target = [n for n in target if n != Dyn] # tensor 1 c3_tensor1 = Disj( @@ -886,10 +882,8 @@ def generate_all_int_dyn_dim_possibilities(my_list: List[DVar]): neq_possibilities = [ BinConstraintD(my_list[i], Dyn, op_neq) for i in range(len(my_list)) ] - d_possibilities = [] - for i in zip(eq_possibilities, neq_possibilities): - d_possibilities.append(list(i)) + d_possibilities = [list(i) for i in zip(eq_possibilities, neq_possibilities)] all_possibilities = list(itertools.product(*d_possibilities)) return all_possibilities @@ -1043,13 +1037,11 @@ def apply_padding( assert len(simulate_padding + d1) == len(d2) - broadcast_padding = [] - # for every padding size, we also consider broadcasting - for j in range(len(d2) - i): - broadcast_padding.append( - broadcast_dim(simulate_padding, d2, d11, d12, j, True) - ) + broadcast_padding = [ + broadcast_dim(simulate_padding, d2, d11, d12, j, True) + for j in range(len(d2) - i) + ] # we consider the possibilities for broadcasting for every dimension. Since we already # padded d1, we do not consider it while broadcasting diff --git a/torch/fx/experimental/partitioner_utils.py b/torch/fx/experimental/partitioner_utils.py index e59921c58fa..5cecb8c6994 100644 --- a/torch/fx/experimental/partitioner_utils.py +++ b/torch/fx/experimental/partitioner_utils.py @@ -302,11 +302,10 @@ def get_latency_of_partitioned_graph( """This function is to return all the partitions without parents as the starting points of all the paths """ - top_partitions = [] - for partition in partitions: - # If a partition has no parents, then it is a top partition - if len(partition.parents) == 0: - top_partitions.append(partition) + # If a partition has no parents, then it is a top partition + top_partitions = [ + partition for partition in partitions if len(partition.parents) == 0 + ] return top_partitions top_partitions = get_top_partitions(partitions) diff --git a/torch/fx/experimental/symbolic_shapes.py b/torch/fx/experimental/symbolic_shapes.py index ca432a39c28..ecf2d666c56 100644 --- a/torch/fx/experimental/symbolic_shapes.py +++ b/torch/fx/experimental/symbolic_shapes.py @@ -5201,10 +5201,9 @@ class ShapeEnv: symints = { s.node.expr for s in symints if isinstance(s.node.expr, sympy.Symbol) } - guards = [] - for g in self.guards: - if all(s in symints for s in g.expr.free_symbols): - guards.append(g) + guards = [ + g for g in self.guards if all(s in symints for s in g.expr.free_symbols) + ] return guards def bind_symbols( diff --git a/torch/fx/passes/pass_manager.py b/torch/fx/passes/pass_manager.py index eb793aa6f11..9b0ccbb82d5 100644 --- a/torch/fx/passes/pass_manager.py +++ b/torch/fx/passes/pass_manager.py @@ -220,10 +220,7 @@ class PassManager: def remove_pass(self, _passes: List[str]): if _passes is None: return - passes_left = [] - for ps in self.passes: - if ps.__name__ not in _passes: - passes_left.append(ps) + passes_left = [ps for ps in self.passes if ps.__name__ not in _passes] self.passes = passes_left self._validated = False diff --git a/torch/jit/_recursive.py b/torch/jit/_recursive.py index 1216cee929d..8a4ef3d4fe6 100644 --- a/torch/jit/_recursive.py +++ b/torch/jit/_recursive.py @@ -881,9 +881,7 @@ def infer_methods_to_compile(nn_module): uniqued_methods.append(name) uniquer.add(name) - stubs = [] - for method in uniqued_methods: - stubs.append(make_stub_from_method(nn_module, method)) + stubs = [make_stub_from_method(nn_module, method) for method in uniqued_methods] return overload_stubs + stubs @@ -959,9 +957,10 @@ def interface_script(mod_interface, nn_module): It is used to know which methods need to act as starting points for compilation. """ - stubs = [] - for method in mod_interface.getMethodNames(): - stubs.append(make_stub_from_method(nn_module, method)) + stubs = [ + make_stub_from_method(nn_module, method) + for method in mod_interface.getMethodNames() + ] return stubs return create_script_module(nn_module, infer_interface_methods_to_compile) diff --git a/torch/jit/_script.py b/torch/jit/_script.py index 1d8dccb049d..b572beca7d8 100644 --- a/torch/jit/_script.py +++ b/torch/jit/_script.py @@ -1493,11 +1493,10 @@ def _get_overloads(obj): _jit_internal.get_overload_no_implementation_error_message("function", obj) ) - compiled_fns = [] - for overload_fn in uncompiled_overloads: - compiled_fns.append( - _compile_function_with_overload(overload_fn, qual_name, obj) - ) + compiled_fns = [ + _compile_function_with_overload(overload_fn, qual_name, obj) + for overload_fn in uncompiled_overloads + ] if existing_compiled_fns: compiled_fns = existing_compiled_fns + compiled_fns diff --git a/torch/mtia/__init__.py b/torch/mtia/__init__.py index af711d49260..36cc6dab0c0 100644 --- a/torch/mtia/__init__.py +++ b/torch/mtia/__init__.py @@ -77,9 +77,7 @@ def _lazy_init() -> None: # However, we must not let any *other* threads in! _tls.is_initializing = True - for calls in _lazy_seed_tracker.get_calls(): - if calls: - _queued_calls.append(calls) + _queued_calls.extend(calls for calls in _lazy_seed_tracker.get_calls() if calls) try: for queued_call, orig_traceback in _queued_calls: diff --git a/torch/nn/parallel/_functions.py b/torch/nn/parallel/_functions.py index 550ec9b5e35..a828bfa0f4a 100644 --- a/torch/nn/parallel/_functions.py +++ b/torch/nn/parallel/_functions.py @@ -23,8 +23,7 @@ class Broadcast(Function): non_differentiables = [] for idx, input_requires_grad in enumerate(ctx.needs_input_grad[1:]): if not input_requires_grad: - for output in outputs: - non_differentiables.append(output[idx]) + non_differentiables.extend(output[idx] for output in outputs) ctx.mark_non_differentiable(*non_differentiables) return tuple([t for tensors in outputs for t in tensors]) diff --git a/torch/nn/utils/_expanded_weights/conv_utils.py b/torch/nn/utils/_expanded_weights/conv_utils.py index f0ecbe9bdcc..1c1a596b0c8 100644 --- a/torch/nn/utils/_expanded_weights/conv_utils.py +++ b/torch/nn/utils/_expanded_weights/conv_utils.py @@ -142,9 +142,7 @@ def conv_backward(func, ctx, grad_output): ctx.groups, ) - kernel_size = [] - for i in range(2, conv_picker(func, 3, 4, 5)): - kernel_size.append(weight_shape[i]) + kernel_size = [weight_shape[i] for i in range(2, conv_picker(func, 3, 4, 5))] batch_size = ctx.batch_size results: List[Optional[torch.Tensor]] = [] diff --git a/torch/onnx/symbolic_helper.py b/torch/onnx/symbolic_helper.py index aae0cd5591d..bdbcb8b1006 100644 --- a/torch/onnx/symbolic_helper.py +++ b/torch/onnx/symbolic_helper.py @@ -356,12 +356,14 @@ def quantized_args( return descriptor and _is_value(arg) and _is_tuple_construct(arg) # Run regular symbolic function if none of the argument is QTensor. - is_quantized = [] + is_quantized: typing.List[bool] = [] for descriptor, arg in descriptor_args: # ListConstruct if _is_packed_list(arg): - for arg_input in arg.node().inputs(): - is_quantized.append(_is_arg_quantized(descriptor, arg_input)) + is_quantized.extend( + _is_arg_quantized(descriptor, arg_input) + for arg_input in arg.node().inputs() + ) else: is_quantized.append(_is_arg_quantized(descriptor, arg)) diff --git a/torch/onnx/symbolic_opset18.py b/torch/onnx/symbolic_opset18.py index d28fadc1bab..b1492ebda40 100644 --- a/torch/onnx/symbolic_opset18.py +++ b/torch/onnx/symbolic_opset18.py @@ -67,10 +67,9 @@ def col2im( stride: Sequence[int], ): # convert [i0, i1, ..., in] into [i0, i0, i1, i1, ..., in, in] - adjusted_padding = [] + adjusted_padding: List[int] = [] for pad in padding: - for _ in range(2): - adjusted_padding.append(pad) + adjusted_padding.extend(pad for _ in range(2)) num_dimensional_axis = symbolic_helper._get_tensor_sizes(output_size)[0] if not adjusted_padding: diff --git a/torch/onnx/verification.py b/torch/onnx/verification.py index 26810b116ff..4977cbf30a8 100644 --- a/torch/onnx/verification.py +++ b/torch/onnx/verification.py @@ -1397,8 +1397,6 @@ class GraphInfo: original_outputs = list(graph.outputs()) original_inputs = list(graph.inputs()) - new_outputs = [] - def _process_bridge_value_for_lower( graph: torch.Graph, bridge_value: torch.Value ) -> torch.Value: @@ -1416,9 +1414,9 @@ class GraphInfo: graph, pivot, process_bridge_value_for_lower ) - for output in original_outputs: - if _produced_by(output, lower_nodes): - new_outputs.append(output) + new_outputs = [ + output for output in original_outputs if _produced_by(output, lower_nodes) + ] for _ in enumerate(original_outputs): graph.eraseOutput(0) for output in new_outputs: diff --git a/torch/package/_directory_reader.py b/torch/package/_directory_reader.py index f58065f47dc..52197fb1c84 100644 --- a/torch/package/_directory_reader.py +++ b/torch/package/_directory_reader.py @@ -50,10 +50,11 @@ class DirectoryReader: def get_all_records( self, ): - files = [] - for filename in glob(f"{self.directory}/**", recursive=True): - if not os.path.isdir(filename): - files.append(filename[len(self.directory) + 1 :]) + files = [ + filename[len(self.directory) + 1 :] + for filename in glob(f"{self.directory}/**", recursive=True) + if not os.path.isdir(filename) + ] return files def serialization_id( diff --git a/torch/package/find_file_dependencies.py b/torch/package/find_file_dependencies.py index dd5c5bb9ea9..7f2386b3ce5 100644 --- a/torch/package/find_file_dependencies.py +++ b/torch/package/find_file_dependencies.py @@ -53,16 +53,16 @@ class _ExtractModuleReferences(ast.NodeVisitor): if hasattr(node.func, "id") and node.func.id == "__import__": try: name = self._grab_node_str(node.args[0]) - fromlist = [] + fromlist: List[str] = [] level = 0 if len(node.args) > 3: - for v in node.args[3].elts: - fromlist.append(self._grab_node_str(v)) + fromlist.extend(self._grab_node_str(v) for v in node.args[3].elts) elif hasattr(node, "keywords"): for keyword in node.keywords: if keyword.arg == "fromlist": - for v in keyword.value.elts: - fromlist.append(self._grab_node_str(v)) + fromlist.extend( + self._grab_node_str(v) for v in keyword.value.elts + ) if len(node.args) > 4: level = self._grab_node_int(node.args[4]) elif hasattr(node, "keywords"): diff --git a/torch/profiler/_pattern_matcher.py b/torch/profiler/_pattern_matcher.py index a7ec5d05dd6..9b2d87a6a37 100644 --- a/torch/profiler/_pattern_matcher.py +++ b/torch/profiler/_pattern_matcher.py @@ -97,10 +97,9 @@ class Pattern: def matched_events(self): if self.skip: return [] - matched_events = [] - for event in self.eventTreeTraversal(): - if self.match(event): - matched_events.append(event) + matched_events = [ + event for event in self.eventTreeTraversal() if self.match(event) + ] return matched_events def root_of(self, event: _ProfilerEvent): diff --git a/torch/sparse/_triton_ops_meta.py b/torch/sparse/_triton_ops_meta.py index 5bbd61b373c..e65981f7265 100644 --- a/torch/sparse/_triton_ops_meta.py +++ b/torch/sparse/_triton_ops_meta.py @@ -233,8 +233,7 @@ def dump(): for op_key in sorted(_operation_device_version_data, key=sort_key): data_part.append(" " + repr(op_key).replace("'", '"') + ": {") op_data = _operation_device_version_data[op_key] - for key in sorted(op_data): - data_part.append(f" {key}: {op_data[key]},") + data_part.extend(f" {key}: {op_data[key]}," for key in sorted(op_data)) data_part.append(" },") new_content = part1 + "\n".join(data_part) + "\n" + part2 if current_content != new_content: diff --git a/torch/testing/_internal/common_methods_invocations.py b/torch/testing/_internal/common_methods_invocations.py index 3d3d645c8d8..d1acb9ff9f3 100644 --- a/torch/testing/_internal/common_methods_invocations.py +++ b/torch/testing/_internal/common_methods_invocations.py @@ -2285,9 +2285,7 @@ def sample_inputs_stack(op_info, device, dtype, requires_grad, **kwargs): ((0, 1, 0), 2),) for shape, num_tensors in cases: - tensors = [] - for _ in range(num_tensors): - tensors.append(make_arg(shape)) + tensors = [make_arg(shape) for _ in range(num_tensors)] for dim in range(-1, len(shape) - 1): yield SampleInput(tensors, args=(dim,)) @@ -2328,9 +2326,7 @@ def sample_inputs_chunk_cat(op_info, device, dtype, requires_grad, **kwargs): ), ) for sizes, dim, num_chunks in same_ndim_cases: - tensors = [] - for size in sizes: - tensors.append(make_arg(size)) + tensors = [make_arg(size) for size in sizes] yield SampleInput(tensors, args=(dim, num_chunks)) different_ndim_case = [ diff --git a/torch/testing/_internal/common_quantization.py b/torch/testing/_internal/common_quantization.py index 7fec835165e..c46d74475f9 100644 --- a/torch/testing/_internal/common_quantization.py +++ b/torch/testing/_internal/common_quantization.py @@ -2373,9 +2373,7 @@ class ModelWithSequentialFusion(nn.Module): super().__init__() self.conv1 = nn.Conv2d(3, 3, 1) self.relu1 = nn.ReLU(inplace=False) - layers = [] - for _ in range(3): - layers.append(ConvBNReLU()) + layers = [ConvBNReLU() for _ in range(3)] self.features = nn.Sequential(*layers) head = [nn.Linear(300, 10), nn.ReLU(inplace=False)] self.classifier = nn.Sequential(*head) diff --git a/torch/testing/_internal/common_quantized.py b/torch/testing/_internal/common_quantized.py index 99d628554ff..2daddf54676 100644 --- a/torch/testing/_internal/common_quantized.py +++ b/torch/testing/_internal/common_quantized.py @@ -127,9 +127,7 @@ def _snr(x, x_hat): """ if isinstance(x, (list, tuple)): assert len(x) == len(x_hat) - res = [] - for idx in range(len(x)): - res.append(_snr(x[idx], x_hat[idx])) + res = [_snr(x[idx], x_hat[idx]) for idx in range(len(x))] return res if x_hat.is_quantized: x_hat = x_hat.dequantize() diff --git a/torch/testing/_internal/common_utils.py b/torch/testing/_internal/common_utils.py index 437bc5eb401..9466eb60494 100644 --- a/torch/testing/_internal/common_utils.py +++ b/torch/testing/_internal/common_utils.py @@ -4669,8 +4669,7 @@ def _generate_indices_prefer_all_rows(rows: int, cols: int, num_indices: int) -> for r in range(rows): # Note that this can yield overlapping indices - for c in random.choices(col_indices, k=n_per_row): - indices.append((r, c)) + indices.extend((r, c) for c in random.choices(col_indices, k=n_per_row)) return torch.tensor(indices[:num_indices]) @@ -5165,9 +5164,7 @@ def get_cycles_per_ms() -> float: # and seems to return stable values. Therefore, we enable caching # using lru_cache decorator above. num = 10 - vals = [] - for _ in range(num): - vals.append(measure()) + vals = [measure() for _ in range(num)] vals = sorted(vals) return mean(vals[2 : num - 2]) diff --git a/torch/testing/_internal/distributed/_tensor/common_dtensor.py b/torch/testing/_internal/distributed/_tensor/common_dtensor.py index d0bd2aa8986..bb1fdf1eaee 100644 --- a/torch/testing/_internal/distributed/_tensor/common_dtensor.py +++ b/torch/testing/_internal/distributed/_tensor/common_dtensor.py @@ -442,14 +442,11 @@ class DTensorConverter: self.flatten_kwargs: List[object] = flatten_kwargs self.flatten_kwargs_spec: TreeSpec = flatten_kwargs_spec - choices_for_args = [] - for arg in self.flatten_args: - if isinstance(arg, torch.Tensor): - choices_for_args.append(self.gen_sharding_choices_for_arg(arg)) + choices_for_args = [self.gen_sharding_choices_for_arg(arg) for arg in self.flatten_args if isinstance(arg, torch.Tensor)] - for arg in self.flatten_kwargs: - if isinstance(arg, torch.Tensor): - choices_for_args.append(self.gen_sharding_choices_for_arg(arg)) + choices_for_args.extend( + self.gen_sharding_choices_for_arg(arg) for arg in self.flatten_kwargs if isinstance(arg, torch.Tensor) + ) self.sharding_combs: Iterator[Sequence[Placement]] = iter( itertools.product(*choices_for_args) diff --git a/torch/testing/_internal/distributed/distributed_test.py b/torch/testing/_internal/distributed/distributed_test.py index 2f724971f64..1380b745454 100644 --- a/torch/testing/_internal/distributed/distributed_test.py +++ b/torch/testing/_internal/distributed/distributed_test.py @@ -2437,9 +2437,7 @@ class DistributedTest: rank_to_GPU = init_multigpu_helper(dist.get_world_size(), BACKEND) device_id = rank_to_GPU[rank][0] - input_split_sizes = [] - for src in group: - input_split_sizes.append(src + 1) + input_split_sizes = [src + 1 for src in group] start_len = sum(input_split_sizes[:rank]) end_len = start_len + input_split_sizes[rank] sum_len = sum(input_split_sizes) @@ -3464,9 +3462,7 @@ class DistributedTest: rank_to_GPU = init_multigpu_helper(dist.get_world_size(), BACKEND) device_id = rank_to_GPU[rank][0] - output_split_sizes = [] - for dst in group: - output_split_sizes.append(dst + 1) + output_split_sizes = [dst + 1 for dst in group] sum_len = sum(output_split_sizes) value = 2 diff --git a/torch/testing/_internal/distributed/multi_threaded_pg.py b/torch/testing/_internal/distributed/multi_threaded_pg.py index 5f8e89d6a8e..aff44128d00 100644 --- a/torch/testing/_internal/distributed/multi_threaded_pg.py +++ b/torch/testing/_internal/distributed/multi_threaded_pg.py @@ -123,13 +123,11 @@ class AllReduce: @torch.no_grad() def work(self, data): for i in range(len(data[0])): - tensors = [] # use rank0 as the device for sum rank_0_device = data[0][i].device # collect all data to the list and make them # all on rank 0 device - for src_rank in range(0, len(data)): - tensors.append(data[src_rank][i].to(rank_0_device)) + tensors = [data[src_rank][i].to(rank_0_device) for src_rank in range(0, len(data))] # now mimic reduce across all ranks res = _reduce_ops[self.op](tensors) diff --git a/torch/testing/_internal/distributed/rpc/dist_autograd_test.py b/torch/testing/_internal/distributed/rpc/dist_autograd_test.py index a0e934fae28..e113b87cceb 100644 --- a/torch/testing/_internal/distributed/rpc/dist_autograd_test.py +++ b/torch/testing/_internal/distributed/rpc/dist_autograd_test.py @@ -714,15 +714,13 @@ class CommonDistAutogradTest(RpcAgentTestFixture): # kick off forward and backward pass on three other workers (trainers) rank_diffs = [1, 2, 3] - futures = [] - for rank_diff in rank_diffs: - futures.append( - rpc.rpc_async( - worker_name((self.rank + rank_diff) % self.world_size), - trainer_fn, - args=(rref_t1, t2, worker_name(self.rank), rank_diff, sparse), - ) - ) + futures = [ + rpc.rpc_async( + worker_name((self.rank + rank_diff) % self.world_size), + trainer_fn, + args=(rref_t1, t2, worker_name(self.rank), rank_diff, sparse), + ) for rank_diff in rank_diffs + ] # check if the trainers have done with their backward pass for rank_diff in rank_diffs: @@ -2747,13 +2745,13 @@ class TensorPipeCudaDistAutogradTest(RpcAgentTestFixture): # this is master layers = [nn.Linear(2000, 2000) for _ in range(self.world_size - 1)] local_layers = [l.to(0) for l in layers] - remote_layers = [] - for rank in range(1, self.world_size): - remote_layers.append(rpc.remote( + remote_layers = [ + rpc.remote( worker_name(rank), WrapperModule, args=(layers[rank - 1], rank) - )) + ) for rank in range(1, self.world_size) + ] x = torch.randn(5000, 2000).to(0) # local iteration diff --git a/torch/testing/_internal/distributed/rpc/examples/parameter_server_test.py b/torch/testing/_internal/distributed/rpc/examples/parameter_server_test.py index 98b41920d2a..928f28c1921 100644 --- a/torch/testing/_internal/distributed/rpc/examples/parameter_server_test.py +++ b/torch/testing/_internal/distributed/rpc/examples/parameter_server_test.py @@ -107,11 +107,7 @@ def run_ps(trainers): timed_log("Start training") start = perf_counter() ps_rref = rpc.RRef(BatchUpdateParameterServer(len(trainers))) - futs = [] - for trainer in trainers: - futs.append( - rpc.rpc_async(trainer, run_trainer, args=(ps_rref,)) - ) + futs = [rpc.rpc_async(trainer, run_trainer, args=(ps_rref,)) for trainer in trainers] torch.futures.wait_all(futs) stop = perf_counter() diff --git a/torch/testing/_internal/distributed/rpc/examples/reinforcement_learning_rpc_test.py b/torch/testing/_internal/distributed/rpc/examples/reinforcement_learning_rpc_test.py index eab07be49e5..4b3ba7de3f4 100644 --- a/torch/testing/_internal/distributed/rpc/examples/reinforcement_learning_rpc_test.py +++ b/torch/testing/_internal/distributed/rpc/examples/reinforcement_learning_rpc_test.py @@ -166,16 +166,14 @@ class Agent: r""" Run one episode. The agent will tell each observer to run n_steps. """ - futs = [] - for ob_rref in self.ob_rrefs: - # make async RPC to kick off an episode on all observers - futs.append( - rpc_async( - ob_rref.owner(), - _call_method, - args=(Observer.run_episode, ob_rref, self.agent_rref, n_steps) - ) - ) + # make async RPC to kick off an episode on all observers + futs = [ + rpc_async( + ob_rref.owner(), + _call_method, + args=(Observer.run_episode, ob_rref, self.agent_rref, n_steps) + ) for ob_rref in self.ob_rrefs + ] # wait until all observers have finished this episode for fut in futs: diff --git a/torch/testing/_internal/distributed/rpc/jit/rpc_test.py b/torch/testing/_internal/distributed/rpc/jit/rpc_test.py index 4270f4bcd00..34c7fc793bf 100644 --- a/torch/testing/_internal/distributed/rpc/jit/rpc_test.py +++ b/torch/testing/_internal/distributed/rpc/jit/rpc_test.py @@ -1356,13 +1356,13 @@ class JitRpcTest( dst2 = worker_name((self.rank + 2) % self.world_size) num = 20 - rrefs = [] - for i in range(num): - rrefs.append( - rpc.remote( - dst1, async_add, args=(dst2, torch.ones(2, 2), torch.ones(2, 2) * i) - ) - ) + rrefs = [ + rpc.remote( + dst1, + async_add, + args=(dst2, torch.ones(2, 2), torch.ones(2, 2) * i) + ) for i in range(num) + ] for i in range(num): self.assertEqual(rrefs[i].to_here(), torch.ones(2, 2) + i) diff --git a/torch/testing/_internal/distributed/rpc/rpc_test.py b/torch/testing/_internal/distributed/rpc/rpc_test.py index 75237061724..a461c3a86bf 100644 --- a/torch/testing/_internal/distributed/rpc/rpc_test.py +++ b/torch/testing/_internal/distributed/rpc/rpc_test.py @@ -1038,15 +1038,13 @@ class RpcTestCommon: n = self.rank + 1 dst_rank1 = n % self.world_size dst_rank2 = (n + 1) % self.world_size - all_rrefs = [] - for _ in range(20): - all_rrefs.append( - rpc.remote( - worker_name(dst_rank1), - f, - args=(worker_name(dst_rank2),), - ) - ) + all_rrefs = [ + rpc.remote( + worker_name(dst_rank1), + f, + args=(worker_name(dst_rank2),), + ) for _ in range(20) + ] for i in range(20): rref_of_rrefs = all_rrefs[i] @@ -1073,18 +1071,15 @@ class RpcTestCommon: def _my_parameter_server(self, sparse): ps_rref = RRef(MyParameterServer(self.world_size - 1)) - futures = [] - for index in range(1, self.world_size): - futures.append( - rpc.rpc_async( - worker_name((self.rank + index) % self.world_size), - self._trainer_func, - args=( - ps_rref, - sparse - ), - ) - ) + futures = [ + rpc.rpc_async( + worker_name((self.rank + index) % self.world_size), + self._trainer_func, + args=( + ps_rref, + sparse + ), + ) for index in range(1, self.world_size)] torch.futures.wait_all(futures) def _test_cuda_future_extraction(self, wrapper, unwrapper, sparse_tensor): @@ -1457,9 +1452,7 @@ class RpcTest(RpcAgentTestFixture, RpcTestCommon): model = torch.nn.parallel.DistributedDataParallel(model) with self.assertRaisesRegex(RuntimeError, 'Current RPC agent is not set! Did you initialize the RPC framework'): - params = [] - for param in model.parameters(): - params.append(RRef(param)) + params = [RRef(param) for param in model.parameters()] def test_world_size_one(self): self._world_size_one( @@ -3889,9 +3882,7 @@ class RpcTest(RpcAgentTestFixture, RpcTestCommon): args=(torch.ones(n, n), torch.ones(n, n)) ) - cb_futs = [] - for idx in range(num_cbs): - cb_futs.append(fut.then(partial(callback, idx))) + cb_futs = [fut.then(partial(callback, idx)) for idx in range(num_cbs)] self.assertEqual(fut.wait(), torch.ones(n, n) * 2) @@ -4368,20 +4359,16 @@ class RpcTest(RpcAgentTestFixture, RpcTestCommon): @dist_init def test_wait_all_with_exception(self): - futs = [] dst = worker_name((self.rank + 1) % self.world_size) - for _ in range(10): - futs.append(rpc.rpc_async(dst, raise_func)) + futs = [rpc.rpc_async(dst, raise_func) for _ in range(10)] with self.assertRaisesRegex(ValueError, "Expected error"): torch.futures.wait_all(futs) @dist_init def test_wait_all_with_partial_exception(self): - futs = [] dst = worker_name((self.rank + 1) % self.world_size) - for _ in range(10): - futs.append(rpc.rpc_async(dst, torch.add, args=(torch.ones(2), 1))) + futs = [rpc.rpc_async(dst, torch.add, args=(torch.ones(2), 1)) for _ in range(10)] futs.append(rpc.rpc_async(dst, raise_func)) @@ -6214,13 +6201,13 @@ class TensorPipeAgentCudaRpcTest(RpcAgentTestFixture, RpcTestCommon): rpc_backend_options=options, ) - futs = [] - for i in range(5): - futs.append(rpc.rpc_async( + futs = [ + rpc.rpc_async( dst, TensorPipeAgentCudaRpcTest._return_tensor_view, args=(i,) - )) + ) for i in range(5) + ] for i in range(5): self.assertEqual(torch.ones(100, 200) * i, futs[i].wait()) diff --git a/torch/testing/_internal/hypothesis_utils.py b/torch/testing/_internal/hypothesis_utils.py index 139470ccc20..f02ef4c9e04 100644 --- a/torch/testing/_internal/hypothesis_utils.py +++ b/torch/testing/_internal/hypothesis_utils.py @@ -316,13 +316,9 @@ def tensor_conv( if isinstance(spatial_dim, Iterable): spatial_dim = draw(st.sampled_from(spatial_dim)) - feature_map_shape = [] - for _ in range(spatial_dim): - feature_map_shape.append(draw(st.integers(*feature_map_range))) + feature_map_shape = [draw(st.integers(*feature_map_range)) for _ in range(spatial_dim)] - kernels = [] - for _ in range(spatial_dim): - kernels.append(draw(st.integers(*kernel_range))) + kernels = [draw(st.integers(*kernel_range)) for _ in range(spatial_dim)] tr = False weight_shape = (output_channels, input_channels_per_group) + tuple(kernels) diff --git a/torch/testing/_internal/opinfo/definitions/nested.py b/torch/testing/_internal/opinfo/definitions/nested.py index bc3e93adf8f..d225d1764c2 100644 --- a/torch/testing/_internal/opinfo/definitions/nested.py +++ b/torch/testing/_internal/opinfo/definitions/nested.py @@ -181,13 +181,12 @@ def unbind_reference(op, sample, wrap_output_as_njt=True): # ensure we get the same number of returns for each invocation assert all(len(o) == num_returns for o in out_ref_components) # construct NJTs from same index returns from each invocation - njt_returns = [] - for r in range(num_returns): - njt_returns.append( - torch.nested.as_nested_tensor( - [o[r] for o in out_ref_components], layout=torch.jagged - ) + njt_returns = [ + torch.nested.as_nested_tensor( + [o[r] for o in out_ref_components], layout=torch.jagged ) + for r in range(num_returns) + ] return type(out_ref_components[0])(njt_returns) return torch.nested.as_nested_tensor(out_ref_components, layout=torch.jagged) @@ -241,11 +240,10 @@ def reduction_reference(op, sample): # ensure we get the same number of returns for each invocation assert all(len(o) == num_returns for o in out_ref_components) # stack same index returns from each invocation - stacked_returns = [] - for r in range(num_returns): - stacked_returns.append( - torch.stack([o[r] for o in out_ref_components], dim=0) - ) + stacked_returns = [ + torch.stack([o[r] for o in out_ref_components], dim=0) + for r in range(num_returns) + ] return type(out_ref_components[0])(stacked_returns) return torch.stack(out_ref_components, dim=0) diff --git a/torch/utils/_device.py b/torch/utils/_device.py index d9e885256ec..d7903fc3b46 100644 --- a/torch/utils/_device.py +++ b/torch/utils/_device.py @@ -70,9 +70,7 @@ class DeviceContext(TorchFunctionMode): # If we set default device within a function mode context # exiting that context mode will pop the device function mode off # of the stack incorrectly - cur_stack = [] - for _ in range(_len_torch_function_stack()): - cur_stack.append(_pop_mode()) + cur_stack = [_pop_mode() for _ in range(_len_torch_function_stack())] _push_mode(self) diff --git a/torch/utils/_pytree.py b/torch/utils/_pytree.py index de9fbebbe37..79f87a25c24 100644 --- a/torch/utils/_pytree.py +++ b/torch/utils/_pytree.py @@ -1412,9 +1412,9 @@ def _json_to_treespec(json_schema: DumpableContext) -> TreeSpec: else: context = serialize_node_def.from_dumpable_context(json_schema["context"]) - children_specs = [] - for child_string in json_schema["children_spec"]: - children_specs.append(_json_to_treespec(child_string)) + children_specs = [ + _json_to_treespec(child_string) for child_string in json_schema["children_spec"] + ] return TreeSpec(typ, context, children_specs) diff --git a/torch/utils/benchmark/utils/compare.py b/torch/utils/benchmark/utils/compare.py index 36c5a77cd1e..ee35f450045 100644 --- a/torch/utils/benchmark/utils/compare.py +++ b/torch/utils/benchmark/utils/compare.py @@ -244,8 +244,7 @@ class Table: def render(self) -> str: string_rows = [[""] + self.column_keys] - for r in self.rows: - string_rows.append(r.as_column_strings()) + string_rows.extend(r.as_column_strings() for r in self.rows) num_cols = max(len(i) for i in string_rows) for sr in string_rows: sr.extend(["" for _ in range(num_cols - len(sr))]) @@ -327,9 +326,7 @@ class Compare: def _render(self): results = common.Measurement.merge(self._results) grouped_results = self._group_by_label(results) - output = [] - for group in grouped_results.values(): - output.append(self._layout(group)) + output = [self._layout(group) for group in grouped_results.values()] return output def _group_by_label(self, results: List[common.Measurement]): diff --git a/torch/utils/data/_utils/worker.py b/torch/utils/data/_utils/worker.py index c61b78d42d8..a275e2e86b6 100644 --- a/torch/utils/data/_utils/worker.py +++ b/torch/utils/data/_utils/worker.py @@ -94,9 +94,7 @@ class WorkerInfo: return super().__setattr__(key, val) def __repr__(self): - items = [] - for k in self.__keys: - items.append(f"{k}={getattr(self, k)}") + items = [f"{k}={getattr(self, k)}" for k in self.__keys] return f"{self.__class__.__name__}({', '.join(items)})" diff --git a/torch/utils/data/datapipes/dataframe/datapipes.py b/torch/utils/data/datapipes/dataframe/datapipes.py index f3f6e7c28e0..d9460788e54 100644 --- a/torch/utils/data/datapipes/dataframe/datapipes.py +++ b/torch/utils/data/datapipes/dataframe/datapipes.py @@ -1,5 +1,6 @@ # mypy: allow-untyped-defs import random +from typing import Any, List from torch.utils.data.datapipes._decorator import functional_datapipe from torch.utils.data.datapipes.dataframe import dataframe_wrapper as df_wrapper @@ -63,12 +64,13 @@ class ShuffleDataFramesPipe(DFIterDataPipe): def __iter__(self): size = None - all_buffer = [] + all_buffer: List[Any] = [] for df in self.source_datapipe: if size is None: size = df_wrapper.get_len(df) - for i in range(df_wrapper.get_len(df)): - all_buffer.append(df_wrapper.get_item(df, i)) + all_buffer.extend( + df_wrapper.get_item(df, i) for i in range(df_wrapper.get_len(df)) + ) random.shuffle(all_buffer) buffer = [] for df in all_buffer: diff --git a/torch/utils/data/datapipes/iter/sharding.py b/torch/utils/data/datapipes/iter/sharding.py index 7f86718e0a9..f5ca3f06b24 100644 --- a/torch/utils/data/datapipes/iter/sharding.py +++ b/torch/utils/data/datapipes/iter/sharding.py @@ -69,10 +69,11 @@ class ShardingFilterIterDataPipe(_ShardingIterDataPipe): self._update_num_of_instances() def _update_num_of_instances(self): - sorted_sharding_groups = [] - for key in sorted(self.groups.keys()): - if self.sharding_group_filter is None or key == self.sharding_group_filter: - sorted_sharding_groups.append(self.groups[key]) + sorted_sharding_groups = [ + self.groups[key] + for key in sorted(self.groups.keys()) + if self.sharding_group_filter is None or key == self.sharding_group_filter + ] sorted_sharding_groups.reverse() diff --git a/torch/utils/data/datapipes/map/grouping.py b/torch/utils/data/datapipes/map/grouping.py index 33284b4d04c..33aeaf34454 100644 --- a/torch/utils/data/datapipes/map/grouping.py +++ b/torch/utils/data/datapipes/map/grouping.py @@ -55,8 +55,7 @@ class BatcherMapDataPipe(MapDataPipe[DataChunk]): batch: List = [] indices = range(index * self.batch_size, (index + 1) * self.batch_size) try: - for i in indices: - batch.append(self.datapipe[i]) + batch.extend(self.datapipe[i] for i in indices) return self.wrapper_class(batch) except IndexError as e: if not self.drop_last and len(batch) > 0: diff --git a/torch/utils/hipify/hipify_python.py b/torch/utils/hipify/hipify_python.py index b4bd96b3817..008aabe8215 100755 --- a/torch/utils/hipify/hipify_python.py +++ b/torch/utils/hipify/hipify_python.py @@ -1149,14 +1149,12 @@ def hipify( header_include_dir_path = Path(header_include_dir) else: header_include_dir_path = Path(os.path.join(output_directory, header_include_dir)) - for path in header_include_dir_path.rglob('*'): - if ( - path.is_file() - and _fnmatch(str(path), includes) - and (not _fnmatch(str(path), ignores)) - and match_extensions(path.name, header_extensions) - ): - all_files.append(str(path)) + all_files.extend( + str(path) for path in header_include_dir_path.rglob('*') if path.is_file() + and _fnmatch(str(path), includes) + and (not _fnmatch(str(path), ignores)) + and match_extensions(path.name, header_extensions) + ) if clean_ctx is None: clean_ctx = GeneratedFileCleaner(keep_intermediates=True) diff --git a/torch/utils/hooks.py b/torch/utils/hooks.py index ee828034bdf..f01c65ddb0e 100644 --- a/torch/utils/hooks.py +++ b/torch/utils/hooks.py @@ -120,9 +120,7 @@ class BackwardHook: return tuple(res) def _unpack_none(self, indices, values): - res = [] - for idx in indices: - res.append(values[idx]) + res = [values[idx] for idx in indices] return tuple(res) diff --git a/torch/utils/mobile_optimizer.py b/torch/utils/mobile_optimizer.py index 6d2230da8ae..faaf48283e6 100644 --- a/torch/utils/mobile_optimizer.py +++ b/torch/utils/mobile_optimizer.py @@ -49,10 +49,7 @@ def optimize_for_mobile( if all(hasattr(script_module, method) for method in bundled_inputs_attributes): preserved_methods_str = list(set(preserved_methods_str + bundled_inputs_attributes)) - non_exist_methods = [] - for method in preserved_methods_str: - if not hasattr(script_module, method): - non_exist_methods.append(method) + non_exist_methods = [method for method in preserved_methods_str if not hasattr(script_module, method)] if non_exist_methods: raise AttributeError( f"The following methods to preserve do not exist in script_module: {', '.join(non_exist_methods)}") diff --git a/torch/utils/tensorboard/_onnx_graph.py b/torch/utils/tensorboard/_onnx_graph.py index c744ca8719f..3b7381737b3 100644 --- a/torch/utils/tensorboard/_onnx_graph.py +++ b/torch/utils/tensorboard/_onnx_graph.py @@ -41,9 +41,7 @@ def parse(graph): ) for node in graph.node: - _attr = [] - for s in node.attribute: - _attr.append(" = ".join([str(f[1]) for f in s.ListFields()])) + _attr = [" = ".join([str(f[1]) for f in s.ListFields()]) for s in node.attribute] attr = ", ".join(_attr).encode(encoding="utf_8") print(node.output[0]) nodes.append( diff --git a/torch/utils/tensorboard/_pytorch_graph.py b/torch/utils/tensorboard/_pytorch_graph.py index 502675ef956..840a18e35c0 100644 --- a/torch/utils/tensorboard/_pytorch_graph.py +++ b/torch/utils/tensorboard/_pytorch_graph.py @@ -55,11 +55,11 @@ class NodeBase: def __repr__(self): repr = [] repr.append(str(type(self))) - for m in dir(self): - if "__" not in m: - repr.append( - m + ": " + str(getattr(self, m)) + str(type(getattr(self, m))) - ) + repr.extend( + m + ": " + str(getattr(self, m)) + str(type(getattr(self, m))) + for m in dir(self) + if "__" not in m + ) return "\n".join(repr) + "\n\n" @@ -216,17 +216,16 @@ class GraphPy: """Convert graph representation of GraphPy object to TensorBoard required format.""" # TODO: compute correct memory usage and CPU time once # PyTorch supports it - nodes = [] - for v in self.nodes_io.values(): - nodes.append( - node_proto( - v.debugName, - input=v.inputs, - outputsize=v.tensor_size, - op=v.kind, - attributes=v.attributes, - ) + nodes = [ + node_proto( + v.debugName, + input=v.inputs, + outputsize=v.tensor_size, + op=v.kind, + attributes=v.attributes, ) + for v in self.nodes_io.values() + ] return nodes diff --git a/torch/xpu/__init__.py b/torch/xpu/__init__.py index 380c30bcc29..55216f9bd4c 100644 --- a/torch/xpu/__init__.py +++ b/torch/xpu/__init__.py @@ -122,9 +122,7 @@ def _lazy_init(): # just return without initializing in that case. _tls.is_initializing = True - for calls in _lazy_seed_tracker.get_calls(): - if calls: - _queued_calls.append(calls) + _queued_calls.extend(calls for calls in _lazy_seed_tracker.get_calls() if calls) try: for queued_call, orig_traceback in _queued_calls: diff --git a/torch/xpu/random.py b/torch/xpu/random.py index b8631ddc185..5bc14241863 100644 --- a/torch/xpu/random.py +++ b/torch/xpu/random.py @@ -31,9 +31,7 @@ def get_rng_state(device: Union[int, str, torch.device] = "xpu") -> Tensor: def get_rng_state_all() -> List[Tensor]: r"""Return a list of ByteTensor representing the random number states of all devices.""" - results = [] - for i in range(device_count()): - results.append(get_rng_state(i)) + results = [get_rng_state(i) for i in range(device_count())] return results diff --git a/torchgen/dest/register_dispatch_key.py b/torchgen/dest/register_dispatch_key.py index 03f94f532de..015537df12e 100644 --- a/torchgen/dest/register_dispatch_key.py +++ b/torchgen/dest/register_dispatch_key.py @@ -879,13 +879,13 @@ return {sig.name()}({', '.join(e.expr for e in translate(cpp_sig.arguments(), si self.g.out.precomputed.add, ] for precomputed_elems in precomputed_values: - for arg in precomputed_elems: - context.append( - Expr( - expr=f"precompute.{arg.name}", - type=structured.argument_type(arg, binds=arg.name), - ) + context.extend( + Expr( + expr=f"precompute.{arg.name}", + type=structured.argument_type(arg, binds=arg.name), ) + for arg in precomputed_elems + ) # Add a use of the precompute struct so FB internal compilers don't # complain that there is an unused variable. diff --git a/torchgen/gen.py b/torchgen/gen.py index d113adf5e4b..e1fbccfab07 100644 --- a/torchgen/gen.py +++ b/torchgen/gen.py @@ -2615,9 +2615,11 @@ codegen to generate the correct cpp call for this op. Contact AOTInductor team f view_map: dict[OperatorName, NativeFunction] = { f.func.name: f for f in concatMap(lambda g: list(g.functions()), view_groups) } - for f in native_functions: - if f.func.name not in structured_map and f.func.name not in view_map: - all_groups.append(f) + all_groups.extend( + f + for f in native_functions + if f.func.name not in structured_map and f.func.name not in view_map + ) cpu_fm.write_sharded( "RegisterFunctionalization.cpp", diff --git a/torchgen/model.py b/torchgen/model.py index cd522780659..f4f57f2ae13 100644 --- a/torchgen/model.py +++ b/torchgen/model.py @@ -5,7 +5,7 @@ import itertools import re from dataclasses import dataclass from enum import auto, Enum -from typing import Callable, Iterator, Sequence +from typing import Callable, Iterator, List, Sequence from torchgen.utils import assert_never, NamespaceHelper, OrderedSet @@ -245,10 +245,9 @@ class _TorchDispatchModeKey(Enum): def codegen_per_backend_entries() -> str: - r = [] + r: List[str] = [] for fk in FUNCTIONALITY_KEYS: - for bc in BACKEND_COMPONENTS: - r.append(f" {fk}{bc} = auto()") + r.extend(f" {fk}{bc} = auto()" for bc in BACKEND_COMPONENTS) return "\n".join(r) diff --git a/torchgen/operator_versions/gen_mobile_upgraders.py b/torchgen/operator_versions/gen_mobile_upgraders.py index 5e4034bc4d6..ea7a8f3effe 100644 --- a/torchgen/operator_versions/gen_mobile_upgraders.py +++ b/torchgen/operator_versions/gen_mobile_upgraders.py @@ -189,15 +189,14 @@ PER_OPERATOR_UPGRADER_LIST = CodeTemplate( def construct_instruction(instruction_list_from_yaml: list[Any]) -> str: - instruction_list_part = [] - for instruction in instruction_list_from_yaml: - instruction_list_part.append( - ONE_INSTRUCTION.substitute( - operator_name=instruction[0], - X=instruction[1], - N=instruction[2], - ) + instruction_list_part = [ + ONE_INSTRUCTION.substitute( + operator_name=instruction[0], + X=instruction[1], + N=instruction[2], ) + for instruction in instruction_list_from_yaml + ] return INSTRUCTION_LIST.substitute( instruction_list="".join(instruction_list_part).lstrip("\n") ) @@ -230,24 +229,23 @@ def construct_constants(constants_list_from_yaml: list[Any]) -> str: def construct_operators(operator_list_from_yaml: list[Any]) -> str: - operator_list_part = [] - for operator in operator_list_from_yaml: - operator_list_part.append( - ONE_OPERATOTR_STRING.substitute( - operator_name=operator[0], - overload_name=operator[1], - num_of_args=operator[2], - ) + operator_list_part = [ + ONE_OPERATOTR_STRING.substitute( + operator_name=operator[0], + overload_name=operator[1], + num_of_args=operator[2], ) + for operator in operator_list_from_yaml + ] return OPERATOR_STRING_LIST.substitute( operator_string_list="".join(operator_list_part).lstrip("\n") ) def construct_types(types_tr_list_from_yaml: list[Any]) -> str: - types_tr_list_part = [] - for types_tr in types_tr_list_from_yaml: - types_tr_list_part.append(ONE_TYPE.substitute(type_str=types_tr)) + types_tr_list_part = [ + ONE_TYPE.substitute(type_str=types_tr) for types_tr in types_tr_list_from_yaml + ] if len(types_tr_list_part) == 0: return TYPE_LIST_EMPTY return TYPE_LIST.substitute(type_list="".join(types_tr_list_part).lstrip("\n"))