[Redo] #33110 with threading limit (#33502)

Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
Co-authored-by: YunzhuLu <lucia.yunzhu@gmail.com>
This commit is contained in:
Cyrus Leung
2026-02-01 17:18:11 +08:00
committed by GitHub
parent 672023877b
commit 21997f45b1
2 changed files with 64 additions and 17 deletions

View File

@@ -17,6 +17,7 @@ from packaging.version import Version
from torch.library import Library, infer_schema
import vllm.envs as envs
from vllm.logger import init_logger
if TYPE_CHECKING:
from vllm.config import ModelConfig
@@ -25,9 +26,7 @@ else:
ModelConfig = object
IntermediateTensors = object
import logging
logger = logging.getLogger(__name__)
logger = init_logger(__name__)
STR_DTYPE_TO_TORCH_DTYPE = {
@@ -104,12 +103,36 @@ def set_default_torch_dtype(dtype: torch.dtype):
@contextlib.contextmanager
def set_default_torch_num_threads(num_threads: int):
"""Sets the default number of threads for PyTorch to the given value."""
def set_default_torch_num_threads(num_threads: int | None = None):
"""
Sets the default number of threads for PyTorch to the given value.
`None` means using the value of the environment variable `OMP_NUM_THREADS`
(or `1` if that is not available).
"""
if num_threads is None:
num_threads = 1
try:
num_threads = int(os.environ["OMP_NUM_THREADS"])
except KeyError:
logger.debug_once(
"OMP_NUM_THREADS is not set; defaulting Torch threads to %d.",
num_threads,
)
except ValueError:
logger.warning_once(
"OMP_NUM_THREADS is invalid; defaulting Torch threads to %d.",
num_threads,
)
old_num_threads = torch.get_num_threads()
torch.set_num_threads(num_threads)
yield
torch.set_num_threads(old_num_threads)
try:
yield
finally:
torch.set_num_threads(old_num_threads)
@contextlib.contextmanager