deepmodeling / deepmodeling/deepmd-kit
[BUG] Native-spin DPA4C graph path fails on zero-node graphs
- Dominant language
- Python
- Stars
- 2k
- Forks
- 649
- Avg merge
- 6d 18h
- Merged PRs (30d)
- 15
Description
### Summary
The portable native-spin DPA4C graph path crashes when the graph has zero nodes. The spin-free path and the compressed native-spin CPU path handle this empty-node case.
This reproduces on `origin/master` at `8cfd46e37448`.
### Reproduction
```python
import torch
from deepmd.dpmodel.utils.neighbor_graph import NeighborGraph
from deepmd.pt_expt.descriptor.dpa4c import DescrptDPA4C
d = DescrptDPA4C(
rcut=3.0,
ntypes=2,
channels=8,
lmax=2,
n_radial=4,
precision="float32",
use_spin=[True, False],
).eval()
g = NeighborGraph(
n_node=torch.zeros(1, dtype=torch.int64),
n_local=torch.zeros(1, dtype=torch.int64),
edge_index=torch.empty((2, 0), dtype=torch.int64),
edge_vec=torch.empty((0, 3), dtype=torch.float32),
edge_mask=torch.empty((0,), dtype=torch.bool),
)
d.call_graph(
g,
torch.empty((0,), dtype=torch.int64),
spin=torch.empty((0, 3), dtype=torch.float32),
)
```
Actual result:
```text
deepmd/dpmodel/descriptor/dpa4c_nn/spin.py:328
RuntimeError: cannot reshape tensor of 0 elements into shape [0, -1]
```
### Cause and expected behavior
The spin/geometric cross Gram is reshaped to `(0, -1)`, for which PyTorch cannot infer the second dimension. The width is already known and can be stated explicitly as `self.quadrupole_width * self.degree_channels[2]`.
The call should return an empty descriptor with shape `(0, descriptor.get_dim_out())`. A regression test should cover `call_graph()` on the same empty native-spin graph already covered by the compressed CPU reference.
---
Coding agent: Codex
Codex version: codex-cli 0.149.0
Model: gpt-5.6-sol
Reasoning effort: xhigh
Contributor guide
Research direction
Start in deepmd/dpmodel/descriptor/dpa4c_nn/spin.py around line 328 and trace the native-spin graph path through call_graph(). Locate the existing compressed CPU reference test for an empty graph and add the same regression coverage for this path. Done means the empty graph returns a descriptor shaped (0, descriptor.get_dim_out()) without the reshape error, and the relevant tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100