Skip to content

Commit 58c23e4

Browse files
author
Han Wang
committed
docs(dpmodel,pt_expt): numpydoc sections for all remaining PR-added methods
Audit of every method added by this branch against the package docstring convention (numpydoc with underlined Parameters/Returns/Raises sections) found 13 prose-only or section-incomplete docstrings; this completes them: - repformers.py: _cal_hg_graph, _cal_grrg_graph, symmetrization_op_graph, _exchange_ghosts_graph, Atten2MultiHeadApply.call_graph, Atten2EquiVarApply.call_graph (full Parameters/Returns); _update_g1_conv_graph, _update_g2_g1g1_graph (Returns added). - dpa2.py: uses_graph_lower (Returns), _block_graph (comment block converted to a full docstring; also updates the stale slice rationale -- the phantom-count compensation made the softmax denominator width-independent, the slice is kept for the minimal static pair enumeration). - pt_expt: repformers._exchange_ghosts_graph (Parameters/Returns/Raises), training._model_trace_device (Parameters/Returns), serialization._build_graph_dynamic_shapes_with_comm (Returns). The inner export closure fn() in forward_lower_graph_exportable_with_comm intentionally stays docstring-less, matching the file's two pre-existing fn closures (implementation detail of the documented enclosing method).
1 parent b73302b commit 58c23e4

5 files changed

Lines changed: 231 additions & 27 deletions

File tree

deepmd/dpmodel/descriptor/dpa2.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,12 @@ def uses_graph_lower(self) -> bool:
719719
(needs the angle machinery -- PR-G-dpa3), compressed descriptors
720720
(geo/tebd tabulation is dense-only), and the explicit disable flag
721721
(used by e.g. the spin model wrapper).
722+
723+
Returns
724+
-------
725+
uses_graph_lower : bool
726+
Whether the graph-native lower (:meth:`call_graph`) is supported
727+
for the current descriptor configuration.
722728
"""
723729
if self._graph_lower_disabled:
724730
return False
@@ -1124,27 +1130,45 @@ def call_graph(
11241130
e_ax = graph.edge_mask.shape[0]
11251131

11261132
def _block_graph(rc: float, ns: int) -> tuple[Any, int | None]:
1127-
# graph analogue of build_multiple_neighbor_list (nlist.py:408):
1128-
# dist mask always; slot TRUNCATION ONLY in the shape-static
1129-
# dense-adapter layout, replicating the dense
1130-
# `nlist[:, :, :ns]` slicing.
1131-
#
1132-
# This must be a genuine array-width SLICE, not just an
1133-
# edge_mask AND: the segment_sum-based channels only see zero
1134-
# contributions from masked-out padding regardless of the
1135-
# array's width, but the smooth-attention softmax
1136-
# (RepformerLayer.call_graph) keeps every padding PAIR in the
1137-
# denominator at exp(-attnw_shift) (dpa1 precedent) -- so the
1138-
# padding-pair COUNT, governed by the static width handed to
1139-
# `center_edge_pairs`/`static_nnei` and not merely by the mask
1140-
# contents, must match the dense sub-nlist width `ns` bit-for-
1141-
# bit, or the attention normalization silently drifts by
1142-
# O(1e-4) (extra always-masked pairs still contribute
1143-
# exp(-shift) to the denominator). Carry-all graphs
1144-
# (static_nnei is None) have no static width at all: sel is
1145-
# normalization-only there (spec decision #9), and the compact
1146-
# attention pairing groups per-center dynamically, so no
1147-
# truncation is needed or possible.
1133+
"""Per-block view of the model graph (local closure).
1134+
1135+
Graph analogue of ``build_multiple_neighbor_list``
1136+
(nlist.py:408): dist mask always; slot TRUNCATION only in the
1137+
shape-static dense-adapter layout, replicating the dense
1138+
``nlist[:, :, :ns]`` slicing.
1139+
1140+
Parameters
1141+
----------
1142+
rc
1143+
The block cutoff radius.
1144+
ns
1145+
The block sel (dense sub-nlist width).
1146+
1147+
Returns
1148+
-------
1149+
block_graph : NeighborGraph
1150+
The block-restricted graph view.
1151+
block_static_nnei : int | None
1152+
The block's static width (``ns``) in the shape-static
1153+
layout, ``None`` for carry-all graphs.
1154+
1155+
Notes
1156+
-----
1157+
The genuine array-width SLICE (rather than an edge_mask AND)
1158+
keeps the shape-static pair enumeration
1159+
(``center_edge_pairs``/``static_nnei``) at exactly the dense
1160+
sub-nlist width ``ns``: the pair count stays minimal, and the
1161+
attention softmax sees the same present-slot layout as the
1162+
dense body. (Before the fixed-phantom-count compensation in
1163+
``segment_softmax`` this width-match was also required for
1164+
numeric parity -- extra always-masked pairs each contributed
1165+
``exp(-attnw_shift)`` to the denominator; the compensation has
1166+
since made the denominator width-independent.) Carry-all graphs
1167+
(``static_nnei is None``) have no static width at all: sel is
1168+
normalization-only there (spec decision #9), and the compact
1169+
attention pairing groups per-center dynamically, so no
1170+
truncation is needed or possible.
1171+
"""
11481172
if static_nnei is None or ns >= static_nnei:
11491173
m = graph.edge_mask & (dist <= rc)
11501174
return dataclasses.replace(graph, edge_mask=m), static_nnei

deepmd/dpmodel/descriptor/repformers.py

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,27 @@ def _exchange_ghosts_graph(
540540
pt_expt subclass overrides this to overwrite halo rows via
541541
``deepmd_export::border_op`` when ``comm_dict`` is provided (C++
542542
multi-rank extended-region graphs).
543+
544+
Parameters
545+
----------
546+
g1
547+
Flat node-wise atomic invariant rep, with shape [n_total, ng1].
548+
comm_dict
549+
MPI communication metadata; must be ``None`` on this (dpmodel)
550+
path.
551+
n_total
552+
Total number of nodes (unused here; the pt_expt override needs
553+
it).
554+
555+
Returns
556+
-------
557+
g1 : Array
558+
The (unchanged) node channel, with shape [n_total, ng1].
559+
560+
Raises
561+
------
562+
NotImplementedError
563+
If ``comm_dict`` is not ``None``.
543564
"""
544565
del n_total
545566
if comm_dict is not None:
@@ -1124,8 +1145,34 @@ def _cal_hg_graph(
11241145
) -> Array:
11251146
"""Graph twin of :func:`_cal_hg`: hg[n, a, b] = sum_{e: dst(e)=n} h_e[a] g_e[b].
11261147
1127-
``nnei`` is the block sel (the smooth-branch normalization constant, spec
1128-
decision #9: sel = normalization only).
1148+
Parameters
1149+
----------
1150+
g
1151+
Flat edge-wise invariant rep, with shape [n_edge, ng].
1152+
h
1153+
Flat edge-wise equivariant rep, with shape [n_edge, 3].
1154+
edge_mask
1155+
Edge mask, where zero means no edge, with shape [n_edge].
1156+
sw
1157+
The switch function per edge, with shape [n_edge].
1158+
dst
1159+
Destination (center) node index of each edge, with shape [n_edge].
1160+
n_total
1161+
Total number of nodes.
1162+
nnei
1163+
The block sel (the smooth-branch normalization constant, spec
1164+
decision #9: sel = normalization only).
1165+
smooth
1166+
Whether to use the smooth (sw-weighted, sel-normalized) branch.
1167+
epsilon
1168+
Degree-normalization protection for the non-smooth branch.
1169+
use_sqrt_nnei
1170+
Whether to normalize by sqrt(nnei) instead of nnei.
1171+
1172+
Returns
1173+
-------
1174+
hg : Array
1175+
Transposed rotation matrix per node, with shape [n_total, 3, ng].
11291176
"""
11301177
from deepmd.dpmodel.utils.neighbor_graph import (
11311178
segment_sum,
@@ -1148,7 +1195,20 @@ def _cal_hg_graph(
11481195

11491196

11501197
def _cal_grrg_graph(hg: Array, axis_neuron: int) -> Array:
1151-
"""Graph twin of :func:`_cal_grrg` (node-local, no neighbor axis)."""
1198+
"""Graph twin of :func:`_cal_grrg` (node-local, no neighbor axis).
1199+
1200+
Parameters
1201+
----------
1202+
hg
1203+
Transposed rotation matrix per node, with shape [n_total, 3, ng].
1204+
axis_neuron
1205+
Size of the submatrix of hg (embedding matrix).
1206+
1207+
Returns
1208+
-------
1209+
grrg : Array
1210+
Atomic invariant rep, with shape [n_total, axis_neuron * ng].
1211+
"""
11521212
xp = array_api_compat.array_namespace(hg)
11531213
n, _, ng = hg.shape
11541214
hgm = hg[..., :axis_neuron] # (N, 3, axis)
@@ -1169,7 +1229,38 @@ def symmetrization_op_graph(
11691229
epsilon: float = 1e-4,
11701230
use_sqrt_nnei: bool = True,
11711231
) -> Array:
1172-
"""Graph twin of :func:`symmetrization_op`."""
1232+
"""Graph twin of :func:`symmetrization_op`.
1233+
1234+
Parameters
1235+
----------
1236+
g
1237+
Flat edge-wise invariant rep, with shape [n_edge, ng].
1238+
h
1239+
Flat edge-wise equivariant rep, with shape [n_edge, 3].
1240+
edge_mask
1241+
Edge mask, where zero means no edge, with shape [n_edge].
1242+
sw
1243+
The switch function per edge, with shape [n_edge].
1244+
dst
1245+
Destination (center) node index of each edge, with shape [n_edge].
1246+
n_total
1247+
Total number of nodes.
1248+
nnei
1249+
The block sel (the smooth-branch normalization constant).
1250+
axis_neuron
1251+
Size of the submatrix of hg (embedding matrix).
1252+
smooth
1253+
Whether to use the smooth (sw-weighted, sel-normalized) branch.
1254+
epsilon
1255+
Degree-normalization protection for the non-smooth branch.
1256+
use_sqrt_nnei
1257+
Whether to normalize by sqrt(nnei) instead of nnei.
1258+
1259+
Returns
1260+
-------
1261+
grrg : Array
1262+
Atomic invariant rep, with shape [n_total, axis_neuron * ng].
1263+
"""
11731264
hg = _cal_hg_graph(
11741265
g,
11751266
h,
@@ -1480,7 +1571,26 @@ def call(
14801571
def call_graph(
14811572
self, AA: Array, g2: Array, q_e: Array, k_e: Array, e_tot: int
14821573
) -> Array:
1483-
"""Graph twin of :meth:`call`: out[q] = sum_k AA[q,k] g2v[k] per head."""
1574+
"""Graph twin of :meth:`call`: out[q] = sum_k AA[q,k] g2v[k] per head.
1575+
1576+
Parameters
1577+
----------
1578+
AA
1579+
Attention map on the flat pair axis, with shape [n_pair, nh].
1580+
g2
1581+
Flat edge-wise pair invariant rep, with shape [n_edge, ng2].
1582+
q_e
1583+
Query edge index of each pair, with shape [n_pair].
1584+
k_e
1585+
Key edge index of each pair, with shape [n_pair].
1586+
e_tot
1587+
Total number of edges.
1588+
1589+
Returns
1590+
-------
1591+
g2_new : Array
1592+
Attention-updated edge channel, with shape [n_edge, ng2].
1593+
"""
14841594
from deepmd.dpmodel.utils.neighbor_graph import (
14851595
segment_sum,
14861596
)
@@ -1578,7 +1688,27 @@ def call(
15781688
def call_graph(
15791689
self, AA: Array, h2: Array, q_e: Array, k_e: Array, e_tot: int
15801690
) -> Array:
1581-
"""Graph twin of :meth:`call` (heads applied to the equivariant channel)."""
1691+
"""Graph twin of :meth:`call` (heads applied to the equivariant channel).
1692+
1693+
Parameters
1694+
----------
1695+
AA
1696+
Attention map on the flat pair axis, with shape [n_pair, nh].
1697+
h2
1698+
Flat edge-wise pair equivariant rep, with shape [n_edge, 3].
1699+
q_e
1700+
Query edge index of each pair, with shape [n_pair].
1701+
k_e
1702+
Key edge index of each pair, with shape [n_pair].
1703+
e_tot
1704+
Total number of edges.
1705+
1706+
Returns
1707+
-------
1708+
h2_new : Array
1709+
Attention-updated equivariant edge channel, with shape
1710+
[n_edge, 3].
1711+
"""
15821712
from deepmd.dpmodel.utils.neighbor_graph import (
15831713
segment_sum,
15841714
)
@@ -2260,6 +2390,11 @@ def _update_g1_conv_graph(
22602390
The block sel: the smooth-branch normalization constant AND the
22612391
fixed dense softmax width for the attention phantom-count
22622392
compensation (see :meth:`Atten2Map.call_graph`).
2393+
2394+
Returns
2395+
-------
2396+
g1_conv : Array
2397+
Convolution-updated node channel, with shape [n_total, ng1].
22632398
"""
22642399
from deepmd.dpmodel.utils.neighbor_graph import (
22652400
segment_sum,
@@ -2339,6 +2474,11 @@ def _update_g2_g1g1_graph(
23392474
Edge mask, where zero means no edge, with shape [n_edge].
23402475
sw
23412476
The switch function, with shape [n_edge].
2477+
2478+
Returns
2479+
-------
2480+
g1g1 : Array
2481+
Per-edge center-neighbor g1 product, with shape [n_edge, ng1].
23422482
"""
23432483
xp = array_api_compat.array_namespace(g1, edge_mask, sw)
23442484
# (n_edge, ng1)

deepmd/pt_expt/descriptor/repformers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ def _exchange_ghosts_graph(
108108
(ghost-free Python graphs / extended single-process graphs). Spin
109109
models never route the graph lower (``disable_graph_lower``), so a
110110
``has_spin`` comm_dict reaching here is a programming error.
111+
112+
Parameters
113+
----------
114+
g1
115+
Flat node-wise atomic invariant rep, with shape [n_total, ng1].
116+
comm_dict
117+
MPI communication metadata (``send_list``, ``send_proc``,
118+
``recv_proc``, ``send_num``, ``recv_num``, ``communicator``,
119+
``nlocal``, ``nghost``); ``None`` for single-process graphs.
120+
n_total
121+
Total number of nodes (``nlocal + nghost``).
122+
123+
Returns
124+
-------
125+
g1 : torch.Tensor
126+
The node channel with halo rows refreshed, with shape
127+
[n_total, ng1].
128+
129+
Raises
130+
------
131+
NotImplementedError
132+
If ``comm_dict`` carries ``has_spin``.
111133
"""
112134
if comm_dict is None:
113135
return super()._exchange_ghosts_graph(g1, comm_dict, n_total)

deepmd/pt_expt/train/training.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,17 @@ def _model_trace_device(model: torch.nn.Module) -> torch.device:
627627
callers may legitimately hold the model on a different device (e.g. a test
628628
pinning the model to CPU while running on a CUDA host). Falls back to
629629
:data:`DEVICE` only if the model exposes no parameters or buffers.
630+
631+
Parameters
632+
----------
633+
model
634+
The model whose parameter/buffer device determines the trace device.
635+
636+
Returns
637+
-------
638+
device : torch.device
639+
The device of the model's first parameter (or buffer), falling back
640+
to the global :data:`DEVICE`.
630641
"""
631642
for _t in model.parameters():
632643
return _t.device

deepmd/pt_expt/utils/serialization.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,13 @@ def _build_graph_dynamic_shapes_with_comm(
538538
charge_spin, send_list, send_proc, recv_proc, send_num, recv_num,
539539
communicator, nlocal, nghost)`` — 16 entries matching
540540
``forward_lower_graph_exportable_with_comm``.
541+
542+
Returns
543+
-------
544+
dynamic_shapes : tuple
545+
Per-input dynamic-shape specs (dicts of ``torch.export.Dim`` or
546+
``None``) in the same order as ``sample_inputs``, for
547+
``torch.export.export(..., dynamic_shapes=...)``.
541548
"""
542549
fparam = sample_inputs[5]
543550
aparam = sample_inputs[6]

0 commit comments

Comments
 (0)