|
16 | 16 | import signal
|
17 | 17 | import struct
|
18 | 18 | import sys
|
| 19 | +import textwrap |
19 | 20 | import time
|
20 | 21 | import zipfile
|
21 | 22 | from abc import ABC, abstractmethod
|
@@ -188,8 +189,10 @@ def guessed(model: LazyModel) -> Params:
|
188 | 189 | n_layer = next(i for i in itertools.count() if f"layers.{i}.attention.wq.weight" not in model)
|
189 | 190 |
|
190 | 191 | 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 | + )) |
193 | 196 |
|
194 | 197 | n_head = n_embd // 128 # guessed
|
195 | 198 | n_mult = 256 # guessed
|
@@ -234,8 +237,10 @@ def loadHFTransformerJson(model: LazyModel, config_path: Path) -> Params:
|
234 | 237 | elif "max_position_embeddings" in config:
|
235 | 238 | n_ctx = config["max_position_embeddings"]
|
236 | 239 | 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 | + )) |
239 | 244 |
|
240 | 245 | n_experts = None
|
241 | 246 | n_experts_used = None
|
@@ -394,7 +399,8 @@ def __init__(self, fname_tokenizer: Path, fname_added_tokens: Path | None):
|
394 | 399 | actual_ids = sorted(added_tokens.values())
|
395 | 400 | if expected_ids != actual_ids:
|
396 | 401 | 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}") |
398 | 404 |
|
399 | 405 | items = sorted(added_tokens.items(), key=lambda text_idx: text_idx[1])
|
400 | 406 | self.added_tokens_dict = added_tokens
|
@@ -908,7 +914,7 @@ def load() -> UnquantizedTensor:
|
908 | 914 | def must_read(fp: IO[bytes], length: int) -> bytes:
|
909 | 915 | ret = fp.read(length)
|
910 | 916 | if len(ret) < length:
|
911 |
| - raise Exception("unexpectedly reached end of file") |
| 917 | + raise EOFError("unexpectedly reached end of file") |
912 | 918 | return ret
|
913 | 919 |
|
914 | 920 |
|
@@ -998,7 +1004,7 @@ def check_vocab_size(params: Params, vocab: BaseVocab, pad_vocab: bool = False)
|
998 | 1004 | if vocab.vocab_size < params.n_vocab:
|
999 | 1005 | msg += " Add the --pad-vocab option and try again."
|
1000 | 1006 |
|
1001 |
| - raise Exception(msg) |
| 1007 | + raise ValueError(msg) |
1002 | 1008 |
|
1003 | 1009 |
|
1004 | 1010 | class OutputFile:
|
@@ -1193,7 +1199,7 @@ def pick_output_type(model: LazyModel, output_type_str: str | None) -> GGMLFileT
|
1193 | 1199 |
|
1194 | 1200 | name_to_type = {name: lazy_tensor.data_type for (name, lazy_tensor) in model.items()}
|
1195 | 1201 |
|
1196 |
| - raise Exception(f"Unexpected combination of types: {name_to_type}") |
| 1202 | + raise ValueError(f"Unexpected combination of types: {name_to_type}") |
1197 | 1203 |
|
1198 | 1204 |
|
1199 | 1205 | 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) ->
|
1230 | 1236 | if skip_unknown:
|
1231 | 1237 | print(f"Unexpected tensor name: {name} - skipping")
|
1232 | 1238 | 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)") |
1235 | 1240 |
|
1236 | 1241 | if tensor_type in should_skip:
|
1237 | 1242 | print(f"skipping tensor {name_new}")
|
@@ -1294,9 +1299,9 @@ def load_some_model(path: Path) -> ModelPlus:
|
1294 | 1299 | globs = ["consolidated.00.pth", "pytorch_model-00001-of-*.bin", "*.pt", "pytorch_model.bin"]
|
1295 | 1300 | files = [file for glob in globs for file in path.glob(glob)]
|
1296 | 1301 | 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}") |
1298 | 1303 | 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}") |
1300 | 1305 | path = files[0]
|
1301 | 1306 |
|
1302 | 1307 | paths = find_multifile_paths(path)
|
@@ -1448,10 +1453,12 @@ def main(args_in: list[str] | None = None) -> None:
|
1448 | 1453 | params = Params.load(model_plus)
|
1449 | 1454 | if params.n_ctx == -1:
|
1450 | 1455 | 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 | + )) |
1455 | 1462 | params.n_ctx = args.ctx
|
1456 | 1463 |
|
1457 | 1464 | if args.outtype:
|
|
0 commit comments