-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_env.py
More file actions
212 lines (178 loc) · 6.68 KB
/
setup_env.py
File metadata and controls
212 lines (178 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
"""
setup_env.py — Universal cross-platform setup for torch-amd-setup
==================================================================
Works on Windows, Linux, macOS — no bash, no batch files needed.
Uses only Python standard library + subprocess.
Usage:
python setup_env.py # auto-detect and install
python setup_env.py --check # verify environment
python setup_env.py --directml # Windows AMD GPU (Python 3.11 req)
python setup_env.py --rocm # Linux/WSL2 AMD GPU
python setup_env.py --cuda # NVIDIA GPU
python setup_env.py --mps # macOS Apple Silicon
python setup_env.py --cpu # CPU only (any OS)
Python: 3.8+ for this script. DirectML: Python 3.11 only (ABI ceiling).
"""
import argparse
import os
import platform
import shutil
import subprocess
import sys
OS = platform.system()
PYTHON = sys.executable
PY_VER = sys.version_info[:2]
def banner(msg: str):
print("=" * 66)
print(f" {msg}")
print("=" * 66)
def run(cmd: list[str]):
print(f" $ {' '.join(cmd)}")
subprocess.run(cmd, check=True)
def check_env():
banner("Environment Check — torch-amd-setup")
print(f" Python : {sys.version}")
print(f" OS : {OS} {platform.version()}")
print(f" Machine : {platform.machine()}")
print()
# torch
try:
import torch
print(f" torch : OK {torch.__version__}")
cuda = torch.cuda.is_available()
if cuda:
hip = getattr(torch.version, "hip", None)
label = "ROCm" if hip else "CUDA"
print(f" {label:<12}: OK {torch.cuda.get_device_name(0)}")
else:
print(" CUDA/ROCm : not available")
mps = hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
print(f" MPS (Apple) : {'OK' if mps else 'not available'}")
except ImportError:
print(" torch : NOT INSTALLED")
if OS == "Windows":
try:
import torch_directml as dml
gname = dml.device_name(0) if hasattr(dml, "device_name") else "unknown"
print(f" DirectML : OK {dml.__version__} ({gname})")
except ImportError:
print(" DirectML : not installed (pip install torch-directml, Python 3.11 only)")
else:
print(f" DirectML : N/A (Windows only)")
# torch-amd-setup
try:
from torch_amd_setup import get_best_device, device_info
best = get_best_device()
print(f"\n amd-setup device: {best}")
info = device_info()
for k, v in list(info.items())[:6]:
print(f" {k:<25}: {v}")
except ImportError:
print("\n torch-amd-setup : NOT INSTALLED (pip install torch-amd-setup)")
print("=" * 66)
def _detect_best() -> str:
if OS == "Windows":
try:
import torch_directml # noqa: F401
return "directml"
except ImportError:
pass
try:
import torch
if torch.cuda.is_available():
return "rocm" if getattr(torch.version, "hip", None) else "cuda"
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return "mps"
except ImportError:
pass
return "cpu"
def _install_torch_amd_setup():
run([PYTHON, "-m", "pip", "install", "torch-amd-setup"])
def install_directml():
banner("Installing torch-directml + torch-amd-setup (Windows, Python 3.11)")
if PY_VER > (3, 11):
print(f"[ERROR] Python {PY_VER[0]}.{PY_VER[1]} — DirectML requires Python <= 3.11.")
print(" Create a 3.11 venv: py -3.11 -m venv .venv311")
print(" Activate: .venv311\\Scripts\\activate")
print(" Then re-run: python setup_env.py --directml")
sys.exit(1)
print(" Note: Installing torch-directml first — it pulls torch 2.4.1 automatically.")
print(" Do NOT pre-install torch (causes version conflicts).")
run([PYTHON, "-m", "pip", "install", "torch-directml"])
_install_torch_amd_setup()
check_env()
def install_rocm():
banner("Installing torch for AMD ROCm + torch-amd-setup (Linux/WSL2)")
if OS == "Windows":
print("[ERROR] ROCm not supported on Windows natively.")
print(" Use WSL2, or run: python setup_env.py --directml")
sys.exit(1)
run([PYTHON, "-m", "pip", "install", "torch",
"--index-url", "https://download.pytorch.org/whl/rocm6.1"])
_install_torch_amd_setup()
print()
print(" For RX 5700 XT (gfx1010) — add to ~/.bashrc:")
print(" export HSA_OVERRIDE_GFX_VERSION=10.3.0")
print()
check_env()
def install_cuda():
banner("Installing torch for NVIDIA CUDA + torch-amd-setup")
run([PYTHON, "-m", "pip", "install", "torch",
"--index-url", "https://download.pytorch.org/whl/cu121"])
_install_torch_amd_setup()
check_env()
def install_mps():
banner("Installing torch for Apple Silicon MPS + torch-amd-setup")
if OS != "Darwin":
print("[WARN] MPS is macOS-only. Continuing with CPU-capable torch.")
run([PYTHON, "-m", "pip", "install", "torch"])
_install_torch_amd_setup()
check_env()
def install_cpu():
banner("Installing torch (CPU only) + torch-amd-setup")
run([PYTHON, "-m", "pip", "install", "torch",
"--index-url", "https://download.pytorch.org/whl/cpu"])
_install_torch_amd_setup()
check_env()
def main():
parser = argparse.ArgumentParser(
description="Cross-platform setup for torch-amd-setup",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument("--check", action="store_true")
parser.add_argument("--directml", action="store_true")
parser.add_argument("--rocm", action="store_true")
parser.add_argument("--cuda", action="store_true")
parser.add_argument("--mps", action="store_true")
parser.add_argument("--cpu", action="store_true")
args = parser.parse_args()
if args.check:
check_env()
elif args.directml:
install_directml()
elif args.rocm:
install_rocm()
elif args.cuda:
install_cuda()
elif args.mps:
install_mps()
elif args.cpu:
install_cpu()
else:
banner("torch-amd-setup — Auto Setup")
best = _detect_best()
print(f" Detected best backend: {best}")
if best == "directml":
install_directml()
elif best == "rocm":
install_rocm()
elif best == "cuda":
install_cuda()
elif best == "mps":
install_mps()
else:
install_cpu()
if __name__ == "__main__":
main()