Load a model of any supported format.
(path: Path)
| 1258 | |
| 1259 | |
| 1260 | def load_some_model(path: Path) -> ModelPlus: |
| 1261 | '''Load a model of any supported format.''' |
| 1262 | # Be extra-friendly and accept either a file or a directory: |
| 1263 | if path.is_dir(): |
| 1264 | # Check if it's a set of safetensors files first |
| 1265 | globs = ["model-00001-of-*.safetensors", "model.safetensors"] |
| 1266 | files = [file for glob in globs for file in path.glob(glob)] |
| 1267 | if not files: |
| 1268 | # Try the PyTorch patterns too, with lower priority |
| 1269 | globs = ["consolidated.00.pth", "pytorch_model-00001-of-*.bin", "*.pt", "pytorch_model.bin"] |
| 1270 | files = [file for glob in globs for file in path.glob(glob)] |
| 1271 | if not files: |
| 1272 | raise Exception(f"Can't find model in directory {path}") |
| 1273 | if len(files) > 1: |
| 1274 | raise Exception(f"Found multiple models in {path}, not sure which to pick: {files}") |
| 1275 | path = files[0] |
| 1276 | |
| 1277 | paths = find_multifile_paths(path) |
| 1278 | models_plus: list[ModelPlus] = [] |
| 1279 | for path in paths: |
| 1280 | print(f"Loading model file {path}") |
| 1281 | models_plus.append(lazy_load_file(path)) |
| 1282 | |
| 1283 | model_plus = merge_multifile_models(models_plus) |
| 1284 | return model_plus |
| 1285 | |
| 1286 | |
| 1287 | class VocabFactory: |
no test coverage detected