-
Notifications
You must be signed in to change notification settings - Fork 225
Add peer access control for DeviceMemoryResource #1289
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
Andy-Jost
wants to merge
5
commits into
NVIDIA:main
Choose a base branch
from
Andy-Jost:multi-gpu2
base: main
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.
+307
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb774e7
Ignore .cursorrules
Andy-Jost 634f931
Implement DeviceMemoryResource.peer_accessible_by
Andy-Jost 84378f4
Merge branch 'main' into multi-gpu2
Andy-Jost 3f808fa
Add a check for device accessibility in peer_accessible_by.
Andy-Jost b155df7
Merge branch 'main' into multi-gpu2
Andy-Jost 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,3 +185,6 @@ cython_debug/ | |
| # pixi environments | ||
| .pixi/* | ||
| !.pixi/config.toml | ||
|
|
||
| # Cursor | ||
| .cursorrules | ||
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 |
|---|---|---|
|
|
@@ -14,4 +14,8 @@ cdef class DeviceMemoryResource(MemoryResource): | |
| bint _mempool_owned | ||
| IPCData _ipc_data | ||
| object _attributes | ||
| object _peer_accessible_by | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will address peer access with IPC memory pools in a follow-up change. The peer access attributes are not inherited when an allocation is sent to another process via IPC, but access can be set. It will require a new test and possibly a small code change. |
||
| object __weakref__ | ||
|
|
||
|
|
||
| cpdef DMR_mempool_get_access(DeviceMemoryResource, int) | ||
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,169 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import cuda.core.experimental | ||
| import pytest | ||
| from cuda.core.experimental import Device, DeviceMemoryResource | ||
| from cuda.core.experimental._utils.cuda_utils import CUDAError | ||
| from helpers.buffers import PatternGen, compare_buffer_to_constant, make_scratch_buffer | ||
|
|
||
| NBYTES = 1024 | ||
|
|
||
|
|
||
| def _mempool_device_impl(num): | ||
| num_devices = len(cuda.core.experimental.system.devices) | ||
| if num_devices < num: | ||
| pytest.skip("Test requires at least {num} GPUs") | ||
|
|
||
| devs = [Device(i) for i in range(num)] | ||
| for i in reversed(range(num)): | ||
| devs[i].set_current() | ||
|
|
||
| if not all(devs[i].can_access_peer(j) for i in range(num) for j in range(num)): | ||
| pytest.skip("Test requires GPUs with peer access") | ||
|
|
||
| if not all(devs[i].properties.memory_pools_supported for i in range(num)): | ||
| pytest.skip("Device does not support mempool operations") | ||
|
|
||
| return devs | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mempool_device_x2(): | ||
| """Fixture that provides two devices if available, otherwise skips test.""" | ||
| return _mempool_device_impl(2) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mempool_device_x3(): | ||
| """Fixture that provides three devices if available, otherwise skips test.""" | ||
| return _mempool_device_impl(3) | ||
|
|
||
|
|
||
| def test_peer_access_basic(mempool_device_x2): | ||
| """Basic tests for dmr.peer_accessible_by.""" | ||
| dev0, dev1 = mempool_device_x2 | ||
| zero_on_dev0 = make_scratch_buffer(dev0, 0, NBYTES) | ||
| one_on_dev0 = make_scratch_buffer(dev0, 1, NBYTES) | ||
| stream_on_dev0 = dev0.create_stream() | ||
| dmr_on_dev1 = DeviceMemoryResource(dev1) | ||
| buf_on_dev1 = dmr_on_dev1.allocate(NBYTES) | ||
|
|
||
| # No access at first. | ||
| assert 0 not in dmr_on_dev1.peer_accessible_by | ||
| with pytest.raises(CUDAError, match="CUDA_ERROR_INVALID_VALUE"): | ||
| one_on_dev0.copy_to(buf_on_dev1, stream=stream_on_dev0) | ||
|
|
||
| with pytest.raises(CUDAError, match="CUDA_ERROR_INVALID_VALUE"): | ||
| zero_on_dev0.copy_from(buf_on_dev1, stream=stream_on_dev0) | ||
|
|
||
| # Allow access to device 1's allocations from device 0. | ||
| dmr_on_dev1.peer_accessible_by = [dev0] | ||
| assert 0 in dmr_on_dev1.peer_accessible_by | ||
| compare_buffer_to_constant(zero_on_dev0, 0) | ||
| one_on_dev0.copy_to(buf_on_dev1, stream=stream_on_dev0) | ||
| zero_on_dev0.copy_from(buf_on_dev1, stream=stream_on_dev0) | ||
| stream_on_dev0.sync() | ||
| compare_buffer_to_constant(zero_on_dev0, 1) | ||
|
|
||
| # Revoke access | ||
| dmr_on_dev1.peer_accessible_by = [] | ||
| assert 0 not in dmr_on_dev1.peer_accessible_by | ||
| with pytest.raises(CUDAError, match="CUDA_ERROR_INVALID_VALUE"): | ||
| one_on_dev0.copy_to(buf_on_dev1, stream=stream_on_dev0) | ||
|
|
||
| with pytest.raises(CUDAError, match="CUDA_ERROR_INVALID_VALUE"): | ||
| zero_on_dev0.copy_from(buf_on_dev1, stream=stream_on_dev0) | ||
|
|
||
|
|
||
| def test_peer_access_property_x2(mempool_device_x2): | ||
| """The the dmr.peer_accessible_by property (but not its functionality).""" | ||
| # The peer access list is a sorted tuple and always excludes the self | ||
| # device. | ||
| dev0, dev1 = mempool_device_x2 | ||
| dmr = DeviceMemoryResource(dev0) | ||
|
|
||
| def check(expected): | ||
| assert isinstance(dmr.peer_accessible_by, tuple) | ||
| assert dmr.peer_accessible_by == expected | ||
|
|
||
| # No access to begin with. | ||
| check(expected=()) | ||
| # fmt: off | ||
| dmr.peer_accessible_by = (0,) ; check(expected=()) # noqa: E702 | ||
| dmr.peer_accessible_by = (1,) ; check(expected=(1,)) # noqa: E702 | ||
| dmr.peer_accessible_by = (0, 1) ; check(expected=(1,)) # noqa: E702 | ||
| dmr.peer_accessible_by = () ; check(expected=()) # noqa: E702 | ||
| dmr.peer_accessible_by = [0, 1] ; check(expected=(1,)) # noqa: E702 | ||
| dmr.peer_accessible_by = set() ; check(expected=()) # noqa: E702 | ||
| dmr.peer_accessible_by = [1, 1, 1, 1, 1] ; check(expected=(1,)) # noqa: E702 | ||
| # fmt: on | ||
|
|
||
| with pytest.raises(ValueError, match=r"device_id must be \>\= 0"): | ||
| dmr.peer_accessible_by = [-1] # device ID out of bounds | ||
|
|
||
| num_devices = len(cuda.core.experimental.system.devices) | ||
|
|
||
| with pytest.raises(ValueError, match=r"device_id must be within \[0, \d+\)"): | ||
| dmr.peer_accessible_by = [num_devices] # device ID out of bounds | ||
|
|
||
|
|
||
| def test_peer_access_transitions(mempool_device_x3): | ||
| """Advanced tests for dmr.peer_accessible_by.""" | ||
|
|
||
| # Check all transitions between peer access states. The implementation | ||
| # performs transactions that add or remove access as needed. This test | ||
| # ensures that that is working as expected. | ||
|
|
||
| # Doing everything from the point-of-view of device 0, there are four | ||
| # access states: | ||
| # | ||
| # [(), (1,), (2,), (1, 2)] | ||
| # | ||
| # and 4^2-4 = 12 non-identity transitions. | ||
|
|
||
| devs = mempool_device_x3 # Three devices | ||
|
|
||
| # Allocate per-device resources. | ||
| streams = [dev.create_stream() for dev in devs] | ||
| pgens = [PatternGen(devs[i], NBYTES, streams[i]) for i in range(3)] | ||
| dmrs = [DeviceMemoryResource(dev) for dev in devs] | ||
| bufs = [dmr.allocate(NBYTES) for dmr in dmrs] | ||
|
|
||
| def verify_state(state, pattern_seed): | ||
| """ | ||
| Verify an access state from the POV of device 0. E.g., (1,) means | ||
| device 1 has access but device 2 does not. | ||
| """ | ||
| # Populate device 0's buffer with a new pattern. | ||
| devs[0].set_current() | ||
| pgens[0].fill_buffer(bufs[0], seed=pattern_seed) | ||
| streams[0].sync() | ||
|
|
||
| for peer in [1, 2]: | ||
| devs[peer].set_current() | ||
| if peer in state: | ||
| # Peer device has access to 0's allocation | ||
| bufs[peer].copy_from(bufs[0], stream=streams[peer]) | ||
| # Check the result on the peer device. | ||
| pgens[peer].verify_buffer(bufs[peer], seed=pattern_seed) | ||
| else: | ||
| # Peer device has no access to 0's allocation | ||
| with pytest.raises(CUDAError, match="CUDA_ERROR_INVALID_VALUE"): | ||
| bufs[peer].copy_from(bufs[0], stream=streams[peer]) | ||
|
|
||
| # For each transition, set the access state before and after, checking for | ||
| # the expected peer access capabilities at each stop. | ||
| pattern_seed = 0 | ||
| states = [(), (1,), (2,), (1, 2)] | ||
| transitions = [(s0, s1) for s0 in states for s1 in states if s0 != s1] | ||
| for init_state, final_state in transitions: | ||
| dmrs[0].peer_accessible_by = init_state | ||
| assert dmrs[0].peer_accessible_by == init_state | ||
| verify_state(init_state, pattern_seed) | ||
| pattern_seed += 1 | ||
|
|
||
| dmrs[0].peer_accessible_by = final_state | ||
| assert dmrs[0].peer_accessible_by == final_state | ||
| verify_state(final_state, pattern_seed) | ||
| pattern_seed += 1 |
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.
Uh oh!
There was an error while loading. Please reload this page.