Skip to content

Commit

Permalink
[Fix] Replace generic exception classes with a more specific ones (#1989
Browse files Browse the repository at this point in the history
)

* Replace generic exception classes with a more specific ones

* rerun pre-commit to pass linter tests

* Revert "rerun pre-commit to pass linter tests"

This reverts commit 67f88cc.

* reduce repetitions in errors or so

* Replace generic exception class with a more specific one
  • Loading branch information
LSinev authored Oct 22, 2024
1 parent 389347e commit d4ae963
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 39 deletions.
16 changes: 8 additions & 8 deletions lm_eval/models/anthropic_llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def anthropic_completion(

try:
import anthropic
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"attempted to use 'anthropic' LM type, but package `anthropic` is not installed. \
please install anthropic via `pip install 'lm-eval[anthropic]'` or `pip install -e '.[anthropic]'`",
)
Expand Down Expand Up @@ -108,8 +108,8 @@ def anthropic_chat(

try:
import anthropic
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"attempted to use 'anthropic' LM type, but package `anthropic` is not installed. \
please install anthropic via `pip install 'lm-eval[anthropic]'` or `pip install -e '.[anthropic]'`",
)
Expand Down Expand Up @@ -168,8 +168,8 @@ def __init__(

try:
import anthropic
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"attempted to use 'anthropic' LM type, but package `anthropic` is not installed. \
please install anthropic via `pip install 'lm-eval[anthropic]'` or `pip install -e '.[anthropic]'`",
)
Expand Down Expand Up @@ -217,8 +217,8 @@ def _loglikelihood_tokens(self, requests, disable_tqdm: bool = False):
def generate_until(self, requests, disable_tqdm: bool = False) -> List[str]:
try:
import anthropic
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"attempted to use 'anthropic' LM type, but package `anthropic` is not installed. \
please install anthropic via `pip install 'lm-eval[anthropic]'` or `pip install -e '.[anthropic]'`",
)
Expand Down
2 changes: 1 addition & 1 deletion lm_eval/models/api_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def __init__(

self.tokenizer = tiktoken.encoding_for_model(self.model)
except ModuleNotFoundError as e:
raise Exception(
raise ModuleNotFoundError(
"Attempted to use 'openai' LM type, but the package `tiktoken` is not installed. "
"Please install it via `pip install lm-eval[api]` or `pip install -e .[api]`."
) from e
Expand Down
4 changes: 3 additions & 1 deletion lm_eval/models/gguf.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def gguf_completion(
logger.error(f"RequestException: {e}")
time.sleep(delay) # wait before retrying
else:
raise Exception(f"Failed to get a valid response after {retries} retries.")
raise RuntimeError(
f"Failed to get a valid response after {retries} retries."
)

def loglikelihood(self, requests, disable_tqdm: bool = False):
if not requests:
Expand Down
4 changes: 2 additions & 2 deletions lm_eval/models/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ def _create_model(
else:
try:
from auto_gptq import AutoGPTQForCausalLM
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Tried to load auto_gptq, but auto-gptq is not installed ",
"please install auto-gptq via pip install lm-eval[gptq] or pip install -e .[gptq]",
)
Expand Down
8 changes: 4 additions & 4 deletions lm_eval/models/mamba_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def _get_config(
) -> None:
try:
from mamba_ssm.utils.hf import load_config_hf # noqa: F811
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"attempted to use 'mamba_ssm' LM type, but package `mamba_ssm` is not installed. \
please install mamba via `pip install lm-eval[mamba]` or `pip install -e .[mamba]`",
)
Expand All @@ -88,8 +88,8 @@ def _create_model(
) -> None:
try:
from mamba_ssm.models.mixer_seq_simple import MambaLMHeadModel # noqa: F811
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"attempted to use 'mamba_ssm' LM type, but package `mamba_ssm` is not installed. \
please install mamba via `pip install lm-eval[mamba]` or `pip install -e .[mamba]`",
)
Expand Down
16 changes: 8 additions & 8 deletions lm_eval/models/nemo_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def _patch_pretrained_cfg(
):
try:
import omegaconf
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Attempted to use 'nemo_lm' model type, but package `nemo` is not installed"
"Please install nemo following the instructions in the README: either with a NVIDIA PyTorch or NeMo container, "
"or installing nemo following https://github.com/NVIDIA/NeMo.",
Expand Down Expand Up @@ -79,8 +79,8 @@ def load_model(
MegatronGPTModel,
)
from nemo.collections.nlp.parts.nlp_overrides import NLPSaveRestoreConnector
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Attempted to use 'nemo_lm' model type, but package `nemo` is not installed"
"Please install nemo following the instructions in the README: either with a NVIDIA PyTorch or NeMo container, "
"or installing nemo following https://github.com/NVIDIA/NeMo.",
Expand Down Expand Up @@ -140,8 +140,8 @@ def _synced_build_tokenizer(self):
def setup_distributed_environment(trainer):
try:
from nemo.utils.app_state import AppState
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Attempted to use 'nemo_lm' model type, but package `nemo` is not installed"
"Please install nemo following the instructions in the README: either with a NVIDIA PyTorch or NeMo container, "
"or installing nemo following https://github.com/NVIDIA/NeMo.",
Expand Down Expand Up @@ -194,8 +194,8 @@ def __init__(
from pytorch_lightning.trainer.trainer import Trainer

self.generate = generate
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Attempted to use 'nemo_lm' model type, but package `nemo` is not installed"
"Please install nemo following the instructions in the README: either with a NVIDIA PyTorch or NeMo container, "
"or installing nemo following https://github.com/NVIDIA/NeMo.",
Expand Down
16 changes: 8 additions & 8 deletions lm_eval/models/neuralmagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def _create_model(
) -> None:
try:
from sparseml.transformers import SparseAutoModelForCausalLM
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Package `sparseml` is not installed. "
"Please install it via `pip install sparseml[transformers]`"
)
Expand Down Expand Up @@ -88,8 +88,8 @@ def _create_model(
def _get_config(self, pretrained: str, **kwargs) -> None:
try:
from sparseml.transformers import SparseAutoConfig
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Package `sparseml` is not installed. "
"Please install it via `pip install sparseml[transformers]`"
)
Expand All @@ -112,8 +112,8 @@ def _create_tokenizer(
) -> None:
try:
from sparseml.transformers import SparseAutoTokenizer
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Package `sparseml` is not installed. "
"Please install it via `pip install sparseml[transformers]`"
)
Expand Down Expand Up @@ -171,8 +171,8 @@ def __init__(

try:
import deepsparse
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Package `deepsparse` is not installed. "
"Please install it via `pip install deepsparse[transformers]`"
)
Expand Down
2 changes: 1 addition & 1 deletion lm_eval/models/neuron_optimum.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def __init__(
add_bos_token: Optional[bool] = False,
) -> None:
if not NEURON_AVAILABLE:
raise Exception(
raise ImportError(
"Tried to load neuron model, but neuron is not installed ",
"please install neuron via pip install transformers-neuron ",
"also make sure you are running on an AWS inf2 instance",
Expand Down
2 changes: 1 addition & 1 deletion lm_eval/models/optimum_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _create_model(
**kwargs,
) -> None:
if not find_spec("optimum"):
raise Exception(
raise ModuleNotFoundError(
"package `optimum` is not installed. Please install it via `pip install optimum[openvino]`"
)
else:
Expand Down
2 changes: 1 addition & 1 deletion lm_eval/models/vllm_causallms.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(
super().__init__()

if not find_spec("vllm"):
raise Exception(
raise ModuleNotFoundError(
"attempted to use 'vllm' LM type, but package `vllm` is not installed. "
"Please install vllm via `pip install lm-eval[vllm]` or `pip install -e .[vllm]`"
)
Expand Down
6 changes: 3 additions & 3 deletions lm_eval/prompts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def get_prompt(prompt_id: str, dataset_name: str = None, subset_name: str = None
if category_name == "promptsource":
try:
from promptsource.templates import DatasetTemplates
except ModuleNotFoundError:
raise Exception(
except ModuleNotFoundError as exception:
raise type(exception)(
"Tried to load a Promptsource template, but promptsource is not installed ",
"please install promptsource via pip install lm-eval[promptsource] or pip install -e .[promptsource]",
)
Expand Down Expand Up @@ -118,7 +118,7 @@ def apply(self, doc):

# TODO need a way to process doc_to_choice
if "doc_to_choice" in self.prompt_string:
raise Exception("Not yet implemented to accept doc_to_choice")
raise NotImplementedError("Not yet implemented to accept doc_to_choice")

text_string = utils.apply_template(doc_to_text, doc)
target_string = utils.apply_template(doc_to_target, doc)
Expand Down
2 changes: 1 addition & 1 deletion scripts/clean_training_data/generate_13_grams.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def yield_pile(start_offsets=None, checkpoint_offset=None):
print(
"We expect the pile archives to be in the 'pile' directory, but this was not found."
)
raise Exception("Pile directory not found.")
raise FileNotFoundError("Pile directory not found.")

files = list(sorted(glob.glob(os.path.join(directory, "*.jsonl.zst*"))))

Expand Down

0 comments on commit d4ae963

Please sign in to comment.