Skip to content

Commit 5f37d97

Browse files
committed
fix(pt_expt): enforce raw checkpoint routing contracts
1 parent 8ef72a3 commit 5f37d97

7 files changed

Lines changed: 431 additions & 258 deletions

File tree

deepmd/backend/pt_expt.py

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
# SPDX-License-Identifier: LGPL-3.0-or-later
22
from collections.abc import (
33
Callable,
4-
Mapping,
54
)
65
from importlib.util import (
76
find_spec,
87
)
98
from typing import (
109
TYPE_CHECKING,
11-
Any,
1210
ClassVar,
1311
)
1412

1513
from deepmd.backend.backend import (
1614
Backend,
1715
)
16+
from deepmd.utils.pt_checkpoint import (
17+
detect_pt_checkpoint_backend,
18+
)
1819

1920
if TYPE_CHECKING:
2021
from argparse import (
@@ -29,46 +30,6 @@
2930
)
3031

3132

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-
7233
@Backend.register("pt-expt")
7334
@Backend.register("pytorch-exportable")
7435
class PyTorchExportableBackend(Backend):

0 commit comments

Comments
 (0)