PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
fix: replace bare except clauses with except Exception (#1981)
Ramakrishnan Sathyavageeswaran81b4db357d2d
diff --git a/auto_round/calib_dataset.py b/auto_round/calib_dataset.pyindex 092505898..b95e38524 100644--- a/auto_round/calib_dataset.py+++ b/auto_round/calib_dataset.py@@ -103,7 +103,7 @@ def apply_chat_template_to_samples(samples, tokenizer, seqlen, system_prompt=Nontokenize=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(@@ -837,7 +837,7 @@ def get_dataset_len(dataset):try:dataset_len = len(dataset)return dataset_len- except:+ except Exception:cnt = 0for _ in dataset:cnt += 1@@ -879,7 +879,7 @@ def select_dataset(dataset, indices):"""try:return dataset.select(indices)- except:+ except Exception:list_data = list(select(dataset, indices))import pandas as pddiff --git a/auto_round/inference/backend.py b/auto_round/inference/backend.pyindex 1ef8ba9b1..dd6edd6fd 100644--- a/auto_round/inference/backend.py+++ b/auto_round/inference/backend.py@@ -1263,7 +1263,7 @@ def process_requirement(requirements: list, target_device="cuda", logger_level="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.pyindex b3fc98de4..491137040 100644--- a/auto_round/inference/convert_model.py+++ b/auto_round/inference/convert_model.py@@ -66,7 +66,7 @@ def skip_not_convert_modules(model, quantization_config, layer_names, layer_confmodules_to_not_convert = getattr(quantization_config, "modules_to_not_convert", [])try: # transformers new apimodules_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:@@ -156,7 +156,7 @@ try:from transformers.quantizers.base import HfQuantizerget_modules_to_not_convert = HfQuantizer.get_modules_to_not_convert-except:+except Exception:get_modules_to_not_convert = _get_modules_to_not_convert@@ -500,7 +500,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__.pyindex a112a2a15..15012aea9 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: boolfor key in model_keys:if "gate_up_proj" in key:return False- except:+ except Exception:return Truereturn Truediff --git a/auto_round/utils/common.py b/auto_round/utils/common.pyindex 8b68351c8..556f3f505 100644--- a/auto_round/utils/common.py+++ b/auto_round/utils/common.py@@ -98,7 +98,7 @@ class LazyImport(object):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.pyindex dcf2bf6d2..884683d08 100644--- a/auto_round/utils/device.py+++ b/auto_round/utils/device.py@@ -312,7 +312,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.pyindex 45b1e4dd7..f2972d733 100644--- a/auto_round/utils/device_manager.py+++ b/auto_round/utils/device_manager.py@@ -396,7 +396,7 @@ class ARDevice:if callable(fn):try:fn() # pylint: disable=E1102 # mps has issues- except:+ except Exception:passdef device_index(self, index: int):diff --git a/auto_round/utils/model.py b/auto_round/utils/model.pyindex 1239fd81f..df4995cd6 100644--- a/auto_round/utils/model.py+++ b/auto_round/utils/model.py@@ -302,7 +302,7 @@ def _is_mxfp4_model(model_path, trust_remote_code=True):try: # in case of config loading failure for new modelsconfig = AutoConfig.from_pretrained(model_path, trust_remote_code=trust_remote_code)- except:+ except Exception:return Falsemodel_type = getattr(config, "model_type", "")@@ -898,7 +898,7 @@ def diffusion_load_model(from transformers import AutoConfigconfig = AutoConfig.from_pretrained(pretrained_model_name_or_path, trust_remote_code=trust_remote_code)- except:+ except Exception:config = Nonemodel_type = getattr(config, "model_type", "")@@ -1313,8 +1313,10 @@ def is_diffusion_model(model_or_path: Union[str, object], trust_remote_code: boo# A special case for NextStepif model_type == "nextstep":return True- except:- pass+ except Exception:+ logger.warning(+ f"Failed to load config for {model_or_path}, trying to check model_index.json for diffusion pipeline."+ )index_file = Noneif not os.path.isdir(model_or_path):try:@@ -1704,7 +1706,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