24#include <ATen/Parallel.h>
57inline std::pair<std::string, int>
60 const auto pos = name.rfind(
'~');
61 if (pos == std::string::npos)
63 const auto suffix = name.substr(pos + 1);
64 if (suffix.empty() || suffix.find_first_not_of(
"0123456789") != std::string::npos)
66 return {name.substr(0, pos), std::stoi(suffix)};
73 return lag == 0 ? base : base +
"~" + std::to_string(lag);
78contains(
const std::vector<std::string> & v,
const std::string & s)
80 return std::find(v.begin(), v.end(), s) != v.end();
90 static constexpr std::array<int64_t, 0> shape{};
91 static constexpr std::array<int64_t, 1> strides{1};
96 static constexpr std::array<int64_t, 1> shape{3};
97 static constexpr std::array<int64_t, 2> strides{3, 1};
102 static constexpr std::array<int64_t, 2> shape{3, 3};
103 static constexpr std::array<int64_t, 3> strides{9, 3, 1};
108 static constexpr std::array<int64_t, 1> shape{6};
109 static constexpr std::array<int64_t, 2> strides{6, 1};
114 static constexpr std::array<int64_t, 4> shape{3, 3, 3, 3};
115 static constexpr std::array<int64_t, 5> strides{81, 27, 9, 3, 1};
120 static constexpr std::array<int64_t, 2> shape{6, 6};
121 static constexpr std::array<int64_t, 3> strides{36, 6, 1};
142 std::vector<int64_t> shape;
144 shape.push_back(
static_cast<int64_t
>(data.size()));
148 return at::from_blob(
const_cast<T *
>(data.data()), shape, at::TensorOptions().dtype(at::kDouble));
168 if (src.dtype() != at::kDouble)
170 "Cannot copy at::Tensor with dtype ", src.dtype(),
" into ", demangle(
typeid(T).name()));
172 mooseError(
"Cannot copy at::Tensor with shape ",
175 demangle(
typeid(T).name()),
176 " with different number of elements.");
177 auto dest_tensor = at::from_blob(
178 reinterpret_cast<Real *
>(&dest),
Layout<T>::shape, at::TensorOptions().dtype(at::kDouble));
197 std::memcpy(
reinterpret_cast<Real *
>(&dest), src,
Layout<T>::strides[0] *
sizeof(Real));
220 at::set_num_threads(n);
225 at::set_num_threads(
_saved);
253 at::set_default_dtype(caffe2::TypeMeta::Make<double>());
264==============================================================================
265To debug NEML2 related issues:
2661. Build and run MOOSE in dbg mode.
2672. Re-run the simulation using the dbg executable, and often times
268 NEML2 will provide a more helpful error message.
2693. If the error message is not helpful, or if there is still no error message,
270 run the simulation through a debugger: See
271 https://mooseframework.inl.gov/application_development/debugging.html
2724. If the issue is due to a NEML2 bug, feel free to report it at
273 https://github.com/applied-material-modeling/neml2/issues
274==============================================================================
298 const std::string & filename);
305std::string
docstring(
const std::string & desc);
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
A helper class for re-directing output streams to Console output objects form MooseObjects.
Every object that can be built by the factory should be derived from this class.
RAII guard forcing libtorch's default scalar type to Float64 for its lifetime, restoring the previous...
ScopedDefaultDtype(const ScopedDefaultDtype &)=delete
ScopedDefaultDtype & operator=(const ScopedDefaultDtype &)=delete
const caffe2::TypeMeta _saved
RAII guard that pins libtorch's intra-op thread count for the current scope and restores the previous...
ScopedNumThreads(const ScopedNumThreads &)=delete
ScopedNumThreads & operator=(const ScopedNumThreads &)=delete
RankFourTensorTempl is designed to handle any N-dimensional fourth order tensor, C.
SymmetricRankFourTensorTempl is designed to handle an N-dimensional fourth order tensor with minor sy...
void dumpInputsToTorchScript(const std::map< std::string, at::Tensor > &inputs, const std::string &filename)
Serialize a set of named NEML2 input tensors to a TorchScript archive at filename (a torch::jit::Modu...
std::pair< std::string, int > parseLag(const std::string &name)
Parse NEML2 v3 lag notation: "var~N" is the N-th old value of a variable (var~1 == old var,...
at::Tensor fromBlob(const std::vector< T > &data)
Map from std::vector<T> to an at::Tensor without copying the data.
std::string docstring(const std::string &desc)
Augment docstring if NEML2 is not enabled.
void redirectLogsToConsole(const ConsoleStream &console)
Route NEML2's log output into MOOSE's console.
void copyTensorToMOOSEData(const at::Tensor &src, T &dest)
Directly copy a contiguous chunk of memory of a at::Tensor to a MOOSE data of type T.
void copyBlobToMOOSEData(const Real *src, T &dest)
Copy one element's worth of doubles from a contiguous host buffer into a MOOSE datum of type T.
std::string lagName(const std::string &base, int lag)
Inverse of parseLag: build the variable name for a base name + lag.
void assertNEML2Enabled()
Assert that NEML2 is enabled.
std::string stringify(MOOSEIOType type)
static std::string NEML2_help_message