Repository: vllm-project/vllm
File: vllm/env_override.py (Python)

Code before:
# ruff: noqa: E402
def _maybe_set_cuda_compatibility_path():
    enable_cuda_compatibility = os.environ.get(
        "VLLM_ENABLE_CUDA_COMPATIBILITY", "0"
    ).strip().lower() in ("1", "true")
    if not enable_cuda_compatibility:
        return
    cuda_compat_path = os.environ.get("VLLM_CUDA_COMPATIBILITY_PATH", None)
    if not cuda_compat_path:
        return
    existing_ld_library = os.environ.get("LD_LIBRARY_PATH", None)
    if not existing_ld_library:
        ld_library_paths = []
    else:
        ld_library_paths = [
            ld_library_path
            for ld_library_path in existing_ld_library.split(":")
            if ld_library_path
        ]
    norm_cuda_compat_path = os.path.normpath(cuda_compat_path)
    if (
        ld_library_paths
        and os.path.normpath(ld_library_paths[0]) == norm_cuda_compat_path
    ):
        return
    new_ld_library_paths = [cuda_compat_path] + [
        ld_library_path
        for ld_library_path in ld_library_paths
        if os.path.normpath(ld_library_path) != norm_cuda_compat_path
    ]
    os.environ["LD_LIBRARY_PATH"] = ":".join(new_ld_library_paths)


_maybe_set_cuda_compatibility_path()

import torch

from vllm.logger import init_logger
from vllm.utils.torch_utils import is_torch_equal

logger = init_logger(__name__)

# set some common config/environment variables that should be set
# for all processes created by vllm and all processes
# that interact with vllm workers.
# they are executed whenever `import vllm` is called.

# see https://github.com/vllm-project/vllm/pull/15951
# it avoids unintentional cuda initialization from torch.cuda.is_available()
os.environ["PYTORCH_NVML_BASED_CUDA_CHECK"] = "1"


Review comment (refactor):
When adding the CUDA compatibility path to LD_LIBRARY_PATH, the original cuda_compat_path should be used instead of the normalized version. On line 35, cuda_compat_path is used, but on line 38 the normalized version is used for comparison. This inconsistency means if the user provides a path with symbolic links or relative components, the original (non-normalized) path will be added to LD_LIBRARY_PATH while the normalized version is used for duplicate detection. This could result in the same path appearing multiple times in LD_LIBRARY_PATH in different forms.
```suggestion
    new_ld_library_paths = [norm_cuda_compat_path] + [
```

Code after:
    PyTorch version must not be determined by importing directly
    because it will trigger the CUDA initialization, losing the
    chance to set the LD_LIBRARY_PATH beforehand.
    """
    try:
        spec = importlib.util.find_spec("torch")
        if not spec:
            return None
        if spec.origin:
            torch_root = os.path.dirname(spec.origin)
        elif spec.submodule_search_locations:
            torch_root = spec.submodule_search_locations[0]
        else:
            return None
        version_path = os.path.join(torch_root, "version.py")
        if not os.path.exists(version_path):
            return None
        # Load the version module without importing torch
        ver_spec = importlib.util.spec_from_file_location("torch.version", version_path)
        if not ver_spec or not ver_spec.loader:
            return None
        module = importlib.util.module_from_spec(ver_spec)
        # Avoid registering in sys.modules to not confuse future imports
        ver_spec.loader.exec_module(module)
        return getattr(module, "cuda", None)
    except Exception:
        return None


def _maybe_set_cuda_compatibility_path():
    """Set LD_LIBRARY_PATH for CUDA forward compatibility if enabled.

    Must run before 'import torch' since torch loads CUDA shared libraries
    at import time and the dynamic linker only consults LD_LIBRARY_PATH when
    a library is first loaded.

    CUDA forward compatibility is only supported on select professional and
    datacenter NVIDIA GPUs. Consumer GPUs (GeForce, RTX) do not support it
    and will get Error 803 if compat libs are loaded.
    """
    enable = os.environ.get("VLLM_ENABLE_CUDA_COMPATIBILITY", "0").strip().lower() in (
        "1",
        "true",
    )
    if not enable:
        return

    cuda_compat_path = os.environ.get("VLLM_CUDA_COMPATIBILITY_PATH", "")
    if not cuda_compat_path or not os.path.isdir(cuda_compat_path):
        conda_prefix = os.environ.get("CONDA_PREFIX", "")
        conda_compat = os.path.join(conda_prefix, "cuda-compat")