-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
[Bugfix][Ray] Set the cuda context eagerly in the ray worker #19583
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5c7d8b2
[Spec Decode][Benchmark] Generalize spec decode offline benchmark to …
ekagra-ranjan c2d4e98
wip
kouroshHakha 395b8d5
wip
kouroshHakha 6d6b26e
clean up
kouroshHakha 749903a
tests
kouroshHakha fa6465d
wip
kouroshHakha ef6c1c8
wip
kouroshHakha 4d9b41a
Update vllm/platforms/cuda.py
kouroshHakha 3541666
Merge branch 'main' of https://github.com/vllm-project/vllm into kh/f…
kouroshHakha 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
||
import ctypes | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
import pytest | ||
import torch | ||
|
||
from vllm.platforms import current_platform | ||
|
||
|
||
def check_cuda_context(): | ||
"""Check CUDA driver context status""" | ||
try: | ||
cuda = ctypes.CDLL('libcuda.so') | ||
device = ctypes.c_int() | ||
result = cuda.cuCtxGetDevice(ctypes.byref(device)) | ||
return (True, device.value) if result == 0 else (False, None) | ||
except Exception: | ||
return False, None | ||
|
||
|
||
def run_cuda_test_in_thread(device_input, expected_device_id): | ||
"""Run CUDA context test in separate thread for isolation""" | ||
try: | ||
# New thread should have no CUDA context initially | ||
valid_before, device_before = check_cuda_context() | ||
if valid_before: | ||
return False, \ | ||
"CUDA context should not exist in new thread, " \ | ||
f"got device {device_before}" | ||
|
||
# Test setting CUDA context | ||
current_platform.set_device(device_input) | ||
|
||
# Verify context is created correctly | ||
valid_after, device_id = check_cuda_context() | ||
if not valid_after: | ||
return False, "CUDA context should be valid after set_cuda_context" | ||
if device_id != expected_device_id: | ||
return False, \ | ||
f"Expected device {expected_device_id}, got {device_id}" | ||
|
||
return True, "Success" | ||
except Exception as e: | ||
return False, f"Exception in thread: {str(e)}" | ||
|
||
|
||
class TestSetCudaContext: | ||
"""Test suite for the set_cuda_context function.""" | ||
|
||
@pytest.mark.skipif(not current_platform.is_cuda(), | ||
reason="CUDA not available") | ||
@pytest.mark.parametrize(argnames="device_input,expected_device_id", | ||
argvalues=[ | ||
(0, 0), | ||
(torch.device('cuda:0'), 0), | ||
('cuda:0', 0), | ||
], | ||
ids=["int", "torch_device", "string"]) | ||
def test_set_cuda_context_parametrized(self, device_input, | ||
expected_device_id): | ||
"""Test setting CUDA context in isolated threads.""" | ||
with ThreadPoolExecutor(max_workers=1) as executor: | ||
future = executor.submit(run_cuda_test_in_thread, device_input, | ||
expected_device_id) | ||
success, message = future.result(timeout=30) | ||
assert success, message | ||
|
||
@pytest.mark.skipif(not current_platform.is_cuda(), | ||
reason="CUDA not available") | ||
def test_set_cuda_context_invalid_device_type(self): | ||
"""Test error handling for invalid device type.""" | ||
with pytest.raises(ValueError, match="Expected a cuda device"): | ||
current_platform.set_device(torch.device('cpu')) | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__, "-v"]) |
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 |
---|---|---|
|
@@ -298,6 +298,13 @@ def seed_everything(cls, seed: Optional[int] = None) -> None: | |
np.random.seed(seed) | ||
torch.manual_seed(seed) | ||
|
||
@classmethod | ||
def set_device(cls, device: torch.device) -> None: | ||
""" | ||
Set the device for the current platform. | ||
""" | ||
torch.cuda.set_device(device) | ||
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. should be |
||
|
||
@classmethod | ||
def pre_register_and_update(cls, | ||
parser: Optional[FlexibleArgumentParser] = None | ||
|
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.
use
torch._C._cuda_hasPrimaryContext(int device)
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.
Not sure how this API capture what we are tying to do here.
torch._C._cuda_hasPrimaryContext(0)
returns true even if the background thread is created. The current method returns false which is consistent with the problem that I was trying to solve.