-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure_gpu.py
More file actions
67 lines (55 loc) · 2.56 KB
/
Copy pathconfigure_gpu.py
File metadata and controls
67 lines (55 loc) · 2.56 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
import os
import sys
def main():
# 1. Identify active virtual env path
venv_path = sys.prefix
if venv_path == sys.base_prefix:
print("[ERROR] No virtual environment active. Please activate .venv-3.11 first!")
sys.exit(1)
# 2. Find site-packages
site_packages = [p for p in sys.path if "site-packages" in p]
if not site_packages:
print("[ERROR] Could not locate site-packages directory.")
sys.exit(1)
site_packages_path = site_packages[0]
nvidia_dir = os.path.join(site_packages_path, "nvidia")
if not os.path.exists(nvidia_dir):
print("[ERROR] NVIDIA package directory not found in site-packages.")
print(" Please make sure you successfully ran: pip install \"tensorflow[and-cuda]\"")
sys.exit(1)
# 3. Find all lib sub-directories inside the nvidia package folder
lib_paths = []
for d in os.listdir(nvidia_dir):
lib_dir = os.path.join(nvidia_dir, d, "lib")
if os.path.isdir(lib_dir):
lib_paths.append(lib_dir)
# Also add the WSL driver pass-through directory
lib_paths.append("/usr/lib/wsl/lib")
# 4. Construct the LD_LIBRARY_PATH value
ld_library_path_val = ":".join(lib_paths)
# 5. Inject it into the activation script
activate_script = os.path.join(venv_path, "bin", "activate")
if not os.path.exists(activate_script):
print(f"[ERROR] Could not find activation script at: {activate_script}")
sys.exit(1)
with open(activate_script, "r") as f:
content = f.read()
# Avoid duplicate injections
if "LD_LIBRARY_PATH" in content:
print("[INFO] Activation script already has LD_LIBRARY_PATH configuration. Skipping injection.")
else:
injection = (
"\n# =========================================================================\n"
"# AUTOMATIC TENSORFLOW GPU CONFIGURATION\n"
"# =========================================================================\n"
f"export LD_LIBRARY_PATH=\"{ld_library_path_val}:$LD_LIBRARY_PATH\"\n"
"# =========================================================================\n"
)
with open(activate_script, "a") as f:
f.write(injection)
print(f"[SUCCESS] Injected GPU libraries path successfully into: {activate_script}")
print("\n[IMPORTANT] To apply these changes:")
print(" 1. Deactivate current session: deactivate")
print(" 2. Reactivate virtual env: source .venv-3.11/bin/activate")
if __name__ == "__main__":
main()