https://mooseframework.inl.gov
Loading...
Searching...
No Matches
NEML2ModelExecutor.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#include "NEML2ModelExecutor.h"
11#include "MOOSEToNEML2.h"
12#include "NEML2Utils.h"
13#include "NonlinearSystemBase.h"
14#include <string>
15#include <cctype>
16
17#ifdef NEML2_ENABLED
18#include <ATen/ATen.h>
19#include "libmesh/id_types.h"
20#include "neml2/csrc/aoti/Exception.h"
21
22// torch::cuda::synchronize() (declared in <torch/cuda.h>, defined in libtorch) does a full device
23// synchronization -- see deviceSynchronize(). We cannot include <torch/cuda.h> here: it lives under
24// torch/csrc/api/include (not necessarily on MOOSE's include path), and the lower-level c10/cuda
25// sync headers transitively include <cuda_runtime.h>, which is not on the include path either.
26// Forward-declare the stable frontend symbol instead; it is resolved from libtorch (always linked)
27// and is present even in a CPU-only torch, where deviceSynchronize() never reaches the call.
28namespace torch
29{
30namespace cuda
31{
32void synchronize(int64_t device_index);
33}
34}
35
36// parseLag / lagName / contains are shared with NEML2Action and live in NEML2Utils so the NEML2
37// unity build does not see duplicate definitions.
41#endif
42
44
47{
48 auto params = emptyInputParameters();
49 params.addParam<bool>(
50 "manage_state_advance",
51 false,
52 "Keep state and forces on the device and advance it to old state and old forces without a "
53 "roundtrip through MOOSE materials. This is only recommended for explicit time integration "
54 "or when absolutely no restepping occurs (e.g. failed timesteps).");
55 params.addParam<bool>(
56 "dump_inputs_on_failure",
57 false,
58 "When a NEML2 constitutive solve fails (recoverable convergence failure), serialize the "
59 "input "
60 "tensors to a per-rank TorchScript file '<model>_count<c>_rank<r>.pt' (c is a persistent "
61 "failure counter). The file can be loaded offline with torch.jit.load to re-evaluate the "
62 "model "
63 "on the failing batch, independently of MOOSE.");
64 return params;
65}
66
69{
72 params.addClassDescription("Execute the specified NEML2 model");
73
74 params.addRequiredParam<UserObjectName>(
75 "batch_index_generator",
76 "The NEML2BatchIndexGenerator used to generate the element-to-batch-index map.");
77 params.addParam<std::vector<UserObjectName>>(
78 "gatherers",
79 {},
80 "List of MOOSE*ToNEML2 user objects gathering MOOSE data as NEML2 input variables");
81 params.addParam<std::vector<UserObjectName>>(
82 "param_gatherers",
83 {},
84 "List of MOOSE*ToNEML2 user objects gathering MOOSE data as NEML2 model parameters");
85
86 // The executor evaluates on EXEC_LINEAR (residual), EXEC_NONLINEAR (Jacobian) and, for on-device
87 // state advance, EXEC_TIMESTEP_END.
90 params.set<ExecFlagEnum>("execute_on") = execute_options;
91
92 return params;
93}
94
97#ifdef NEML2_ENABLED
98 ,
99 _batch_index_generator(getUserObject<NEML2BatchIndexGenerator>("batch_index_generator")),
100 _manage_state_advance(getParam<bool>("manage_state_advance")),
101 _dump_inputs_on_failure(getParam<bool>("dump_inputs_on_failure")),
102 _output_ready(false),
103 // No derivative retained yet -> nothing stale; the retriever's undefined-derivative path (zero
104 // fill) covers the first steps. Only an actual batch resize (meshChanged) makes it invalid.
105 _derivative_valid(true),
106 _error_message(""),
107 _num_failed_dumps(0)
108#endif
109{
110#ifdef NEML2_ENABLED
112
113 // add user object dependencies by name (the UOs do not need to exist yet for this)
114 for (const auto & gatherer_name : getParam<std::vector<UserObjectName>>("gatherers"))
115 _depend_uo.insert(gatherer_name);
116 for (const auto & gatherer_name : getParam<std::vector<UserObjectName>>("param_gatherers"))
117 _depend_uo.insert(gatherer_name);
118#endif
119}
120
121#ifdef NEML2_ENABLED
122void
124{
125 // deal with user object provided inputs
126 for (const auto & gatherer_name : getParam<std::vector<UserObjectName>>("gatherers"))
127 {
128 // gather coupled user objects late to ensure they are constructed. Do not add them as
129 // dependencies (that's already done in the constructor).
130 const auto & uo = getUserObjectByName<MOOSEToNEML2>(gatherer_name, /*is_dependency=*/false);
131
132 // there's no need to gather old values if we're managing state advance
133 const auto [base_name, lag] = parseLag(uo.NEML2Name());
134 if (_manage_state_advance && lag > 0)
135 paramError("gatherers",
136 "The gatherer for old variable `",
137 uo.NEML2Name(),
138 "` is not needed when `manage_state_advance = true`.");
139
140 addGatheredVariable(gatherer_name, uo.NEML2Name());
141 _gatherers.push_back(&uo);
142 }
143
144 // deal with user object provided model parameters
145 for (const auto & gatherer_name : getParam<std::vector<UserObjectName>>("param_gatherers"))
146 {
147 const auto & uo = getUserObjectByName<MOOSEToNEML2>(gatherer_name, /*is_dependency=*/false);
148 addGatheredParameter(gatherer_name, uo.NEML2Name());
149 _param_gatherers.push_back(&uo);
150 }
151
152 // A NEML2 order-0 input that is ALSO a model output is an in-place-updated unknown (e.g. an
153 // ImplicitUpdate initial guess with no old counterpart). Such an input, if gathered, would read
154 // its own stale output material -- the previous converged step's value -- as the "current"
155 // initial guess, warm-starting a rate-like unknown from a value that can prevent the return-map
156 // Newton solve from converging. NEML2Action skips the default gatherer for these, so here we
157 // cold-start each at a base-shaped zero (broadcast over the batch) injected on every evaluation.
158 // An input the user mapped explicitly IS gathered, so the !gathered guard leaves that override
159 // untouched.
160 {
161 const auto & in_names = model().input_names();
162 const auto & in_shapes = model().input_base_shapes();
163 const auto & out_names = model().output_names();
164 const auto opts = at::TensorOptions().dtype(at::kDouble).device(device());
165 for (const auto i : index_range(in_names))
166 if (parseLag(in_names[i]).second == 0 && contains(out_names, in_names[i]) &&
167 !_gathered_variable_names.count(in_names[i]))
168 _zero_seed_vars[in_names[i]] = at::zeros(in_shapes[i], opts);
169 }
170
171 // iterate over set of required inputs and error out if we find one that is not provided
172 for (const auto & iname : model().input_names())
173 {
174 // if tensors are kept on device, we are not going to gather old values from moose
175 if (_manage_state_advance && parseLag(iname).second > 0)
176 continue;
177 // in-place-updated unknowns are cold-started at zero by the executor, not gathered
178 if (_zero_seed_vars.count(iname))
179 continue;
180 if (!_gathered_variable_names.count(iname))
181 paramError("gatherers", "The required model input `", iname, "` is not gathered");
182 }
183
184 // Keep track of stateful (old) variables if manage_state_advance is true. NEML2 v3's eager
185 // runtime requires every declared input to be present on each call (unlike v2, which silently
186 // zeroed undefined inputs), so we seed each lagged state variable with a base-shaped zero tensor
187 // (the correct initial "old" value; it broadcasts over the batch). After each converged step
188 // advanceState() overwrites these with the cached current values.
190 {
191 const auto & in_names = model().input_names();
192 const auto & in_shapes = model().input_base_shapes();
193 const auto opts = at::TensorOptions().dtype(at::kDouble).device(device());
194 for (const auto i : index_range(in_names))
195 if (parseLag(in_names[i]).second > 0)
196 _state_vars[in_names[i]] = at::zeros(in_shapes[i], opts);
197 }
198}
199
200std::size_t
201NEML2ModelExecutor::getBatchIndex(dof_id_type elem_id) const
202{
204}
205
206void
207NEML2ModelExecutor::addGatheredVariable(const UserObjectName & gatherer_name,
208 const std::string & var)
209{
210 if (_gathered_variable_names.count(var))
211 paramError("gatherers",
212 "The NEML2 input variable `",
213 var,
214 "` gathered by UO '",
215 gatherer_name,
216 "' is already gathered by another gatherer.");
217 _gathered_variable_names.insert(var);
218}
219
220void
221NEML2ModelExecutor::addGatheredParameter(const UserObjectName & gatherer_name,
222 const std::string & param)
223{
224 if (_gathered_parameter_names.count(param))
225 paramError("param_gatherers",
226 "The NEML2 model parameter `",
227 param,
228 "` gathered by UO '",
229 gatherer_name,
230 "' is already gathered by another gatherer.");
231 _gathered_parameter_names.insert(param);
232}
233
234void
236{
237 _output_ready = false;
238 _error = false;
239 _error_message.clear();
240}
241
242void
244{
245 _output_ready = false;
246 // The element-to-batch-index map is regenerated on a mesh change, resizing the batch. The
247 // retained input derivative is now stale (sized to the old batch); invalidate it so execute()
248 // recomputes it before any retriever reads it.
249 _derivative_valid = false;
251 mooseError("The mesh changed while `manage_state_advance = true` for NEML2 model executor '",
252 name(),
253 "'. This mode requires a fixed mesh because state history is cached on the device.");
254}
255
256void
258{
259 // Nothing to recompute at EXEC_TIMESTEP_END: the value, parameter derivatives, and input Jacobian
260 // are all retained from the solve (see below), so output/coupling consumers read the retained
261 // tensors. The only job here is the optional on-device state advance after a converged step.
263 {
265 advanceState();
266 return;
267 }
268
269 // If the batch is empty, we do not need to do anything
271 return;
272
273 // Pin libtorch's intra-op thread count to this model's setting for the duration of the
274 // evaluation, then restore it. MOOSE runs its element loops with libMesh threads and sets
275 // libtorch to the same count (usually 1) globally; the NEML2 batched solve is a different
276 // workload and may want a different count, controlled independently via the 'num_threads'
277 // parameter.
278 const NEML2Utils::ScopedNumThreads scoped_threads(neml2NumThreads());
279
280 // MOOSE drives NEML2 exclusively in Float64; force the libtorch default dtype to Float64 around
281 // the evaluation so any default-dtype-dependent tensor created while the model runs is Float64.
282 const NEML2Utils::ScopedDefaultDtype scoped_dtype;
283
284 fillInputs();
285
286 if (_t_step > 0)
287 {
288 // Compute the (comparatively expensive) input Jacobian only when MOOSE is assembling the
289 // Jacobian -- currentlyComputingJacobian() is set for a Jacobian evaluation, the automatic-
290 // scaling Jacobian, and a combined residual-and-Jacobian evaluation. A pure residual evaluation
291 // (line search, damping, JFNK residuals) takes the value-only path. (We cannot key off the
292 // residual flag: the tagged residual path never sets currentlyComputingResidual().) Consumers
293 // reading the input derivative at EXEC_TIMESTEP_END therefore see the last Jacobian's value.
294 // Parameter derivatives are handled separately in solve()/extractOutputs(): because they are
295 // typically consumed as outputs (e.g. inverse-optimization sensitivities), they are evaluated
296 // on every pass so they are always current.
297 //
298 // Also recompute when the retained input derivative is no longer valid for the current batch
299 // (e.g. after a mesh change resized the batch); otherwise a residual sweep would reuse a stale,
300 // wrongly-sized derivative tensor and a retriever would index out of range.
301 const bool compute_derivative = _fe_problem.currentlyComputingJacobian() || !_derivative_valid;
302
303 auto success = solve(compute_derivative);
304 if (success)
305 {
306 extractOutputs(compute_derivative);
307 if (compute_derivative)
308 _derivative_valid = true;
309 }
310 }
311}
312
313void
315{
316 if (device().is_cuda())
318}
319
320void
322{
323 TIME_SECTION("NEML2::fillInputs", 1, "Gathering NEML2 inputs and copying them to the device");
324 try
325 {
326 for (const auto & uo : _gatherers)
327 uo->insertInto(_in);
328 for (const auto & uo : _param_gatherers)
329 uo->insertInto(_model_params);
330
331 // Cold-start in-place-updated unknowns at zero (see initialSetup): a base-shaped zero broadcast
332 // over the batch, re-injected on every evaluation so the return-map initial guess is 0 each
333 // nonlinear iteration rather than the stale previous output value.
334 for (const auto & [name, val] : _zero_seed_vars)
335 _in[name] = val;
336
338 for (const auto & [name, val] : _state_vars)
339 if (val.defined())
340 _in[name] = val;
341
342 // Send input variables to the compute device
343 for (auto & [var, val] : _in)
344 val = val.to(device());
345
346 // Push the gathered model parameters into the NEML2 model (on the compute device) so the
347 // subsequent evaluation and its parameter Jacobian use the MOOSE-provided values.
348 for (auto & [pname, pval] : _model_params)
349 model().set_parameter(pname, pval.to(device()));
350 _model_params.clear();
351
352 // The H2D copies above are asynchronous on CUDA; block so this phase's time reflects the
353 // transfer rather than leaking into the next synchronization point.
355 }
356 catch (std::exception & e)
357 {
358 mooseError("An error occurred while filling inputs for the NEML2 model. Error message:\n",
359 e.what(),
361 }
362}
363
364void
366{
367 if (!_manage_state_advance || _t_step == 0)
368 return;
369
370 for (const auto & [name, val] : _state_vars)
371 {
372 const auto [base_name, lag] = parseLag(name);
373 mooseAssert(lag > 0, "Invalid lag for a stateful variable");
374 // cache the value from the current step (favor output over input); the value that feeds
375 // "base~lag" next step is the current "base~(lag-1)" (which is "base" itself for lag == 1).
376 const auto curr_name = lagName(base_name, lag - 1);
377 if (_out.count(curr_name))
378 _state_vars[name] = _out.at(curr_name);
379 else if (_in.count(curr_name))
380 _state_vars[name] = _in.at(curr_name);
381 else
382 mooseError("Failed to find cached value for old variable: ", name);
383 }
384}
385
386bool
387NEML2ModelExecutor::solve(bool compute_derivative)
388{
389 try
390 {
391 // Evaluate the NEML2 material model. The value is always computed; the (comparatively
392 // expensive) derivatives are computed selectively:
393 // - input Jacobian d(output)/d(input): only when MOOSE is assembling the Jacobian (compute_-
394 // derivative). A pure residual evaluation skips it.
395 // - parameter Jacobian d(output)/d(parameter): whenever any object requested it, on EVERY
396 // pass,
397 // because parameter derivatives are consumed as outputs (e.g. inverse-optimization
398 // sensitivities) and must be current whenever they are read.
399 // Each of jacobian()/param_jacobian() also returns the value, so we only fall back to the
400 // value-only forward() when neither derivative is needed.
401 TIME_SECTION("NEML2::solve", 1, "Evaluating the NEML2 model");
402
403 const bool compute_parameter_derivative = !_retrieved_parameter_derivatives.empty();
404
405 if (compute_derivative)
406 {
407 auto [out, dout_din] = model().jacobian(_in);
408 _out = std::move(out);
409 _dout_din = std::move(dout_din);
410 }
411 if (compute_parameter_derivative)
412 {
413 // .first repeats the (identical) outputs; keep only the parameter-derivative blocks.
414 auto [out, dout_dparam] = model().param_jacobian(_in);
415 _out = std::move(out);
416 _dout_dparam = std::move(dout_dparam);
417 }
418 if (!compute_derivative && !compute_parameter_derivative)
419 _out = model().value(_in);
420
421 // NEML2 launches its kernels asynchronously on CUDA; block here so the compute time is
422 // attributed to this section instead of leaking into the output D2H copy in extractOutputs().
424
426 _in.clear();
427 }
428 catch (const neml2::aoti::Exception & e)
429 {
430 // NEML2 v3 distinguishes recoverable numerical failures (e.g. ConvergenceError: a Newton
431 // divergence / max-iters) from non-recoverable ones (FatalError: shape / device / config). A
432 // retry can only clear the former -- by cutting the time step -- so a non-recoverable error
433 // must hard-fail instead of triggering a futile sequence of time-step cuts.
434 if (!e.recoverable())
435 mooseError("NEML2 model evaluation failed with a non-recoverable error:\n",
436 e.what(),
438
439 _error_message = e.what();
440 _error = true;
441 }
442 catch (const std::exception & e)
443 {
444 // Not a NEML2 exception -- unexpected; do not silently retry it as a recoverable failure.
445 mooseError("NEML2 model evaluation raised an unexpected error:\n",
446 e.what(),
448 }
449
450 // NOTE: a failed constitutive update is dumped in finalize(), not here. solve() is skipped on
451 // ranks whose local batch is empty (see execute()'s isEmpty() early-return), so any collective
452 // placed here would deadlock -- the empty-batch ranks would never reach it. finalize() runs
453 // collectively on every rank, so the cross-rank failure counter lives there instead.
454 return !_error;
455}
456
457std::string
458NEML2ModelExecutor::failedInputDumpName(unsigned int count, const std::string & rank_token) const
459{
460 // Model name for the file; fall back to the object name when the AOTI folder input carries no
461 // explicit model name. Sanitize to a safe filename fragment.
462 std::string model_name = getParam<std::string>("model");
463 if (model_name.empty())
464 model_name = name();
465 for (auto & c : model_name)
466 if (!std::isalnum(static_cast<unsigned char>(c)) && c != '_' && c != '-')
467 c = '_';
468 return model_name + "_count" + std::to_string(count) + "_rank" + rank_token + ".pt";
469}
470
471void
473{
474 TIME_SECTION(
475 "NEML2::extractOutputs", 1, "Copying NEML2 outputs and derivatives back to the host");
476 try
477 {
478 // retrieve outputs. The D2H copies to output_device() (host) below are synchronous, so with
479 // solve()'s post-compute sync in place this phase's time reflects the transfer alone.
480 // .contiguous() so the consuming NEML2ToMOOSEMaterialProperty can read the batch with a plain
481 // per-element memcpy off data_ptr() instead of per-qp at::Tensor ops.
482 for (auto & [y, target] : _retrieved_outputs)
483 target = _out[y].to(output_device()).contiguous();
484
485 // clear output unless we need it for on-device state advance
487 _out.clear();
488
489 // Input derivatives are produced only on Jacobian evaluations (see solve()); a pure residual
490 // evaluation leaves the previously retrieved input derivatives untouched -- they are not read
491 // until the next Jacobian assembly, and consumers reading them for output at EXEC_TIMESTEP_END
492 // see the last Jacobian's value.
493 if (compute_derivative)
494 {
495 // retrieve derivatives J[y][x]
496 for (auto & [y, dy] : _retrieved_derivatives)
497 for (auto & [x, target] : dy)
498 {
499 const auto & source = _dout_din[y][x];
500 if (source.defined())
501 target = source.to(output_device()).contiguous();
502 }
503 _dout_din.clear();
504 }
505
506 // Parameter derivatives are computed on every pass they are requested (see solve()), so
507 // retrieve them whenever any were requested -- they stay current for output/coupling consumers.
509 {
510 // retrieve parameter derivatives P[y][p]
511 for (auto & [y, dy] : _retrieved_parameter_derivatives)
512 for (auto & [p, target] : dy)
513 {
514 const auto & source = _dout_dparam[y][p];
515 if (source.defined())
516 target = source.to(output_device()).contiguous();
517 }
518 _dout_dparam.clear();
519 }
520 }
521 catch (std::exception & e)
522 {
523 mooseError("An error occurred while retrieving outputs from the NEML2 model. Error message:\n",
524 e.what(),
526 }
527}
528
529void
531{
532 // Remember this rank's own outcome before the collective below overwrites _error with the global
533 // value -- only ranks that actually failed should write a dump file.
534 const bool local_error = _error;
535
536 // See if any rank failed
537 processor_id_type pid;
539
540 // Optionally capture the exact inputs of a failed constitutive update to per-rank TorchScript
541 // files for offline re-evaluation. This lives in finalize() -- not solve() -- because solve() is
542 // skipped on ranks whose local batch is empty (see execute()), so a collective there would
543 // deadlock, whereas finalize() runs collectively on every rank. The counter is advanced on all
544 // ranks in lockstep so it stays identical everywhere; since the console streams rank 0 only, the
545 // reported message can then point at a single '<model>_count<c>_rank_*.pt' glob (one file per
546 // failing rank).
548 {
549 const unsigned int count = _num_failed_dumps++; // advanced on all ranks -> stays consistent
550 if (local_error)
552 _in, failedInputDumpName(count, std::to_string(processor_id())));
553 const std::string exec = _fe_problem.getCurrentExecuteOnFlag().name();
554 const unsigned int nl_it =
556 _error_message += "\nNEML2 constitutive update failed (step " + std::to_string(_t_step) +
557 ", execute_on=" + exec + ", nonlinear iteration " + std::to_string(nl_it) +
558 "). Input tensors dumped to " + failedInputDumpName(count, "*") +
559 " (one file per failing rank).";
560 }
561
562 // Fail the next nonlinear convergence check if any rank failed
563 if (_error)
564 {
566 if (_communicator.rank() == 0)
567 {
568 std::string msg = "NEML2 model execution failed on at least one processor with ID " +
569 std::to_string(pid) + ". Error message:\n";
570 msg += _error_message;
572 msg += "\nTo recover, the solution will fail and then be re-attempted with a reduced time "
573 "step.";
574 _console << COLOR_YELLOW << msg << COLOR_DEFAULT << std::endl;
575 }
577 }
578 else if (_t_step > 0)
579 _output_ready = true;
580}
581
582void
584{
586 mooseError("NEML2 output variables and derivatives must be retrieved during object "
587 "construction. This is a code problem.");
588}
589
590const at::Tensor &
591NEML2ModelExecutor::getOutput(const std::string & output_name) const
592{
594
595 if (!contains(model().output_names(), output_name))
596 mooseError("Trying to retrieve a non-existent NEML2 output variable '", output_name, "'.");
597
598 return _retrieved_outputs[output_name];
599}
600
601const at::Tensor &
602NEML2ModelExecutor::getOutputDerivative(const std::string & output_name,
603 const std::string & input_name) const
604{
606
607 if (!contains(model().output_names(), output_name))
608 mooseError("Trying to retrieve the derivative of NEML2 output variable '",
609 output_name,
610 "' with respect to NEML2 input variable '",
611 input_name,
612 "', but the NEML2 output variable does not exist.");
613
614 if (!contains(model().input_names(), input_name))
615 mooseError("Trying to retrieve the derivative of NEML2 output variable '",
616 output_name,
617 "' with respect to NEML2 input variable '",
618 input_name,
619 "', but the NEML2 input variable does not exist.");
620
621 return _retrieved_derivatives[output_name][input_name];
622}
623
624const at::Tensor &
626 const std::string & parameter_name) const
627{
629
630 if (!contains(model().output_names(), output_name))
631 mooseError("Trying to retrieve the derivative of NEML2 output variable '",
632 output_name,
633 "' with respect to NEML2 model parameter '",
634 parameter_name,
635 "', but the NEML2 output variable does not exist.");
636
637 if (!model().parameter_base_shapes().count(parameter_name))
638 mooseError("Trying to retrieve the derivative of NEML2 output variable '",
639 output_name,
640 "' with respect to NEML2 model parameter '",
641 parameter_name,
642 "', but the NEML2 model parameter does not exist.");
643
644 return _retrieved_parameter_derivatives[output_name][parameter_name];
645}
646
647#endif
InputParameters emptyInputParameters()
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
const ExecFlagType EXEC_TIMESTEP_END
Definition Moose.C:37
const ExecFlagType EXEC_INITIAL
Definition Moose.C:31
const ExecFlagType EXEC_LINEAR
Definition Moose.C:32
const ExecFlagType EXEC_NONLINEAR
Definition Moose.C:34
unsigned int count
Definition MortarUtils.C:53
registerMooseObject("MooseApp", NEML2ModelExecutor)
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
A MultiMooseEnum object to hold "execute_on" flags.
void setFailNextNonlinearConvergenceCheck()
Skip further residual evaluations and fail the next nonlinear convergence check(s)
virtual bool solverSystemConverged(const unsigned int solver_sys_num) override
const ExecFlagType & getCurrentExecuteOnFlag() const
Return/set the current execution flag.
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
virtual bool isTransient() const override
virtual bool startedInitialSetup()
Returns true if we are in or beyond the initialSetup stage.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition MooseBase.h:457
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition MooseBase.h:406
const std::string & name() const
NEML2BatchIndexGenerator iterates over the mesh and generates a map from element ID to batch index wh...
bool isEmpty() const
Whether the batch is empty.
std::size_t getBatchIndex() const
Get the current batch index (in almost all cases this is the total batch size)
NEML2ModelExecutor executes a NEML2 model.
const bool _dump_inputs_on_failure
Dump the NEML2 input tensors to a per-rank TorchScript file on solve failure, for offline debugging.
std::map< std::string, at::Tensor > _state_vars
Cached stateful variables from the last successful step (for on-device advance)
bool _derivative_valid
Whether the retained input-derivative tensor is consistent with the current batch.
void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
std::map< std::string, at::Tensor > _zero_seed_vars
Base-shaped zero tensors for cold-started in-place-updated unknowns (an order-0 NEML2 input that is a...
bool _error
Whether an error was encountered.
void execute() override
Execute method.
std::string _error_message
Error message.
std::map< std::string, std::map< std::string, at::Tensor > > _dout_din
The derivative of the output variables w.r.t. the input variables: J[output][input].
static InputParameters actionParams()
Parameters that can be specified under the NEML2Action common area.
virtual bool solve(bool compute_derivative)
Perform the material update.
std::set< std::string > _gathered_parameter_names
void deviceSynchronize()
Block until all asynchronous device (CUDA) work has completed.
std::map< std::string, at::Tensor > _model_params
Model parameter values gathered from MOOSE (qualified name -> tensor), set on the model before each e...
const at::Tensor & getOutput(const std::string &output_name) const
Get a reference(!) to the requested output view.
std::map< std::string, at::Tensor > _out
The output variables of the material model.
std::vector< const MOOSEToNEML2 * > _param_gatherers
MOOSE data gathering user objects (model parameters)
std::map< std::string, std::map< std::string, at::Tensor > > _retrieved_derivatives
set of derivatives that were retrieved (by other objects)
virtual void checkExecutionStage() const final
Prevent output and derivative retrieval after construction.
static InputParameters validParams()
const bool _manage_state_advance
Advance state on device (rather than via MOOSE material properties)
bool _output_ready
flag that indicates if output data has been fully computed
std::map< std::string, at::Tensor > _in
The input variables of the material model (name -> (batch, *base_shape) tensor)
std::size_t getBatchIndex(dof_id_type elem_id) const
Get the batch index for the given element ID.
const at::Tensor & getOutputDerivative(const std::string &output_name, const std::string &input_name) const
Get a reference(!) to the requested output derivative view.
void advanceState()
Save stateful variables for on-device state advance.
virtual void extractOutputs(bool compute_derivative)
Extract outputs, and their derivatives with respect to the input variables when compute_derivative is...
const at::Tensor & getOutputParameterDerivative(const std::string &output_name, const std::string &parameter_name) const
Get a reference(!) to the requested output-parameter-derivative view.
std::set< std::string > _gathered_variable_names
void finalize() override
Finalize.
std::string failedInputDumpName(unsigned int count, const std::string &rank_token) const
Build the dump-file name for a failed constitutive update: '<model>_count<count>_rank<rank>....
std::map< std::string, std::map< std::string, at::Tensor > > _dout_dparam
The derivative of the output variables w.r.t. the model parameters: P[output][parameter].
const NEML2BatchIndexGenerator & _batch_index_generator
The NEML2BatchIndexGenerator used to generate the element-to-batch-index map.
unsigned int _num_failed_dumps
Running count of failed constitutive updates dumped on this rank (disambiguates repeated dumps)
void initialize() override
Called before execute() is ever called so that data can be cleared.
virtual void fillInputs()
Fill input variables using the gatherers.
NEML2ModelExecutor(const InputParameters &params)
std::map< std::string, at::Tensor > _retrieved_outputs
set of output variables that were retrieved (by other objects)
std::vector< const MOOSEToNEML2 * > _gatherers
MOOSE data gathering user objects (input variables)
virtual void addGatheredParameter(const UserObjectName &, const std::string &)
Register a NEML2 model parameter gathered by a gatherer.
void meshChanged() override
Called on this object when the mesh changes.
std::map< std::string, std::map< std::string, at::Tensor > > _retrieved_parameter_derivatives
set of output-parameter-derivatives that were retrieved (by other objects)
virtual void addGatheredVariable(const UserObjectName &, const std::string &)
Register a NEML2 input variable gathered by a gatherer.
virtual const std::vector< std::string > & input_names() const =0
virtual std::pair< std::map< std::string, at::Tensor >, neml2::aoti::VariablePairJacobian > jacobian(const std::map< std::string, at::Tensor > &inputs) const =0
Evaluate the model and its full input Jacobian.
virtual void set_parameter(const std::string &name, const at::Tensor &value)=0
Replace a (runtime-flexible) model parameter's value.
virtual std::pair< std::map< std::string, at::Tensor >, neml2::aoti::VariablePairJacobian > param_jacobian(const std::map< std::string, at::Tensor > &inputs) const =0
Evaluate the model and its parameter Jacobian (d output / d parameter).
virtual std::map< std::string, at::Tensor > value(const std::map< std::string, at::Tensor > &inputs) const =0
virtual const std::vector< std::vector< int64_t > > & input_base_shapes() const =0
virtual const std::vector< std::string > & output_names() const =0
Interface class to provide common input parameters, members, and methods for MOOSEObjects that use NE...
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.
unsigned int neml2NumThreads() const
libtorch intra-op thread count to use while evaluating the NEML2 model.
RAII guard forcing libtorch's default scalar type to Float64 for its lifetime, restoring the previous...
Definition NEML2Utils.h:249
RAII guard that pins libtorch's intra-op thread count for the current scope and restores the previous...
Definition NEML2Utils.h:215
virtual unsigned int getCurrentNonlinearIterationNumber()=0
const ExecFlagType & _current_execute_flag
Reference to FEProblemBase.
const bool & currentlyComputingJacobian() const
Returns true if the problem is in the process of computing the Jacobian.
Definition SubProblem.h:695
void maxloc(T &r, unsigned int &max_id) const
processor_id_type rank() const
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
int & _t_step
The number of the time step.
std::set< std::string > _depend_uo
Depend UserObjects that to be used both for determining user object sorting and by AuxKernel for find...
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
const Parallel::Communicator & _communicator
processor_id_type processor_id() const
ExecFlagEnum getDefaultExecFlagEnum()
bool contains(const std::vector< std::string > &v, const std::string &s)
Whether a vector of strings contains a given string.
Definition NEML2Utils.h:78
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...
Definition NEML2Utils.C:25
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,...
Definition NEML2Utils.h:58
std::string lagName(const std::string &base, int lag)
Inverse of parseLag: build the variable name for a base name + lag.
Definition NEML2Utils.h:71
static std::string NEML2_help_message
Definition NEML2Utils.h:263
bool contains(std::string_view superstring, std::string_view substring)
auto index_range(const T &sizable)
void synchronize(int64_t device_index)