Skip to content

Commit

Permalink
Add dtype guard
Browse files Browse the repository at this point in the history
  • Loading branch information
hugary1995 committed Dec 19, 2023
1 parent f7bbc53 commit a7b0971
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/include/TransientRegression.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@

namespace neml2
{

struct AutoDefaultDtypeMode
{
static std::mutex default_dtype_mutex;

AutoDefaultDtypeMode(c10::ScalarType default_dtype)
: prev_default_dtype(torch::typeMetaToScalarType(torch::get_default_dtype()))
{
default_dtype_mutex.lock();
torch::set_default_dtype(torch::scalarTypeToTypeMeta(default_dtype));
}
~AutoDefaultDtypeMode()
{
default_dtype_mutex.unlock();
torch::set_default_dtype(torch::scalarTypeToTypeMeta(prev_default_dtype));
}
c10::ScalarType prev_default_dtype;
};

class TransientDriver;

class TransientRegression : public Driver
Expand Down
5 changes: 5 additions & 0 deletions tests/src/TransientRegression.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace fs = std::filesystem;

namespace neml2
{
std::mutex AutoDefaultDtypeMode::default_dtype_mutex;

register_NEML2_object(TransientRegression);

OptionSet
Expand All @@ -56,6 +58,9 @@ TransientRegression::TransientRegression(const OptionSet & options)
bool
TransientRegression::run()
{
// Paranoid guard
AutoDefaultDtypeMode dtype_mode(torch::kFloat64);

_driver.run();

// Verify the result
Expand Down

0 comments on commit a7b0971

Please sign in to comment.