Skip to content

neml2 submodule update#32871

Open
hugary1995 wants to merge 6 commits into
idaholab:nextfrom
hugary1995:neml2_update_042626
Open

neml2 submodule update#32871
hugary1995 wants to merge 6 commits into
idaholab:nextfrom
hugary1995:neml2_update_042626

Conversation

@hugary1995
Copy link
Copy Markdown
Contributor

This pull request introduces significant improvements to the integration between MOOSE and NEML2, focusing on simplifying and generalizing the mapping of MOOSE data structures to NEML2 input variables and parameters. The changes unify the handling of different MOOSE data sources, update documentation to reflect the new, more flexible approach, and refactor related code for clarity and maintainability.

Also trying to move torch link flags to the front in the build system to work around a torch bug.

@hugary1995 hugary1995 changed the title Neml2 update 042626 neml2 submodule update May 4, 2026
@hugary1995 hugary1995 force-pushed the neml2_update_042626 branch from 49fa72c to 7475d35 Compare May 4, 2026 21:18
@hugary1995
Copy link
Copy Markdown
Contributor Author

@dschwen @roystgnr @loganharbour @permcody Can you review my changes in framework/moose.mk and framework/app.mk? I have good reasons for making those changes (see pytorch/pytorch#182263). But let me know if they could lead to unintended consequences.

@hugary1995 hugary1995 requested review from permcody and roystgnr May 4, 2026 22:01
@permcody
Copy link
Copy Markdown
Member

permcody commented May 5, 2026

Changing flags and flag order? Oh boy, I think this is still a black art, even in the era of AI. We'll just have to see how the testing does!

@hugary1995 hugary1995 force-pushed the neml2_update_042626 branch from 7475d35 to 45c4fcc Compare May 6, 2026 15:28
@moosebuild
Copy link
Copy Markdown
Contributor

Job Test, step Results summary on 2c316aa wanted to post the following:

Framework test summary

Compared against 5b391df in job civet.inl.gov/job/3787211.

No added tests

Run time changes

Test Base (s) Head (s) +/- Base (MB) Head (MB)
problems/reference_residual_problem.zero_tolerance_ref 3.97 6.16 +55.16% 147.95 134.67
time_integrators/scalar.group/scalar 5.22 8.04 +53.87% 176.68 157.49

Modules test summary

Compared against 5b391df in job civet.inl.gov/job/3787211.

Removed tests

Test Time (s) Memory (MB)
solid_mechanics/test:neml2/plasticity.plasticity/perfect_keep_tensors_on_device 0.99 144.10

Added tests

Test Time (s) Memory (MB)
solid_mechanics/test:neml2/plasticity.manage_state_advance 1.23 149.50

Run time changes

Test Base (s) Head (s) +/- Base (MB) Head (MB)
solid_mechanics/test:smeared_cracking.rz_exponential 11.03 18.89 +71.29% 126.14 132.48
solid_mechanics/test:rate_independent_cyclic_hardening.nonlin_isokinharden_symmetric_strain_controlled 10.13 17.04 +68.22% 150.94 131.63
solid_mechanics/test:dynamics/acceleration_bc.acceleration_bc_ti 3.45 5.69 +64.88% 130.91 130.29
combined/test:combined_plasticity_temperature.ad_temp_dep_yield-jac 6.55 10.69 +63.09% 173.21 173.16
solid_mechanics/test:rate_independent_cyclic_hardening.nonlin_kinharden_nonsymmetric_strain_controlled 5.80 9.35 +61.27% 130.95 135.51
solid_mechanics/test:smeared_cracking.cracking_rotation_pres_dir_z 3.87 6.21 +60.56% 132.21 151.24
solid_mechanics/test:rate_independent_cyclic_hardening.1D_ratcheting_nonlin_kinharden_stress_controlled 3.73 5.97 +60.22% 116.25 119.94
solid_mechanics/test:rate_independent_cyclic_hardening.linear_kinharden_nonsymmetric_stress_controlled 4.34 6.95 +60.02% 122.60 138.96
solid_mechanics/test:smeared_cracking.cracking_rotation_pres_dir_x 3.82 6.11 +59.93% 136.85 141.33
stochastic_tools/test:transfers/sampler_reporter.transfer/distributed 3.27 5.22 +59.87% 472.19 478.69
solid_mechanics/test:crystal_plasticity/cp_eigenstrains.thermal_eigenstrain_011orientation 5.38 8.59 +59.73% 124.00 128.27
solid_mechanics/test:combined_creep_plasticity.creepWithPlasticity 5.24 8.37 +59.71% 121.92 116.54
solid_mechanics/test:temperature_dependent_hardening.test 2.17 3.47 +59.66% 121.70 127.28
solid_mechanics/test:beam/static.euler_finite_rot_y 3.11 4.97 +59.65% 126.28 128.28
solid_mechanics/test:combined_creep_plasticity.stress_prescribed 3.47 5.53 +59.23% 124.49 121.23
solid_mechanics/test:rate_independent_cyclic_hardening.nonlin_kinharden_symmetric_strain_controlled 4.70 7.45 +58.62% 126.33 130.16
combined/test:adaptive_timestepping.test_function_change 3.49 5.54 +58.47% 160.62 159.24
solid_mechanics/test:dynamics/acceleration_bc.acceleration_bc 3.47 5.49 +58.19% 135.66 134.27
solid_mechanics/test:beam/static.euler_finite_y_with_action 3.13 4.93 +57.55% 121.64 126.85
solid_mechanics/test:smeared_cracking.cracking_rotation 3.97 6.25 +57.55% 135.74 135.42
stochastic_tools/test:transfers/sampler_reporter.transfer/normal 6.76 10.63 +57.25% 158.47 166.17
solid_mechanics/test:combined_creep_plasticity.combined_start_time 5.72 8.97 +56.89% 118.07 133.47
solid_mechanics/test:torque_reaction.disp_about_axis_motion 2.00 3.13 +56.50% 118.95 116.45
solid_mechanics/test:rate_independent_cyclic_hardening.nonlin_isoharden_symmetric_strain_controlled 3.37 5.27 +56.29% 126.03 120.05
solid_mechanics/test:smeared_cracking.xyz 2.82 4.41 +56.20% 129.79 136.25
solid_mechanics/test:beam/static.euler_finite_rot_z 3.32 5.18 +56.14% 124.22 133.23
combined/test:adaptive_timestepping.test_function_change_restart2 2.57 4.01 +56.09% 173.47 158.29
solid_mechanics/test:rate_independent_cyclic_hardening.linear_kinharden_nonsymmetric_strain_controlled 10.14 15.80 +55.90% 130.89 139.80
solid_mechanics/test:torque_reaction.disp_about_axis_axial_motion_delayed 2.02 3.14 +55.43% 114.45 118.73
solid_mechanics/test:smeared_cracking.exponential 3.96 6.14 +55.29% 123.71 124.01
solid_mechanics/test:dynamics/wave_1D.rayleigh_hht_ad_jac 3.86 5.90 +53.09% 119.75 131.04
solid_mechanics/test:ad_smeared_cracking.rz_exponential 12.02 18.37 +52.79% 135.70 137.06
solid_mechanics/test:crystal_plasticity/cp_eigenstrains.thermal_eigenstrain 4.12 6.30 +52.75% 125.07 124.23
solid_mechanics/test:smeared_cracking.cracking_rotation_pres_dir_xz 3.95 6.02 +52.40% 132.19 140.22
navier_stokes/test:finite_volume/ins/channel-flow/linear-segregated/1d-scalar.channel/transient-physics 3.95 5.97 +51.09% 129.61 142.93
solid_mechanics/test:rate_independent_cyclic_hardening.linear_kinharden_symmetric_strain_controlled 4.83 7.29 +50.90% 144.16 126.92
solid_mechanics/test:combined_creep_plasticity.combined 5.84 8.81 +50.89% 120.34 117.05
solid_mechanics/test:smeared_cracking.energybased_exponential 2.95 4.44 +50.70% 122.47 128.09
solid_mechanics/test:crystal_plasticity/stress_update_material_based.rotation_matrix_update_euler_angle_011_orientation 2.57 3.87 +50.42% 120.92 126.68

@roystgnr
Copy link
Copy Markdown
Contributor

roystgnr commented May 6, 2026

But let me know if they could lead to unintended consequences.

If we're doing things correctly, they wouldn't lead to unintended consequences, because they wouldn't lead to any consequences. Get all your LDFLAGS before all your LIBS flags, and because they don't conflict with each other and there aren't any distinct versions of identical symbols it doesn't matter what order the LDFLAGS are in amongst themselves, likewise for the LIBS.

If it does matter, then this isn't a fix for that problem, it's a workaround. Might it be the best possible workaround? Maybe. From your comment it sounds like we have multiple different BLAS getting linked to, and PyTorch needs to definitely resolve its own BLAS calls to its own BLAS rather than to what PETSc brings in? The fix would be to make sure we only link one BLAS into one final program, pointing PETSc (or its dependencies) to PyTorch's when building PETSc, but I'm not sure if there are other incompatibilities between them that might make that impossible.

@hugary1995
Copy link
Copy Markdown
Contributor Author

hugary1995 commented May 6, 2026

Yeah, what really bothers me is that libtorch doesn't directly link to blas as far as I can tell, but it does have symbols with jump tables. So I guess libtorch is expected to be linked to some final program who either define those symbols or is linked to blas somehow, AND that blas needs to have compatible ABI with whatever symbol libtorch used (for its own compilation). I honestly think that's a bad decision on the libtorch end.

I agree with you on all accounts. This is a workaround. This is not the best fix. I am open to suggestions on what we could do on our end before the torch team fixes this problem (assuming they will eventually).

@hugary1995
Copy link
Copy Markdown
Contributor Author

So I guess libtorch is expected to be linked to some final program who either define those symbols or is linked to blas somehow, AND that blas needs to have compatible ABI with whatever symbol libtorch used (for its own compilation).

Oh and to be fair, that is indeed what we do when we build containers. PyTorch is built from source using the same blas from petsc. Our containers will work just fine without this workaround.

However, that's not our general instruction for users -- we suggest people to use conda, and ask users to download their own PyTorch (libtorch). That creates problems.

@lindsayad
Copy link
Copy Markdown
Member

lindsayad commented May 6, 2026

Ah I was going to say I spent a lot of time on this very issue which I handled by making sure we're using the same blas everywhere! (For our container builds)

@hugary1995
Copy link
Copy Markdown
Contributor Author

Perhaps... perhaps

We should discourage users from using their own PyTorch/libtorch and instead provide a scripts/update_and_rebuild_libtorch.sh

@hugary1995
Copy link
Copy Markdown
Contributor Author

hugary1995 commented May 6, 2026

Or, as a middle ground, we can provide a conftest script to check if the user's libtorch is compatible with our petsc? Then the user can use the script to decide whether it's necessary to build torch from source or download a compatible torch.

@moosebuild
Copy link
Copy Markdown
Contributor

Job Documentation, step Docs: sync website on 2c316aa wanted to post the following:

View the site here

This comment will be updated on new commits.

@lindsayad
Copy link
Copy Markdown
Member

Or, as a middle ground, we can provide a conftest script to check if the user's libtorch is compatible with our petsc?

Will it ever be? We create the blas library that PETSc uses in all cases: container, conda, user builds PETSc via scripts/update_and_rebuild_petsc.sh. The only time they'd use the same BLAS is if the user built libtorch themselves pointing to PETSc's BLAS.

We should discourage users from using their own PyTorch/libtorch and instead provide a scripts/update_and_rebuild_libtorch.sh

I would support this

@moosebuild
Copy link
Copy Markdown
Contributor

Job Coverage, step Generate coverage on 2c316aa wanted to post the following:

Framework coverage

5b391d #32871 2c316a
Total Total +/- New
Rate 85.87% 85.89% +0.02% 100.00%
Hits 132500 132445 -55 399
Misses 21797 21759 -38 0

Diff coverage report

Full coverage report

Modules coverage

Solid mechanics

5b391d #32871 2c316a
Total Total +/- New
Rate 85.38% 85.38% - 100.00%
Hits 28595 28595 - 3
Misses 4896 4896 - 0

Diff coverage report

Full coverage report

Full coverage reports

Reports

This comment will be updated on new commits.

@hugary1995
Copy link
Copy Markdown
Contributor Author

I'll wait for @loganharbour to come back and chime in. I can work on a build script for torch if he also approves. I suspect he already has something like that.

@loganharbour
Copy link
Copy Markdown
Member

If you guys are both confident and comfortable with a build script for libtorch, that's fine with me.

@hugary1995, this was the minimal config I was able to get:

    -GNinja \
    -DBUILD_SHARED_LIBS:BOOL=ON \
    -DCMAKE_BUILD_TYPE:STRING=Release \
    -DCMAKE_INSTALL_PREFIX:PATH="$LIBTORCH_DIR" \
    -DBUILD_PYTHON:BOOL=OFF \
    -DCMAKE_INCLUDE_PATH:PATH="${PETSC_DIR}/include" \
    -DCMAKE_LIBRARY_PATH:PATH="${PETSC_DIR}/lib" \
    -DUSE_BLAS:BOOL=ON \
    -DBLAS:STRING=OpenBLAS \
    -DUSE_LAPACK:BOOL=ON \
    -DUSE_MPI:BOOL=OFF \
    -DUSE_DISTRIBUTED:BOOL=OFF \
    -DUSE_NNPACK:BOOL=OFF \
    -DUSE_XNNPACK:BOOL=OFF \
    -DUSE_PYTORCH_QNNPACK:BOOL=OFF \
    -DBUILD_TEST:BOOL=OFF \
    -DUSE_KINETO:BOOL=OFF \
    -DUSE_FBGEMM:BOOL=OFF \

Best to start with that and enable what you need for neml2?

@hugary1995
Copy link
Copy Markdown
Contributor Author

Should be ready now. I added scripts to help build torch from source and added pytorch as a update=none submodule. Let me also start a discussion on slack to gather feedback.

@hugary1995
Copy link
Copy Markdown
Contributor Author

@loganharbour would you help me test out the configure_libtorch.sh script? I modified (mostly addition) some flags from yours.

@grmnptr
Copy link
Copy Markdown
Contributor

grmnptr commented May 8, 2026

Right now we kind-of support linking to conda-based torch libraries on macs. Would this also mean that we should distribute the self-built torch within our conda environment? Or are those avenues still doable with the distribution they have through conda?

@lindsayad
Copy link
Copy Markdown
Member

lindsayad commented May 8, 2026

I don't think it will be possible to be definitively compatible with a torch distribution that's not our own unless we stop building our own blas

@hugary1995
Copy link
Copy Markdown
Contributor Author

hugary1995 commented May 8, 2026

That could have been an alternative solution, i.e., download torch and let petsc use torch's blas and lapack. However, since torch does NOT distribute those libraries, that's not an option...

@grmnptr
Copy link
Copy Markdown
Contributor

grmnptr commented May 8, 2026

Sounds painful from the conda side, but I think it would actually be cool to finally officially have pytorch in our conda environment.

@lindsayad
Copy link
Copy Markdown
Member

Sounds painful from the conda side, but I think it would actually be cool to finally officially have pytorch in our conda environment.

So you're proposing we conda package torch? I'm not going to auto signup our team for more work, but if someone wants to take it on, they may. In the short-term I think what we've moved towards here is better than what we had before. We don't conda package conduit/MFEM at the moment; instead a Mac user (or Linux user not using the containers) must build them using the scripts. I think this is a good parallel

@grmnptr
Copy link
Copy Markdown
Contributor

grmnptr commented May 8, 2026

We don't conda package conduit/MFEM at the moment; instead a Mac user (or Linux user not using the containers) must build them using the scripts.

I see, yeah that sounds good to me too.

@hugary1995
Copy link
Copy Markdown
Contributor Author

I want to point out that the conda users can still download pytorch and hope that it is compatible with petsc. It's just that we don't/can't officially support that any more.

Copy link
Copy Markdown
Contributor

@grmnptr grmnptr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool work!

to be altered.

!alert-end!
A working compiler stack and CMake are also required. The [MOOSE conda environment](installation/conda.md) satisfies both.
Copy link
Copy Markdown
Contributor

@grmnptr grmnptr May 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it used to have python package dependencies too (even if you only wanted to compile the C++ parts) like numpy at some point, maybe they fixed it. But we should have those too in the moose conda package.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They have an option now to disable numpy. Though I haven't tried to run the configure script in an environment without python, guess I should try that.


The way one enables LibTorch [!cite](paszke2019pytorch) capabilities in MOOSE depends on
the operating system (Linux or Mac) and if we use HPC or just a local workstation.
MOOSE can be linked against [libtorch](https://pytorch.org/cppdocs/) [!cite](paszke2019pytorch) to enable hardware acceleration. The libtorch source is provided as a git submodule under `framework/contrib/pytorch`, and a setup script is provided to build and install it with the configuration MOOSE expects.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not just for hardware acceleration, but also for high level easy-to-use interfaces to parts of ML workflows.

@moosebuild
Copy link
Copy Markdown
Contributor

Job Precheck, step Versioner verify on aba6984 wanted to post the following:

Versioner templates

Found 11 templates, 0 failed

Versioner influential files

Found 54 influential files, 5 changed, 2 added, 1 removed

package status file
moose-dev CHANGE conda/moose-dev/meta.yaml
moose-dev CHANGE framework/contrib/neml2
moose-dev CHANGE framework/contrib/neml2.mk
moose-dev NEW scripts/configure_libtorch.sh
moose-dev CHANGE scripts/configure_neml2.sh
moose-dev NEW scripts/update_and_rebuild_libtorch.sh
moose-dev CHANGE scripts/update_and_rebuild_neml2.sh
moose-dev REMOVED scripts/setup_libtorch.sh

Versioner versions

Found 10 packages, 1 changed, 0 failed

package status hash to hash version to version
moose-dev CHANGE 0bf4d15 e0c83a8 2026.04.21 2026.05.06

@moosebuild
Copy link
Copy Markdown
Contributor

Job Precheck, step Python: black format on aba6984 wanted to post the following:

Python black formatting

Your code requires style changes.

A patch was generated and copied here.

You can directly apply the patch by running the following at the top level of your repository:

curl -s https://mooseframework.inl.gov/docs/PRs/32871/black/black.patch | git apply -v

Alternatively, you can run the following at the top level of your repository:

black --config pyproject.toml --workers 1 .

Copy link
Copy Markdown
Member

@loganharbour loganharbour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow the input diffs are so beautiful.

@@ -0,0 +1,125 @@
#!/usr/bin/env bash
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use these scripts in apptainer/moose-dev.def as well

sub_block_params.applyParameters(common_action.parameters());

// Variables to skip
_skip_input_variables = getParam<std::vector<std::string>>("skip_input_variables");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is these a reason this isn't in the initializer list?

paramError("moose_input_types",
"Unsupported type corresponding to the moose input ",
input.moose.name);
auto obj_name = obscureObjectName(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't required, but I think there is a lot of common setting that you should be able to do in a lambda for adding all of these objects (type, from_moose, to_neml2, etc)

bool has_scalar = _problem->hasScalarVariable(name);
bool has_func = _problem->hasFunction(name);
bool has_var = _problem->hasVariable(name);
if (int(is_time) + int(has_scalar) + int(has_func) + int(has_var) > 1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obscure but can you verify that hitting this error provides the context of the top level block as an input file and position? Good sanity check on much of the error handling work I've done

Comment thread test/tests/neml2/tests
@@ -2,10 +2,19 @@
# The original issue is idaholab/blackbear#333
issues = '#26450 #26920'
design = 'NEML2/index.md NEML2Action.md'
[custom_model]
requirement = 'The framework shall be capable of running custom NEML2 model implemented in MOOSE (optionally with automatic differentiation).'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
requirement = 'The framework shall be capable of running custom NEML2 model implemented in MOOSE (optionally with automatic differentiation).'
requirement = 'The framework shall be capable of running custom NEML2 models implemented in MOOSE (optionally with automatic differentiation).'

@loganharbour
Copy link
Copy Markdown
Member

So you're proposing we conda package torch? I'm not going to auto signup our team for more work, but if someone wants to take it on, they may.

@lindsayad @grmnptr: Extending the conda piece is pretty significant. Long term, I don't think growing the number of conda packages is particularly sustainable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants