-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpatch_pytorch.py
More file actions
69 lines (58 loc) · 2.34 KB
/
Copy pathpatch_pytorch.py
File metadata and controls
69 lines (58 loc) · 2.34 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
#!/usr/bin/env python3
"""Patch PyTorch to gracefully handle missing CUDA libraries.
Wraps torch/__init__.py's `_load_global_deps()` call in a try/except so an
AppImage built with CPU-only PyTorch still starts on machines with no CUDA
libraries present.
Exits non-zero if it could not find anything to patch. That matters: this runs
inside container-build.sh, and the previous version simply fell out of its
search loop, rewrote the file unchanged, printed a success tick and exited 0.
A PyTorch upgrade that renamed or moved the call would therefore produce an
AppImage that crashes on every machine without CUDA, with nothing in the build
log to show the patch had quietly become a no-op.
"""
import sys
# Written into the patched source so a re-run can recognise its own work
# instead of wrapping the call a second time.
MARKER = "# TalkType: tolerate missing CUDA libraries"
if len(sys.argv) != 2:
print("Usage: patch_pytorch.py <path_to_torch/__init__.py>", file=sys.stderr)
sys.exit(1)
torch_init = sys.argv[1]
try:
with open(torch_init, 'r') as f:
lines = f.readlines()
except OSError as e:
print(f"✗ Could not read {torch_init}: {e}", file=sys.stderr)
sys.exit(1)
if any(MARKER in line for line in lines):
print("✓ PyTorch already patched — leaving it alone")
sys.exit(0)
# Find and wrap the _load_global_deps() call
patched = False
for i, line in enumerate(lines):
if line.strip() == '_load_global_deps()':
indent = line[:len(line) - len(line.lstrip())]
lines[i] = (
f'{indent}try: {MARKER}\n'
f'{indent} _load_global_deps()\n'
f'{indent}except Exception:\n'
f'{indent} pass # Ignore CUDA errors\n'
)
patched = True
break
if not patched:
print(
f"✗ Could not find '_load_global_deps()' in {torch_init}.\n"
f" PyTorch's startup code has changed shape, so this patch no longer\n"
f" applies. Building on would ship an AppImage that crashes on any\n"
f" machine without CUDA libraries. Fix the patch before releasing.",
file=sys.stderr,
)
sys.exit(1)
try:
with open(torch_init, 'w') as f:
f.writelines(lines)
except OSError as e:
print(f"✗ Could not write {torch_init}: {e}", file=sys.stderr)
sys.exit(1)
print("✓ PyTorch patched for CPU/GPU flexibility")