From 3255e7872bc94d95c63db844f4279d50884741d7 Mon Sep 17 00:00:00 2001 From: Yuanyuan Chen Date: Sun, 19 Oct 2025 00:59:28 +0000 Subject: [PATCH] Enable all flake8-logging-format rules (#164655) These rules are enabled by removing existing suppressions. Pull Request resolved: https://github.com/pytorch/pytorch/pull/164655 Approved by: https://github.com/janeyx99, https://github.com/mlazos --- .ci/lumen_cli/cli/lib/common/git_helper.py | 4 ++-- .flake8 | 2 -- benchmarks/dynamo/common.py | 4 ++-- .../microbenchmarks/operator_inp_utils.py | 4 ++-- pyproject.toml | 2 -- test/test_quantization.py | 24 +++++++++---------- tools/linter/adapters/clangformat_linter.py | 2 +- tools/linter/adapters/flake8_linter.py | 2 +- tools/linter/adapters/ruff_linter.py | 2 +- tools/linter/adapters/s3_init.py | 4 ++-- tools/packaging/build_wheel.py | 2 +- torch/_dynamo/convert_frame.py | 2 +- torch/_dynamo/eval_frame.py | 6 +++-- torch/_dynamo/exc.py | 4 ++-- torch/_dynamo/graph_region_tracker.py | 2 +- torch/_dynamo/package.py | 4 ++-- torch/_dynamo/precompile_context.py | 2 +- torch/_dynamo/variables/builtin.py | 6 ++--- torch/_dynamo/variables/higher_order_ops.py | 2 +- .../_aot_autograd/autograd_cache.py | 8 +++---- torch/_inductor/codecache.py | 2 +- torch/_inductor/codegen/common.py | 2 +- torch/_inductor/codegen/cuda/cuda_env.py | 8 +++---- torch/_inductor/codegen/cuda/cutlass_cache.py | 6 ++--- torch/_inductor/codegen/cuda/cutlass_utils.py | 8 +++---- .../codegen/cutedsl/cutedsl_template.py | 4 ++-- .../rocm/ck_universal_gemm_template.py | 2 +- torch/_inductor/codegen/triton.py | 2 +- torch/_inductor/comm_analysis.py | 2 +- torch/_inductor/compile_fx_ext.py | 2 +- .../_inductor/compile_worker/subproc_pool.py | 4 ++-- torch/_inductor/fx_passes/numeric_utils.py | 2 +- torch/_inductor/memory.py | 8 +++---- .../runtime/coordinate_descent_tuner.py | 2 +- torch/_inductor/runtime/triton_heuristics.py | 8 +++---- torch/_inductor/scheduler.py | 8 +++---- torch/_inductor/select_algorithm.py | 15 ++++++------ torch/_inductor/triton_bundler.py | 6 ++--- torch/_library/fake_class_registry.py | 2 +- torch/_subclasses/fake_tensor.py | 2 +- .../_experimental/checkpoint_process.py | 12 ++++------ torch/distributed/distributed_c10d.py | 2 +- torch/distributed/elastic/agent/server/api.py | 2 +- .../elastic/multiprocessing/api.py | 12 ++++++---- .../elastic/multiprocessing/tail_log.py | 5 ++-- .../elastic/rendezvous/etcd_rendezvous.py | 6 ++--- .../elastic/rendezvous/etcd_server.py | 2 +- torch/distributed/pipelining/schedules.py | 2 +- torch/distributed/rpc/api.py | 8 +++---- torch/export/__init__.py | 4 ++-- torch/export/pt2_archive/_package.py | 4 ++-- torch/fx/experimental/symbolic_shapes.py | 4 ++-- .../onnx/_internal/exporter/_registration.py | 2 +- .../onnx/_internal/exporter/_verification.py | 7 ++---- torch/testing/_internal/common_distributed.py | 16 ++++++------- 55 files changed, 131 insertions(+), 140 deletions(-) diff --git a/.ci/lumen_cli/cli/lib/common/git_helper.py b/.ci/lumen_cli/cli/lib/common/git_helper.py index 9833caca956c..c4d6f8a0b6f5 100644 --- a/.ci/lumen_cli/cli/lib/common/git_helper.py +++ b/.ci/lumen_cli/cli/lib/common/git_helper.py @@ -57,8 +57,8 @@ def clone_external_repo(target: str, repo: str, dst: str = "", update_submodules logger.info("Successfully cloned %s", target) return r, commit - except GitCommandError as e: - logger.error("Git operation failed: %s", e) + except GitCommandError: + logger.exception("Git operation failed") raise diff --git a/.flake8 b/.flake8 index 2be8eab0dc83..aff8849fa6d4 100644 --- a/.flake8 +++ b/.flake8 @@ -13,8 +13,6 @@ ignore = EXE001, # these ignores are from flake8-bugbear; please fix! B007,B008,B017,B019,B023,B028,B903,B905,B906,B907,B908,B910 - # these ignores are from flake8-logging-format; please fix! - G100,G101,G200 # these ignores are from flake8-simplify. please fix or ignore with commented reason SIM105,SIM108,SIM110,SIM111,SIM113,SIM114,SIM115,SIM116,SIM117,SIM118,SIM119,SIM12, # SIM104 is already covered by pyupgrade ruff diff --git a/benchmarks/dynamo/common.py b/benchmarks/dynamo/common.py index f3b75e9f72ea..54900de1ed91 100644 --- a/benchmarks/dynamo/common.py +++ b/benchmarks/dynamo/common.py @@ -1751,8 +1751,8 @@ def maybe_snapshot_memory(should_snapshot_memory, suffix): f"{output_filename.rstrip('.csv')}_{suffix}.pickle", ) ) - except Exception as e: - log.error("Failed to save memory snapshot, %s", e) + except Exception: + log.exception("Failed to save memory snapshot") torch.cuda.memory._record_memory_history(enabled=None) diff --git a/benchmarks/dynamo/microbenchmarks/operator_inp_utils.py b/benchmarks/dynamo/microbenchmarks/operator_inp_utils.py index f1f9ea9b30ba..8a6978dd448b 100644 --- a/benchmarks/dynamo/microbenchmarks/operator_inp_utils.py +++ b/benchmarks/dynamo/microbenchmarks/operator_inp_utils.py @@ -296,8 +296,8 @@ class OperatorInputsLoader: for key in self.operator_db.keys(): try: op = eval(key) - except AttributeError as ae: - log.warning("Evaluating an op name into an OpOverload: %s", ae) + except AttributeError: + log.warning("Evaluating an op name into an OpOverload", exc_info=True) continue yield op diff --git a/pyproject.toml b/pyproject.toml index f18368b90d8d..5bb7f301b8a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -159,8 +159,6 @@ ignore = [ "EXE001", "F405", "FURB122", # writelines - # these ignores are from flake8-logging-format; please fix! - "G101", # these ignores are from ruff NPY; please fix! "NPY002", # these ignores are from ruff PERF; please fix! diff --git a/test/test_quantization.py b/test/test_quantization.py index 6d72da3279e1..01006e3f6e22 100644 --- a/test/test_quantization.py +++ b/test/test_quantization.py @@ -72,7 +72,7 @@ try: except ImportError as e: # In FBCode we separate FX out into a separate target for the sake of dev # velocity. These are covered by a separate test target `quantization_fx` - log.warning(e) + log.warning(e) # noqa:G200 # PyTorch 2 Export Quantization try: @@ -94,7 +94,7 @@ try: except ImportError as e: # In FBCode we separate PT2 out into a separate target for the sake of dev # velocity. These are covered by a separate test target `quantization_pt2e` - log.warning(e) + log.warning(e) # noqa:G200 try: from quantization.fx.test_numeric_suite_fx import TestFXGraphMatcher # noqa: F401 @@ -103,7 +103,7 @@ try: from quantization.fx.test_numeric_suite_fx import TestFXNumericSuiteNShadows # noqa: F401 from quantization.fx.test_numeric_suite_fx import TestFXNumericSuiteCoreAPIsModels # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 # Test the model report module try: @@ -115,19 +115,19 @@ try: from quantization.fx.test_model_report_fx import TestFxDetectOutliers # noqa: F401 from quantization.fx.test_model_report_fx import TestFxModelReportVisualizer # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 # Equalization for FX mode try: from quantization.fx.test_equalize_fx import TestEqualizeFx # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 # Backward Compatibility. Tests serialization and BC for quantized modules. try: from quantization.bc.test_backward_compatibility import TestSerialization # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 # JIT Graph Mode Quantization from quantization.jit.test_quantize_jit import TestQuantizeJit # noqa: F401 @@ -146,29 +146,29 @@ from quantization.ao_migration.test_ao_migration import TestAOMigrationNNIntrins try: from quantization.ao_migration.test_quantization_fx import TestAOMigrationQuantizationFx # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 # Experimental functionality try: from quantization.core.experimental.test_bits import TestBitsCPU # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 try: from quantization.core.experimental.test_bits import TestBitsCUDA # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 try: from quantization.core.experimental.test_floatx import TestFloat8DtypeCPU # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 try: from quantization.core.experimental.test_floatx import TestFloat8DtypeCUDA # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 try: from quantization.core.experimental.test_floatx import TestFloat8DtypeCPUOnlyCPU # noqa: F401 except ImportError as e: - log.warning(e) + log.warning(e) # noqa:G200 if __name__ == '__main__': run_tests() diff --git a/tools/linter/adapters/clangformat_linter.py b/tools/linter/adapters/clangformat_linter.py index 9289dcd6375f..0d82ddd939b1 100644 --- a/tools/linter/adapters/clangformat_linter.py +++ b/tools/linter/adapters/clangformat_linter.py @@ -73,7 +73,7 @@ def run_command( if remaining_retries == 0: raise err remaining_retries -= 1 - logging.warning( + logging.warning( # noqa: G200 "(%s/%s) Retrying because command failed with: %r", retries - remaining_retries, retries, diff --git a/tools/linter/adapters/flake8_linter.py b/tools/linter/adapters/flake8_linter.py index 0bc522821cab..d51ef09fec75 100644 --- a/tools/linter/adapters/flake8_linter.py +++ b/tools/linter/adapters/flake8_linter.py @@ -172,7 +172,7 @@ def run_command( ): raise err remaining_retries -= 1 - logging.warning( + logging.warning( # noqa: G200 "(%s/%s) Retrying because command failed with: %r", retries - remaining_retries, retries, diff --git a/tools/linter/adapters/ruff_linter.py b/tools/linter/adapters/ruff_linter.py index d8120461b13b..28feae002f36 100644 --- a/tools/linter/adapters/ruff_linter.py +++ b/tools/linter/adapters/ruff_linter.py @@ -112,7 +112,7 @@ def run_command( if remaining_retries == 0: raise err remaining_retries -= 1 - logging.warning( + logging.warning( # noqa: G200 "(%s/%s) Retrying because command failed with: %r", retries - remaining_retries, retries, diff --git a/tools/linter/adapters/s3_init.py b/tools/linter/adapters/s3_init.py index b33497d2ce6a..154e3d56ad26 100644 --- a/tools/linter/adapters/s3_init.py +++ b/tools/linter/adapters/s3_init.py @@ -95,8 +95,8 @@ Deleting %s just to be safe. try: binary_path.unlink() - except OSError as e: - logging.critical("Failed to delete binary: %s", e) + except OSError: + logging.critical("Failed to delete binary", exc_info=True) logging.critical( "Delete this binary as soon as possible and do not execute it!" ) diff --git a/tools/packaging/build_wheel.py b/tools/packaging/build_wheel.py index dad2d8084967..5f6f262ab820 100644 --- a/tools/packaging/build_wheel.py +++ b/tools/packaging/build_wheel.py @@ -114,7 +114,7 @@ def _find_manylinux_interpreters() -> list[str]: ) except subprocess.CalledProcessError as e: - logger.debug("Failed to get version for %s: %s", python_path, e) + logger.debug("Failed to get version for %s: %s", python_path, e) # noqa:G200 continue return interpreters diff --git a/torch/_dynamo/convert_frame.py b/torch/_dynamo/convert_frame.py index 6f87d1cd445e..e1b4e051672e 100644 --- a/torch/_dynamo/convert_frame.py +++ b/torch/_dynamo/convert_frame.py @@ -1215,7 +1215,7 @@ def compile_frame( # type: ignore[return] except exc.SkipFrame as e: if not isinstance(e, exc.TensorifyScalarRestartAnalysis): TensorifyState.clear() - log.debug( + log.debug( # noqa: G200 "Skipping frame %s %s \ %s %s", e, diff --git a/torch/_dynamo/eval_frame.py b/torch/_dynamo/eval_frame.py index 451776ef25fd..f0b32976e5be 100644 --- a/torch/_dynamo/eval_frame.py +++ b/torch/_dynamo/eval_frame.py @@ -753,8 +753,10 @@ class _TorchDynamoContext: fn, result.dynamo, ignore_inlined_sources=False ) self._package.install(result.backends) - except RuntimeError as e: - log.warning("Failed to load entry from dynamo cache: %s", e) + except RuntimeError: + log.warning( + "Failed to load entry from dynamo cache", exc_info=True + ) self._package.initialize(fn, None, ignore_inlined_sources=False) fn = innermost_fn(fn) diff --git a/torch/_dynamo/exc.py b/torch/_dynamo/exc.py index 2667bee7aacb..295fed5618ea 100644 --- a/torch/_dynamo/exc.py +++ b/torch/_dynamo/exc.py @@ -532,8 +532,8 @@ def _load_gb_type_to_gb_id_map() -> dict[str, Any]: ) with open(registry_path) as f: registry = json.load(f) - except Exception as e: - log.error("Error accessing the registry file: %s", e) + except Exception: + log.exception("Error accessing the registry file") registry = {} mapping = {} diff --git a/torch/_dynamo/graph_region_tracker.py b/torch/_dynamo/graph_region_tracker.py index 19211bd4491b..5fcf4e83cacb 100644 --- a/torch/_dynamo/graph_region_tracker.py +++ b/torch/_dynamo/graph_region_tracker.py @@ -269,7 +269,7 @@ class GraphRegionTracker: duplicates.append(node) self.node_to_duplicates[node] = duplicates except NodeHashException as e: - log.debug("Unable to hash node %s with exception %s", node, e) + log.debug("Unable to hash node %s with exception %s", node, e) # noqa: G200 def track_node_mutations( self, diff --git a/torch/_dynamo/package.py b/torch/_dynamo/package.py index 9c5dec0a98f9..b61728d03f05 100644 --- a/torch/_dynamo/package.py +++ b/torch/_dynamo/package.py @@ -1122,9 +1122,9 @@ class DiskDynamoCache(DiskDynamoStore): result = super().load_cache_entry(key) counters["dynamo_cache"]["dynamo_cache_hit"] += 1 return result - except Exception as e: + except Exception: counters["dynamo_cache"]["dynamo_cache_error"] += 1 - logger.warning("Failed to load package from path %s: %s", path, str(e)) + logger.warning("Failed to load package from path %s", exc_info=True) return None logger.info("No package found for %s", key) counters["dynamo_cache"]["dynamo_cache_miss"] += 1 diff --git a/torch/_dynamo/precompile_context.py b/torch/_dynamo/precompile_context.py index d3b2c7df1f47..65ceab92262c 100644 --- a/torch/_dynamo/precompile_context.py +++ b/torch/_dynamo/precompile_context.py @@ -203,7 +203,7 @@ class PrecompileContext: if result is not None: precompile_cache_entries[key] = result except Exception as e: - logger.warning("Failed to create cache entry %s: %s", key, str(e)) + logger.warning("Failed to create cache entry %s", key, exc_info=True) error = e data = json.dumps( diff --git a/torch/_dynamo/variables/builtin.py b/torch/_dynamo/variables/builtin.py index 09bdb81150e6..24136b5ddad6 100644 --- a/torch/_dynamo/variables/builtin.py +++ b/torch/_dynamo/variables/builtin.py @@ -1041,7 +1041,7 @@ class BuiltinVariable(VariableTracker): except TypeError as e: has_constant_handler = obj.has_constant_handler(args, kwargs) if not has_constant_handler: - log.warning( + log.warning( # noqa: G200 "incorrect arg count %s %s and no constant handler", self_handler, e, @@ -1560,9 +1560,9 @@ class BuiltinVariable(VariableTracker): try: # Only supports certain function types user_func_variable = variables.UserFunctionVariable(bound_method) - except AssertionError as e: + except AssertionError: # Won't be able to do inline the str method, return to avoid graph break - log.warning("Failed to create UserFunctionVariable: %s", e) + log.warning("Failed to create UserFunctionVariable", exc_info=True) return # Inline the user function diff --git a/torch/_dynamo/variables/higher_order_ops.py b/torch/_dynamo/variables/higher_order_ops.py index 956eb4676018..753b0a5414f0 100644 --- a/torch/_dynamo/variables/higher_order_ops.py +++ b/torch/_dynamo/variables/higher_order_ops.py @@ -1183,7 +1183,7 @@ def speculate_subgraph( f"fall back to eager-mode PyTorch, which could lead to a slowdown." ) log.info(msg) - log.info(ex) + log.info(ex) # noqa: G200 raise ex diff --git a/torch/_functorch/_aot_autograd/autograd_cache.py b/torch/_functorch/_aot_autograd/autograd_cache.py index 0ac2407269ac..47506aff1ef2 100644 --- a/torch/_functorch/_aot_autograd/autograd_cache.py +++ b/torch/_functorch/_aot_autograd/autograd_cache.py @@ -1221,7 +1221,7 @@ class AOTAutogradCache(GuardedCache[GenericAOTAutogradCacheEntry]): except Exception as e: cache_key = None counters["aot_autograd"]["autograd_cache_bypass"] += 1 - log.info("Bypassing autograd cache due to: %s", e) + log.info("Bypassing autograd cache due to: %s", e) # noqa: G200 cache_state = "bypass" cache_event_time = time.time_ns() cache_info["cache_bypass_reason"] = str(e) @@ -1368,7 +1368,7 @@ class AOTAutogradCache(GuardedCache[GenericAOTAutogradCacheEntry]): ), ) except Exception as e: - log.info("AOTAutograd cache unable to load compiled graph: %s", e) + log.info("AOTAutograd cache unable to load compiled graph: %s", e) # noqa: G200 if config.strict_autograd_cache: raise e if entry is not None: @@ -1414,12 +1414,12 @@ class AOTAutogradCache(GuardedCache[GenericAOTAutogradCacheEntry]): counters["aot_autograd"]["autograd_cache_saved"] += 1 except BypassAOTAutogradCache as e: counters["aot_autograd"]["autograd_cache_bypass"] += 1 - log.info("Bypassing autograd cache due to: %s", e) + log.info("Bypassing autograd cache due to: %s", e) # noqa: G200 if remote: log_cache_bypass("bypass_aot_autograd", str(e)) return None except Exception as e: - log.info("AOTAutograd cache unable to serialize compiled graph: %s", e) + log.info("AOTAutograd cache unable to serialize compiled graph: %s", e) # noqa: G200 if remote: log_cache_bypass( "bypass_aot_autograd", "Unable to serialize: " + str(e) diff --git a/torch/_inductor/codecache.py b/torch/_inductor/codecache.py index 5cc178db2fc3..3ead901e1a36 100644 --- a/torch/_inductor/codecache.py +++ b/torch/_inductor/codecache.py @@ -1516,7 +1516,7 @@ class FxGraphCache(GuardedCache[CompiledFxGraph]): ) except BypassFxGraphCache as e: counters["inductor"]["fxgraph_cache_bypass"] += 1 - log.info("Bypassing FX Graph Cache because '%s'", e) + log.info("Bypassing FX Graph Cache because '%s'", e) # noqa: G200 if remote: log_cache_bypass("bypass_fx_graph", str(e)) cache_info = { diff --git a/torch/_inductor/codegen/common.py b/torch/_inductor/codegen/common.py index 743baec01dfa..5a953f80a1a2 100644 --- a/torch/_inductor/codegen/common.py +++ b/torch/_inductor/codegen/common.py @@ -2493,7 +2493,7 @@ class KernelTemplate: choices.append(self.generate(**kwargs)) return None except NotImplementedError as e: - log.info( + log.info( # noqa: G200 "Cannot Append Choice: %s. KernelTemplate type is %s", e, type(self), diff --git a/torch/_inductor/codegen/cuda/cuda_env.py b/torch/_inductor/codegen/cuda/cuda_env.py index 3eb65273285e..9ca3afbd9ca5 100644 --- a/torch/_inductor/codegen/cuda/cuda_env.py +++ b/torch/_inductor/codegen/cuda/cuda_env.py @@ -22,8 +22,8 @@ def get_cuda_arch() -> Optional[str]: major, minor = torch.cuda.get_device_capability(0) return str(major * 10 + minor) return str(cuda_arch) - except Exception as e: - log.error("Error getting cuda arch: %s", e) + except Exception: + log.exception("Error getting cuda arch") return None @@ -45,8 +45,8 @@ def get_cuda_version() -> Optional[str]: if cuda_version is None: cuda_version = torch.version.cuda return cuda_version - except Exception as e: - log.error("Error getting cuda version: %s", e) + except Exception: + log.exception("Error getting cuda version") return None diff --git a/torch/_inductor/codegen/cuda/cutlass_cache.py b/torch/_inductor/codegen/cuda/cutlass_cache.py index 519125888c16..66db98867b41 100644 --- a/torch/_inductor/codegen/cuda/cutlass_cache.py +++ b/torch/_inductor/codegen/cuda/cutlass_cache.py @@ -94,11 +94,11 @@ def maybe_fetch_ops() -> Optional[list[Any]]: assert isinstance(serialized_ops, list), ( f"Expected serialized ops is a list, got {type(serialized_ops)}" ) - except Exception as e: + except Exception: log.warning( - "Failed to load CUTLASS config %s from local cache: %s", + "Failed to load CUTLASS config %s from local cache", filename, - e, + exc_info=True, ) serialized_ops = None elif config.is_fbcode(): diff --git a/torch/_inductor/codegen/cuda/cutlass_utils.py b/torch/_inductor/codegen/cuda/cutlass_utils.py index 2f673e92e24b..be812347188b 100644 --- a/torch/_inductor/codegen/cuda/cutlass_utils.py +++ b/torch/_inductor/codegen/cuda/cutlass_utils.py @@ -53,8 +53,8 @@ def move_cutlass_compiled_cache() -> None: filename = os.path.basename(cutlass_cppgen.CACHE_FILE) shutil.move(cutlass_cppgen.CACHE_FILE, os.path.join(cache_dir(), filename)) log.debug("Moved CUTLASS compiled cache file to %s", cache_dir()) - except OSError as e: - log.warning("Failed to move CUTLASS compiled cache file: %s", e) + except OSError: + log.warning("Failed to move CUTLASS compiled cache file", exc_info=True) def _rename_cutlass_import(content: str, cutlass_modules: list[str]) -> str: @@ -79,7 +79,7 @@ def try_import_cutlass() -> bool: import cutlass_cppgen # type: ignore[import-not-found] # noqa: F401 import cutlass_library # type: ignore[import-not-found] except ImportError as e: - log.warning( + log.warning( # noqa: G200 "Failed to import CUTLASS packages in fbcode: %s, ignoring the CUTLASS backend.", str(e), ) @@ -164,7 +164,7 @@ def try_import_cutlass() -> bool: return True except ImportError as e: - log.debug( + log.debug( # noqa: G200 "Failed to import CUTLASS packages: %s, ignoring the CUTLASS backend.", str(e), ) diff --git a/torch/_inductor/codegen/cutedsl/cutedsl_template.py b/torch/_inductor/codegen/cutedsl/cutedsl_template.py index 016edb63a352..31ff7e43afc5 100644 --- a/torch/_inductor/codegen/cutedsl/cutedsl_template.py +++ b/torch/_inductor/codegen/cutedsl/cutedsl_template.py @@ -58,10 +58,10 @@ class CuteDSLTemplate(KernelTemplate): choices.append(self.generate(**kwargs)) return None except NotImplementedError as e: - log.debug("CuteDSL template choice generation failed: %s", e) + log.debug("CuteDSL template choice generation failed: %s", e) # noqa: G200 return e except Exception as e: - log.debug("CuteDSL template choice generation error: %s", e) + log.debug("CuteDSL template choice generation error: %s", e) # noqa: G200 return NotImplementedError(f"CuteDSL template failed: {e}") def generate(self, **kwargs: Any) -> ChoiceCaller: diff --git a/torch/_inductor/codegen/rocm/ck_universal_gemm_template.py b/torch/_inductor/codegen/rocm/ck_universal_gemm_template.py index db2bd69b1d09..8357e9fba774 100644 --- a/torch/_inductor/codegen/rocm/ck_universal_gemm_template.py +++ b/torch/_inductor/codegen/rocm/ck_universal_gemm_template.py @@ -510,7 +510,7 @@ class CKGemmTemplate(CKTemplate): torch.cuda.get_device_properties(X_meta.device).warp_size, ) except Exception as e: - log.debug( + log.debug( # noqa: G200 "Failed to prefetch_stages for %s with exception %s", op.name, e ) # be conservative here and disable the op diff --git a/torch/_inductor/codegen/triton.py b/torch/_inductor/codegen/triton.py index e8d7996460fe..cc938de0ca22 100644 --- a/torch/_inductor/codegen/triton.py +++ b/torch/_inductor/codegen/triton.py @@ -5638,7 +5638,7 @@ class TritonScheduling(SIMDScheduling): except Exception as e: if config.triton.disallow_failing_autotune_kernels_TESTING_ONLY: raise - log.debug( + log.debug( # noqa: G200 "Exception (%s) in compiling fused nodes %s", e, node_names, diff --git a/torch/_inductor/comm_analysis.py b/torch/_inductor/comm_analysis.py index 2bf9ff39f81f..51c5472c7fe3 100644 --- a/torch/_inductor/comm_analysis.py +++ b/torch/_inductor/comm_analysis.py @@ -204,7 +204,7 @@ def estimate_nccl_collective_runtime_nccl_estimator(snode) -> Optional[float]: torch.ops._c10d_functional.wait_tensor.default(w) except Exception as e: # NCCL estimator can fail - log.info(e) + log.info(e) # noqa: G200 return None est_time_us = time_estimator.estimated_time diff --git a/torch/_inductor/compile_fx_ext.py b/torch/_inductor/compile_fx_ext.py index 743819af7e67..113a7c92606d 100644 --- a/torch/_inductor/compile_fx_ext.py +++ b/torch/_inductor/compile_fx_ext.py @@ -445,7 +445,7 @@ class _SerializedFxCompile(FxCompile): # we can't cache (or serialize) FxGraphCache._check_for_hop(gm) except BypassFxGraphCache as e: - log.debug("Skipping %s compile: %s", type(self), e) + log.debug("Skipping %s compile: %s", type(self), e) # noqa: G200 return None context = torch._guards.TracingContext.try_get() diff --git a/torch/_inductor/compile_worker/subproc_pool.py b/torch/_inductor/compile_worker/subproc_pool.py index 474cd86eb362..c6b094cc52c6 100644 --- a/torch/_inductor/compile_worker/subproc_pool.py +++ b/torch/_inductor/compile_worker/subproc_pool.py @@ -284,8 +284,8 @@ class SubprocPool: self.process.wait(300) if self.log_file: self.log_file.close() - except OSError as e: - log.warning("Ignored OSError in pool shutdown: %s", e) + except OSError: + log.warning("Ignored OSError in pool shutdown", exc_info=True) finally: with self.futures_lock: for future in self.pending_futures.values(): diff --git a/torch/_inductor/fx_passes/numeric_utils.py b/torch/_inductor/fx_passes/numeric_utils.py index d5b140b49d20..b50859448f07 100644 --- a/torch/_inductor/fx_passes/numeric_utils.py +++ b/torch/_inductor/fx_passes/numeric_utils.py @@ -207,7 +207,7 @@ def numeric_check_if_enabled( precision=precision, ) except Exception as e: - logger.warning( + logger.warning( # noqa: G200 "Runtime numeric check failed in pre grad fx passes with error: %s", e ) traceback.print_exc() diff --git a/torch/_inductor/memory.py b/torch/_inductor/memory.py index 1a02dbb1e6af..a8df2fe55987 100644 --- a/torch/_inductor/memory.py +++ b/torch/_inductor/memory.py @@ -913,8 +913,8 @@ def reorder_for_peak_memory( try: validate_graph_acyclic(nodes) validate_unique_buffer_names(nodes, name_to_buf, name_to_freeable_input_buf) - except RuntimeError as e: - torch_log.error("Memory planning validation failed: %s", e) + except RuntimeError: + torch_log.exception("Memory planning validation failed") if not is_fbcode(): # TODO: remove after ensuring OSS side is safe raise @@ -942,8 +942,8 @@ def reorder_for_peak_memory( PeakMemoryResult(order, peak_memory, method.__name__) ) torch_log.info("%s peak memory: %d", method.__name__, peak_memory) - except Exception as e: - torch_log.error("Failed to reorder for %s: %s", method.__name__, e) + except Exception: + torch_log.exception("Failed to reorder for %s", method.__name__) if not is_fbcode(): # TODO: remove after ensuring OSS side is safe raise diff --git a/torch/_inductor/runtime/coordinate_descent_tuner.py b/torch/_inductor/runtime/coordinate_descent_tuner.py index faa2b06bcaf1..68db68ca11c7 100644 --- a/torch/_inductor/runtime/coordinate_descent_tuner.py +++ b/torch/_inductor/runtime/coordinate_descent_tuner.py @@ -238,7 +238,7 @@ class CoordescTuner: try: candidate_timing = self.call_func(func, candidate_config) except Exception as e: - log.debug("Got exception %s", e) + log.debug("Got exception %s", e) # noqa: G200 return False, float("inf") if self.has_improvement(best_timing, candidate_timing): diff --git a/torch/_inductor/runtime/triton_heuristics.py b/torch/_inductor/runtime/triton_heuristics.py index b49b9ac54228..f809d9f7d50a 100644 --- a/torch/_inductor/runtime/triton_heuristics.py +++ b/torch/_inductor/runtime/triton_heuristics.py @@ -1618,7 +1618,7 @@ class StaticTritonCompileResult(CompileResult[StaticallyLaunchedCudaKernel]): result = check_can_launch() return result except CannotStaticallyLaunchKernel as e: - log.info("Bypassing StaticallyLaunchedCudaKernel due to %s", str(e)) + log.info("Bypassing StaticallyLaunchedCudaKernel due to %s", str(e)) # noqa: G200 if torch._inductor.config.strict_static_cuda_launcher: raise e return None @@ -1997,11 +1997,11 @@ def end_graph(output_file): ) file.write(bw_info_str + "\n") file.write(f"{summary_str}\n\n") - except Exception as e: + except Exception: log.warning( - "failed to write profile bandwidth result into %s: %s", + "failed to write profile bandwidth result into %s", output_file, - e, + exc_info=True, ) diff --git a/torch/_inductor/scheduler.py b/torch/_inductor/scheduler.py index d76036d3859b..d68ce41251f9 100644 --- a/torch/_inductor/scheduler.py +++ b/torch/_inductor/scheduler.py @@ -896,11 +896,11 @@ class BaseSchedulerNode: except ValueError as e: # We don't know how to estimate runtime for this collective, # falling back to 0 - log.info(e) + log.info(e) # noqa: G200 return 0 except TypeError as e: # this happens when the collective is not of type ir._CollectiveKernel - log.info(e) + log.info(e) # noqa: G200 return 0 elif is_wait(self.node): @@ -3366,7 +3366,7 @@ class Scheduler: future.result() except Exception as e: if fusion_log.isEnabledFor(logging.DEBUG): - fusion_log.debug( + fusion_log.debug( # noqa: G200 "Exception in compiling %s: %s", "prologue" if not epilogue_fusion else "epilogue", str(e), @@ -3442,7 +3442,7 @@ class Scheduler: # triton will unpredictably error with valid prologue fusions except Exception as e: if fusion_log.isEnabledFor(logging.DEBUG): - fusion_log.debug( + fusion_log.debug( # noqa: G200 "Exception in compiling %s: %s", "prologue" if not epilogue_fusion else "epilogue", str(e), diff --git a/torch/_inductor/select_algorithm.py b/torch/_inductor/select_algorithm.py index b0e81444ad84..24fd3ccbfe10 100644 --- a/torch/_inductor/select_algorithm.py +++ b/torch/_inductor/select_algorithm.py @@ -1702,7 +1702,7 @@ class TritonTemplate(KernelTemplate): choices.append(choice) return None except NotImplementedError as e: - log.info( + log.info( # noqa: G200 "Cannot Append Choice: %s. KernelTemplate type is %s", e, type(self), @@ -3223,17 +3223,16 @@ class AlgorithmSelectorCache(PersistentCache): for choice in choices: try: timing = cls.benchmark_choice(choice, autotune_args) - except CUDACompileError as e: + except CUDACompileError: from torch._inductor.codegen.cuda.cuda_kernel import CUDATemplateCaller if not isinstance(choice, CUDATemplateCaller): - log.error( - "CUDA compilation error during autotuning: \n%s. \nIgnoring this choice.", - e, + log.exception( + "CUDA compilation error during autotuning: \n%s. \nIgnoring this choice." ) timing = float("inf") - except NotImplementedError as e: - log.warning("Not yet implemented: %s", e) + except NotImplementedError: + log.warning("Not yet implemented", exc_info=True) timing = float("inf") except RuntimeError as e: from torch._inductor.codegen.cuda.cuda_kernel import CUDATemplateCaller @@ -3266,7 +3265,7 @@ class AlgorithmSelectorCache(PersistentCache): from triton.runtime.autotuner import OutOfResources if isinstance(e, OutOfResources): - log.warning(e) + log.warning(e) # noqa: G200 timing = float("inf") else: raise e diff --git a/torch/_inductor/triton_bundler.py b/torch/_inductor/triton_bundler.py index b210dbff5c84..5bf5210a2cf4 100644 --- a/torch/_inductor/triton_bundler.py +++ b/torch/_inductor/triton_bundler.py @@ -224,11 +224,11 @@ class TritonBundler: # Make sure the cubin path exists and is valid for compile_result in result.kernel.compile_results: compile_result.reload_cubin_path() - except RuntimeError as e: + except RuntimeError: log.warning( - "Failed to reload cubin file statically launchable autotuner %s: %s", + "Failed to reload cubin file statically launchable autotuner %s", result.kernel_name, - e, + exc_info=True, ) continue # We make a future instead of returning the kernel here so that diff --git a/torch/_library/fake_class_registry.py b/torch/_library/fake_class_registry.py index 1902eafc0a48..b98949b388a9 100644 --- a/torch/_library/fake_class_registry.py +++ b/torch/_library/fake_class_registry.py @@ -21,7 +21,7 @@ class FakeScriptObject: with _disable_current_modes(): self.real_obj = copy.deepcopy(x) except RuntimeError as e: - log.warning( + log.warning( # noqa: G200 "Unable to deepcopy the custom object %s due to %s. " "Defaulting to the user given object. This might be " "dangerous as side effects may be directly applied " diff --git a/torch/_subclasses/fake_tensor.py b/torch/_subclasses/fake_tensor.py index 31d129a3c861..3c2d609b7367 100644 --- a/torch/_subclasses/fake_tensor.py +++ b/torch/_subclasses/fake_tensor.py @@ -2568,7 +2568,7 @@ class FakeTensorMode(TorchDispatchMode): # we shouldn't broadly catch all errors here; # some come from real-kernel mutation/aliasing checks we want to run. # add more exception types as needed. - log.debug( + log.debug( # noqa: G200 "real-tensor fallback failed for %s: %s; silently ignoring", func, exc, diff --git a/torch/distributed/checkpoint/_experimental/checkpoint_process.py b/torch/distributed/checkpoint/_experimental/checkpoint_process.py index 4e1c8e7f8253..5fde55053eed 100644 --- a/torch/distributed/checkpoint/_experimental/checkpoint_process.py +++ b/torch/distributed/checkpoint/_experimental/checkpoint_process.py @@ -224,7 +224,7 @@ class CheckpointProcess: ) ) parent_pipe.close() - logger.error("Subprocess terminated due to exception: %s", e) + logger.exception("Subprocess terminated due to exception") def _send(self, request_type: RequestType, payload: dict[str, Any]) -> None: try: @@ -238,8 +238,8 @@ class CheckpointProcess: ) except OSError as e: error_msg = "Child process terminated unexpectedly" - logger.error( - "Communication failed during %s request: %s", request_type.value, e + logger.exception( + "Communication failed during %s request", request_type.value ) raise RuntimeError(error_msg) from e @@ -354,10 +354,8 @@ class CheckpointProcess: ) self.process.processes[0].kill() logger.info("Subprocess killed forcefully") - except ProcessExitedException as e: - logger.error( - "ProcessExitedException during subprocess termination: %s", e - ) + except ProcessExitedException: + logger.exception("ProcessExitedException during subprocess termination") raise logger.debug("CheckpointProcess closed successfully") diff --git a/torch/distributed/distributed_c10d.py b/torch/distributed/distributed_c10d.py index 2419e5aecca3..c39847176517 100644 --- a/torch/distributed/distributed_c10d.py +++ b/torch/distributed/distributed_c10d.py @@ -972,7 +972,7 @@ def _store_based_barrier( except RuntimeError as e: worker_count = store.add(store_key, 0) # Print status periodically to keep track. - logger.debug( + logger.debug( # noqa: G200 "Waiting in store based barrier to initialize process group for %s seconds" "rank: %s, key: %s (world_size=%s, num_workers_joined=%s, timeout=%s error=%s)", time.time() - start, diff --git a/torch/distributed/elastic/agent/server/api.py b/torch/distributed/elastic/agent/server/api.py index f0fc50dd70b9..b02095304391 100644 --- a/torch/distributed/elastic/agent/server/api.py +++ b/torch/distributed/elastic/agent/server/api.py @@ -721,7 +721,7 @@ class SimpleElasticAgent(ElasticAgent): self._record_worker_events(result) return result except RendezvousGracefulExitError as e: - logger.info("Rendezvous gracefully exited: %s", e) + logger.info("Rendezvous gracefully exited: %s", e) # noqa: G200 except SignalException as e: logger.warning("Received %s death signal, shutting down workers", e.sigval) self._shutdown(e.sigval) diff --git a/torch/distributed/elastic/multiprocessing/api.py b/torch/distributed/elastic/multiprocessing/api.py index 9bb580c5bf78..ede23f8b801c 100644 --- a/torch/distributed/elastic/multiprocessing/api.py +++ b/torch/distributed/elastic/multiprocessing/api.py @@ -489,11 +489,13 @@ class PContext(abc.ABC): sig = getattr(signal, sig_name.strip()) signal.signal(sig, _terminate_process_handler) logger.info("Registered signal handler for %s", sig_name) - except (AttributeError, ValueError) as e: + except (AttributeError, ValueError): logger.warning( - "Failed to register signal handler for %s: %s", sig_name, e + "Failed to register signal handler for %s", + sig_name, + exc_info=True, ) - except RuntimeError as e: + except RuntimeError: if IS_WINDOWS and sig_name.strip() in [ "SIGHUP", "SIGQUIT", @@ -505,7 +507,9 @@ class PContext(abc.ABC): ) else: logger.warning( - "Failed to register signal handler for %s: %s", sig_name, e + "Failed to register signal handler for %s", + sig_name, + exc_info=True, ) else: logger.warning( diff --git a/torch/distributed/elastic/multiprocessing/tail_log.py b/torch/distributed/elastic/multiprocessing/tail_log.py index 9ff628157361..2aa73dc19dd6 100644 --- a/torch/distributed/elastic/multiprocessing/tail_log.py +++ b/torch/distributed/elastic/multiprocessing/tail_log.py @@ -142,12 +142,11 @@ class TailLog: try: f.result() except Exception as e: - logger.error( - "error in log tailor for %s%s. %s: %s", + logger.exception( + "error in log tailor for %s%s. %s", self._name, local_rank, e.__class__.__qualname__, - e, ) if self._threadpool: diff --git a/torch/distributed/elastic/rendezvous/etcd_rendezvous.py b/torch/distributed/elastic/rendezvous/etcd_rendezvous.py index 0e4da86d4621..300399414d9c 100644 --- a/torch/distributed/elastic/rendezvous/etcd_rendezvous.py +++ b/torch/distributed/elastic/rendezvous/etcd_rendezvous.py @@ -208,8 +208,8 @@ class EtcdRendezvousHandler(RendezvousHandler): try: self.set_closed() return True - except BaseException as e: # noqa: B036 - logger.warning("Shutdown failed. Error occurred: %s", str(e)) + except BaseException: # noqa: B036 + logger.warning("Shutdown failed", exc_info=True) return False @@ -333,7 +333,7 @@ class EtcdRendezvous: # to avoid spamming etcd # FIXME: there are a few things that fall under this like # etcd.EtcdKeyNotFound, etc, which could be handled more explicitly. - logger.info("Rendezvous attempt failed, will retry. Reason: %s", e) + logger.info("Rendezvous attempt failed, will retry. Reason: %s", e) # noqa: G200 time.sleep(1) def init_phase(self): diff --git a/torch/distributed/elastic/rendezvous/etcd_server.py b/torch/distributed/elastic/rendezvous/etcd_server.py index 8af8c01c028a..7e54fdd9839a 100644 --- a/torch/distributed/elastic/rendezvous/etcd_server.py +++ b/torch/distributed/elastic/rendezvous/etcd_server.py @@ -176,7 +176,7 @@ class EtcdServer: except Exception as e: curr_retries += 1 stop_etcd(self._etcd_proc) - logger.warning( + logger.warning( # noqa: G200 "Failed to start etcd server, got error: %s, retrying", str(e) ) if curr_retries >= num_retries: diff --git a/torch/distributed/pipelining/schedules.py b/torch/distributed/pipelining/schedules.py index 067a9351d823..d265bd295009 100644 --- a/torch/distributed/pipelining/schedules.py +++ b/torch/distributed/pipelining/schedules.py @@ -1734,7 +1734,7 @@ class PipelineScheduleMulti(_PipelineSchedule): # do the communication _wait_batch_p2p(_batch_p2p(ops)) except Exception as e: - logger.error( + logger.error( # noqa: G200 "[Rank %s] pipeline schedule %s caught the following exception '%s' \ at time_step %s when running action %s", self.rank, diff --git a/torch/distributed/rpc/api.py b/torch/distributed/rpc/api.py index dc552a7482ed..883b6b324f9b 100644 --- a/torch/distributed/rpc/api.py +++ b/torch/distributed/rpc/api.py @@ -295,8 +295,8 @@ def _barrier(worker_names): """ try: _all_gather(None, set(worker_names)) - except RuntimeError as ex: - logger.error("Failed to complete barrier, got error %s", ex) + except RuntimeError: + logger.exception("Failed to complete barrier") @_require_initialized @@ -311,9 +311,7 @@ def _wait_all_workers(timeout=DEFAULT_SHUTDOWN_TIMEOUT): try: _all_gather(None, timeout=timeout) except RuntimeError as ex: - logger.error( - "Failed to respond to 'Shutdown Proceed' in time, got error %s", ex - ) + logger.exception("Failed to respond to 'Shutdown Proceed' in time") raise ex diff --git a/torch/export/__init__.py b/torch/export/__init__.py index aeadf3e0e3a9..83b6b87fe4d8 100644 --- a/torch/export/__init__.py +++ b/torch/export/__init__.py @@ -448,8 +448,8 @@ def load( f, expected_opset_version=expected_opset_version, ) - except RuntimeError as e: - log.warning("Ran into the following error when deserializing: %s", e) + except RuntimeError: + log.warning("Ran into the following error when deserializing", exc_info=True) pt2_contents = PT2ArchiveContents({}, {}, {}) if len(pt2_contents.exported_programs) > 0 or len(pt2_contents.extra_files) > 0: diff --git a/torch/export/pt2_archive/_package.py b/torch/export/pt2_archive/_package.py index 7d9c0991721b..1a2e74b84e32 100644 --- a/torch/export/pt2_archive/_package.py +++ b/torch/export/pt2_archive/_package.py @@ -83,8 +83,8 @@ def is_pt2_package(serialized_model: Union[bytes, str]) -> bool: archive_format_path = f"{root_folder}/{ARCHIVE_FORMAT_PATH}" if archive_format_path in zip_reader.namelist(): return zip_reader.read(archive_format_path) == b"pt2" - except Exception as ex: - logger.info("Model is not a PT2 package: %s", str(ex)) + except Exception: + logger.info("Model is not a PT2 package") return False diff --git a/torch/fx/experimental/symbolic_shapes.py b/torch/fx/experimental/symbolic_shapes.py index 771e75272018..67f8c0f66574 100644 --- a/torch/fx/experimental/symbolic_shapes.py +++ b/torch/fx/experimental/symbolic_shapes.py @@ -3209,8 +3209,8 @@ class DimConstraints: self._dynamic_results.add(self._dcp.doprint(arg)) else: self._dynamic_results.add(self._dcp.doprint(solution)) - except (NotImplementedError, AssertionError) as e: - log.warning("Failed to reduce inequalities: %s", e) + except (NotImplementedError, AssertionError): + log.warning("Failed to reduce inequalities", exc_info=True) for expr2 in exprs: self._dynamic_results.add(self._dcp.doprint(expr2)) diff --git a/torch/onnx/_internal/exporter/_registration.py b/torch/onnx/_internal/exporter/_registration.py index f4c7cfbf5127..38d9f31afab6 100644 --- a/torch/onnx/_internal/exporter/_registration.py +++ b/torch/onnx/_internal/exporter/_registration.py @@ -83,7 +83,7 @@ class OnnxDecompMeta: # When the function is targeting an HOP, for example, it will accept # functions as arguments and fail to generate an ONNX signature. # In this case we set signature to None and dispatch to this function always. - logger.warning( + logger.warning( # noqa: G200 "Failed to infer the signature for function '%s' because '%s'" "All nodes targeting `%s` will be dispatched to this function", self.onnx_function, diff --git a/torch/onnx/_internal/exporter/_verification.py b/torch/onnx/_internal/exporter/_verification.py index a475908b5825..9741ae81bfff 100644 --- a/torch/onnx/_internal/exporter/_verification.py +++ b/torch/onnx/_internal/exporter/_verification.py @@ -317,12 +317,9 @@ class _VerificationInterpreter(torch.fx.Interpreter): return result try: (onnx_result,) = self._onnx_program.compute_values([node_name], self._args) - except Exception as e: + except Exception: logger.warning( - "Failed to compute value for node %s: %s", - node_name, - e, - exc_info=True, + "Failed to compute value for node %s", node_name, exc_info=True ) return result info = VerificationInfo.from_tensors( diff --git a/torch/testing/_internal/common_distributed.py b/torch/testing/_internal/common_distributed.py index 6cd372a8596c..18384b311b93 100644 --- a/torch/testing/_internal/common_distributed.py +++ b/torch/testing/_internal/common_distributed.py @@ -875,7 +875,7 @@ class MultiProcessTestCase(TestCase): try: getattr(self, test_name)() except unittest.SkipTest as se: - logger.info( + logger.info( # noqa: G200 "Process %s skipping test %s for following reason: %s", self.rank, test_name, @@ -917,11 +917,10 @@ class MultiProcessTestCase(TestCase): try: pipe.send(MultiProcessTestCase.Event.GET_TRACEBACK) pipes.append((i, pipe)) - except ConnectionError as e: - logger.error( - "Encountered error while trying to get traceback for process %s: %s", + except ConnectionError: + logger.exception( + "Encountered error while trying to get traceback for process %s", i, - e, ) # Wait for results. @@ -944,11 +943,10 @@ class MultiProcessTestCase(TestCase): logger.error( "Could not retrieve traceback for timed out process: %s", rank ) - except ConnectionError as e: - logger.error( - "Encountered error while trying to get traceback for process %s: %s", + except ConnectionError: + logger.exception( + "Encountered error while trying to get traceback for process %s", rank, - e, ) def _join_processes(self, fn) -> None: