Load a model of any supported format.
(path: Path)
| 1042 | |
| 1043 | |
| 1044 | def load_some_model(path: Path) -> ModelPlus: |
| 1045 | '''Load a model of any supported format.''' |
| 1046 | # Be extra-friendly and accept either a file or a directory: |
| 1047 | if path.is_dir(): |
| 1048 | # Check if it's a set of safetensors files first |
| 1049 | globs = ["model-00001-of-*.safetensors", "model.safetensors"] |
| 1050 | files = [file for glob in globs for file in path.glob(glob)] |
| 1051 | if not files: |
| 1052 | # Try the PyTorch patterns too, with lower priority |
| 1053 | globs = ["consolidated.00.pth", "pytorch_model-00001-of-*.bin", "*.pt", "pytorch_model.bin"] |
| 1054 | files = [file for glob in globs for file in path.glob(glob)] |
| 1055 | if not files: |
| 1056 | raise Exception(f"Can't find model in directory {path}") |
| 1057 | if len(files) > 1: |
| 1058 | raise Exception(f"Found multiple models in {path}, not sure which to pick: {files}") |
| 1059 | path = files[0] |
| 1060 | |
| 1061 | paths = find_multifile_paths(path) |
| 1062 | models_plus: list[ModelPlus] = [] |
| 1063 | for path in paths: |
| 1064 | print(f"Loading model file {path}") |
| 1065 | models_plus.append(lazy_load_file(path)) |
| 1066 | |
| 1067 | model_plus = merge_multifile_models(models_plus) |
| 1068 | return model_plus |
| 1069 | |
| 1070 | |
| 1071 | def load_vocab(path: Path, vocabtype: str | None) -> Vocab: |
no test coverage detected