Skip to content

fix(lammps): load models on the GPU selected by Kokkos - #6154

Open
Miller10101010 wants to merge 3 commits into
deepmodeling:masterfrom
Miller10101010:fix-kokkos-model-device
Open

Miller10101010 wants to merge 3 commits into
deepmodeling:masterfrom
Miller10101010:fix-kokkos-model-device

Conversation

@Miller10101010

@Miller10101010 Miller10101010 commented Nov 24, 2026 •

Copy link
Copy Markdown

Use the CUDA device selected by Kokkos when loading a model for deepmd/kk or dpa4spin/kk. This honors an explicit device selection even when it differs from the node-local MPI rank.

Tested on a two-A800 node with a single rank explicitly selecting GPU 1. Both styles loaded on the selected device. Two regression tests passed, including energy and force comparisons on devices 0 and 1.

Closes #5994.

Summary by CodeRabbit

  • Bug Fixes
    • Improved GPU selection for Kokkos-based DeepMD and DPA4Spin models when running across multiple CUDA devices.
  • Tests
    • Added multi-GPU checks for GPU assignment and consistency of model energies and forces.
    • Updated test setup to use the built LAMMPS executable and document how to request a dedicated two-GPU run.
  • Build
    • Updated the requested LAMMPS executable target.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The LAMMPS pair styles now pass a selected model device during model initialization. Kokkos CUDA styles select the active CUDA device. A two-device integration test covers deepmd/kk and dpa4spin/kk.

Changes: Kokkos device binding

Layer / File(s) Summary
Model-device selection contract and overrides
source/lmp/pair_deepmd.h, source/lmp/pair_deepmd_kokkos.h, source/lmp/pair_deepmd_kokkos.cpp, source/lmp/pair_dpa4spin.h, source/lmp/pair_dpa4spin_kokkos.h, source/lmp/pair_dpa4spin_kokkos.cpp
The base pair styles default to the node rank. Kokkos CUDA overrides return the active CUDA device.
Pass the selected device to model initialization
source/lmp/pair_deepmd.cpp, source/lmp/pair_dpa4spin.cpp
DeepMD single-model and ensemble initialization, and DPA4Spin initialization, now use model_device().
Two-device test and LAMMPS test setup
source/lmp/tests/test_kokkos_model_device.py, source/install/test_lammps_kokkos.sh
The integration test checks GPU assignment and compares energies and forces for both Kokkos styles. The test setup builds the lmp target and prepends the build directory to PATH for the pytest invocation.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: outisli, njzjz

Merge Risk: Minimal · up to e5d9d

Kokkos pair styles now load the model on the CUDA device Kokkos selected instead of the node-local MPI rank. The change is small and the non-Kokkos behavior is unchanged. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(lammps): load models on the GPU selected by Kokkos" matches the main change. The diff makes deepmd/kk and dpa4spin/kk load models on the CUDA device that Kokkos selects, instead of the …
Linked Issues check Passed [#5994] requires that deepmd/kk and dpa4spin/kk load the model on the Kokkos-selected device, and that a two-GPU regression covers an explicit nonzero device for both styles. At the reviewed head, sou…
Out of Scope Changes check Passed All changed files relate to #5994. The source changes in pair_deepmd.cpp/.h, pair_deepmd_kokkos.cpp/.h, pair_dpa4spin.cpp/.h, and pair_dpa4spin_kokkos.cpp/.h add model_device() and use it for model in…
Full details: Title check

Explanation

The title "fix(lammps): load models on the GPU selected by Kokkos" matches the main change. The diff makes deepmd/kk and dpa4spin/kk load models on the CUDA device that Kokkos selects, instead of the node-local MPI rank, and adds a two-GPU regression test. The title is specific, short, and uses a conventional commit prefix. It does not list every file, which is expected.

Full details: Linked Issues check

Explanation

[#5994] requires that deepmd/kk and dpa4spin/kk load the model on the Kokkos-selected device, and that a two-GPU regression covers an explicit nonzero device for both styles. At the reviewed head, source/lmp/pair_deepmd_kokkos.cpp defines PairDeepMDKokkos::model_device(). Under KOKKOS_ENABLE_CUDA with DeviceType equal to LMPDeviceType, it returns Kokkos::Cuda().cuda_device(). Otherwise it delegates to PairDeepMD::model_device(). The PR summary states that PairDeepMD::settings and PairDPA4Spin::settings pass model_device() instead of get_node_rank() to model initialization. The PR summary also describes the matching override in pair_dpa4spin_kokkos.cpp. The issue states that the PT-experimental loader computes gpu_id = gpu_rank % device_count, so passing the selected device index loads the model on that device. This matches the accepted fix. The head test source/lmp/tests/test_kokkos_model_device.py is parametrized over deepmd/kk and dpa4spin/kk. It runs Kokkos LAMMPS with -k on g 1 d 0 and d 1. It checks the reported GPU, asserts nonzero device-0 forces, and compares energies and forces across devices. This covers the regression requirement. Limitations: the settings() call sites in pair_deepmd.cpp and pair_dpa4spin.cpp, and the loaders DeepPotPTExpt.cc and NativeSpinPTExpt.cc, were not read directly. The modulo mapping in the loader means a model_device() value at or above device_count would wrap. The Kokkos device index comes from visible devices, so this does not affect the reported case.

Full details: Out of Scope Changes check

Explanation

All changed files relate to #5994. The source changes in pair_deepmd.cpp/.h, pair_deepmd_kokkos.cpp/.h, pair_dpa4spin.cpp/.h, and pair_dpa4spin_kokkos.cpp/.h add model_device() and use it for model initialization. This implements the issue requirement. The change to source/install/test_lammps_kokkos.sh (lmp target, PATH for the device pytest, and comments) supports the new regression test. The new file source/lmp/tests/test_kokkos_model_device.py is the requested two-GPU regression. The raw summary reports no unrelated functional changes. The head file reads of pair_deepmd_kokkos.cpp and the test file match the summary. Limitations: the incremental diff from the previous reviewed heads was not read, and the remaining changed files were assessed from the PR summary only.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3a977a36-1178-4f52-8f8e-7bba395e96c2
Commits

Reviewing files that changed from the base of the PR and between 22d0c30 and e5d9d6d.

Files selected for processing: 2

  • source/install/test_lammps_kokkos.sh
  • source/lmp/tests/test_kokkos_model_device.py
Files skipped from review as they are similar to previous changes: 1

  • source/install/test_lammps_kokkos.sh

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@OutisLi OutisLi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The two model-loading paths now use the Kokkos device. The regression needs a way to actually run on the affected setup.

if os.environ.get("DPA4SPIN_KOKKOS_REQUIRED") != "1":
pytest.skip("requires the matching Kokkos LAMMPS build")
torch = pytest.importorskip("torch")
if torch.cuda.device_count() < 2:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The hosted Kokkos test step sets CUDA_VISIBLE_DEVICES: 0, so the new cases always skip there, even on a runner with two physical GPUs. Please keep the ordinary one-GPU suite unchanged, but provide a dedicated/opt-in two-visible-device invocation for this test that fails rather than skips when validation is requested. If there is no suitable hosted runner, document the invocation and attach exact-head, non-skipped two-GPU results for both styles.

@Miller10101010 Miller10101010 Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated in e5d9d6d.

Added an opt-in two-GPU mode. I ran CUDA_VISIBLE_DEVICES=0,1 DEEPMD_KOKKOS_TWO_GPUS_REQUIRED=1 python -m pytest -q source/lmp/tests/test_kokkos_model_device.py on two AISI A800s with the matching Kokkos lmp on PATH. Both deepmd/kk and dpa4spin/kk reported the requested device for GPU 0 and GPU 1, and the energies and forces matched across devices (2 passed, 0 skipped). With only one GPU visible, both cases failed as intended.

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.32%. Comparing base (ed85942) to head (e5d9d6d).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6154      +/-   ##
==========================================
- Coverage   78.09%   77.32%   -0.77%     
==========================================
  Files        1214     1219       +5     
  Lines      147782   147555     -227     
  Branches     5246     5243       -3     
==========================================
- Hits       115410   114104    -1306     
- Misses      30392    31477    +1085     
+ Partials     1980     1974       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@OutisLi
OutisLi self-requested a review November 26, 2026 02:36

@njzjz-bot njzjz-bot left a comment

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.

Python shard 8 fails all three test_kokkos_report_commands_preserve_pytest_status cases. The fixture executes only the shell tail under bash -euo pipefail, so it never runs the prologue that defines LAMMPS_BUILD_DIR, which the new PATH export reads. Please pass str(build / "lammps") in the fixture's subprocess environment and add test_kokkos_model_device.py to the stub's expected spin.xml test list. I ran the exact fixture function locally: all three cases failed before those two updates and passed after them, preserving both failure-propagation checks and the report inventory assertions. This checks the reporting harness only; the two-A800 validation is separate.

Agent: dot

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] deepmd/kk can bind Torch to a different GPU than Kokkos

3 participants