Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
- id: trailing-whitespace
exclude: "^.+\\.pbtxt$"
- id: end-of-file-fixer
exclude: "^.+\\.pbtxt$|deeppot_sea.*\\.json$"
exclude: "^.+\\.pbtxt$|deeppot_sea.*|deeppot_dpa.*\\.json$"
- id: check-yaml
- id: check-json
- id: check-added-large-files
Expand Down
4 changes: 2 additions & 2 deletions deepmd/pd/entrypoints/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def freeze(
None, # fparam
None, # aparam
# InputSpec([], dtype="bool", name="do_atomic_virial"), # do_atomic_virial
False, # do_atomic_virial
False, # do_atomic_virial, NOTE: set to True if needed
],
full_graph=True,
)
Expand All @@ -396,7 +396,7 @@ def freeze(
None, # fparam
None, # aparam
# InputSpec([], dtype="bool", name="do_atomic_virial"), # do_atomic_virial
False, # do_atomic_virial
False, # do_atomic_virial, NOTE: set to True if needed
(
InputSpec([-1], "int64", name="send_list"),
InputSpec([-1], "int32", name="send_proc"),
Expand Down
43 changes: 35 additions & 8 deletions deepmd/pd/model/model/ener_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@ def __init__(
) -> None:
DPModelCommon.__init__(self)
DPEnergyModel_.__init__(self, *args, **kwargs)
self._hessian_enabled = False

def enable_hessian(self):
raise NotImplementedError(
"Hessian calculation is not implemented yet on PaddlePaddle platform."
)

def get_observed_type_list(self) -> list[str]:
"""Get observed types (elements) of the model during data statistics.

Returns
-------
observed_type_list: a list of the observed types in this model.
"""
type_map = self.get_type_map()
out_bias = self.atomic_model.get_out_bias()[0]

assert out_bias is not None, "No out_bias found in the model."
assert out_bias.dim() == 2, "The supported out_bias should be a 2D tensor."
assert out_bias.size(0) == len(type_map), (
"The out_bias shape does not match the type_map length."
)
bias_mask = (
paddle.greater_than(paddle.abs(out_bias), 1e-6).any(axis=-1).detach().cpu()
) # 1e-6 for stability

observed_type_list: list[str] = []
for i in range(len(type_map)):
if bias_mask[i]:
observed_type_list.append(type_map[i])
return observed_type_list

def translated_output_def(self):
out_def_data = self.model_output_def().get_data()
Expand All @@ -50,6 +81,8 @@ def translated_output_def(self):
output_def["atom_virial"].squeeze(-3)
if "mask" in out_def_data:
output_def["mask"] = out_def_data["mask"]
if self._hessian_enabled:
output_def["hessian"] = out_def_data["energy_derv_r_derv_r"]
return output_def

def forward(
Expand Down Expand Up @@ -81,14 +114,12 @@ def forward(
model_predict["atom_virial"] = model_ret["energy_derv_c"].squeeze(
-3
)
else:
model_predict["atom_virial"] = paddle.zeros(
[model_predict["energy"].shape[0], 1, 9], dtype=paddle.float64
)
else:
model_predict["force"] = model_ret["dforce"]
if "mask" in model_ret:
model_predict["mask"] = model_ret["mask"]
if self._hessian_enabled:
model_predict["hessian"] = model_ret["energy_derv_r_derv_r"].squeeze(-2)
else:
model_predict = model_ret
model_predict["updated_coord"] += coord
Expand Down Expand Up @@ -128,10 +159,6 @@ def forward_lower(
model_predict["extended_virial"] = model_ret[
"energy_derv_c"
].squeeze(-3)
else:
model_predict["extended_virial"] = paddle.zeros(
[model_predict["energy"].shape[0], 1, 9], dtype=paddle.float64
)
else:
assert model_ret["dforce"] is not None
model_predict["dforce"] = model_ret["dforce"]
Expand Down
24 changes: 21 additions & 3 deletions deepmd/pd/model/model/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from typing import (
NoReturn,
Optional,
)

Expand All @@ -8,6 +9,9 @@
from deepmd.dpmodel.model.base_model import (
make_base_model,
)
from deepmd.pd.utils import (
env,
)
from deepmd.utils.path import (
DPPath,
)
Expand All @@ -18,13 +22,16 @@ def __init__(self, *args, **kwargs):
"""Construct a basic model for different tasks."""
paddle.nn.Layer.__init__(self)
self.model_def_script = ""
self.min_nbor_dist = None
self.register_buffer(
"min_nbor_dist",
paddle.to_tensor(-1.0, dtype=paddle.float64, place=env.DEVICE),
)

def compute_or_load_stat(
self,
sampled_func,
stat_file_path: Optional[DPPath] = None,
):
) -> NoReturn:
"""
Compute or load the statistics parameters of the model,
such as mean and standard deviation of descriptors or the energy bias of the fitting net.
Expand All @@ -42,13 +49,24 @@ def compute_or_load_stat(
"""
raise NotImplementedError

def get_observed_type_list(self) -> list[str]:
"""Get observed types (elements) of the model during data statistics.

Returns
-------
observed_type_list: a list of the observed types in this model.
"""
raise NotImplementedError

def get_model_def_script(self) -> str:
"""Get the model definition script."""
return self.model_def_script

def get_min_nbor_dist(self) -> Optional[float]:
"""Get the minimum distance between two atoms."""
return self.min_nbor_dist
if self.min_nbor_dist.item() == -1.0:
return None
return self.min_nbor_dist.item()

def get_ntypes(self):
"""Returns the number of element types."""
Expand Down
40 changes: 24 additions & 16 deletions deepmd/pd/utils/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ def deserialize_to_file(model_file: str, data: dict) -> None:
"""
model.forward = paddle.jit.to_static(
model.forward,
full_graph=True,
input_spec=[
InputSpec([-1, -1, 3], dtype="float64", name="coord"),
InputSpec([-1, -1], dtype="int64", name="atype"),
InputSpec([-1, 9], dtype="float64", name="box"),
None,
None,
True,
InputSpec([-1, -1, 3], dtype="float64", name="coord"), # coord
InputSpec([-1, -1], dtype="int64", name="atype"), # atype
InputSpec([-1, 9], dtype="float64", name="box"), # box
None, # fparam
None, # aparam
True, # do_atomic_virial
],
full_graph=True,
)
""" example output shape and dtype of forward_lower
fetch_name_0: atom_energy [1, 192, 1] paddle.float64
Expand All @@ -86,17 +86,25 @@ def deserialize_to_file(model_file: str, data: dict) -> None:
"""
model.forward_lower = paddle.jit.to_static(
model.forward_lower,
full_graph=True,
input_spec=[
InputSpec([-1, -1, 3], dtype="float64", name="coord"),
InputSpec([-1, -1], dtype="int32", name="atype"),
InputSpec([-1, -1, -1], dtype="int32", name="nlist"),
None,
None,
None,
True,
None,
InputSpec([-1, -1, 3], dtype="float64", name="coord"), # extended_coord
InputSpec([-1, -1], dtype="int32", name="atype"), # extended_atype
InputSpec([-1, -1, -1], dtype="int32", name="nlist"), # nlist
InputSpec([-1, -1], dtype="int64", name="mapping"), # mapping
None, # fparam
None, # aparam
True, # do_atomic_virial
(
InputSpec([-1], "int64", name="send_list"),
InputSpec([-1], "int32", name="send_proc"),
InputSpec([-1], "int32", name="recv_proc"),
InputSpec([-1], "int32", name="send_num"),
InputSpec([-1], "int32", name="recv_num"),
InputSpec([-1], "int64", name="communicator"),
# InputSpec([1], "int64", name="has_spin"),
), # comm_dict
],
full_graph=True,
)
paddle.jit.save(
model,
Expand Down
18 changes: 6 additions & 12 deletions doc/install/install-from-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,16 @@ To install Paddle, run

```sh
# cu126
# release version
pip install paddlepaddle-gpu==3.1.1 -i https://www.paddlepaddle.org.cn/packages/stable/cu126/
# nightly-build version
# pip install --pre paddlepaddle-gpu -i https://www.paddlepaddle.org.cn/packages/nightly/cu126/
pip install paddlepaddle-gpu==3.1.1 -i https://www.paddlepaddle.org.cn/packages/stable/cu126/ # release
# pip install --pre paddlepaddle-gpu -i https://www.paddlepaddle.org.cn/packages/nightly/cu126/ # nightly-build

# cu118
# release version
pip install paddlepaddle-gpu==3.1.1 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/
# nightly-build version
# pip install --pre paddlepaddle-gpu -i https://www.paddlepaddle.org.cn/packages/nightly/cu118/
pip install paddlepaddle-gpu==3.1.1 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/ # release
# pip install --pre paddlepaddle-gpu -i https://www.paddlepaddle.org.cn/packages/nightly/cu118/ # nightly-build

# cpu
# release version
pip install paddlepaddle==3.1.1 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/
# nightly-build version
# pip install --pre paddlepaddle -i https://www.paddlepaddle.org.cn/packages/nightly/cpu/
pip install paddlepaddle==3.1.1 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/ # release
# pip install --pre paddlepaddle -i https://www.paddlepaddle.org.cn/packages/nightly/cpu/ # nightly-build
```

:::
Expand Down
2 changes: 1 addition & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if(ENABLE_PADDLE)
"PADDLE_INFERENCE_DIR is not defined, downloading CPU inference lib to: ${CMAKE_BINARY_DIR}/"
)
set(DOWNLOAD_URL
"https://paddle-qa.bj.bcebos.com/paddle-pipeline/GITHUB_Docker_Compile_Test_Cpu_Mkl_Avx_D1/6ed5dd3833c32c3b21e14b1fb1a71f5a535a0fcc/paddle_inference.tgz"
"https://paddle-org.bj.bcebos.com/paddlescience/deepmd/paddle_inference.tgz"
)
endif()
set(TGZ_FILE "${CMAKE_BINARY_DIR}/paddle_inference.tgz")
Expand Down
17 changes: 13 additions & 4 deletions source/api_cc/src/DeepPotPD.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using namespace deepmd;
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>

class Logger {
Expand Down Expand Up @@ -669,10 +670,18 @@ void DeepPotPD::get_type_map(std::string& type_map) {
template <typename BUFFERTYPE>
void DeepPotPD::get_buffer(const std::string& buffer_name,
std::vector<BUFFERTYPE>& buffer_array) {
auto buffer_tensor = predictor->GetOutputHandle(buffer_name);
int buffer_size = numel(*buffer_tensor);
buffer_array.resize(buffer_size);
buffer_tensor->CopyToCpu(buffer_array.data());
try {
auto buffer_tensor = predictor->GetOutputHandle(buffer_name);
int buffer_size = numel(*buffer_tensor);
buffer_array.resize(buffer_size);
buffer_tensor->CopyToCpu(buffer_array.data());
} catch (const std::out_of_range& e) {
std::cerr << "Error: Output handle for buffer_name '" << buffer_name
<< "' not found. Check if the model output names are correct."
<< std::endl;
std::cerr << "Underlying exception: " << e.what() << std::endl;
throw;
}
Comment on lines +673 to +684
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Strengthen error handling and use consistent logging in get_buffer

  • Use logg::error() instead of std::cerr for consistency.
  • Catch broader exceptions as a fallback.
  • Surface available output names to aid debugging.
  • Optionally wrap with deepmd_exception for uniform error propagation.
  • Avoid CopyToCpu when size is zero.
-  try {
-    auto buffer_tensor = predictor->GetOutputHandle(buffer_name);
-    int buffer_size = numel(*buffer_tensor);
-    buffer_array.resize(buffer_size);
-    buffer_tensor->CopyToCpu(buffer_array.data());
-  } catch (const std::out_of_range& e) {
-    std::cerr << "Error: Output handle for buffer_name '" << buffer_name
-              << "' not found. Check if the model output names are correct."
-              << std::endl;
-    std::cerr << "Underlying exception: " << e.what() << std::endl;
-    throw;
-  }
+  try {
+    auto buffer_tensor = predictor->GetOutputHandle(buffer_name);
+    const int buffer_size = numel(*buffer_tensor);
+    buffer_array.resize(buffer_size);
+    if (buffer_size > 0) {
+      buffer_tensor->CopyToCpu(buffer_array.data());
+    }
+  } catch (const std::out_of_range& e) {
+    auto names = predictor->GetOutputNames();
+    std::ostringstream os;
+    for (size_t i = 0; i < names.size(); ++i) {
+      os << (i ? ", " : "") << names[i];
+    }
+    logg::error() << "Output handle for buffer '" << buffer_name
+                  << "' not found. Available outputs: [" << os.str() << "]. "
+                  << "Underlying exception: " << e.what() << std::endl;
+    throw deepmd::deepmd_exception("Missing output buffer: " + buffer_name);
+  } catch (const std::exception& e) {
+    logg::error() << "Failed to get buffer '" << buffer_name
+                  << "': " << e.what() << std::endl;
+    throw;
+  }
🤖 Prompt for AI Agents
In source/api_cc/src/DeepPotPD.cc around lines 673 to 684, replace the current
std::cerr-only catch for std::out_of_range with consistent logg::error logging,
add a broader catch for std::exception as a fallback, and when reporting the
error also log the model's available output names to aid debugging; before
calling CopyToCpu check buffer_size and skip the copy if zero; finally wrap and
rethrow errors using deepmd_exception to maintain uniform error propagation.

}

template <typename BUFFERTYPE>
Expand Down
Loading
Loading