|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Report the versions and sizes of the binaries a validation run installed. |
| 3 | +
|
| 4 | +Emits a small markdown table that validate_binaries.sh prints to the job log and |
| 5 | +appends to the job summary. Values are read from the installed packages rather |
| 6 | +than from the build matrix, so a mismatch between what was requested and what |
| 7 | +pip resolved is visible in the report. |
| 8 | +
|
| 9 | +Two sizes are reported because they answer different questions: |
| 10 | +
|
| 11 | +* wheel -- the compressed download size, i.e. what a user pulls from the |
| 12 | + index. Only pip knows it, so the caller passes it in; it is |
| 13 | + absent on the uv/wheel-variants path and when the wheel was |
| 14 | + already satisfied. |
| 15 | +* installed -- unpacked bytes on disk, measured here from the installed |
| 16 | + package, so it is available on every install path and every OS. |
| 17 | +
|
| 18 | +Nothing in here may fail the validation job: every lookup that depends on how |
| 19 | +torch was built (CUDA, cuDNN, NCCL) degrades to "-" instead of raising. |
| 20 | +
|
| 21 | +Note on imports: this is deliberately a script file rather than a heredoc piped |
| 22 | +to python. For a script, sys.path[0] is the script's own directory, so `import |
| 23 | +torch` cannot pick up the pytorch source checkout the validation runs from -- |
| 24 | +which a `python -` heredoc would, since that puts the cwd on sys.path. |
| 25 | +
|
| 26 | +Usage: |
| 27 | + build_report.py --target-os linux --python-version 3.12 \ |
| 28 | + --gpu-arch-type cuda --gpu-arch-version 12.8 \ |
| 29 | + --torch-wheel-mb 812.4 --torchvision-wheel-mb 8.1 |
| 30 | +""" |
| 31 | + |
| 32 | +from __future__ import annotations |
| 33 | + |
| 34 | +import argparse |
| 35 | +import os |
| 36 | +from types import ModuleType |
| 37 | + |
| 38 | + |
| 39 | +def installed_size_mb(module: ModuleType) -> str: |
| 40 | + """Total size of a package directory on disk, formatted as MB. |
| 41 | +
|
| 42 | + Files that vanish or cannot be stat'd mid-walk are skipped rather than |
| 43 | + aborting the report. |
| 44 | + """ |
| 45 | + path = getattr(module, "__file__", None) |
| 46 | + if not path: |
| 47 | + return "-" |
| 48 | + |
| 49 | + total = 0 |
| 50 | + for dirpath, _, filenames in os.walk(os.path.dirname(path)): |
| 51 | + for filename in filenames: |
| 52 | + try: |
| 53 | + total += os.path.getsize(os.path.join(dirpath, filename)) |
| 54 | + except OSError: |
| 55 | + continue |
| 56 | + return f"{total / 1024 / 1024:.1f} MB" |
| 57 | + |
| 58 | + |
| 59 | +def format_mb(value: str) -> str: |
| 60 | + """Render a caller-supplied megabyte figure, or "-" when it is unknown.""" |
| 61 | + return f"{value} MB" if value else "-" |
| 62 | + |
| 63 | + |
| 64 | +def cudnn_version(torch: ModuleType) -> str: |
| 65 | + """Decode torch's packed cuDNN version integer, e.g. 91002 -> 9.10.2.""" |
| 66 | + try: |
| 67 | + packed = torch.backends.cudnn.version() |
| 68 | + except Exception: |
| 69 | + return "-" |
| 70 | + if not packed: |
| 71 | + return "-" |
| 72 | + return f"{packed // 10000}.{packed % 10000 // 100}.{packed % 100}" |
| 73 | + |
| 74 | + |
| 75 | +def nccl_version(torch: ModuleType) -> str: |
| 76 | + """Format torch's NCCL version tuple, e.g. (2, 30, 7) -> 2.30.7.""" |
| 77 | + try: |
| 78 | + return ".".join(str(part) for part in torch.cuda.nccl.version()) |
| 79 | + except Exception: |
| 80 | + return "-" |
| 81 | + |
| 82 | + |
| 83 | +def collect_rows(args: argparse.Namespace) -> list[tuple[str, str]]: |
| 84 | + arch = args.gpu_arch_type |
| 85 | + if args.gpu_arch_version: |
| 86 | + arch = f"{arch} {args.gpu_arch_version}" |
| 87 | + rows = [("build", f"{args.target_os} / py{args.python_version} / {arch}")] |
| 88 | + |
| 89 | + try: |
| 90 | + import torch |
| 91 | + except Exception as exc: |
| 92 | + rows.append(("torch", f"import failed: {exc}")) |
| 93 | + else: |
| 94 | + rows.extend( |
| 95 | + [ |
| 96 | + ("torch", torch.__version__), |
| 97 | + ("torch wheel", format_mb(args.torch_wheel_mb)), |
| 98 | + ("torch installed", installed_size_mb(torch)), |
| 99 | + ("CUDA", torch.version.cuda or "-"), |
| 100 | + ("cuDNN", cudnn_version(torch)), |
| 101 | + ("NCCL", nccl_version(torch)), |
| 102 | + ] |
| 103 | + ) |
| 104 | + |
| 105 | + try: |
| 106 | + import torchvision |
| 107 | + except Exception: |
| 108 | + rows.append(("torchvision", "-")) |
| 109 | + else: |
| 110 | + rows.extend( |
| 111 | + [ |
| 112 | + ("torchvision", torchvision.__version__), |
| 113 | + ("torchvision wheel", format_mb(args.torchvision_wheel_mb)), |
| 114 | + ("torchvision installed", installed_size_mb(torchvision)), |
| 115 | + ] |
| 116 | + ) |
| 117 | + |
| 118 | + return rows |
| 119 | + |
| 120 | + |
| 121 | +def parse_args() -> argparse.Namespace: |
| 122 | + parser = argparse.ArgumentParser(description=__doc__) |
| 123 | + parser.add_argument("--target-os", default="?") |
| 124 | + parser.add_argument("--python-version", default="?") |
| 125 | + parser.add_argument("--gpu-arch-type", default="cpu") |
| 126 | + parser.add_argument("--gpu-arch-version", default="") |
| 127 | + parser.add_argument( |
| 128 | + "--torch-wheel-mb", |
| 129 | + default="", |
| 130 | + help="Compressed torch wheel size in MB, as read from pip's output", |
| 131 | + ) |
| 132 | + parser.add_argument( |
| 133 | + "--torchvision-wheel-mb", |
| 134 | + default="", |
| 135 | + help="Compressed torchvision wheel size in MB, as read from pip's output", |
| 136 | + ) |
| 137 | + return parser.parse_args() |
| 138 | + |
| 139 | + |
| 140 | +def main() -> None: |
| 141 | + rows = collect_rows(parse_args()) |
| 142 | + print("| field | value |") |
| 143 | + print("| --- | --- |") |
| 144 | + for field, value in rows: |
| 145 | + print(f"| {field} | {value} |") |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + main() |
0 commit comments