Erva Ulusoy commited on
Commit
89a4f79
·
1 Parent(s): 589e75e

potential fix for torch import error

Browse files
Files changed (1) hide show
  1. run_prothgt_app.py +50 -1
run_prothgt_app.py CHANGED
@@ -1,9 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
  from torch.nn import Linear
3
  from torch_geometric.nn import HGTConv, MLP
4
  import pandas as pd
5
  import yaml
6
- import os
7
  from datasets import load_dataset
8
  import gdown
9
  import copy
 
1
+ import os
2
+ import site
3
+ import glob
4
+ import subprocess
5
+
6
+ def _maybe_fix_torch_execstack() -> None:
7
+ """
8
+ Fix for environments that refuse to load shared objects requiring an executable stack
9
+ (e.g., newer glibc hardening). Some PyTorch wheels ship libtorch_cpu.so with the
10
+ execstack flag set, causing:
11
+ ImportError: libtorch_cpu.so: cannot enable executable stack ...
12
+
13
+ We clear the flag *before* importing torch.
14
+ """
15
+ if os.environ.get("PROTHGT_TORCH_EXECSTACK_FIXED") == "1":
16
+ return
17
+
18
+ # patchelf is installed via packages.txt in this repo.
19
+ patchelf = "patchelf"
20
+
21
+ paths = []
22
+ for fn in (getattr(site, "getsitepackages", None), getattr(site, "getusersitepackages", None)):
23
+ if fn is None:
24
+ continue
25
+ try:
26
+ p = fn()
27
+ if isinstance(p, str):
28
+ paths.append(p)
29
+ else:
30
+ paths.extend(list(p))
31
+ except Exception:
32
+ pass
33
+
34
+ targets = []
35
+ for p in paths:
36
+ targets += glob.glob(os.path.join(p, "torch", "lib", "libtorch_cpu.so"))
37
+ targets += glob.glob(os.path.join(p, "torch", "lib", "libtorch_python.so"))
38
+
39
+ for so in sorted(set(targets)):
40
+ try:
41
+ if os.path.exists(so):
42
+ subprocess.run([patchelf, "--clear-execstack", so], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
43
+ except Exception:
44
+ # Best-effort; if it fails we'll still try importing torch and surface the real error.
45
+ pass
46
+
47
+ os.environ["PROTHGT_TORCH_EXECSTACK_FIXED"] = "1"
48
+
49
+ _maybe_fix_torch_execstack()
50
+
51
  import torch
52
  from torch.nn import Linear
53
  from torch_geometric.nn import HGTConv, MLP
54
  import pandas as pd
55
  import yaml
 
56
  from datasets import load_dataset
57
  import gdown
58
  import copy