-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
[Hardware][NVIDIA] Add non-NVML CUDA mode for Jetson #9735
Merged
youkaichao
merged 12 commits into
vllm-project:main
from
conroy-cheers:fix-nvml-jetson-support
Nov 26, 2024
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1e7423c
Add non-NVML CUDA backend for Jetson
conroy-cheers a87ebad
add CUDA arch 8.7 for Jetson Orin
conroy-cheers e4f470a
Format
conroy-cheers 17aadd2
fix ImportError when pynvml not available
conroy-cheers d0ca846
Remove nvml-specific exception check in platform determination
conroy-cheers aaba9ee
Unify jetson and generic cuda platforms
conroy-cheers 9700cef
revert accidental changes
conroy-cheers 2a5ea0c
refactor out NonNVMLContext for cuda.py
conroy-cheers d231445
Merge branch 'main' into fix-nvml-jetson-support
conroy-cheers 931dddd
CudaPlatform: roll context abstraction into platforms
conroy-cheers ef3253a
Merge remote-tracking branch 'upstream/main' into fix-nvml-jetson-sup…
conroy-cheers 77daa0f
NvmlCudaPlatform: remove some internal methods
conroy-cheers 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 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 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 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 |
---|---|---|
|
@@ -3,8 +3,10 @@ | |
""" | ||
|
||
import os | ||
from functools import lru_cache, wraps | ||
from typing import Callable, List, Tuple, TypeVar | ||
from collections.abc import Iterator | ||
from contextlib import contextmanager | ||
from functools import lru_cache | ||
from typing import List, Tuple, TypeVar | ||
|
||
import pynvml | ||
import torch | ||
|
@@ -31,67 +33,6 @@ | |
# see https://github.com/huggingface/diffusers/issues/9704 for details | ||
torch.backends.cuda.enable_cudnn_sdp(False) | ||
|
||
# NVML utils | ||
# Note that NVML is not affected by `CUDA_VISIBLE_DEVICES`, | ||
# all the related functions work on real physical device ids. | ||
# the major benefit of using NVML is that it will not initialize CUDA | ||
|
||
|
||
def with_nvml_context(fn: Callable[_P, _R]) -> Callable[_P, _R]: | ||
|
||
@wraps(fn) | ||
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R: | ||
pynvml.nvmlInit() | ||
try: | ||
return fn(*args, **kwargs) | ||
finally: | ||
pynvml.nvmlShutdown() | ||
|
||
return wrapper | ||
|
||
|
||
@lru_cache(maxsize=8) | ||
@with_nvml_context | ||
def get_physical_device_capability(device_id: int = 0) -> Tuple[int, int]: | ||
handle = pynvml.nvmlDeviceGetHandleByIndex(device_id) | ||
return pynvml.nvmlDeviceGetCudaComputeCapability(handle) | ||
|
||
|
||
@lru_cache(maxsize=8) | ||
@with_nvml_context | ||
def get_physical_device_name(device_id: int = 0) -> str: | ||
handle = pynvml.nvmlDeviceGetHandleByIndex(device_id) | ||
return pynvml.nvmlDeviceGetName(handle) | ||
|
||
|
||
@lru_cache(maxsize=8) | ||
@with_nvml_context | ||
def get_physical_device_total_memory(device_id: int = 0) -> int: | ||
handle = pynvml.nvmlDeviceGetHandleByIndex(device_id) | ||
return int(pynvml.nvmlDeviceGetMemoryInfo(handle).total) | ||
|
||
|
||
@with_nvml_context | ||
def warn_if_different_devices(): | ||
device_ids: int = pynvml.nvmlDeviceGetCount() | ||
if device_ids > 1: | ||
device_names = [get_physical_device_name(i) for i in range(device_ids)] | ||
if len(set(device_names)) > 1 and os.environ.get( | ||
"CUDA_DEVICE_ORDER") != "PCI_BUS_ID": | ||
logger.warning( | ||
"Detected different devices in the system: \n%s\nPlease" | ||
" make sure to set `CUDA_DEVICE_ORDER=PCI_BUS_ID` to " | ||
"avoid unexpected behavior.", "\n".join(device_names)) | ||
|
||
|
||
try: | ||
from sphinx.ext.autodoc.mock import _MockModule | ||
|
||
if not isinstance(pynvml, _MockModule): | ||
warn_if_different_devices() | ||
except ModuleNotFoundError: | ||
warn_if_different_devices() | ||
|
||
|
||
def device_id_to_physical_device_id(device_id: int) -> int: | ||
if "CUDA_VISIBLE_DEVICES" in os.environ: | ||
|
@@ -105,27 +46,51 @@ def device_id_to_physical_device_id(device_id: int) -> int: | |
return device_id | ||
|
||
|
||
class CudaPlatform(Platform): | ||
_enum = PlatformEnum.CUDA | ||
class BaseContext: | ||
|
||
@classmethod | ||
def get_device_capability(cls, device_id: int = 0) -> DeviceCapability: | ||
def get_device_capability(cls, device_id: int = 0) -> Tuple[int, int]: | ||
raise NotImplementedError | ||
|
||
@classmethod | ||
def get_device_name(cls, device_id: int = 0) -> str: | ||
raise NotImplementedError | ||
|
||
@classmethod | ||
def get_device_total_memory(cls, device_id: int = 0) -> int: | ||
raise NotImplementedError | ||
|
||
@classmethod | ||
def is_full_nvlink(cls, device_ids: List[int]) -> bool: | ||
raise NotImplementedError | ||
|
||
@classmethod | ||
def log_warnings(cls): | ||
pass | ||
|
||
|
||
# NVML utils | ||
# Note that NVML is not affected by `CUDA_VISIBLE_DEVICES`, | ||
# all the related functions work on real physical device ids. | ||
# the major benefit of using NVML is that it will not initialize CUDA | ||
class NVMLContext(BaseContext): | ||
|
||
@classmethod | ||
def get_device_capability(cls, device_id: int = 0) -> Tuple[int, int]: | ||
physical_device_id = device_id_to_physical_device_id(device_id) | ||
major, minor = get_physical_device_capability(physical_device_id) | ||
return DeviceCapability(major=major, minor=minor) | ||
return cls._get_physical_device_capability(physical_device_id) | ||
|
||
@classmethod | ||
def get_device_name(cls, device_id: int = 0) -> str: | ||
physical_device_id = device_id_to_physical_device_id(device_id) | ||
return get_physical_device_name(physical_device_id) | ||
return cls._get_physical_device_name(physical_device_id) | ||
|
||
@classmethod | ||
def get_device_total_memory(cls, device_id: int = 0) -> int: | ||
physical_device_id = device_id_to_physical_device_id(device_id) | ||
return get_physical_device_total_memory(physical_device_id) | ||
return cls._get_physical_device_total_memory(physical_device_id) | ||
|
||
@classmethod | ||
@with_nvml_context | ||
def is_full_nvlink(cls, physical_device_ids: List[int]) -> bool: | ||
""" | ||
query if the set of gpus are fully connected by nvlink (1 hop) | ||
|
@@ -144,7 +109,118 @@ def is_full_nvlink(cls, physical_device_ids: List[int]) -> bool: | |
return False | ||
except pynvml.NVMLError: | ||
logger.exception( | ||
"NVLink detection failed. This is normal if your" | ||
" machine has no NVLink equipped.") | ||
"NVLink detection failed. This is normal if" | ||
" your machine has no NVLink equipped.") | ||
return False | ||
return True | ||
|
||
@classmethod | ||
@lru_cache(maxsize=8) | ||
def _get_physical_device_capability(cls, | ||
device_id: int = 0) -> Tuple[int, int]: | ||
handle = pynvml.nvmlDeviceGetHandleByIndex(device_id) | ||
return pynvml.nvmlDeviceGetCudaComputeCapability(handle) | ||
|
||
@classmethod | ||
@lru_cache(maxsize=8) | ||
def _get_physical_device_name(cls, device_id: int = 0) -> str: | ||
handle = pynvml.nvmlDeviceGetHandleByIndex(device_id) | ||
return pynvml.nvmlDeviceGetName(handle) | ||
|
||
@classmethod | ||
@lru_cache(maxsize=8) | ||
def _get_physical_device_total_memory(cls, device_id: int = 0) -> int: | ||
handle = pynvml.nvmlDeviceGetHandleByIndex(device_id) | ||
return int(pynvml.nvmlDeviceGetMemoryInfo(handle).total) | ||
|
||
@classmethod | ||
def log_warnings(cls): | ||
device_ids: int = pynvml.nvmlDeviceGetCount() | ||
if device_ids > 1: | ||
device_names = [ | ||
cls._get_physical_device_name(i) for i in range(device_ids) | ||
] | ||
if len(set(device_names)) > 1 and os.environ.get( | ||
"CUDA_DEVICE_ORDER") != "PCI_BUS_ID": | ||
logger.warning( | ||
"Detected different devices in the system: \n%s\nPlease" | ||
" make sure to set `CUDA_DEVICE_ORDER=PCI_BUS_ID` to " | ||
"avoid unexpected behavior.", "\n".join(device_names)) | ||
|
||
|
||
class NonNVMLContext(BaseContext): | ||
|
||
@classmethod | ||
def get_device_capability(cls, device_id: int = 0) -> Tuple[int, int]: | ||
return torch.cuda.get_device_capability(device_id) | ||
|
||
@classmethod | ||
def get_device_name(cls, device_id: int = 0) -> str: | ||
return torch.cuda.get_device_name(device_id) | ||
|
||
@classmethod | ||
def get_device_total_memory(cls, device_id: int = 0) -> int: | ||
device_props = torch.cuda.get_device_properties(device_id) | ||
return device_props.total_memory | ||
|
||
@classmethod | ||
def is_full_nvlink(cls, physical_device_ids: List[int]) -> bool: | ||
logger.exception( | ||
"NVLink detection not possible, as context support was" | ||
" not found. Assuming no NVLink available.") | ||
return False | ||
|
||
|
||
@contextmanager | ||
def get_context() -> Iterator[BaseContext]: | ||
nvml_init_ok = False | ||
try: | ||
try: | ||
pynvml.nvmlInit() | ||
nvml_init_ok = True | ||
yield NVMLContext() | ||
except Exception: | ||
# On Jetson, NVML is not supported. | ||
yield NonNVMLContext() | ||
finally: | ||
if nvml_init_ok: | ||
pynvml.nvmlShutdown() | ||
|
||
|
||
try: | ||
from sphinx.ext.autodoc.mock import _MockModule | ||
|
||
if not isinstance(pynvml, _MockModule): | ||
with get_context() as context: | ||
context.log_warnings() | ||
except ModuleNotFoundError: | ||
with get_context() as context: | ||
context.log_warnings() | ||
|
||
|
||
class CudaPlatform(Platform): | ||
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. we can have |
||
_enum = PlatformEnum.CUDA | ||
|
||
@classmethod | ||
def get_device_capability(cls, device_id: int = 0) -> DeviceCapability: | ||
with get_context() as context: | ||
major, minor = context.get_device_capability(device_id) | ||
return DeviceCapability(major=major, minor=minor) | ||
|
||
@classmethod | ||
def get_device_name(cls, device_id: int = 0) -> str: | ||
with get_context() as context: | ||
return context.get_device_name(device_id) | ||
|
||
@classmethod | ||
def get_device_total_memory(cls, device_id: int = 0) -> int: | ||
with get_context() as context: | ||
return context.get_device_total_memory(device_id) | ||
|
||
@classmethod | ||
def is_full_nvlink(cls, physical_device_ids: List[int]) -> bool: | ||
""" | ||
query if the set of gpus are fully connected by nvlink (1 hop) | ||
""" | ||
with get_context() as context: | ||
return context.is_full_nvlink(physical_device_ids) |
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.
I need to check with nvidia folks, how robust it is.
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 check came from this thread:
rapidsai/dask-cuda#400 (comment)