Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cc,pt): translate PT exceptions to the DeePMD-kit exception #3918

Merged
merged 7 commits into from
Jun 27, 2024
Merged
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
6 changes: 6 additions & 0 deletions source/api_cc/include/DeepPotPT.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ class DeepPotPT : public DeepPotBase {
bool gpu_enabled;
at::Tensor firstneigh_tensor;
torch::Dict<std::string, torch::Tensor> comm_dict;
/**
* @brief Translate PyTorch exceptions to the DeePMD-kit exception.
* @param[in] f The function to run.
* @example translate_error([&](){...});
*/
void translate_error(std::function<void()> f);
};

} // namespace deepmd
47 changes: 38 additions & 9 deletions source/api_cc/src/DeepPotPT.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@
#ifdef BUILD_PYTORCH
#include "DeepPotPT.h"

#include <torch/csrc/jit/runtime/jit_exception.h>

#include <cstdint>

#include "common.h"
#include "device.h"
#include "errors.h"

using namespace deepmd;

void DeepPotPT::translate_error(std::function<void()> f) {
try {
f();
// it seems that libtorch may throw different types of exceptions which are
// inherbited from different base classes
// https://github.com/pytorch/pytorch/blob/13316a8d4642454012d34da0d742f1ba93fc0667/torch/csrc/jit/runtime/interpreter.cpp#L924-L939
} catch (const c10::Error& e) {
throw deepmd::deepmd_exception("DeePMD-kit PyTorch backend error: " +
std::string(e.what()));
} catch (const torch::jit::JITException& e) {
throw deepmd::deepmd_exception("DeePMD-kit PyTorch backend JIT error: " +
std::string(e.what()));
} catch (const std::runtime_error& e) {
throw deepmd::deepmd_exception("DeePMD-kit PyTorch backend error: " +
std::string(e.what()));

Check warning on line 29 in source/api_cc/src/DeepPotPT.cc

View check run for this annotation

Codecov / codecov/patch

source/api_cc/src/DeepPotPT.cc#L25-L29

Added lines #L25 - L29 were not covered by tests
}
}

torch::Tensor createNlistTensor(const std::vector<std::vector<int>>& data) {
std::vector<torch::Tensor> row_tensors;

Expand All @@ -26,7 +47,7 @@
const std::string& file_content)
: inited(false) {
try {
init(model, gpu_rank, file_content);
translate_error([&] { init(model, gpu_rank, file_content); });
} catch (...) {
// Clean up and rethrow, as the destructor will not be called
throw;
Expand Down Expand Up @@ -444,8 +465,10 @@
const std::vector<double>& box,
const std::vector<double>& fparam,
const std::vector<double>& aparam) {
compute(ener, force, virial, atom_energy, atom_virial, coord, atype, box,
fparam, aparam);
translate_error([&] {
compute(ener, force, virial, atom_energy, atom_virial, coord, atype, box,
fparam, aparam);
});
}
void DeepPotPT::computew(std::vector<double>& ener,
std::vector<float>& force,
Expand All @@ -457,8 +480,10 @@
const std::vector<float>& box,
const std::vector<float>& fparam,
const std::vector<float>& aparam) {
compute(ener, force, virial, atom_energy, atom_virial, coord, atype, box,
fparam, aparam);
translate_error([&] {
compute(ener, force, virial, atom_energy, atom_virial, coord, atype, box,
fparam, aparam);
});
}
void DeepPotPT::computew(std::vector<double>& ener,
std::vector<double>& force,
Expand All @@ -473,8 +498,10 @@
const int& ago,
const std::vector<double>& fparam,
const std::vector<double>& aparam) {
compute(ener, force, virial, atom_energy, atom_virial, coord, atype, box,
nghost, inlist, ago, fparam, aparam);
translate_error([&] {
compute(ener, force, virial, atom_energy, atom_virial, coord, atype, box,
nghost, inlist, ago, fparam, aparam);
});
}
void DeepPotPT::computew(std::vector<double>& ener,
std::vector<float>& force,
Expand All @@ -489,8 +516,10 @@
const int& ago,
const std::vector<float>& fparam,
const std::vector<float>& aparam) {
compute(ener, force, virial, atom_energy, atom_virial, coord, atype, box,
nghost, inlist, ago, fparam, aparam);
translate_error([&] {
compute(ener, force, virial, atom_energy, atom_virial, coord, atype, box,
nghost, inlist, ago, fparam, aparam);
});
}
void DeepPotPT::computew_mixed_type(std::vector<double>& ener,
std::vector<double>& force,
Expand Down
4 changes: 4 additions & 0 deletions source/api_cc/tests/test_deepmd_exception.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ TEST(TestDeepmdException, deepmdexception_nofile_deeppot) {
ASSERT_THROW(deepmd::DeepPot("_no_such_file.pb"), deepmd::deepmd_exception);
}

TEST(TestDeepmdException, deepmdexception_nofile_deeppot_pt) {
ASSERT_THROW(deepmd::DeepPot("_no_such_file.pth"), deepmd::deepmd_exception);
}

TEST(TestDeepmdException, deepmdexception_nofile_deeppotmodeldevi) {
ASSERT_THROW(
deepmd::DeepPotModelDevi({"_no_such_file.pb", "_no_such_file.pb"}),
Expand Down