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

Code before:
    if not enable_cuda_compatibility:
        return
    cuda_compat_path = os.environ.get("VLLM_CUDA_COMPATIBILITY_PATH", None)
    if not cuda_compat_path or not os.path.isdir(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 = [norm_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"

# see https://github.com/vllm-project/vllm/issues/10480
os.environ["TORCHINDUCTOR_COMPILE_THREADS"] = "1"
# see https://github.com/vllm-project/vllm/issues/10619
torch._inductor.config.compile_threads = 1


Review comment (refactor):
When CUDA compatibility is enabled, this code rewrites `LD_LIBRARY_PATH` by splitting on `":"` and filtering out empty segments. Empty segments in `LD_LIBRARY_PATH` have meaning (current directory), so this can unintentionally change runtime library resolution. Consider using `os.pathsep` and preserving empty segments (or explicitly documenting that they will be removed) while still deduplicating the compat path.
```suggestion
        # Preserve empty segments (which mean current directory) and use
        # os.pathsep instead of a hard-coded ":".
        ld_library_paths = existing_ld_library.split(os.pathsep)
    norm_cuda_compat_path = os.path.normpath(cuda_compat_path)
    # Check whether the first non-empty path is already the compat path.
    first_non_empty = next(
        (p for p in ld_library_paths if p),
        None,
    )
    if first_non_empty and os.path.normpath(first_non_empty) == norm_cuda_compat_path:
        return
    # Prepend the compat path and remove duplicate occurrences of it,
    # while preserving empty segments (current directory) and order.
    new_ld_library_paths = [norm_cuda_compat_path]
    for ld_library_path in ld_library_paths:
        if ld_library_path and os.path.normpath(ld_library_path) == norm_cuda_compat_path:
            continue
        new_ld_library_paths.append(ld_library_path)
    os.environ["LD_LIBRARY_PATH"] = os.pathsep.join(new_ld_library_paths)
```

Code after:
        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")
        if conda_prefix and os.path.isdir(conda_compat):
            cuda_compat_path = conda_compat
    if not cuda_compat_path or not os.path.isdir(cuda_compat_path):
        torch_cuda_version = _get_torch_cuda_version()
        if torch_cuda_version: