17#include "libmesh/libmesh_base.h"
33 template <
typename...
P>
68 const auto files = params.
get<std::vector<DataFileName>>(
"load");
69 return std::vector<std::string>(files.begin(), files.end());
81 const bool eager = params.
get<
bool>(
"eager");
85 if (!eager && !load.empty())
86 self.paramError(
"load",
87 "The 'load' parameter imports Python extensions into the embedded "
88 "interpreter and is only valid with the cpp-eager runtime. Set eager=true to "
89 "use it, or remove it for the cpp-aoti runtime.");
94 "'input' (the NEML2 source '.i' for cpp-eager, or the neml2-compile artifact folder for "
95 "cpp-aoti) is required.");
98 auto devices = params.
get<std::vector<std::string>>(
"device");
100 devices = {at::Device(self.getMooseApp().getLibtorchDevice()).str()};
103 auto db = params.
get<std::vector<unsigned int>>(
"device_batch");
106 if (db.size() != 1 && db.size() != devices.size())
107 self.paramError(
"device_batch",
108 "'device_batch' must have length 1 or the same length as 'device'.");
109 const std::vector<std::size_t> batch_sizes(db.begin(), db.end());
113 const auto mpi_comm = self.comm().get();
121 std::string(params.
get<DataFileName>(
"input")),
122 params.
get<std::string>(
"model"),
132 std::unique_ptr<NEML2ModelHandle>
_model;
152 "Use the cpp-eager NEML2 runtime, which embeds a Python interpreter and loads the model "
153 "from the source 'input' file at runtime. Default false -- use the cpp-aoti runtime, which "
154 "loads an ahead-of-time-compiled artifact (no Python at runtime) from the artifact folder "
155 "given by 'input'. The 'load' parameter is only valid with eager=true.");
158 "For the cpp-eager runtime, the path to the source NEML2 '.i' model file. For the cpp-aoti "
159 "runtime, any of: the artifact folder produced by neml2-compile (a self-describing directory "
160 "holding metadata.json plus per-<device>/<dtype> '.pt2' binaries), the metadata.json inside "
161 "it, or the neml2-compile stub '.i'. Prefer the artifact folder -- it is self-contained and "
162 "relocatable as a unit, and needs no 'model' name. The stub '.i' is accepted for "
163 "compatibility but points at its artifact folder by path, so the two must stay together.");
164 params.
addParam<std::vector<std::string>>(
167 "Additional command line arguments to use when parsing the NEML2 input file.");
168 params.
addParam<std::vector<DataFileName>>(
171 "External Python extension files (paths to .py files or package directories, resolved "
172 "relative to the input file and the application's data search path) imported into the "
173 "embedded interpreter before the NEML2 model is built. Importing them registers any "
174 "@register_neml2_object types they define -- e.g. NEML2 models hosted inside a MOOSE app -- "
175 "so the NEML2 input file can reference them. Mirrors the neml2 CLI --load flag. Only valid "
176 "with the cpp-eager runtime (eager=true).");
180 "Name of the NEML2 model, i.e., the string inside the brackets [] in the NEML2 input file "
181 "that corresponds to the model you want to use. Required for the cpp-eager runtime, and for "
182 "cpp-aoti only when 'input' is a stub '.i'; the cpp-aoti folder and metadata.json forms use "
183 "the single model recorded in the artifact metadata and ignore it.");
184 params.
addParam<std::vector<std::string>>(
187 "Compute device(s) for the NEML2 model, each following the schema "
188 "(cpu|cuda)[:<device-index>] "
189 "(e.g. 'cpu', 'cuda:1'). cpp-eager pins the model to the first device. cpp-aoti dispatches "
190 "over the list: a CUDA list is round-robined over the MPI ranks on each node (one device per "
191 "rank when counts match, sharing when there are more ranks than devices, an error when there "
192 "are fewer); a single 'cpu' runs each rank's local batch on CPU. If not specified, defaults "
193 "to the compute device from the --compute-device command line argument.");
194 params.
addParam<std::vector<unsigned int>>(
197 "Per-device chunk size along the leading batch axis (0 = run the whole local batch at once). "
198 "Must have length 1 (broadcast to all devices) or match the length of 'device'. Defaults to "
202 "Device on which to store the model outputs, following the same schema as a single 'device' "
203 "entry. Defaults to cpu, where MOOSE consumes them. Set this only if a downstream object "
205 "the outputs on a specific device.");
208 "Number of threads to use when evaluating the NEML2 model. Defaults to the number of threads "
209 "MOOSE is run with (the --n-threads command-line value). Some models evaluate fastest with a "
210 "single thread regardless of --n-threads; set this to 1 in that case.");
218template <
typename...
P>
227template <
typename...
P>
229 : T(params, args...),
233 _model(makeModelHandle(params, *this)),
234 _device(_model->device()),
238 _output_device(params.isParamValid(
"output_device")
239 ? at::Device(params.get<
std::string>(
"output_device"))
240 : at::Device(at::kCPU)),
243 _num_threads(params.isParamValid(
"num_threads") ? params.get<unsigned
int>(
"num_threads")
std::unique_ptr< NEML2ModelHandle > makeNEML2ModelHandle(bool eager, const std::string &input, const std::string &model, const std::vector< std::string > &devices, const std::vector< std::size_t > &batch_sizes, const void *comm, const std::vector< std::string > &load)
Construct a NEML2 model handle for the requested runtime.
void ErrorVector unsigned int
Runtime-agnostic handle over a NEML2 v3 model.
Interface class to provide common input parameters, members, and methods for MOOSEObjects that use NE...
NEML2ModelInterface(const InputParameters ¶ms, P &&... args)
const unsigned int _num_threads
libtorch intra-op thread count applied (scoped) during model evaluation; defaults to the MOOSE/libMes...
static std::vector< std::string > loadExtensionPaths(const InputParameters ¶ms)
Resolve the 'load' DataFileName list (whose relative paths InputParameters has already resolved again...
static InputParameters validParams()
const NEML2ModelHandle & model() const
Get the NEML2 model handle (cpp-aoti by default, cpp-eager when eager=true)
virtual void validateModel() const
Validate the NEML2 material model.
const at::Device & output_device() const
Get the target output device.
const at::Device & device() const
Get the target compute device.
static std::unique_ptr< NEML2ModelHandle > makeModelHandle(const InputParameters ¶ms, NEML2ModelInterface< T > &self)
Parse the runtime parameters and build the model handle.
const at::Device _output_device
The device on which to store the outputs.
const at::Device _device
The device on which to evaluate the NEML2 model (this rank's scheduler-assigned device)
unsigned int neml2NumThreads() const
libtorch intra-op thread count to use while evaluating the NEML2 model.
std::unique_ptr< NEML2ModelHandle > _model
The NEML2 material model handle.
NEML2ModelHandle & model()
Non-const access to the NEML2 model handle, for runtime parameter writes (set_parameter)
RAII guard forcing libtorch's default scalar type to Float64 for its lifetime, restoring the previous...
void redirectLogsToConsole(const ConsoleStream &console)
Route NEML2's log output into MOOSE's console.
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...