Skip to content

Build kokkos-cpu LAMMPS as a shared library usable from Python - #13

Merged
shyuep merged 2 commits into
mainfrom
feat/kokkos-cpu-lammps-python
Nov 4, 2026
Merged

shyuep merged 2 commits into
mainfrom
feat/kokkos-cpu-lammps-python

Conversation

@shyuep

@shyuep shyuep commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

materialyzeai/matgl:latest-kokkos-cpu currently can't drive LAMMPS from Python:

  • LAMMPS is statically linked into lmp. There is no liblammps.so and no lammps Python module.
  • LAMMPS links a separate libtorch 2.5.1, while Python gets the torch wheel (2.15). Loaded into one process, the two collide on library names (libc10.so, libtorch_cpu.so).

matcalc's new LAMMPS backend (MATCALC_BACKEND=LAMMPS) runs LAMMPS in-process through the Python module, and its CI is meant to use this image. This PR makes the image support that.

Changes

  • Dockerfile.matgl.kokkos-cpu
    • The builder starts from ghcr.io/astral-sh/uv:python3.12-bookworm, the same Python layout as the runtime image.
    • It installs matgl with the CPU torch wheel and builds LAMMPS against the wheel's libtorch. CMAKE_CXX_STANDARD=20 is needed because torch 2.15 headers require C++20.
    • LAMMPS is built with BUILD_SHARED_LIBS=ON. The lammps Python package is copied into site-packages, and /opt/lammps/lib is registered with ldconfig. The RPATH covers liblammps.so and the torch lib/ directory.
    • The torch version is written to /opt/lammps/torch-constraint.txt. The runtime image installs against it, so Python and LAMMPS share one libtorch.
    • Adds PKG_ML-SNAP.
    • The separate libtorch zip is no longer used, so the recipe also builds natively on arm64. The workflow still publishes amd64 only.
    • Side benefit: lmp now loads TorchScript models with the same torch that exported them. Before, models exported with 2.15 were loaded by libtorch 2.5.1.
  • Dockerfile.matml.kokkos-cpu: pins torch to the version already in the base image (uv pip freeze), so adding matcalc/matpes can't change torch. Freezing, rather than reading /opt/lammps/torch-constraint.txt, also works with the currently published base, which this image is built FROM in CI until the new base is pushed.
  • Workflow smoke test: also checks in-process from lammps import lammps (with Kokkos) and that pair_style matgl, matgl/kk and snap are present.

Derived images must keep torch fixed with uv pip install --system -c /opt/lammps/torch-constraint.txt ....

Testing

  • Built locally on arm64 (Docker Desktop, BUILD_JOBS=8): torch 2.15.0+cpu, Kokkos backends OpenMP and Serial. Installed packages: EXTRA-COMPUTE, EXTRA-DUMP, EXTRA-FIX, KOKKOS, MANYBODY, ML-SNAP, MOLECULE, plus ML-MATGL.
  • Inside the image, ran matcalc's new tests/test_backend_lammps.py with failures instead of skips when LAMMPS pieces are missing. 35/35 passed with pair_style matgl, and 35/35 with matgl/kk (-k on t 2 -sf kk -pk kokkos newton on neigh half).
  • Derived matml image builds on both the currently published base (amd64) and the new base; in the latter, from lammps import lammps with matgl/kk works alongside matcalc.
  • The amd64 build in this workflow has not run yet; this PR's CI is the first one.

🤖 Generated with Claude Code

shyuep and others added 2 commits November 4, 2026 10:48
Build LAMMPS against the libtorch of the pip torch wheel (instead of a
separate libtorch 2.5.1 zip), as a shared library with the lammps Python
module installed, so it can run in-process from Python (e.g. matcalc's
MATCALC_BACKEND=LAMMPS) with one libtorch shared by Python and LAMMPS.
Pin torch in the runtime and derived images to the build version via
/opt/lammps/torch-constraint.txt, and add ML-SNAP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The derived image builds FROM the published matgl:latest-kokkos-cpu,
which lacks /opt/lammps/torch-constraint.txt until this change is
published. Freeze the base image's torch instead, which works with
both the old and the new base.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shyuep
shyuep merged commit 5656431 into main Nov 4, 2026
2 checks passed
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.

1 participant