Repository navigation
fix(lammps): load models on the GPU selected by Kokkos #6154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Miller10101010
wants to merge
3
commits into
deepmodeling:master
Choose a base branch
from
Miller10101010:fix-kokkos-model-device
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
| 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) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.pyon two AISI A800s with the matching Kokkoslmpon PATH. Bothdeepmd/kkanddpa4spin/kkreported 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.