Given any path belonging to a multi-file model (e.g. foo.bin.1), return the nth path in the model.
(path: Path, n: int)
| 1048 | return out |
| 1049 | |
| 1050 | def nth_multifile_path(path: Path, n: int) -> Path | None: |
| 1051 | '''Given any path belonging to a multi-file model (e.g. foo.bin.1), return |
| 1052 | the nth path in the model. |
| 1053 | ''' |
| 1054 | # Support the following patterns: |
| 1055 | patterns: list[tuple[str, str]] = [ |
| 1056 | # - x.00.pth, x.01.pth, etc. |
| 1057 | (r'\.[0-9]{2}\.pth$', f'.{n:02}.pth'), |
| 1058 | # - x-00001-of-00002.bin, x-00002-of-00002.bin, etc. |
| 1059 | (r'-[0-9]{5}-of-(.*)$', fr'-{n:05}-of-\1'), |
| 1060 | # x.bin, x.bin.1, etc. |
| 1061 | (r'(\.[0-9]+)?$', r'\1' if n == 0 else fr'\1.{n}'), |
| 1062 | # x_0.pt, x_1.pt, etc. |
| 1063 | (r'(_[0-9]+)?\.pt$', fr'_{n}.pt'), |
| 1064 | ] |
| 1065 | for regex, replacement in patterns: |
| 1066 | if re.search(regex, path.name): |
| 1067 | new_path = path.with_name(re.sub(regex, replacement, path.name)) |
| 1068 | if new_path.exists(): |
| 1069 | return new_path |
| 1070 | return None |
| 1071 | |
| 1072 | |
| 1073 | def find_multifile_paths(path: Path) -> list[Path]: |
no test coverage detected