|
| 1 | +"""soup doctor — check dependency compatibility and system health.""" |
| 2 | + |
| 3 | +import platform |
| 4 | +import sys |
| 5 | + |
| 6 | +from rich.console import Console |
| 7 | +from rich.panel import Panel |
| 8 | +from rich.table import Table |
| 9 | + |
| 10 | +console = Console() |
| 11 | + |
| 12 | +# Dependencies to check: (import_name, package_name, min_version, required) |
| 13 | +DEPS = [ |
| 14 | + ("torch", "torch", "2.0.0", True), |
| 15 | + ("transformers", "transformers", "4.36.0", True), |
| 16 | + ("peft", "peft", "0.7.0", True), |
| 17 | + ("trl", "trl", "0.7.0", True), |
| 18 | + ("datasets", "datasets", "2.14.0", True), |
| 19 | + ("bitsandbytes", "bitsandbytes", "0.41.0", True), |
| 20 | + ("accelerate", "accelerate", "0.25.0", True), |
| 21 | + ("pydantic", "pydantic", "2.0.0", True), |
| 22 | + ("typer", "typer", "0.9.0", True), |
| 23 | + ("rich", "rich", "13.0.0", True), |
| 24 | + ("yaml", "pyyaml", "6.0", True), |
| 25 | + ("plotext", "plotext", "5.2.0", True), |
| 26 | + # Optional |
| 27 | + ("fastapi", "fastapi", "0.104.0", False), |
| 28 | + ("uvicorn", "uvicorn", "0.24.0", False), |
| 29 | + ("datasketch", "datasketch", "1.6.0", False), |
| 30 | + ("lm_eval", "lm-eval", "0.4.0", False), |
| 31 | + ("wandb", "wandb", "0.15.0", False), |
| 32 | + ("deepspeed", "deepspeed", "0.12.0", False), |
| 33 | + ("httpx", "httpx", "0.24.0", False), |
| 34 | +] |
| 35 | + |
| 36 | + |
| 37 | +def doctor(): |
| 38 | + """Check system dependencies, GPU, and compatibility.""" |
| 39 | + console.print("[bold]Soup Doctor[/] — checking your environment...\n") |
| 40 | + |
| 41 | + # System info |
| 42 | + console.print( |
| 43 | + Panel( |
| 44 | + f"Python: [bold]{sys.version.split()[0]}[/]\n" |
| 45 | + f"Platform: [bold]{platform.system()} {platform.release()}[/]\n" |
| 46 | + f"Arch: [bold]{platform.machine()}[/]", |
| 47 | + title="System", |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + # GPU check |
| 52 | + _check_gpu() |
| 53 | + |
| 54 | + # Dependencies table |
| 55 | + table = Table(title="Dependencies") |
| 56 | + table.add_column("Package", style="bold") |
| 57 | + table.add_column("Required", justify="center") |
| 58 | + table.add_column("Installed", justify="center") |
| 59 | + table.add_column("Min Version") |
| 60 | + table.add_column("Status") |
| 61 | + |
| 62 | + issues = [] |
| 63 | + |
| 64 | + for import_name, pkg_name, min_ver, required in DEPS: |
| 65 | + try: |
| 66 | + mod = __import__(import_name) |
| 67 | + version = getattr(mod, "__version__", getattr(mod, "VERSION", "?")) |
| 68 | + version_str = str(version) |
| 69 | + |
| 70 | + if _version_ok(version_str, min_ver): |
| 71 | + status = "[green]OK[/]" |
| 72 | + else: |
| 73 | + status = f"[yellow]outdated (need >={min_ver})[/]" |
| 74 | + issues.append(f"Upgrade {pkg_name}: pip install '{pkg_name}>={min_ver}'") |
| 75 | + |
| 76 | + table.add_row( |
| 77 | + pkg_name, |
| 78 | + "yes" if required else "optional", |
| 79 | + version_str, |
| 80 | + f">={min_ver}", |
| 81 | + status, |
| 82 | + ) |
| 83 | + except ImportError: |
| 84 | + if required: |
| 85 | + status = "[red]MISSING[/]" |
| 86 | + issues.append(f"Install {pkg_name}: pip install '{pkg_name}>={min_ver}'") |
| 87 | + else: |
| 88 | + status = "[dim]not installed[/]" |
| 89 | + |
| 90 | + table.add_row( |
| 91 | + pkg_name, |
| 92 | + "yes" if required else "optional", |
| 93 | + "—", |
| 94 | + f">={min_ver}", |
| 95 | + status, |
| 96 | + ) |
| 97 | + |
| 98 | + console.print(table) |
| 99 | + |
| 100 | + # Summary |
| 101 | + if issues: |
| 102 | + console.print(f"\n[yellow]Found {len(issues)} issue(s):[/]") |
| 103 | + for issue in issues: |
| 104 | + console.print(f" [red]>[/] {issue}") |
| 105 | + console.print("\n[dim]Fix all: pip install -U " + " ".join( |
| 106 | + f"'{pkg_name}>={min_ver}'" |
| 107 | + for _, pkg_name, min_ver, required in DEPS |
| 108 | + if required |
| 109 | + ) + "[/]") |
| 110 | + else: |
| 111 | + console.print("\n[bold green]All checks passed![/] Your environment is ready.") |
| 112 | + |
| 113 | + |
| 114 | +def _check_gpu(): |
| 115 | + """Check GPU availability and display info.""" |
| 116 | + try: |
| 117 | + import torch |
| 118 | + |
| 119 | + if torch.cuda.is_available(): |
| 120 | + gpu_count = torch.cuda.device_count() |
| 121 | + gpus = [] |
| 122 | + for idx in range(gpu_count): |
| 123 | + name = torch.cuda.get_device_name(idx) |
| 124 | + mem = torch.cuda.get_device_properties(idx) |
| 125 | + total_gb = getattr(mem, "total_memory", getattr(mem, "total_mem", 0)) |
| 126 | + total_gb = total_gb / (1024 ** 3) |
| 127 | + gpus.append(f" GPU {idx}: [bold]{name}[/] ({total_gb:.1f} GB)") |
| 128 | + gpu_info = "\n".join(gpus) |
| 129 | + cuda_ver = torch.version.cuda or "N/A" |
| 130 | + console.print( |
| 131 | + Panel( |
| 132 | + f"CUDA: [bold green]available[/] (v{cuda_ver})\n" |
| 133 | + f"GPUs: [bold]{gpu_count}[/]\n{gpu_info}", |
| 134 | + title="GPU", |
| 135 | + ) |
| 136 | + ) |
| 137 | + elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available(): |
| 138 | + console.print( |
| 139 | + Panel( |
| 140 | + "Backend: [bold green]MPS (Apple Silicon)[/]\n" |
| 141 | + "Status: [bold green]available[/]", |
| 142 | + title="GPU", |
| 143 | + ) |
| 144 | + ) |
| 145 | + else: |
| 146 | + console.print( |
| 147 | + Panel( |
| 148 | + "Backend: [bold yellow]CPU only[/]\n" |
| 149 | + "Warning: Training will be slow without GPU.", |
| 150 | + title="GPU", |
| 151 | + ) |
| 152 | + ) |
| 153 | + except ImportError: |
| 154 | + console.print( |
| 155 | + Panel( |
| 156 | + "Backend: [red]unknown (torch not installed)[/]", |
| 157 | + title="GPU", |
| 158 | + ) |
| 159 | + ) |
| 160 | + |
| 161 | + |
| 162 | +def _version_ok(installed: str, minimum: str) -> bool: |
| 163 | + """Check if installed version meets minimum requirement.""" |
| 164 | + try: |
| 165 | + inst_parts = [int(x) for x in installed.split(".")[:3]] |
| 166 | + min_parts = [int(x) for x in minimum.split(".")[:3]] |
| 167 | + # Pad to same length |
| 168 | + while len(inst_parts) < 3: |
| 169 | + inst_parts.append(0) |
| 170 | + while len(min_parts) < 3: |
| 171 | + min_parts.append(0) |
| 172 | + return inst_parts >= min_parts |
| 173 | + except (ValueError, AttributeError): |
| 174 | + return True # Can't parse, assume OK |
0 commit comments