Skip to content

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

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

Miller10101010 wants to merge 2 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
    • Corrected GPU selection for Kokkos-based DeepMD and DPA4Spin models, improving device assignment when running across multiple CUDA GPUs.
  • Tests
    • Added multi-GPU checks for model execution, GPU assignment, and consistency of energies and forces.
    • Updated the spin-test setup to run with the built LAMMPS executable and confirm it is available during testing.
  • Build
    • Updated the Kokkos build to request the appropriate 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 spin-test pytest invocation.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: bug, C++, Python

Suggested reviewers: njzjz, outisli

Merge Risk: Minimal · up to 22d0c

This change makes the Kokkos CUDA pair styles load the model on the GPU that Kokkos actually selected, instead of the node-local MPI rank's GPU. Other pair styles behave as before. No actionable merge-blocking risk was found, and a two-GPU integration test covers both Kokkos styles.

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 and DPA4Spin models initialize on the device Kokkos selects (via a new model_device() …
Linked Issues check Passed Issue #5994 requires that deepmd/kk and dpa4spin/kk load the model on the Kokkos-selected device. It also requires a two-GPU regression with an explicit nonzero Kokkos device for both styles. At the r…
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 and DPA4Spin models initialize on the device Kokkos selects (via a new model_device() hook) instead of the node-local MPI rank's device. The title is short, specific, and names the affected component (lammps) and the behavior change. It does not name the dpa4spin/kk path explicitly, but that is a minor omission.

Full details: Linked Issues check

Explanation

Issue #5994 requires that deepmd/kk and dpa4spin/kk load the model on the Kokkos-selected device. It also requires a two-GPU regression with an explicit nonzero Kokkos 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(). 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 PT-experimental loading computes gpu_id = gpu_rank % device_count. Passing the selected device index therefore loads the model on that device, which matches the accepted fix. The new test test_kokkos_model_device.py covers both styles on devices 0 and 1 and checks the device report, nonzero forces, and cross-device energy and force agreement. Limitations: the incremental diff from the previous reviewed head could not be read, so changes since the prior review were checked only through head file reads. The settings() call sites and DeepPotPTExpt.cc / NativeSpinPTExpt.cc were not read directly. The modulo mapping means a model_device() value at or above device_count would wrap. The Kokkos device 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, which implements the issue. The change to source/install/test_lammps_kokkos.sh (lmp target and PATH for the spin pytest) supports the new regression test. The new file test_kokkos_model_device.py is the requested two-GPU regression. The summary reports no unrelated functional changes. The incremental diff could not be read, so the assessment relies on the PR summary and head file reads.

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: 341381e2-2530-4090-98c3-8b31d78a2bba
Commits

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

Files selected for processing: 1

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

  • source/lmp/tests/test_kokkos_model_device.py

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

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

1 participant