collect_env.py 34.3 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
# ruff: noqa
5
6
7
8
# code borrowed from https://github.com/pytorch/pytorch/blob/main/torch/utils/collect_env.py

import datetime
import locale
9
import os
10
11
import subprocess
import sys
12

13
14
15
# Unlike the rest of the PyTorch this file must be python2 compliant.
# This script outputs relevant system environment info
# Run it with `python collect_env.py` or `python -m torch.utils.collect_env`
16
17
from collections import namedtuple

18
19
import regex as re

20
21
from vllm.envs import environment_variables

22
23
try:
    import torch
24

25
26
27
28
29
    TORCH_AVAILABLE = True
except (ImportError, NameError, AttributeError, OSError):
    TORCH_AVAILABLE = False

# System Environment Information
30
SystemEnv = namedtuple(
31
    "SystemEnv",
32
    [
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        "torch_version",
        "is_debug_build",
        "cuda_compiled_version",
        "gcc_version",
        "clang_version",
        "cmake_version",
        "os",
        "libc_version",
        "python_version",
        "python_platform",
        "is_cuda_available",
        "cuda_runtime_version",
        "cuda_module_loading",
        "nvidia_driver_version",
        "nvidia_gpu_models",
        "cudnn_version",
49
50
51
52
53
54
55
56
57
58
59
        "xpu_available",
        "xpu_runtime_version",
        "intel_graphics_compiler_version",
        "intel_gpu_models",
        "oneapi_compiler_version",
        "level_zero_loader_version",
        "level_zero_driver_version",
        "oneccl_version",
        "libigdgmm_version",
        "vllm_xpu_kernels_version",
        "sycl_version",
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        "pip_version",  # 'pip' or 'pip3'
        "pip_packages",
        "conda_packages",
        "hip_compiled_version",
        "hip_runtime_version",
        "miopen_runtime_version",
        "caching_allocator_config",
        "is_xnnpack_available",
        "cpu_info",
        "rocm_version",  # vllm specific field
        "vllm_version",  # vllm specific field
        "vllm_build_flags",  # vllm specific field
        "gpu_topo",  # vllm specific field
        "env_vars",
    ],
)
76
77
78
79
80
81
82
83
84
85

DEFAULT_CONDA_PATTERNS = {
    "torch",
    "numpy",
    "cudatoolkit",
    "soumith",
    "mkl",
    "magma",
    "triton",
    "optree",
86
    "nccl",
87
    "transformers",
88
    "zmq",
89
90
    "nvidia",
    "pynvml",
91
    "flashinfer-python",
92
    "helion",
93
94
95
96
97
98
99
100
101
102
}

DEFAULT_PIP_PATTERNS = {
    "torch",
    "numpy",
    "mypy",
    "flake8",
    "triton",
    "optree",
    "onnx",
103
    "nccl",
104
    "transformers",
105
    "zmq",
106
107
    "nvidia",
    "pynvml",
108
    "flashinfer-python",
109
    "helion",
110
111
112
113
114
115
}


def run(command):
    """Return (return-code, stdout, stderr)."""
    shell = True if type(command) is str else False
116
    try:
117
118
119
        p = subprocess.Popen(
            command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell
        )
120
121
        raw_output, raw_err = p.communicate()
        rc = p.returncode
122
123
        if get_platform() == "win32":
            enc = "oem"
124
125
126
        else:
            enc = locale.getpreferredencoding()
        output = raw_output.decode(enc)
127
        if command == "nvidia-smi topo -m":
128
129
130
131
132
133
134
135
136
137
            # don't remove the leading whitespace of `nvidia-smi topo -m`
            #   because they are meaningful
            output = output.rstrip()
        else:
            output = output.strip()
        err = raw_err.decode(enc)
        return rc, output, err.strip()

    except FileNotFoundError:
        cmd_str = command if isinstance(command, str) else command[0]
138
        return 127, "", f"Command not found: {cmd_str}"
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158


def run_and_read_all(run_lambda, command):
    """Run command using run_lambda; reads and returns entire output if rc is 0."""
    rc, out, _ = run_lambda(command)
    if rc != 0:
        return None
    return out


def run_and_parse_first_match(run_lambda, command, regex):
    """Run command using run_lambda, returns the first regex match if it exists."""
    rc, out, _ = run_lambda(command)
    if rc != 0:
        return None
    match = re.search(regex, out)
    if match is None:
        return None
    return match.group(1)

159

160
161
162
def get_conda_packages(run_lambda, patterns=None):
    if patterns is None:
        patterns = DEFAULT_CONDA_PATTERNS
163
164
    conda = os.environ.get("CONDA_EXE", "conda")
    out = run_and_read_all(run_lambda, [conda, "list"])
165
166
167
    if out is None:
        return out

168
169
170
171
172
    return "\n".join(
        line
        for line in out.splitlines()
        if not line.startswith("#") and any(name in line for name in patterns)
    )
173

174
175

def get_gcc_version(run_lambda):
176
    return run_and_parse_first_match(run_lambda, "gcc --version", r"gcc (.*)")
177

178

179
def get_clang_version(run_lambda):
180
181
182
    return run_and_parse_first_match(
        run_lambda, "clang --version", r"clang version (.*)"
    )
183
184
185


def get_cmake_version(run_lambda):
186
    return run_and_parse_first_match(run_lambda, "cmake --version", r"cmake (.*)")
187
188
189


def get_nvidia_driver_version(run_lambda):
190
191
192
193
194
    if get_platform() == "darwin":
        cmd = "kextstat | grep -i cuda"
        return run_and_parse_first_match(
            run_lambda, cmd, r"com[.]nvidia[.]CUDA [(](.*?)[)]"
        )
195
    smi = get_nvidia_smi()
196
    return run_and_parse_first_match(run_lambda, smi, r"Driver Version: (.*?) ")
197
198
199


def get_gpu_info(run_lambda):
200
201
202
203
204
    if get_platform() == "darwin" or (
        TORCH_AVAILABLE
        and hasattr(torch.version, "hip")
        and torch.version.hip is not None
    ):
205
206
207
208
209
210
211
212
213
214
215
216
        if TORCH_AVAILABLE and torch.cuda.is_available():
            if torch.version.hip is not None:
                prop = torch.cuda.get_device_properties(0)
                if hasattr(prop, "gcnArchName"):
                    gcnArch = " ({})".format(prop.gcnArchName)
                else:
                    gcnArch = "NoGCNArchNameOnOldPyTorch"
            else:
                gcnArch = ""
            return torch.cuda.get_device_name(None) + gcnArch
        return None
    smi = get_nvidia_smi()
217
218
    uuid_regex = re.compile(r" \(UUID: .+?\)")
    rc, out, _ = run_lambda(smi + " -L")
219
220
221
    if rc != 0:
        return None
    # Anonymize GPUs by removing their UUID
222
    return re.sub(uuid_regex, "", out)
223
224
225


def get_running_cuda_version(run_lambda):
226
    return run_and_parse_first_match(run_lambda, "nvcc --version", r"release .+ V(.*)")
227
228
229
230


def get_cudnn_version(run_lambda):
    """Return a list of libcudnn.so; it's hard to tell which one is being used."""
231
232
233
234
    if get_platform() == "win32":
        system_root = os.environ.get("SYSTEMROOT", "C:\\Windows")
        cuda_path = os.environ.get("CUDA_PATH", "%CUDA_PATH%")
        where_cmd = os.path.join(system_root, "System32", "where")
235
        cudnn_cmd = '{} /R "{}\\bin" cudnn*.dll'.format(where_cmd, cuda_path)
236
    elif get_platform() == "darwin":
237
238
239
240
        # CUDA libraries and drivers can be found in /usr/local/cuda/. See
        # https://docs.nvidia.com/cuda/cuda-installation-guide-mac-os-x/index.html#install
        # https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installmac
        # Use CUDNN_LIBRARY when cudnn library is installed elsewhere.
241
        cudnn_cmd = "ls /usr/local/cuda/lib/libcudnn*"
242
243
244
245
246
    else:
        cudnn_cmd = 'ldconfig -p | grep libcudnn | rev | cut -d" " -f1 | rev'
    rc, out, _ = run_lambda(cudnn_cmd)
    # find will return 1 if there are permission errors or if not found
    if len(out) == 0 or (rc != 1 and rc != 0):
247
        l = os.environ.get("CUDNN_LIBRARY")
248
249
250
251
        if l is not None and os.path.isfile(l):
            return os.path.realpath(l)
        return None
    files_set = set()
252
    for fn in out.split("\n"):
253
254
255
256
257
258
259
260
261
        fn = os.path.realpath(fn)  # eliminate symbolic links
        if os.path.isfile(fn):
            files_set.add(fn)
    if not files_set:
        return None
    # Alphabetize the result because the order is non-deterministic otherwise
    files = sorted(files_set)
    if len(files) == 1:
        return files[0]
262
263
    result = "\n".join(files)
    return "Probably one of the following:\n{}".format(result)
264
265
266
267


def get_nvidia_smi():
    # Note: nvidia-smi is currently available only on Windows and Linux
268
269
270
271
272
273
274
275
    smi = "nvidia-smi"
    if get_platform() == "win32":
        system_root = os.environ.get("SYSTEMROOT", "C:\\Windows")
        program_files_root = os.environ.get("PROGRAMFILES", "C:\\Program Files")
        legacy_path = os.path.join(
            program_files_root, "NVIDIA Corporation", "NVSMI", smi
        )
        new_path = os.path.join(system_root, "System32", smi)
276
277
278
279
280
281
282
283
284
285
        smis = [new_path, legacy_path]
        for candidate_smi in smis:
            if os.path.exists(candidate_smi):
                smi = '"{}"'.format(candidate_smi)
                break
    return smi


def get_rocm_version(run_lambda):
    """Returns the ROCm version if available, otherwise 'N/A'."""
286
287
288
    return run_and_parse_first_match(
        run_lambda, "hipcc --version", r"HIP version: (\S+)"
    )
289
290


291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
def get_xpu_available():
    if TORCH_AVAILABLE and hasattr(torch, "xpu") and torch.xpu.is_available():
        return True
    return False


def get_xpu_runtime_version():
    if TORCH_AVAILABLE and hasattr(torch.version, "xpu"):
        return torch.version.xpu
    return None


def get_pkg_version(run_lambda, pkg):
    assert get_platform() == "linux"

    if pkg == "vllm_xpu_kernels":
        rc, out, _ = run_lambda("pip show vllm-xpu-kernels")
        if rc == 0:
            match = re.search(r"Version: (.*)", out)
            return match.group(1).strip() if match else None
        return None

    pkg_map = {
        "igc": ["intel-igc-core", "libigc2", "libigc1"],
        "level_zero_loader": ["level-zero", "libze1"],
        "level_zero_driver": ["libze-intel-gpu1", "intel-level-zero-gpu"],
        "oneccl": ["intel-oneapi-ccl", "oneccl"],
        "libigdgmm": ["libigdgmm12", "libigdgmm"],
    }

    pkg_candidates = pkg_map.get(pkg, [])
    if not pkg_candidates:
        return None

    mgr_name = None
    for mgr in ["dpkg", "dnf", "yum", "zypper"]:
        rc, _, _ = run_lambda(f"which {mgr}")
        if rc == 0:
            mgr_name = mgr
            break

    if not mgr_name:
        return None

    ret = ""
    index = -1

    for pkg_name in pkg_candidates:
        if not pkg_name:
            continue

        cmd = ""
        if mgr_name in ["dnf", "yum"]:
            index = 1
            cmd = f"{mgr_name} list | grep -w {pkg_name}"
        elif mgr_name == "zypper":
            index = 2
            cmd = f"{mgr_name} info {pkg_name} | grep Version"
        elif mgr_name == "dpkg":
            index = 2
            cmd = f"{mgr_name} -l | grep -w {pkg_name}"

        if cmd:
            out = run_and_read_all(run_lambda, cmd)
            if out:
                ret = out.splitlines()[0]
                break

    if not ret or index == -1:
        return None

    lst = re.sub(" +", " ", ret).strip().split(" ")
    if len(lst) > index:
        return lst[index]

    return None


def get_intel_graphics_compiler_version(run_lambda):
    """Return Intel Graphics Compiler (IGC) version."""
    return get_pkg_version(run_lambda, "igc")


def get_level_zero_loader_version(run_lambda):
    """Return Level Zero loader runtime version."""
    return get_pkg_version(run_lambda, "level_zero_loader")


def get_level_zero_driver_version(run_lambda):
    """Return Level Zero driver version."""
    return get_pkg_version(run_lambda, "level_zero_driver")


def get_oneapi_ccl_version(run_lambda):
    """Return oneAPI Collective Communications Library (oneCCL) version."""
    return get_pkg_version(run_lambda, "oneccl")


def get_libigdgmm_version(run_lambda):
    return get_pkg_version(run_lambda, "libigdgmm")


def get_vllm_xpu_kernels_version(run_lambda):
    return get_pkg_version(run_lambda, "vllm_xpu_kernels")


def get_intel_gpu_models():
    if TORCH_AVAILABLE and hasattr(torch, "xpu") and torch.xpu.is_available():
        device_count = torch.xpu.device_count()
        return "\n".join(
            "GPU {}: {}".format(i, torch.xpu.get_device_name(i))
            for i in range(device_count)
        )
    return None


def get_oneapi_compiler_version(run_lambda):
    """Return Intel oneAPI DPC++/C++ Compiler version via icpx."""
    return run_and_parse_first_match(
        run_lambda, "icpx --version", r"oneAPI DPC\+\+/C\+\+ Compiler (\S+)"
    )


def get_sycl_version(run_lambda):
    """Return SYCL/DPC++ compiler build version."""
    return run_and_parse_first_match(run_lambda, "icpx --version", r"\((\d[\d.]+)\)")


419
def get_vllm_version():
420
421
422
423
    from vllm import __version__, __version_tuple__

    if __version__ == "dev":
        return "N/A (dev)"
424
    version_str = __version_tuple__[-1]
425
    if isinstance(version_str, str) and version_str.startswith("g"):
426
        # it's a dev build
427
        if "." in version_str:
428
            # it's a dev build containing local changes
429
430
            git_sha = version_str.split(".")[0][1:]
            date = version_str.split(".")[-1][1:]
431
432
433
434
435
            return f"{__version__} (git sha: {git_sha}, date: {date})"
        else:
            # it's a dev build without local changes
            git_sha = version_str[1:]  # type: ignore
            return f"{__version__} (git sha: {git_sha})"
436
    return __version__
437

438

439
def summarize_vllm_build_flags():
440
    flags = "CUDA Archs: {}; ROCm: {}; XPU: {}".format(
441
442
        os.environ.get("TORCH_CUDA_ARCH_LIST", "Not Set"),
        "Enabled" if os.environ.get("ROCM_HOME") else "Disabled",
443
        "Enabled" if get_xpu_available() else "Disabled",
444
    )
445
    return flags
446
447
448


def get_gpu_topo(run_lambda):
449
450
    output = None

451
452
    if get_platform() == "linux":
        output = run_and_read_all(run_lambda, "nvidia-smi topo -m")
453
        if output is None:
454
            output = run_and_read_all(run_lambda, "rocm-smi --showtopo")
455
456

    return output
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533


# example outputs of CPU infos
#  * linux
#    Architecture:            x86_64
#      CPU op-mode(s):        32-bit, 64-bit
#      Address sizes:         46 bits physical, 48 bits virtual
#      Byte Order:            Little Endian
#    CPU(s):                  128
#      On-line CPU(s) list:   0-127
#    Vendor ID:               GenuineIntel
#      Model name:            Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz
#        CPU family:          6
#        Model:               106
#        Thread(s) per core:  2
#        Core(s) per socket:  32
#        Socket(s):           2
#        Stepping:            6
#        BogoMIPS:            5799.78
#        Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr
#                             sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl
#                             xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq monitor ssse3 fma cx16
#                             pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand
#                             hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced
#                             fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap
#                             avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1
#                             xsaves wbnoinvd ida arat avx512vbmi pku ospke avx512_vbmi2 gfni vaes vpclmulqdq
#                             avx512_vnni avx512_bitalg tme avx512_vpopcntdq rdpid md_clear flush_l1d arch_capabilities
#    Virtualization features:
#      Hypervisor vendor:     KVM
#      Virtualization type:   full
#    Caches (sum of all):
#      L1d:                   3 MiB (64 instances)
#      L1i:                   2 MiB (64 instances)
#      L2:                    80 MiB (64 instances)
#      L3:                    108 MiB (2 instances)
#    NUMA:
#      NUMA node(s):          2
#      NUMA node0 CPU(s):     0-31,64-95
#      NUMA node1 CPU(s):     32-63,96-127
#    Vulnerabilities:
#      Itlb multihit:         Not affected
#      L1tf:                  Not affected
#      Mds:                   Not affected
#      Meltdown:              Not affected
#      Mmio stale data:       Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
#      Retbleed:              Not affected
#      Spec store bypass:     Mitigation; Speculative Store Bypass disabled via prctl and seccomp
#      Spectre v1:            Mitigation; usercopy/swapgs barriers and __user pointer sanitization
#      Spectre v2:            Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
#      Srbds:                 Not affected
#      Tsx async abort:       Not affected
#  * win32
#    Architecture=9
#    CurrentClockSpeed=2900
#    DeviceID=CPU0
#    Family=179
#    L2CacheSize=40960
#    L2CacheSpeed=
#    Manufacturer=GenuineIntel
#    MaxClockSpeed=2900
#    Name=Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz
#    ProcessorType=3
#    Revision=27142
#
#    Architecture=9
#    CurrentClockSpeed=2900
#    DeviceID=CPU1
#    Family=179
#    L2CacheSize=40960
#    L2CacheSpeed=
#    Manufacturer=GenuineIntel
#    MaxClockSpeed=2900
#    Name=Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz
#    ProcessorType=3
#    Revision=27142

534

535
def get_cpu_info(run_lambda):
536
537
538
539
    rc, out, err = 0, "", ""
    if get_platform() == "linux":
        rc, out, err = run_lambda("lscpu")
    elif get_platform() == "win32":
540
        rc, out, err = run_lambda(
541
542
            "wmic cpu get Name,Manufacturer,Family,Architecture,ProcessorType,DeviceID, \
        CurrentClockSpeed,MaxClockSpeed,L2CacheSize,L2CacheSpeed,Revision /VALUE"
543
        )
544
    elif get_platform() == "darwin":
545
        rc, out, err = run_lambda("sysctl -n machdep.cpu.brand_string")
546
    cpu_info = "None"
547
548
549
550
551
552
553
554
    if rc == 0:
        cpu_info = out
    else:
        cpu_info = err
    return cpu_info


def get_platform():
555
556
557
558
559
560
561
562
    if sys.platform.startswith("linux"):
        return "linux"
    elif sys.platform.startswith("win32"):
        return "win32"
    elif sys.platform.startswith("cygwin"):
        return "cygwin"
    elif sys.platform.startswith("darwin"):
        return "darwin"
563
564
565
566
567
    else:
        return sys.platform


def get_mac_version(run_lambda):
568
    return run_and_parse_first_match(run_lambda, "sw_vers -productVersion", r"(.*)")
569
570
571


def get_windows_version(run_lambda):
572
573
574
    system_root = os.environ.get("SYSTEMROOT", "C:\\Windows")
    wmic_cmd = os.path.join(system_root, "System32", "Wbem", "wmic")
    findstr_cmd = os.path.join(system_root, "System32", "findstr")
575
    return run_and_read_all(
576
577
        run_lambda, "{} os get Caption | {} /v Caption".format(wmic_cmd, findstr_cmd)
    )
578
579
580


def get_lsb_version(run_lambda):
581
582
583
    return run_and_parse_first_match(
        run_lambda, "lsb_release -a", r"Description:\t(.*)"
    )
584
585
586


def check_release_file(run_lambda):
587
588
589
    return run_and_parse_first_match(
        run_lambda, "cat /etc/*-release", r'PRETTY_NAME="(.*)"'
    )
590
591
592
593


def get_os(run_lambda):
    from platform import machine
594

595
596
    platform = get_platform()

597
    if platform == "win32" or platform == "cygwin":
598
599
        return get_windows_version(run_lambda)

600
    if platform == "darwin":
601
602
603
        version = get_mac_version(run_lambda)
        if version is None:
            return None
604
        return "macOS {} ({})".format(version, machine())
605

606
    if platform == "linux":
607
608
609
        # Ubuntu/Debian based
        desc = get_lsb_version(run_lambda)
        if desc is not None:
610
            return "{} ({})".format(desc, machine())
611
612
613
614

        # Try reading /etc/*-release
        desc = check_release_file(run_lambda)
        if desc is not None:
615
            return "{} ({})".format(desc, machine())
616

617
        return "{} ({})".format(platform, machine())
618
619
620
621
622
623
624

    # Unknown platform
    return platform


def get_python_platform():
    import platform
625

626
627
628
629
630
    return platform.platform()


def get_libc_version():
    import platform
631
632
633
634

    if get_platform() != "linux":
        return "N/A"
    return "-".join(platform.libc_ver())
635
636


637
638
639
def is_uv_venv():
    if os.environ.get("UV"):
        return True
640
    pyvenv_cfg_path = os.path.join(sys.prefix, "pyvenv.cfg")
641
    if os.path.exists(pyvenv_cfg_path):
642
643
        with open(pyvenv_cfg_path, "r") as f:
            return any(line.startswith("uv = ") for line in f)
644
645
646
    return False


647
648
649
650
651
def get_pip_packages(run_lambda, patterns=None):
    """Return `pip list` output. Note: will also find conda-installed pytorch and numpy packages."""
    if patterns is None:
        patterns = DEFAULT_PIP_PATTERNS

652
653
654
    def run_with_pip():
        try:
            import importlib.util
655
656

            pip_spec = importlib.util.find_spec("pip")
657
658
659
660
661
            pip_available = pip_spec is not None
        except ImportError:
            pip_available = False

        if pip_available:
662
            cmd = [sys.executable, "-mpip", "list", "--format=freeze"]
663
        elif is_uv_venv():
664
665
666
            print("uv is set")
            cmd = ["uv", "pip", "list", "--format=freeze"]
        else:
667
668
669
            raise RuntimeError(
                "Could not collect pip list output (pip or uv module not available)"
            )
670
671

        out = run_and_read_all(run_lambda, cmd)
672
673
674
        return "\n".join(
            line for line in out.splitlines() if any(name in line for name in patterns)
        )
675

676
    pip_version = "pip3" if sys.version[0] == "3" else "pip"
677
    out = run_with_pip()
678
679
680
681
    return pip_version, out


def get_cachingallocator_config():
682
    ca_config = os.environ.get("PYTORCH_CUDA_ALLOC_CONF", "")
683
684
685
686
687
688
    return ca_config


def get_cuda_module_loading_config():
    if TORCH_AVAILABLE and torch.cuda.is_available():
        torch.cuda.init()
689
        config = os.environ.get("CUDA_MODULE_LOADING", "")
690
691
692
693
694
695
696
697
        return config
    else:
        return "N/A"


def is_xnnpack_available():
    if TORCH_AVAILABLE:
        import torch.backends.xnnpack
698
699

        return str(torch.backends.xnnpack.enabled)  # type: ignore[attr-defined]
700
701
702
    else:
        return "N/A"

703

704
def get_env_vars():
705
706
707
708
709
710
711
712
713
714
715
716
    env_vars = ""
    secret_terms = ("secret", "token", "api", "access", "password")
    report_prefix = (
        "TORCH",
        "NCCL",
        "PYTORCH",
        "CUDA",
        "CUBLAS",
        "CUDNN",
        "OMP_",
        "MKL_",
        "NVIDIA",
717
718
719
720
721
722
723
        "ZE_",
        "ONEAPI_",
        "SYCL_",
        "NEOReadDebugKeys",
        "IGC_",
        "CCL_",
        "I_MPI_",
724
    )
725
726
727
728
729
730
731
732
733
    for k, v in os.environ.items():
        if any(term in k.lower() for term in secret_terms):
            continue
        if k in environment_variables:
            env_vars = env_vars + "{}={}".format(k, v) + "\n"
        if k.startswith(report_prefix):
            env_vars = env_vars + "{}={}".format(k, v) + "\n"

    return env_vars
734

735

736
737
738
739
740
741
742
743
744
def get_env_info():
    run_lambda = run
    pip_version, pip_list_output = get_pip_packages(run_lambda)

    if TORCH_AVAILABLE:
        version_str = torch.__version__
        debug_mode_str = str(torch.version.debug)
        cuda_available_str = str(torch.cuda.is_available())
        cuda_version_str = torch.version.cuda
745
746
747
748
        if (
            not hasattr(torch.version, "hip") or torch.version.hip is None
        ):  # cuda version
            hip_compiled_version = hip_runtime_version = miopen_runtime_version = "N/A"
749
        else:  # HIP version
750

751
752
            def get_version_or_na(cfg, prefix):
                _lst = [s.rsplit(None, 1)[-1] for s in cfg if prefix in s]
753
                return _lst[0] if _lst else "N/A"
754

755
756
757
758
            cfg = torch._C._show_config().split("\n")
            hip_runtime_version = get_version_or_na(cfg, "HIP Runtime")
            miopen_runtime_version = get_version_or_na(cfg, "MIOpen")
            cuda_version_str = "N/A"
759
760
            hip_compiled_version = torch.version.hip
    else:
761
762
        version_str = debug_mode_str = cuda_available_str = cuda_version_str = "N/A"
        hip_compiled_version = hip_runtime_version = miopen_runtime_version = "N/A"
763
764
765
766
767
768
769
770
771
772
773
774
775

    sys_version = sys.version.replace("\n", " ")

    conda_packages = get_conda_packages(run_lambda)

    rocm_version = get_rocm_version(run_lambda)
    vllm_version = get_vllm_version()
    vllm_build_flags = summarize_vllm_build_flags()
    gpu_topo = get_gpu_topo(run_lambda)

    return SystemEnv(
        torch_version=version_str,
        is_debug_build=debug_mode_str,
776
777
778
        python_version="{} ({}-bit runtime)".format(
            sys_version, sys.maxsize.bit_length() + 1
        ),
779
780
781
782
783
784
785
786
        python_platform=get_python_platform(),
        is_cuda_available=cuda_available_str,
        cuda_compiled_version=cuda_version_str,
        cuda_runtime_version=get_running_cuda_version(run_lambda),
        cuda_module_loading=get_cuda_module_loading_config(),
        nvidia_gpu_models=get_gpu_info(run_lambda),
        nvidia_driver_version=get_nvidia_driver_version(run_lambda),
        cudnn_version=get_cudnn_version(run_lambda),
787
788
789
790
791
792
793
794
795
796
797
        xpu_available=str(get_xpu_available()),
        xpu_runtime_version=get_xpu_runtime_version(),
        intel_graphics_compiler_version=get_intel_graphics_compiler_version(run_lambda),
        intel_gpu_models=get_intel_gpu_models(),
        oneapi_compiler_version=get_oneapi_compiler_version(run_lambda),
        level_zero_loader_version=get_level_zero_loader_version(run_lambda),
        level_zero_driver_version=get_level_zero_driver_version(run_lambda),
        oneccl_version=get_oneapi_ccl_version(run_lambda),
        libigdgmm_version=get_libigdgmm_version(run_lambda),
        vllm_xpu_kernels_version=get_vllm_xpu_kernels_version(run_lambda),
        sycl_version=get_sycl_version(run_lambda),
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
        hip_compiled_version=hip_compiled_version,
        hip_runtime_version=hip_runtime_version,
        miopen_runtime_version=miopen_runtime_version,
        pip_version=pip_version,
        pip_packages=pip_list_output,
        conda_packages=conda_packages,
        os=get_os(run_lambda),
        libc_version=get_libc_version(),
        gcc_version=get_gcc_version(run_lambda),
        clang_version=get_clang_version(run_lambda),
        cmake_version=get_cmake_version(run_lambda),
        caching_allocator_config=get_cachingallocator_config(),
        is_xnnpack_available=is_xnnpack_available(),
        cpu_info=get_cpu_info(run_lambda),
        rocm_version=rocm_version,
        vllm_version=vllm_version,
        vllm_build_flags=vllm_build_flags,
        gpu_topo=gpu_topo,
816
        env_vars=get_env_vars(),
817
818
    )

819

820
env_info_fmt = """
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
==============================
        System Info
==============================
OS                           : {os}
GCC version                  : {gcc_version}
Clang version                : {clang_version}
CMake version                : {cmake_version}
Libc version                 : {libc_version}

==============================
       PyTorch Info
==============================
PyTorch version              : {torch_version}
Is debug build               : {is_debug_build}
CUDA used to build PyTorch   : {cuda_compiled_version}
ROCM used to build PyTorch   : {hip_compiled_version}
837
XPU used to build PyTorch    : {xpu_runtime_version}
838
839
840
841
842
843

==============================
      Python Environment
==============================
Python version               : {python_version}
Python platform              : {python_platform}
844
845
    
{gpu_info}
846
847
848
==============================
          CPU Info
==============================
849
850
{cpu_info}

851
852
853
==============================
Versions of relevant libraries
==============================
854
855
856
857
{pip_packages}
{conda_packages}
""".strip()

youkaichao's avatar
youkaichao committed
858
859
860
# both the above code and the following code use `strip()` to
# remove leading/trailing whitespaces, so we need to add a newline
# in between to separate the two sections
861
env_info_fmt += "\n\n"
youkaichao's avatar
youkaichao committed
862

863
env_info_fmt += """
864
865
866
867
868
==============================
         vLLM Info
==============================
ROCM Version                 : {rocm_version}
vLLM Version                 : {vllm_version}
869
vLLM Build Flags:
870
  {vllm_build_flags}
871
GPU Topology:
872
  {gpu_topo}
873

874
875
876
==============================
     Environment Variables
==============================
877
{env_vars}
878
879
880
881
""".strip()


def pretty_str(envinfo):
882
    def replace_nones(dct, replacement="Could not collect"):
883
884
885
886
887
888
        for key in dct.keys():
            if dct[key] is not None:
                continue
            dct[key] = replacement
        return dct

889
    def replace_bools(dct, true="Yes", false="No"):
890
891
892
893
894
895
896
        for key in dct.keys():
            if dct[key] is True:
                dct[key] = true
            elif dct[key] is False:
                dct[key] = false
        return dct

897
898
    def prepend(text, tag="[prepend]"):
        lines = text.split("\n")
899
        updated_lines = [tag + line for line in lines]
900
        return "\n".join(updated_lines)
901

902
    def replace_if_empty(text, replacement="No relevant packages"):
903
904
905
906
907
908
        if text is not None and len(text) == 0:
            return replacement
        return text

    def maybe_start_on_next_line(string):
        # If `string` is multiline, prepend a \n to it.
909
910
        if string is not None and len(string.split("\n")) > 1:
            return "\n{}\n".format(string)
911
912
913
914
915
        return string

    mutable_dict = envinfo._asdict()

    # If nvidia_gpu_models is multiline, start on the next line
916
917
918
    mutable_dict["nvidia_gpu_models"] = maybe_start_on_next_line(
        envinfo.nvidia_gpu_models
    )
919
920
921

    # If the machine doesn't have CUDA, report some fields as 'No CUDA'
    dynamic_cuda_fields = [
922
923
924
        "cuda_runtime_version",
        "nvidia_gpu_models",
        "nvidia_driver_version",
925
    ]
926
927
928
929
930
931
932
933
934
    all_cuda_fields = dynamic_cuda_fields + ["cudnn_version"]
    all_dynamic_cuda_fields_missing = all(
        mutable_dict[field] is None for field in dynamic_cuda_fields
    )
    if (
        TORCH_AVAILABLE
        and not torch.cuda.is_available()
        and all_dynamic_cuda_fields_missing
    ):
935
        for field in all_cuda_fields:
936
            mutable_dict[field] = "No CUDA"
937
        if envinfo.cuda_compiled_version is None:
938
            mutable_dict["cuda_compiled_version"] = "None"
939

940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
    # If the machine doesn't have XPU, report XPU fields as 'No XPU'
    dynamic_xpu_fields = [
        "intel_graphics_compiler_version",
        "intel_gpu_models",
        "level_zero_loader_version",
        "level_zero_driver_version",
        "oneccl_version",
        "libigdgmm_version",
        "vllm_xpu_kernels_version",
    ]
    all_xpu_fields = dynamic_xpu_fields + [
        "oneapi_compiler_version",
        "sycl_version",
    ]
    all_dynamic_xpu_fields_missing = all(
        mutable_dict[field] is None for field in dynamic_xpu_fields
    )
    xpu_available = mutable_dict.get("xpu_available") == "True"
    if not xpu_available and all_dynamic_xpu_fields_missing:
        for field in all_xpu_fields:
            mutable_dict[field] = "No XPU"
    if envinfo.xpu_runtime_version is None or envinfo.xpu_runtime_version == "N/A":
        mutable_dict["xpu_runtime_version"] = "N/A"

    # If intel_gpu_models is multiline, start on the next line
    mutable_dict["intel_gpu_models"] = maybe_start_on_next_line(
        mutable_dict.get("intel_gpu_models")
    )

969
970
971
972
973
974
975
    # Replace True with Yes, False with No
    mutable_dict = replace_bools(mutable_dict)

    # Replace all None objects with 'Could not collect'
    mutable_dict = replace_nones(mutable_dict)

    # If either of these are '', replace with 'No relevant packages'
976
977
    mutable_dict["pip_packages"] = replace_if_empty(mutable_dict["pip_packages"])
    mutable_dict["conda_packages"] = replace_if_empty(mutable_dict["conda_packages"])
978
979
980

    # Tag conda and pip packages with a prefix
    # If they were previously None, they'll show up as ie '[conda] Could not collect'
981
982
983
984
985
986
987
988
989
    if mutable_dict["pip_packages"]:
        mutable_dict["pip_packages"] = prepend(
            mutable_dict["pip_packages"], "[{}] ".format(envinfo.pip_version)
        )
    if mutable_dict["conda_packages"]:
        mutable_dict["conda_packages"] = prepend(
            mutable_dict["conda_packages"], "[conda] "
        )
    mutable_dict["cpu_info"] = envinfo.cpu_info
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045

    CUDA_FMT = """
==============================
       CUDA / GPU Info
==============================
Is CUDA available            : {is_cuda_available}
CUDA runtime version         : {cuda_runtime_version}
CUDA_MODULE_LOADING set to   : {cuda_module_loading}
GPU models and configuration : {nvidia_gpu_models}
Nvidia driver version        : {nvidia_driver_version}
cuDNN version                : {cudnn_version}
HIP runtime version          : {hip_runtime_version}
MIOpen runtime version       : {miopen_runtime_version}
Is XNNPACK available         : {is_xnnpack_available}
""".strip()

    XPU_FMT = """
==============================
      Intel XPU / GPU Info
==============================
Is XPU available             : {xpu_available}
XPU runtime version          : {xpu_runtime_version}
Intel GPU models             : {intel_gpu_models}

--Compile time--
oneAPI compiler version      : {oneapi_compiler_version}
SYCL compiler build          : {sycl_version}
oneCCL version               : {oneccl_version}

--Runtime--
Intel Graphics Compiler (IGC): {intel_graphics_compiler_version}
Intel GMM (libigdgmm)        : {libigdgmm_version}
Level Zero loader version    : {level_zero_loader_version}
Level Zero driver version    : {level_zero_driver_version}
vLLM XPU kernels version     : {vllm_xpu_kernels_version}
""".strip()

    invalid_vers = {"N/A", "Could not collect", "None"}
    sections = []

    if (
        mutable_dict.get("is_cuda_available") in ("True", "Yes")
        or mutable_dict.get("cuda_compiled_version") not in invalid_vers
    ):
        sections.append(CUDA_FMT)

    if (
        mutable_dict.get("xpu_available") in ("True", "Yes")
        or mutable_dict.get("xpu_runtime_version") not in invalid_vers
    ):
        sections.append(XPU_FMT)

    mutable_dict["gpu_info"] = (
        ("\n\n".join(sections) + "\n").format(**mutable_dict) if sections else ""
    )

1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
    return env_info_fmt.format(**mutable_dict)


def get_pretty_env_info():
    return pretty_str(get_env_info())


def main():
    print("Collecting environment information...")
    output = get_pretty_env_info()
    print(output)

1058
1059
1060
1061
1062
    if (
        TORCH_AVAILABLE
        and hasattr(torch, "utils")
        and hasattr(torch.utils, "_crash_handler")
    ):
1063
1064
        minidump_dir = torch.utils._crash_handler.DEFAULT_MINIDUMP_DIR
        if sys.platform == "linux" and os.path.exists(minidump_dir):
1065
            dumps = [
1066
                os.path.join(minidump_dir, dump) for dump in os.listdir(minidump_dir)
1067
            ]
1068
1069
            latest = max(dumps, key=os.path.getctime)
            ctime = os.path.getctime(latest)
1070
            creation_time = datetime.datetime.fromtimestamp(ctime).strftime(
1071
1072
1073
1074
1075
1076
1077
1078
                "%Y-%m-%d %H:%M:%S"
            )
            msg = (
                "\n*** Detected a minidump at {} created on {}, ".format(
                    latest, creation_time
                )
                + "if this is related to your bug please include it when you file a report ***"
            )
1079
1080
1081
            print(msg, file=sys.stderr)


1082
if __name__ == "__main__":
1083
    main()