Skip to content

Commit 8d2ac2c

Browse files
committed
convert : use appropriate exception types
Signed-off-by: Jared Van Bortel <[email protected]>
1 parent d12a63c commit 8d2ac2c

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

convert.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import signal
1717
import struct
1818
import sys
19+
import textwrap
1920
import time
2021
import zipfile
2122
from abc import ABC, abstractmethod
@@ -188,8 +189,10 @@ def guessed(model: LazyModel) -> Params:
188189
n_layer = next(i for i in itertools.count() if f"layers.{i}.attention.wq.weight" not in model)
189190

190191
if n_layer < 1:
191-
raise Exception("failed to guess 'n_layer'. This model is unknown or unsupported.\n"
192-
"Suggestion: provide 'config.json' of the model in the same directory containing model files.")
192+
raise KeyError(textwrap.dedent("""\
193+
failed to guess 'n_layer'. This model is unknown or unsupported.
194+
Suggestion: provide 'config.json' of the model in the same directory containing model files.""",
195+
))
193196

194197
n_head = n_embd // 128 # guessed
195198
n_mult = 256 # guessed
@@ -234,8 +237,10 @@ def loadHFTransformerJson(model: LazyModel, config_path: Path) -> Params:
234237
elif "max_position_embeddings" in config:
235238
n_ctx = config["max_position_embeddings"]
236239
else:
237-
raise Exception("failed to guess 'n_ctx'. This model is unknown or unsupported.\n"
238-
"Suggestion: provide 'config.json' of the model in the same directory containing model files.")
240+
raise KeyError(textwrap.dedent("""\
241+
failed to guess 'n_ctx'. This model is unknown or unsupported.
242+
Suggestion: provide 'config.json' of the model in the same directory containing model files.""",
243+
))
239244

240245
n_experts = None
241246
n_experts_used = None
@@ -394,7 +399,8 @@ def __init__(self, fname_tokenizer: Path, fname_added_tokens: Path | None):
394399
actual_ids = sorted(added_tokens.values())
395400
if expected_ids != actual_ids:
396401
expected_end_id = vocab_size + len(actual_ids) - 1
397-
raise Exception(f"Expected the {len(actual_ids)} added token ID(s) to be sequential in the range {vocab_size} - {expected_end_id}; got {actual_ids}")
402+
raise ValueError(f"Expected the {len(actual_ids)} added token ID(s) to be sequential in the range "
403+
f"{vocab_size} - {expected_end_id}; got {actual_ids}")
398404

399405
items = sorted(added_tokens.items(), key=lambda text_idx: text_idx[1])
400406
self.added_tokens_dict = added_tokens
@@ -908,7 +914,7 @@ def load() -> UnquantizedTensor:
908914
def must_read(fp: IO[bytes], length: int) -> bytes:
909915
ret = fp.read(length)
910916
if len(ret) < length:
911-
raise Exception("unexpectedly reached end of file")
917+
raise EOFError("unexpectedly reached end of file")
912918
return ret
913919

914920

@@ -998,7 +1004,7 @@ def check_vocab_size(params: Params, vocab: BaseVocab, pad_vocab: bool = False)
9981004
if vocab.vocab_size < params.n_vocab:
9991005
msg += " Add the --pad-vocab option and try again."
10001006

1001-
raise Exception(msg)
1007+
raise ValueError(msg)
10021008

10031009

10041010
class OutputFile:
@@ -1193,7 +1199,7 @@ def pick_output_type(model: LazyModel, output_type_str: str | None) -> GGMLFileT
11931199

11941200
name_to_type = {name: lazy_tensor.data_type for (name, lazy_tensor) in model.items()}
11951201

1196-
raise Exception(f"Unexpected combination of types: {name_to_type}")
1202+
raise ValueError(f"Unexpected combination of types: {name_to_type}")
11971203

11981204

11991205
def convert_to_output_type(model: LazyModel, output_type: GGMLFileType) -> LazyModel:
@@ -1230,8 +1236,7 @@ def convert_model_names(model: LazyModel, params: Params, skip_unknown: bool) ->
12301236
if skip_unknown:
12311237
print(f"Unexpected tensor name: {name} - skipping")
12321238
continue
1233-
else:
1234-
raise Exception(f"Unexpected tensor name: {name}. Use --skip-unknown to ignore it (e.g. LLaVA)")
1239+
raise ValueError(f"Unexpected tensor name: {name}. Use --skip-unknown to ignore it (e.g. LLaVA)")
12351240

12361241
if tensor_type in should_skip:
12371242
print(f"skipping tensor {name_new}")
@@ -1294,9 +1299,9 @@ def load_some_model(path: Path) -> ModelPlus:
12941299
globs = ["consolidated.00.pth", "pytorch_model-00001-of-*.bin", "*.pt", "pytorch_model.bin"]
12951300
files = [file for glob in globs for file in path.glob(glob)]
12961301
if not files:
1297-
raise Exception(f"Can't find model in directory {path}")
1302+
raise FileNotFoundError(f"Can't find model in directory {path}")
12981303
if len(files) > 1:
1299-
raise Exception(f"Found multiple models in {path}, not sure which to pick: {files}")
1304+
raise ValueError(f"Found multiple models in {path}, not sure which to pick: {files}")
13001305
path = files[0]
13011306

13021307
paths = find_multifile_paths(path)
@@ -1448,10 +1453,12 @@ def main(args_in: list[str] | None = None) -> None:
14481453
params = Params.load(model_plus)
14491454
if params.n_ctx == -1:
14501455
if args.ctx is None:
1451-
raise Exception("The model doesn't have a context size, and you didn't specify one with --ctx\n"
1452-
"Please specify one with --ctx:\n"
1453-
" - LLaMA v1: --ctx 2048\n"
1454-
" - LLaMA v2: --ctx 4096\n")
1456+
parser.error(textwrap.dedent("""\
1457+
The model doesn't have a context size, and you didn't specify one with --ctx
1458+
Please specify one with --ctx:
1459+
- LLaMA v1: --ctx 2048
1460+
- LLaMA v2: --ctx 4096""",
1461+
))
14551462
params.n_ctx = args.ctx
14561463

14571464
if args.outtype:

0 commit comments

Comments
 (0)