Skip to content

Commit 80a4c5f

Browse files
fix
1 parent 171675b commit 80a4c5f

File tree

3 files changed

+40
-56
lines changed

3 files changed

+40
-56
lines changed

deepmd/pd/infer/deep_eval.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def __init__(
196196
else False
197197
)
198198
if callable(self._has_spin):
199-
self._has_spin = self._has_spin()
199+
setattr(self, "_has_spin", self._has_spin())
200200
self._has_hessian = False
201201

202202
def get_rcut(self) -> float:
@@ -495,10 +495,14 @@ def _eval_model(
495495
self.box_handle.copy_from_cpu(cells.reshape([nframes, 3, 3]))
496496

497497
if fparam is not None:
498-
raise NotImplementedError("fparam_input is not supported yet")
498+
raise NotImplementedError(
499+
"fparam_input is not supported for .json files. Please use a .pd file instead."
500+
)
499501

500502
if aparam is not None:
501-
raise NotImplementedError("aparam_input is not supported yet")
503+
raise NotImplementedError(
504+
"aparam_input is not supported for .json files. Please use a .pd file instead."
505+
)
502506

503507
else:
504508
coord_input = paddle.to_tensor(

doc/third-party/ase.md

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
:::{note}
44
See [Environment variables](../env.md) for the runtime environment variables.
5-
**Supported backends**: TensorFlow {{ tensorflow_icon }}, PyTorch {{ pytorch_icon }}, Paddle {{ paddle_icon }}
65
:::
76

87
Deep potential can be set up as a calculator with ASE to obtain potential energies and forces.
@@ -25,16 +24,6 @@ print(water.get_potential_energy())
2524
print(water.get_forces())
2625
```
2726

28-
Optimization is also available:
29-
30-
```python
31-
from ase.optimize import BFGS
32-
33-
dyn = BFGS(water)
34-
dyn.run(fmax=1e-6)
35-
print(water.get_positions())
36-
```
37-
3827
:::
3928

4029
:::{tab-item} PyTorch {{ pytorch_icon }}
@@ -53,16 +42,6 @@ print(water.get_potential_energy())
5342
print(water.get_forces())
5443
```
5544

56-
Optimization is also available:
57-
58-
```python
59-
from ase.optimize import BFGS
60-
61-
dyn = BFGS(water)
62-
dyn.run(fmax=1e-6)
63-
print(water.get_positions())
64-
```
65-
6645
:::
6746

6847
:::{tab-item} Paddle {{ paddle_icon }}
@@ -81,6 +60,10 @@ print(water.get_potential_energy())
8160
print(water.get_forces())
8261
```
8362

63+
:::
64+
65+
::::
66+
8467
Optimization is also available:
8568

8669
```python
@@ -90,7 +73,3 @@ dyn = BFGS(water)
9073
dyn.run(fmax=1e-6)
9174
print(water.get_positions())
9275
```
93-
94-
:::
95-
96-
::::

source/api_cc/src/DeepPotPD.cc

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,23 @@ inline void enableTimestamp(bool enable = true) {
164164
}
165165
} // namespace logg
166166

167-
std::vector<int> createNlistTensorPD(
168-
const std::vector<std::vector<int>>& data) {
169-
std::vector<int> ret;
167+
void fillNlistTensor(const std::vector<std::vector<int>>& data,
168+
std::unique_ptr<paddle_infer::Tensor>& flat_tensor) {
169+
size_t total_size = 0;
170170
for (const auto& row : data) {
171-
ret.insert(ret.end(), row.begin(), row.end());
171+
total_size += row.size();
172+
}
173+
std::vector<int> flat_data;
174+
flat_data.reserve(total_size);
175+
for (const auto& row : data) {
176+
flat_data.insert(flat_data.end(), row.begin(), row.end());
172177
}
173-
return ret;
174-
}
175178

179+
int nloc = data.size();
180+
int nnei = nloc > 0 ? total_size / nloc : 0;
181+
flat_tensor->Reshape({1, nloc, nnei});
182+
flat_tensor->CopyFromCpu(flat_data.data());
183+
}
176184
DeepPotPD::DeepPotPD() : inited(false) {}
177185
DeepPotPD::DeepPotPD(const std::string& model,
178186
const int& gpu_rank,
@@ -375,16 +383,14 @@ void DeepPotPD::compute(ENERGYVTYPE& ener,
375383
auto coord_wrapped_Tensor = predictor_fl->GetInputHandle("coord");
376384
coord_wrapped_Tensor->Reshape({1, nall_real, 3});
377385
coord_wrapped_Tensor->CopyFromCpu(coord_wrapped.data());
378-
379386
auto atype_Tensor = predictor_fl->GetInputHandle("atype");
380387
atype_Tensor->Reshape({1, nall_real});
381388
atype_Tensor->CopyFromCpu(datype.data());
382-
383389
if (ago == 0) {
384-
nlist_data.copy_from_nlist(lmp_list);
390+
nlist_data.copy_from_nlist(lmp_list, nall - nghost);
385391
nlist_data.shuffle_exclude_empty(fwd_map);
386392
nlist_data.padding();
387-
if (do_message_passing == 1 && nghost > 0) {
393+
if (do_message_passing) {
388394
auto sendproc_tensor = predictor_fl->GetInputHandle("send_proc");
389395
auto recvproc_tensor = predictor_fl->GetInputHandle("recv_proc");
390396
auto recvnum_tensor = predictor_fl->GetInputHandle("recv_num");
@@ -446,11 +452,8 @@ void DeepPotPD::compute(ENERGYVTYPE& ener,
446452
this->mapping_tensor->CopyFromCpu(mapping.data());
447453
}
448454
}
449-
std::vector<int> firstneigh = createNlistTensorPD(nlist_data.jlist);
450455
this->firstneigh_tensor = predictor_fl->GetInputHandle("nlist");
451-
this->firstneigh_tensor->Reshape(
452-
{1, nloc, (int)firstneigh.size() / (int)nloc});
453-
this->firstneigh_tensor->CopyFromCpu(firstneigh.data());
456+
fillNlistTensor(nlist_data.jlist, this->firstneigh_tensor);
454457
bool do_atom_virial_tensor = atomic;
455458
if (!fparam.empty()) {
456459
std::unique_ptr<paddle_infer::Tensor> fparam_tensor;
@@ -510,7 +513,7 @@ void DeepPotPD::compute(ENERGYVTYPE& ener,
510513
}
511514
}
512515
template void DeepPotPD::compute<double, std::vector<ENERGYTYPE>>(
513-
std::vector<ENERGYTYPE>& dener,
516+
std::vector<ENERGYTYPE>& ener,
514517
std::vector<double>& force,
515518
std::vector<double>& virial,
516519
std::vector<double>& atom_energy,
@@ -522,11 +525,10 @@ template void DeepPotPD::compute<double, std::vector<ENERGYTYPE>>(
522525
const InputNlist& lmp_list,
523526
const int& ago,
524527
const std::vector<double>& fparam,
525-
const std::vector<double>& aparam_,
528+
const std::vector<double>& aparam,
526529
const bool atomic);
527-
528530
template void DeepPotPD::compute<float, std::vector<ENERGYTYPE>>(
529-
std::vector<ENERGYTYPE>& dener,
531+
std::vector<ENERGYTYPE>& ener,
530532
std::vector<float>& force,
531533
std::vector<float>& virial,
532534
std::vector<float>& atom_energy,
@@ -538,9 +540,8 @@ template void DeepPotPD::compute<float, std::vector<ENERGYTYPE>>(
538540
const InputNlist& lmp_list,
539541
const int& ago,
540542
const std::vector<float>& fparam,
541-
const std::vector<float>& aparam_,
543+
const std::vector<float>& aparam,
542544
const bool atomic);
543-
544545
// ENERGYVTYPE: std::vector<ENERGYTYPE> or ENERGYTYPE
545546
template <typename VALUETYPE, typename ENERGYVTYPE>
546547
void DeepPotPD::compute(ENERGYVTYPE& ener,
@@ -562,9 +563,9 @@ void DeepPotPD::compute(ENERGYVTYPE& ener,
562563
coord_wrapped_Tensor->Reshape({1, natoms, 3});
563564
coord_wrapped_Tensor->CopyFromCpu(coord_wrapped.data());
564565

565-
std::vector<std::int64_t> atype_64(atype.begin(), atype.end());
566566
auto atype_Tensor = predictor->GetInputHandle("atype");
567567
atype_Tensor->Reshape({1, natoms});
568+
std::vector<std::int64_t> atype_64(atype.begin(), atype.end());
568569
atype_Tensor->CopyFromCpu(atype_64.data());
569570

570571
std::unique_ptr<paddle_infer::Tensor> box_Tensor;
@@ -573,15 +574,15 @@ void DeepPotPD::compute(ENERGYVTYPE& ener,
573574
box_Tensor->Reshape({1, 9});
574575
box_Tensor->CopyFromCpu((box.data()));
575576
}
576-
std::unique_ptr<paddle_infer::Tensor> fparam_tensor;
577577
if (!fparam.empty()) {
578-
fparam_tensor = predictor->GetInputHandle("box");
578+
std::unique_ptr<paddle_infer::Tensor> fparam_tensor;
579+
fparam_tensor = predictor->GetInputHandle("fparam");
579580
fparam_tensor->Reshape({1, static_cast<int>(fparam.size())});
580581
fparam_tensor->CopyFromCpu((fparam.data()));
581582
}
582-
std::unique_ptr<paddle_infer::Tensor> aparam_tensor;
583583
if (!aparam.empty()) {
584-
aparam_tensor = predictor->GetInputHandle("box");
584+
std::unique_ptr<paddle_infer::Tensor> aparam_tensor;
585+
aparam_tensor = predictor->GetInputHandle("aparam");
585586
aparam_tensor->Reshape(
586587
{1, natoms, static_cast<int>(aparam.size()) / natoms});
587588
aparam_tensor->CopyFromCpu((aparam.data()));
@@ -628,11 +629,11 @@ void DeepPotPD::compute(ENERGYVTYPE& ener,
628629

629630
template void DeepPotPD::compute<double, std::vector<ENERGYTYPE>>(
630631
std::vector<ENERGYTYPE>& ener,
631-
std::vector<double>& dforce,
632+
std::vector<double>& force,
632633
std::vector<double>& virial,
633634
std::vector<double>& atom_energy,
634635
std::vector<double>& atom_virial,
635-
const std::vector<double>& dcoord,
636+
const std::vector<double>& coord,
636637
const std::vector<int>& atype,
637638
const std::vector<double>& box,
638639
const std::vector<double>& fparam,
@@ -645,7 +646,7 @@ template void DeepPotPD::compute<float, std::vector<ENERGYTYPE>>(
645646
std::vector<float>& virial,
646647
std::vector<float>& atom_energy,
647648
std::vector<float>& atom_virial,
648-
const std::vector<float>& dcoord,
649+
const std::vector<float>& coord,
649650
const std::vector<int>& atype,
650651
const std::vector<float>& box,
651652
const std::vector<float>& fparam,

0 commit comments

Comments
 (0)