[core] port pynvml into vllm codebase (#12963)

Signed-off-by: youkaichao <youkaichao@gmail.com>
This commit is contained in:
youkaichao 2025-02-09 15:00:00 +08:00 committed by GitHub
parent 24700c346b
commit cf797aa856
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 6169 additions and 35 deletions

View File

@ -8,25 +8,28 @@ repos:
- id: yapf - id: yapf
args: [--in-place, --verbose] args: [--in-place, --verbose]
additional_dependencies: [toml] # TODO: Remove when yapf is upgraded additional_dependencies: [toml] # TODO: Remove when yapf is upgraded
exclude: 'vllm/third_party/.*'
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.3 rev: v0.9.3
hooks: hooks:
- id: ruff - id: ruff
args: [--output-format, github] args: [--output-format, github]
exclude: 'vllm/third_party/.*'
- repo: https://github.com/codespell-project/codespell - repo: https://github.com/codespell-project/codespell
rev: v2.4.0 rev: v2.4.0
hooks: hooks:
- id: codespell - id: codespell
exclude: 'benchmarks/sonnet.txt|(build|tests/(lora/data|models/fixtures|prompts))/.*' exclude: 'benchmarks/sonnet.txt|(build|tests/(lora/data|models/fixtures|prompts))/.*|vllm/third_party/.*'
- repo: https://github.com/PyCQA/isort - repo: https://github.com/PyCQA/isort
rev: 5.13.2 rev: 5.13.2
hooks: hooks:
- id: isort - id: isort
exclude: 'vllm/third_party/.*'
- repo: https://github.com/pre-commit/mirrors-clang-format - repo: https://github.com/pre-commit/mirrors-clang-format
rev: v19.1.7 rev: v19.1.7
hooks: hooks:
- id: clang-format - id: clang-format
exclude: 'csrc/(moe/topk_softmax_kernels.cu|quantization/gguf/(ggml-common.h|dequantize.cuh|vecdotq.cuh|mmq.cuh|mmvq.cuh))' exclude: 'csrc/(moe/topk_softmax_kernels.cu|quantization/gguf/(ggml-common.h|dequantize.cuh|vecdotq.cuh|mmq.cuh|mmvq.cuh))|vllm/third_party/.*'
types_or: [c++, cuda] types_or: [c++, cuda]
args: [--style=file, --verbose] args: [--style=file, --verbose]
- repo: https://github.com/jackdewinter/pymarkdown - repo: https://github.com/jackdewinter/pymarkdown
@ -34,10 +37,12 @@ repos:
hooks: hooks:
- id: pymarkdown - id: pymarkdown
args: [fix] args: [fix]
exclude: 'vllm/third_party/.*'
- repo: https://github.com/rhysd/actionlint - repo: https://github.com/rhysd/actionlint
rev: v1.7.7 rev: v1.7.7
hooks: hooks:
- id: actionlint - id: actionlint
exclude: 'vllm/third_party/.*'
- repo: local - repo: local
hooks: hooks:
- id: mypy-local - id: mypy-local
@ -47,6 +52,7 @@ repos:
types: [python] types: [python]
additional_dependencies: &mypy_deps [mypy==1.11.1, types-setuptools, types-PyYAML, types-requests] additional_dependencies: &mypy_deps [mypy==1.11.1, types-setuptools, types-PyYAML, types-requests]
stages: [pre-commit] # Don't run in CI stages: [pre-commit] # Don't run in CI
exclude: 'vllm/third_party/.*'
- id: mypy-3.9 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward - id: mypy-3.9 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward
name: Run mypy for Python 3.9 name: Run mypy for Python 3.9
entry: tools/mypy.sh 1 "3.9" entry: tools/mypy.sh 1 "3.9"
@ -54,6 +60,7 @@ repos:
types: [python] types: [python]
additional_dependencies: *mypy_deps additional_dependencies: *mypy_deps
stages: [manual] # Only run in CI stages: [manual] # Only run in CI
exclude: 'vllm/third_party/.*'
- id: mypy-3.10 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward - id: mypy-3.10 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward
name: Run mypy for Python 3.10 name: Run mypy for Python 3.10
entry: tools/mypy.sh 1 "3.10" entry: tools/mypy.sh 1 "3.10"
@ -61,6 +68,7 @@ repos:
types: [python] types: [python]
additional_dependencies: *mypy_deps additional_dependencies: *mypy_deps
stages: [manual] # Only run in CI stages: [manual] # Only run in CI
exclude: 'vllm/third_party/.*'
- id: mypy-3.11 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward - id: mypy-3.11 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward
name: Run mypy for Python 3.11 name: Run mypy for Python 3.11
entry: tools/mypy.sh 1 "3.11" entry: tools/mypy.sh 1 "3.11"
@ -68,6 +76,7 @@ repos:
types: [python] types: [python]
additional_dependencies: *mypy_deps additional_dependencies: *mypy_deps
stages: [manual] # Only run in CI stages: [manual] # Only run in CI
exclude: 'vllm/third_party/.*'
- id: mypy-3.12 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward - id: mypy-3.12 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward
name: Run mypy for Python 3.12 name: Run mypy for Python 3.12
entry: tools/mypy.sh 1 "3.12" entry: tools/mypy.sh 1 "3.12"
@ -75,16 +84,19 @@ repos:
types: [python] types: [python]
additional_dependencies: *mypy_deps additional_dependencies: *mypy_deps
stages: [manual] # Only run in CI stages: [manual] # Only run in CI
exclude: 'vllm/third_party/.*'
- id: shellcheck - id: shellcheck
name: Lint shell scripts name: Lint shell scripts
entry: tools/shellcheck.sh entry: tools/shellcheck.sh
language: script language: script
types: [shell] types: [shell]
exclude: 'vllm/third_party/.*'
- id: png-lint - id: png-lint
name: Lint PNG exports from excalidraw name: Lint PNG exports from excalidraw
entry: tools/png-lint.sh entry: tools/png-lint.sh
language: script language: script
types: [png] types: [png]
exclude: 'vllm/third_party/.*'
- id: signoff-commit - id: signoff-commit
name: Sign-off Commit name: Sign-off Commit
entry: bash entry: bash
@ -97,17 +109,20 @@ repos:
language: system language: system
verbose: true verbose: true
stages: [commit-msg] stages: [commit-msg]
exclude: 'vllm/third_party/.*'
- id: check-spdx-header - id: check-spdx-header
name: Check SPDX headers name: Check SPDX headers
entry: python tools/check_spdx_header.py entry: python tools/check_spdx_header.py
language: python language: python
types: [python] types: [python]
exclude: 'vllm/third_party/.*'
- id: suggestion - id: suggestion
name: Suggestion name: Suggestion
entry: bash -c 'echo "To bypass pre-commit hooks, add --no-verify to git commit."' entry: bash -c 'echo "To bypass pre-commit hooks, add --no-verify to git commit."'
language: system language: system
verbose: true verbose: true
pass_filenames: false pass_filenames: false
exclude: 'vllm/third_party/.*'
- id: check-filenames - id: check-filenames
name: Check for spaces in all filenames name: Check for spaces in all filenames
entry: bash entry: bash
@ -117,3 +132,4 @@ repos:
language: system language: system
always_run: true always_run: true
pass_filenames: false pass_filenames: false
exclude: 'vllm/third_party/.*'

View File

@ -3,7 +3,6 @@
# Dependencies for NVIDIA GPUs # Dependencies for NVIDIA GPUs
ray[default] >= 2.9 ray[default] >= 2.9
nvidia-ml-py >= 12.560.30 # for pynvml package
torch == 2.5.1 torch == 2.5.1
torchaudio==2.5.1 torchaudio==2.5.1
# These must be updated alongside torch # These must be updated alongside torch

View File

@ -46,8 +46,9 @@ if current_platform.is_rocm():
finally: finally:
amdsmi_shut_down() amdsmi_shut_down()
elif current_platform.is_cuda(): elif current_platform.is_cuda():
from pynvml import (nvmlDeviceGetHandleByIndex, nvmlDeviceGetMemoryInfo, from vllm.third_party.pynvml import (nvmlDeviceGetHandleByIndex,
nvmlInit, nvmlShutdown) nvmlDeviceGetMemoryInfo, nvmlInit,
nvmlShutdown)
@contextmanager @contextmanager
def _nvml(): def _nvml():

0
vllm/third_party/__init__.py vendored Normal file
View File

6139
vllm/third_party/pynvml.py vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2239,34 +2239,13 @@ def import_pynvml():
This causes errors when both of them are installed. This causes errors when both of them are installed.
Starting from version 12.0, it migrates to a new module Starting from version 12.0, it migrates to a new module
named `pynvml_utils` to avoid the conflict. named `pynvml_utils` to avoid the conflict.
It is so confusing that many packages in the community use the
TL;DR: if users have pynvml<12.0 installed, it will cause problems. unofficial one by mistake, and we have to handle this case.
Otherwise, `import pynvml` will import the correct module. For example, `nvcr.io/nvidia/pytorch:24.12-py3` uses the unofficial
We take the safest approach here, to manually import the correct one, and it will cause errors, see the issue
`pynvml.py` module from the `nvidia-ml-py` package. https://github.com/vllm-project/vllm/issues/12847 for example.
After all the troubles, we decide to copy the official `pynvml`
module to our codebase, and use it directly.
""" """
if TYPE_CHECKING: import vllm.third_party.pynvml as pynvml
import pynvml return pynvml
return pynvml
if "pynvml" in sys.modules:
import pynvml
if pynvml.__file__.endswith("__init__.py"):
# this is pynvml < 12.0
raise RuntimeError(
"You are using a deprecated `pynvml` package. "
"Please uninstall `pynvml` or upgrade to at least"
" version 12.0. See https://pypi.org/project/pynvml "
"for more information.")
return sys.modules["pynvml"]
import importlib.util
import os
import site
for site_dir in site.getsitepackages():
pynvml_path = os.path.join(site_dir, "pynvml.py")
if os.path.exists(pynvml_path):
spec = importlib.util.spec_from_file_location(
"pynvml", pynvml_path)
pynvml = importlib.util.module_from_spec(spec)
sys.modules["pynvml"] = pynvml
spec.loader.exec_module(pynvml)
return pynvml