|
1 | 1 | # SPDX-License-Identifier: LGPL-3.0-or-later |
2 | 2 | from collections.abc import ( |
3 | 3 | Callable, |
4 | | - Mapping, |
5 | 4 | ) |
6 | 5 | from importlib.util import ( |
7 | 6 | find_spec, |
8 | 7 | ) |
9 | 8 | from typing import ( |
10 | 9 | TYPE_CHECKING, |
11 | | - Any, |
12 | 10 | ClassVar, |
13 | 11 | ) |
14 | 12 |
|
15 | 13 | from deepmd.backend.backend import ( |
16 | 14 | Backend, |
17 | 15 | ) |
| 16 | +from deepmd.utils.pt_checkpoint import ( |
| 17 | + detect_pt_checkpoint_backend, |
| 18 | +) |
18 | 19 |
|
19 | 20 | if TYPE_CHECKING: |
20 | 21 | from argparse import ( |
|
29 | 30 | ) |
30 | 31 |
|
31 | 32 |
|
32 | | -def detect_pt_checkpoint_backend(checkpoint: Any) -> str | None: |
33 | | - """Detect the backend dialect of a raw PyTorch checkpoint. |
34 | | -
|
35 | | - Parameters |
36 | | - ---------- |
37 | | - checkpoint : Any |
38 | | - A checkpoint payload or its unwrapped model state dictionary. |
39 | | -
|
40 | | - Returns |
41 | | - ------- |
42 | | - str or None |
43 | | - ``"pt-expt"`` or ``"pt"`` when the parameter names identify one |
44 | | - backend unambiguously, otherwise ``None``. |
45 | | - """ |
46 | | - state_dict = checkpoint |
47 | | - if isinstance(state_dict, Mapping) and "model" in state_dict: |
48 | | - state_dict = state_dict["model"] |
49 | | - if not isinstance(state_dict, Mapping): |
50 | | - return None |
51 | | - |
52 | | - keys = tuple(key for key in state_dict if isinstance(key, str)) |
53 | | - |
54 | | - # Weight names are decisive. pt_expt DPA4 also contains ordinary |
55 | | - # torch-native ``.bias`` parameters, so bias names cannot override a |
56 | | - # clear ``.w`` versus ``.matrix`` distinction. |
57 | | - has_pt_expt_weight = any(key.endswith(".w") for key in keys) |
58 | | - has_pt_weight = any(key.endswith(".matrix") for key in keys) |
59 | | - if has_pt_expt_weight or has_pt_weight: |
60 | | - if has_pt_expt_weight == has_pt_weight: |
61 | | - return None |
62 | | - return "pt-expt" if has_pt_expt_weight else "pt" |
63 | | - |
64 | | - # Bias-only state dictionaries retain the original dialect fallback. |
65 | | - has_pt_expt_bias = any(key.endswith(".b") for key in keys) |
66 | | - has_pt_bias = any(key.endswith(".bias") for key in keys) |
67 | | - if has_pt_expt_bias == has_pt_bias: |
68 | | - return None |
69 | | - return "pt-expt" if has_pt_expt_bias else "pt" |
70 | | - |
71 | | - |
72 | 33 | @Backend.register("pt-expt") |
73 | 34 | @Backend.register("pytorch-exportable") |
74 | 35 | class PyTorchExportableBackend(Backend): |
|
0 commit comments