Skip to content

Commit 7e539d6

Browse files
author
Han Wang
committed
Merge upstream/master into feat-sfpg-multirank
Conflict in deepmd/pt_expt/utils/serialization.py: upstream deepmodeling#5913 relocated the graph edge-dtype helper to deepmd/pt_expt/model/graph_lower.py as a public graph_edge_dtype(), while this branch had rewritten the same helper in place to answer from the atomic-model capability instead of reaching through a single .descriptor. Resolution keeps BOTH: upstream's location (serialization.py imports it from graph_lower) with this branch's implementation. The reach-in version is wrong for compositions -- a LinearEnergyAtomicModel has no .descriptor, so a bridged/ZBL model would silently report float64 regardless of its children -- which is exactly what the capability aggregation fixes. Dropped the now-unused torch import from graph_lower.py.
2 parents 975e17e + 9b2582f commit 7e539d6

33 files changed

Lines changed: 2064 additions & 372 deletions

deepmd/backend/pt_expt.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from deepmd.backend.backend import (
1414
Backend,
1515
)
16+
from deepmd.utils.pt_checkpoint import (
17+
detect_pt_checkpoint_backend,
18+
)
1619

1720
if TYPE_CHECKING:
1821
from argparse import (
@@ -51,11 +54,8 @@ def match_filename(cls, filename: str) -> int:
5154
Returns
5255
-------
5356
- 1 for the regular `.pte` / `.pt2` suffixes (default behaviour).
54-
- 2 for `.pt` files whose state-dict uses pt_expt's dpmodel
55-
parameter naming (`.w`/`.b`); this outranks the legacy pt
56-
backend's default suffix score (1) so pt_expt-trained `.pt`
57-
checkpoints route here, while genuine pt-trained `.pt` files
58-
(which use `.matrix`/`.bias`) keep going to the pt backend.
57+
- 2 for `.pt` files whose state dictionary uses the pt_expt parameter
58+
dialect. This outranks the pt backend's default suffix score (1).
5959
- 0 otherwise.
6060
"""
6161
score = super().match_filename(filename)
@@ -69,21 +69,14 @@ def match_filename(cls, filename: str) -> int:
6969

7070
# weights_only=True avoids unpickling arbitrary code from an
7171
# untrusted .pt — sniffing only needs the dict keys.
72-
sd = torch.load(filename, map_location="cpu", weights_only=True)
72+
checkpoint = torch.load(filename, map_location="cpu", weights_only=True)
7373
except Exception:
7474
# Not a valid torch archive (corrupt file, wrong format, or a
7575
# weights_only=True restriction trip). Surrender the claim so
7676
# the dispatcher falls back to the default suffix match — pt's
7777
# default score (1) will pick up the file under `dp --pt`.
7878
return 0
79-
if isinstance(sd, dict) and "model" in sd:
80-
sd = sd["model"]
81-
keys = list(sd.keys()) if hasattr(sd, "keys") else []
82-
has_pt_expt = any(k.endswith(".w") or k.endswith(".b") for k in keys)
83-
has_pt = any(k.endswith(".matrix") or k.endswith(".bias") for k in keys)
84-
if has_pt_expt and not has_pt:
85-
return 2
86-
return 0
79+
return 2 if detect_pt_checkpoint_backend(checkpoint) == "pt-expt" else 0
8780

8881
def is_available(self) -> bool:
8982
"""Check if the backend is available.

deepmd/pt/optimizer/hybrid_muon.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
Routing is controlled by parameter dimensionality, parameter names, and
77
``muon_mode``:
88
9-
- Parameters whose final effective name segment contains ``bias``
10-
(case-insensitive), or starts with ``adam_`` (case-insensitive): Adam.
9+
- Parameters whose final effective name segment is ``b``, contains ``bias``,
10+
or starts with ``adam_`` (case-insensitive): Adam.
1111
- Parameters whose final effective name segment starts with ``adamw_``
1212
(case-insensitive): Adam with decoupled weight decay (AdamW-style).
1313
The final effective segment means the last non-numeric segment in the full
@@ -715,7 +715,7 @@ def get_adam_route(
715715
effective name segment after stripping trailing numeric ParameterList
716716
indices):
717717
718-
1. Contains ``"bias"`` -> ``"adam"`` (no weight decay).
718+
1. Is ``"b"`` or contains ``"bias"`` -> ``"adam"`` (no weight decay).
719719
2. Starts with ``"adam_"`` -> ``"adam"`` (no weight decay).
720720
Typical: norm scales, radial frequencies.
721721
3. Starts with ``"adamw_"`` -> ``"adamw"`` (decoupled weight decay).
@@ -730,7 +730,7 @@ def get_adam_route(
730730
while leaf_name_idx > 0 and name_segments[leaf_name_idx].isdigit():
731731
leaf_name_idx -= 1
732732
leaf_name = name_segments[leaf_name_idx]
733-
if "bias" in leaf_name:
733+
if leaf_name == "b" or "bias" in leaf_name:
734734
return "adam"
735735
if leaf_name.startswith("adam_"):
736736
return "adam"
@@ -808,9 +808,9 @@ class HybridMuonOptimizer(Optimizer):
808808
809809
This optimizer applies different update rules based on parameter dimensionality,
810810
parameter names, and ``muon_mode``:
811-
- Parameters with final effective name segment containing ``bias``
812-
(case-insensitive), or starting with ``adam_`` (case-insensitive):
813-
standard Adam update.
811+
- Parameters with final effective name segment equal to ``b``, containing
812+
``bias``, or starting with ``adam_`` (case-insensitive): standard Adam
813+
update.
814814
- Parameters with final effective name segment starting with ``adamw_``
815815
(case-insensitive): Adam with decoupled weight decay (AdamW-style).
816816
- 1D parameters: standard Adam update.
@@ -826,8 +826,8 @@ class HybridMuonOptimizer(Optimizer):
826826
``(m, n)`` slice.
827827
828828
Naming convention for explicit Adam routing:
829-
- Parameters representing bias terms should include ``bias`` in their
830-
final effective name segment (case-insensitive).
829+
- Parameters representing bias terms should use ``b`` or include ``bias``
830+
in their final effective name segment (case-insensitive).
831831
- Parameters that are not semantic bias but should still use Adam should
832832
use an ``adam_`` prefix in their final effective name segment
833833
(case-insensitive).
@@ -887,10 +887,10 @@ class HybridMuonOptimizer(Optimizer):
887887
- ``"slice"``: >=3D parameters use per-slice Muon routing on last two dims.
888888
named_parameters : iterable[tuple[str, torch.Tensor]] | None
889889
Optional named parameter iterable used for name-based routing.
890-
Parameters with final effective name segment containing ``bias``
891-
(case-insensitive), or starting with ``adam_`` (case-insensitive),
892-
are forced to Adam (no weight decay). Parameters starting with
893-
``adamw_`` are forced to AdamW-style decoupled decay path.
890+
Parameters with final effective name segment equal to ``b``, containing
891+
``bias``, or starting with ``adam_`` (case-insensitive) are forced to
892+
Adam (no weight decay). Parameters starting with ``adamw_`` are forced
893+
to AdamW-style decoupled decay path.
894894
enable_gram : bool
895895
Enable the compiled Gram Newton-Schulz path for rectangular Muon
896896
matrices. Square matrices continue to use the current standard
@@ -1502,7 +1502,7 @@ def _build_param_routing(self) -> None:
15021502
Classify parameters into Muon, Adam, and AdamW routes (static routing).
15031503
15041504
Routing logic:
1505-
- name-based ``adam_`` prefix or contains ``bias`` → Adam (no decay)
1505+
- name-based ``b``, ``bias``, or ``adam_`` route → Adam (no decay)
15061506
- name-based ``adamw_`` prefix → AdamW (decoupled weight decay)
15071507
- effective shape rank <2 → Adam (no decay)
15081508
- non-matrix effective shape for current muon_mode → AdamW (decoupled)

deepmd/pt/utils/compile_compat.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ def _torch_release() -> tuple[int, int]:
8484
def apply_global_compile_patches() -> None:
8585
"""Apply every process-global PyTorch adjustment the compile path needs.
8686
87-
The adjustments are mutually independent and individually idempotent. The
88-
function is intended to run exactly once, when the model module is
89-
imported, so that the global state is established before the first
90-
compilation. The symbolic-divisibility repair is applied only on the
91-
releases where the regression exists.
87+
The adjustments are mutually independent and individually idempotent.
88+
Invoke this function before the first Dynamo or Inductor compilation in
89+
each entry path; repeated calls from independent compile paths are safe.
90+
The symbolic-divisibility repair is applied only on releases where the
91+
regression exists.
9292
"""
9393
# Silence Inductor / Triton autotune console dumps. ``torch.compile``
9494
# reads these environment variables once, when its backend is first

0 commit comments

Comments
 (0)