From 819152f19314bd4bca4ef56cbc2d015a5d8ece5f Mon Sep 17 00:00:00 2001 From: Ram Sathyavageeswaran Date: Wed, 1 Jul 2026 22:39:55 +0000 Subject: [PATCH] fix: replace bare except clauses with except Exception MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bare `except:` catches BaseException — including KeyboardInterrupt and SystemExit — which prevents users from interrupting long-running quantization jobs with Ctrl-C and masks unrelated crashes. Replace all 15 occurrences across the codebase with `except Exception:`, which preserves the intended fallback behaviour while allowing signals and interpreter exit to propagate normally. Files changed: - auto_round/calib_dataset.py (3) - auto_round/inference/backend.py (1) - auto_round/inference/convert_model.py (3) - auto_round/modeling/unfused_moe/__init__.py (1) - auto_round/utils/common.py (1) - auto_round/utils/device.py (1) - auto_round/utils/device_manager.py (1) - auto_round/utils/model.py (4) Signed-off-by: Ram Sathyavageeswaran --- auto_round/calib_dataset.py | 6 +++--- auto_round/inference/backend.py | 2 +- auto_round/inference/convert_model.py | 6 +++--- auto_round/modeling/unfused_moe/__init__.py | 2 +- auto_round/utils/common.py | 2 +- auto_round/utils/device.py | 2 +- auto_round/utils/device_manager.py | 2 +- auto_round/utils/model.py | 8 ++++---- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/auto_round/calib_dataset.py b/auto_round/calib_dataset.py index 7d2e3a2f80..901f767cf3 100644 --- a/auto_round/calib_dataset.py +++ b/auto_round/calib_dataset.py @@ -74,7 +74,7 @@ def apply_chat_template_to_samples(samples, tokenizer, seqlen, system_prompt=Non tokenize=False, add_generation_prompt=True, ) - except: + except Exception: logger.warning("Failed to apply chat template. removing the system role in chat history.") message_modified = [msg for msg in message if msg["role"] != "system"] chat_templated = tokenizer.apply_chat_template( @@ -706,7 +706,7 @@ def get_dataset_len(dataset): try: dataset_len = len(dataset) return dataset_len - except: + except Exception: cnt = 0 for _ in dataset: cnt += 1 @@ -748,7 +748,7 @@ def select_dataset(dataset, indices): """ try: return dataset.select(indices) - except: + except Exception: list_data = list(select(dataset, indices)) import pandas as pd diff --git a/auto_round/inference/backend.py b/auto_round/inference/backend.py index ade2ca1ce7..01b044c2f1 100644 --- a/auto_round/inference/backend.py +++ b/auto_round/inference/backend.py @@ -1192,7 +1192,7 @@ def build_pip_commands(gptq_req, other_reqs): for req in requirements: try: require_version(req) - except: + except Exception: missing_requirements.append(req) gptq_req = next((f'"{req}"' for req in missing_requirements if "gptqmodel" in req), None) diff --git a/auto_round/inference/convert_model.py b/auto_round/inference/convert_model.py index 722cfd3181..f7873a364e 100644 --- a/auto_round/inference/convert_model.py +++ b/auto_round/inference/convert_model.py @@ -65,7 +65,7 @@ def skip_not_convert_modules(model, quantization_config, layer_names, layer_conf modules_to_not_convert = getattr(quantization_config, "modules_to_not_convert", []) try: # transformers new api modules_to_not_convert = get_modules_to_not_convert(model, modules_to_not_convert, add_default_skips=True) - except: + except Exception: modules_to_not_convert = _get_modules_to_not_convert(model, modules_to_not_convert) if modules_to_not_convert: for layer_name in layer_names: @@ -155,7 +155,7 @@ def _get_modules_to_not_convert( from transformers.quantizers.base import HfQuantizer get_modules_to_not_convert = HfQuantizer.get_modules_to_not_convert -except: +except Exception: get_modules_to_not_convert = _get_modules_to_not_convert @@ -492,7 +492,7 @@ def _import_exllamav2_kernels(): """Attempts to import ExLlamaV2 kernels for performance optimization.""" try: from exllamav2_kernels import gemm_half_q_half, make_q_matrix # pylint: disable=E0611, E0401 - except: + except Exception: logger.warning_once( "AutoGPTQ ExLlamaV2 has not been installed, Please install it using the following command: " "`pip install git+https://github.com/AutoGPTQ/AutoGPTQ.git@b8b4127`" diff --git a/auto_round/modeling/unfused_moe/__init__.py b/auto_round/modeling/unfused_moe/__init__.py index a112a2a154..15012aea9c 100644 --- a/auto_round/modeling/unfused_moe/__init__.py +++ b/auto_round/modeling/unfused_moe/__init__.py @@ -178,7 +178,7 @@ def pre_check_config(model_name: str | torch.nn.Module, trust_remote_code: bool for key in model_keys: if "gate_up_proj" in key: return False - except: + except Exception: return True return True diff --git a/auto_round/utils/common.py b/auto_round/utils/common.py index 5e7a50e6ae..ae9df56658 100644 --- a/auto_round/utils/common.py +++ b/auto_round/utils/common.py @@ -98,7 +98,7 @@ def __getattr__(self, name): try: self.module = importlib.import_module(self.module_name) mod = getattr(self.module, name) - except: + except Exception: spec = importlib.util.find_spec(str(self.module_name + "." + name)) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) diff --git a/auto_round/utils/device.py b/auto_round/utils/device.py index 5364a82803..a43f42b3e8 100644 --- a/auto_round/utils/device.py +++ b/auto_round/utils/device.py @@ -313,7 +313,7 @@ def set_cuda_visible_devices(device: str): indices = [int(device) for device in devices] try: pick_device = [current_visible_devices[i] for i in indices] - except: + except Exception: raise ValueError( "Invalid '--device' value: It must be smaller than the number of available devices." " For example, with CUDA_VISIBLE_DEVICES=4,5, " diff --git a/auto_round/utils/device_manager.py b/auto_round/utils/device_manager.py index 14fd97d1b4..45fae683e5 100644 --- a/auto_round/utils/device_manager.py +++ b/auto_round/utils/device_manager.py @@ -394,7 +394,7 @@ def empty_cache(self) -> None: if callable(fn): try: fn() # pylint: disable=E1102 # mps has issues - except: + except Exception: pass def device_index(self, index: int): diff --git a/auto_round/utils/model.py b/auto_round/utils/model.py index 1cd28df7ae..4696446c64 100644 --- a/auto_round/utils/model.py +++ b/auto_round/utils/model.py @@ -301,7 +301,7 @@ def _is_mxfp4_model(model_path, trust_remote_code=True): try: # in case of config loading failure for new models config = AutoConfig.from_pretrained(model_path, trust_remote_code=trust_remote_code) - except: + except Exception: return False model_type = getattr(config, "model_type", "") @@ -844,7 +844,7 @@ def diffusion_load_model( from transformers import AutoConfig config = AutoConfig.from_pretrained(pretrained_model_name_or_path, trust_remote_code=trust_remote_code) - except: + except Exception: config = None model_type = getattr(config, "model_type", "") @@ -1043,7 +1043,7 @@ def is_diffusion_model(model_or_path: Union[str, object], trust_remote_code: boo # A special case for NextStep if model_type == "nextstep": return True - except: + except Exception: logger.warning( f"Failed to load config for {model_or_path}, trying to check model_index.json for diffusion pipeline." ) @@ -1436,7 +1436,7 @@ def _to_model_dtype(model, model_dtype): model = model.to(torch.bfloat16) elif model_dtype == "float32" or model_dtype == "fp32" and model.dtype != torch.bfloat32: model = model.to(torch.float32) - except: + except Exception: logger.error("please use more device to fit the device or just use one device") exit() return model