https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
NEML2Utils::ScopedDefaultDtype Class Reference

RAII guard forcing libtorch's default scalar type to Float64 for its lifetime, restoring the previous default on destruction. More...

#include <NEML2Utils.h>

Public Member Functions

 ScopedDefaultDtype ()
 
 ~ScopedDefaultDtype ()
 
 ScopedDefaultDtype (const ScopedDefaultDtype &)=delete
 
ScopedDefaultDtype & operator= (const ScopedDefaultDtype &)=delete
 

Private Attributes

const caffe2::TypeMeta _saved
 

Detailed Description

RAII guard forcing libtorch's default scalar type to Float64 for its lifetime, restoring the previous default on destruction.

MOOSE gathers and produces double (Float64) data, so every NEML2 model it drives must evaluate in Float64. A NEML2 model's working dtype is inferred from its first floating-point parameter/buffer and otherwise falls back to libtorch's default (Float32) – so a parameter-less eager model would default to Float32 and reject MOOSE's double inputs. Wrapping model construction and evaluation in this guard makes that fallback (and any default-dtype-dependent tensor created while evaluating) resolve to Float64. Save/restore rather than set-once because the default is process-global (shared with the embedded Python interpreter) and we must not disturb it outside NEML2.

Definition at line 248 of file NEML2Utils.h.

Constructor & Destructor Documentation

◆ ScopedDefaultDtype() [1/2]

NEML2Utils::ScopedDefaultDtype::ScopedDefaultDtype ( )
inline

Definition at line 251 of file NEML2Utils.h.

251 : _saved(at::get_default_dtype())
252 {
253 at::set_default_dtype(caffe2::TypeMeta::Make<double>());
254 }
const caffe2::TypeMeta _saved
Definition NEML2Utils.h:260

◆ ~ScopedDefaultDtype()

NEML2Utils::ScopedDefaultDtype::~ScopedDefaultDtype ( )
inline

Definition at line 255 of file NEML2Utils.h.

255{ at::set_default_dtype(_saved); }

◆ ScopedDefaultDtype() [2/2]

NEML2Utils::ScopedDefaultDtype::ScopedDefaultDtype ( const ScopedDefaultDtype &  )
delete

Member Function Documentation

◆ operator=()

ScopedDefaultDtype & NEML2Utils::ScopedDefaultDtype::operator= ( const ScopedDefaultDtype &  )
delete

Member Data Documentation

◆ _saved

const caffe2::TypeMeta NEML2Utils::ScopedDefaultDtype::_saved
private

Definition at line 260 of file NEML2Utils.h.

Referenced by ~ScopedDefaultDtype().


The documentation for this class was generated from the following file: