Commit d8892c7
fix(tf): reject non-prefix use_spin layouts in the spin helper (deepmodeling#5727)
## Problem
Fixes deepmodeling#5680. The legacy TensorFlow spin implementation assumes
spin-enabled types form a contiguous prefix of the type map. The SE-A
`sel` extension takes the first `ntypes_spin` selections
(`sel_a[:ntypes_spin]`), and the coordinate splitting
(`deepmd/tf/descriptor/se_a.py`), force splitting
(`deepmd/tf/model/ener.py`), and bias merging (`deepmd/tf/fit/ener.py`)
all address the virtual block with a dense real-to-virtual offset (`i +
len(use_spin)`). For a non-prefix layout such as `use_spin=[False,
True]`, these read the wrong real/virtual type ranges or raise deep
inside the graph, and nothing rejected the configuration up front. In
practice all supported spin models list spin-enabled types first, so the
broken layout went unnoticed.
## Approach
The maintained PyTorch backend already handles the sparse/non-prefix
layout via `Spin.spin_type`. Rather than refactor all four coupled sites
in this legacy backend (which has almost no coverage and where a subtle
mistake would silently corrupt training), this guards against the
unsupported layout: the TF `Spin` helper now rejects a `use_spin` where
a non-spin type precedes a spin-enabled one, with a message telling the
user to list spin-enabled types first. This turns a silent-wrong result
or an obscure crash into an actionable error and documents the invariant
in one place.
## Test
Adds `source/tests/tf/test_spin_prefix_guard.py`: non-prefix layouts
(`[False, True]` and `[True, False, True]`) raise `ValueError`, while
prefix layouts (`[True, False]`, `[True, True]`) and an all-non-spin
list (`[False, False]`) are accepted. The existing TF spin model test
uses a valid `[True, False]` prefix and continues to pass. The prefix
requirement previously had no test.
Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>1 parent aff66bd commit d8892c7
2 files changed
Lines changed: 63 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
29 | 48 | | |
30 | 49 | | |
31 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
0 commit comments