Commit ab6e300
perf: use torch.topk to construct nlist (#4751)
This pull request updates the `build_neighbor_list` function in
`deepmd/pt/utils/nlist.py` to improve its performance and correctness
when selecting neighbors. The key change involves replacing `torch.sort`
with `torch.topk` to optimize the selection of the nearest neighbors.
<details><summary>Details</summary>
<p>
Before: 16.4ms
<img width="495" alt="image"
src="https://github.com/user-attachments/assets/e6b0c091-b11b-491a-b2fd-e0aadc6c35ba"
/>
After: 3.4ms
<img width="303" alt="image"
src="https://github.com/user-attachments/assets/81931c76-60eb-46a5-9934-0d837b954df9"
/>
Step time goes from 212.5ms to 200.5ms, ~5% speed-up.
</p>
</details>
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **Refactor**
- Enhanced efficiency and clarity in neighbor selection, improving
performance in identifying closest neighbors while maintaining the same
user experience.
- **Tests**
- Improved reliability of neighbor list tests by validating neighbor
sets regardless of order.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Signed-off-by: Chun Cai <amoycaic@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>1 parent 5b1bbc2 commit ab6e300
File tree
2 files changed
+31
-17
lines changed- deepmd/pt/utils
- source/tests/pt/model
2 files changed
+31
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
97 | 96 | | |
98 | 97 | | |
99 | 98 | | |
100 | 99 | | |
101 | 100 | | |
102 | 101 | | |
103 | 102 | | |
| 103 | + | |
| 104 | + | |
104 | 105 | | |
105 | 106 | | |
106 | | - | |
107 | | - | |
108 | | - | |
| 107 | + | |
| 108 | + | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
118 | 119 | | |
119 | 120 | | |
120 | 121 | | |
121 | | - | |
122 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
123 | 133 | | |
124 | 134 | | |
125 | 135 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
155 | 159 | | |
156 | | - | |
157 | | - | |
| 160 | + | |
| 161 | + | |
158 | 162 | | |
159 | 163 | | |
160 | | - | |
161 | | - | |
| 164 | + | |
| 165 | + | |
162 | 166 | | |
163 | 167 | | |
164 | 168 | | |
| |||
0 commit comments