Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions source/install/test_lammps_kokkos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cmake -S "${LAMMPS_SOURCE_ROOT}/cmake" -B "${LAMMPS_BUILD_DIR}" \
-D FFT=KISS \
-D FFT_KOKKOS=KISS \
"${MPI_ARGS[@]}"
cmake --build "${LAMMPS_BUILD_DIR}" --target lammps --parallel "${BUILD_JOBS}"
cmake --build "${LAMMPS_BUILD_DIR}" --target lmp --parallel "${BUILD_JOBS}"

cmake -S "${REPO_ROOT}/source" -B "${PLUGIN_BUILD_DIR}" \
-D CMAKE_BUILD_TYPE=Release \
Expand Down Expand Up @@ -120,10 +120,13 @@ with lammps.lammps(cmdargs=["-log", "none", "-screen", "none"]) as lmp:
print(f"Verified matching LAMMPS Python and shared library: {mapped_libraries}")
PY

# For the dedicated two-GPU run, launch this script with
# CUDA_VISIBLE_DEVICES=0,1 DEEPMD_KOKKOS_TWO_GPUS_REQUIRED=1.
# Keep each invocation and each pytest process separate, including failures.
mkdir -p "${BUILD_ROOT}/test-reports"
TEST_REPORT_DIR=$(mktemp -d "${BUILD_ROOT}/test-reports/run.XXXXXX")
DPA4SPIN_KOKKOS_REQUIRED=1 python -m pytest -v -s \
PATH="${LAMMPS_BUILD_DIR}:$PATH" DPA4SPIN_KOKKOS_REQUIRED=1 python -m pytest -v -s \
"${REPO_ROOT}/source/lmp/tests/test_kokkos_model_device.py" \
"${REPO_ROOT}/source/lmp/tests/test_dpa4spin_kokkos_magnetic_force.py" \
"${REPO_ROOT}/source/lmp/tests/test_lammps_dpa4c_multiblock_kokkos.py" \
--junitxml="${TEST_REPORT_DIR}/spin.xml"
Expand Down
6 changes: 3 additions & 3 deletions source/lmp/pair_deepmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ void PairDeepMD::settings(int narg, char** arg) {
numb_models = models.size();
if (numb_models == 1) {
try {
deep_pot.init(arg[0], get_node_rank(), get_file_content(arg[0]));
deep_pot.init(arg[0], model_device(), get_file_content(arg[0]));
} catch (deepmd_compat::deepmd_exception& e) {
error->one(FLERR, e.what());
}
Expand All @@ -687,8 +687,8 @@ void PairDeepMD::settings(int narg, char** arg) {
dim_chg_spin = deep_pot.dim_chg_spin();
} else {
try {
deep_pot.init(arg[0], get_node_rank(), get_file_content(arg[0]));
deep_pot_model_devi.init(models, get_node_rank(),
deep_pot.init(arg[0], model_device(), get_file_content(arg[0]));
deep_pot_model_devi.init(models, model_device(),
get_file_content(models));
} catch (deepmd_compat::deepmd_exception& e) {
error->one(FLERR, e.what());
Expand Down
1 change: 1 addition & 0 deletions source/lmp/pair_deepmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class PairDeepMD : public PairDeepBaseModel {
double eval_energy_with_fparam(const std::vector<double>& fparam_override);

protected:
virtual int model_device() { return get_node_rank(); }
deepmd_compat::DeepPot deep_pot;
deepmd_compat::DeepPotModelDevi deep_pot_model_devi;
// Assemble the send/recv swap metadata (a comm-only neighbor list; its
Expand Down
10 changes: 10 additions & 0 deletions source/lmp/pair_deepmd_kokkos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ PairDeepMDKokkos<DeviceType>::PairDeepMDKokkos(LAMMPS* lmp)
reverse_comm_device = 1;
}

template <class DeviceType>
int PairDeepMDKokkos<DeviceType>::model_device() {
#ifdef KOKKOS_ENABLE_CUDA
if (std::is_same<DeviceType, LMPDeviceType>::value) {
return Kokkos::Cuda().cuda_device();
}
#endif
return PairDeepMD::model_device();
}

template <class DeviceType>
PairDeepMDKokkos<DeviceType>::~PairDeepMDKokkos() {
if (copymode) {
Expand Down
1 change: 1 addition & 0 deletions source/lmp/pair_deepmd_kokkos.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class PairDeepMDKokkos : public PairDeepMD, public KokkosBase {
const double* model_node_positions();

protected:
int model_device() override;
// Model node set and, for a compact canonical artifact, the graph itself.
CompactCanonicalGraphKokkos<DeviceType> compact_graph;
Kokkos::View<int*, DeviceType>
Expand Down
2 changes: 1 addition & 1 deletion source/lmp/pair_dpa4spin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void PairDPA4Spin::settings(int narg, char** arg) {
}

try {
deep_spin.init(arg[0], get_node_rank());
deep_spin.init(arg[0], model_device());
} catch (deepmd_compat::deepmd_exception& e) {
error->one(FLERR, e.what());
}
Expand Down
1 change: 1 addition & 0 deletions source/lmp/pair_dpa4spin.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class PairDPA4Spin : public Pair {
void* extract(const char*, int&) override;

protected:
virtual int model_device() { return get_node_rank(); }
void allocate();
// Emit the DeePMD-kit and LAMMPS module banner through the LAMMPS logger.
void print_summary(const std::string& pre) const;
Expand Down
10 changes: 10 additions & 0 deletions source/lmp/pair_dpa4spin_kokkos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ PairDPA4SpinKokkos<DeviceType>::PairDPA4SpinKokkos(LAMMPS* lmp)
reverse_comm_device = 1;
}

template <class DeviceType>
int PairDPA4SpinKokkos<DeviceType>::model_device() {
#ifdef KOKKOS_ENABLE_CUDA
if (std::is_same<DeviceType, LMPDeviceType>::value) {
return Kokkos::Cuda().cuda_device();
}
#endif
return PairDPA4Spin::model_device();
}

template <class DeviceType>
PairDPA4SpinKokkos<DeviceType>::~PairDPA4SpinKokkos() {
if (copymode) {
Expand Down
1 change: 1 addition & 0 deletions source/lmp/pair_dpa4spin_kokkos.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class PairDPA4SpinKokkos : public PairDPA4Spin, public KokkosBase {
void gather_moment_device();

protected:
int model_device() override;
// Model node set and the compact canonical graph over it.
CompactCanonicalGraphKokkos<DeviceType> compact_graph;
bool multi_rank; // domain-decomposed run -> extended (local+ghost) node set
Expand Down
103 changes: 103 additions & 0 deletions source/lmp/tests/test_kokkos_model_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
"""Exercise a nonzero Kokkos CUDA device from MPI rank zero."""

import json
import os
import shutil
import subprocess
import zipfile
from pathlib import (
Path,
)

import numpy as np
import pytest


@pytest.mark.parametrize(
("style", "filename"),
[
("deepmd/kk", "deeppot_dpa4c_canonical.pt2"),
("dpa4spin/kk", "deeppot_dpa4c_spin_lmp_kokkos.pt2"),
],
)
def test_explicit_cuda_device(tmp_path: Path, style: str, filename: str) -> None:
require_two = os.environ.get("DEEPMD_KOKKOS_TWO_GPUS_REQUIRED") == "1"
if not require_two and os.environ.get("DPA4SPIN_KOKKOS_REQUIRED") != "1":
pytest.skip("requires the matching Kokkos LAMMPS build")
if require_two:
import torch
else:
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.

if require_two:
pytest.fail("two visible CUDA devices are required")
pytest.skip("requires two visible CUDA devices")
executable = shutil.which("lmp")
infer = Path(__file__).resolve().parents[2] / "tests/infer"
model = infer / filename
assert executable, "Kokkos LAMMPS executable is required"
assert model.is_file(), model
with zipfile.ZipFile(model) as archive:
species = json.loads(archive.read("model/extra/metadata.json"))["type_map"][0]
spin = style == "dpa4spin/kk"
energies = []
forces = []
for device in (0, 1):
script = tmp_path / "in.lmp"
dump = tmp_path / f"forces-{device}"
script.write_text(
f"units metal\natom_style {'spin' if spin else 'atomic'}\n"
"atom_modify map yes\nregion box block 0 20 0 20 0 20 units box\n"
"create_box 1 box\ncreate_atoms 1 single 6 6 6 units box\n"
"create_atoms 1 single 7.25 6.1 6 units box\nmass * 12\n"
+ ("set atom * spin/atom 1.2 0 0 1\n" if spin else "")
+ f"pair_style {style} {model}\npair_coeff * * {species}\n"
'run 0\nprint "ENERGY $(pe:%.17g)"\n'
f"write_dump all custom {dump} id fx fy fz modify sort id format float %.17g\n"
)
run = subprocess.run(
[
executable,
"-k",
"on",
"g",
"1",
"d",
str(device),
"-sf",
"kk",
"-pk",
"kokkos",
"newton",
"on",
"neigh",
"half",
"gpu/aware",
"off",
"-log",
"none",
"-in",
str(script),
],
cwd=tmp_path,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
timeout=120,
)
assert run.returncode == 0, run.stdout
assert f"to gpu {device}" in run.stdout, run.stdout
energies.append(
float(
next(
line.split()[1]
for line in run.stdout.splitlines()
if line.startswith("ENERGY ")
)
)
)
forces.append(np.loadtxt(dump, skiprows=9)[:, 1:])
assert np.max(np.abs(forces[0])) > 0
np.testing.assert_allclose(energies[1], energies[0], atol=1e-6, rtol=1e-5)
np.testing.assert_allclose(forces[1], forces[0], atol=1e-5, rtol=1e-5)
Loading