liguang0115 commited on
Commit
531a707
·
1 Parent(s): 28b109f

Refactor install_croco function in app.py to compile the curope CUDA extension using torch.utils.cpp_extension.load() instead of subprocess calls, improving compatibility with zerogpu. Added error handling and fallback to PyTorch implementation if compilation fails.

Browse files
Files changed (1) hide show
  1. app.py +47 -5
app.py CHANGED
@@ -3,14 +3,56 @@ import sys
3
  import spaces
4
  import torch
5
 
6
- @spaces.GPU(duration=10)
7
  @torch.autocast("cuda")
8
  @torch.no_grad()
9
  def install_croco():
10
- subprocess.check_call([
11
- sys.executable, "-m", "pip", "install", "-e",
12
- "./extern/CUT3R/src/croco/models/curope"
13
- ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  install_croco()
16
 
 
3
  import spaces
4
  import torch
5
 
6
+ @spaces.GPU
7
  @torch.autocast("cuda")
8
  @torch.no_grad()
9
  def install_croco():
10
+ """
11
+ Compile curope CUDA extension using torch.utils.cpp_extension.load()
12
+ This approach works better with zerogpu than subprocess calls.
13
+ """
14
+ try:
15
+ import os
16
+ from torch.utils.cpp_extension import load
17
+
18
+ # Path to the curope source files
19
+ curope_path = "./extern/CUT3R/src/croco/models/curope"
20
+
21
+ # Define source files
22
+ sources = [
23
+ os.path.join(curope_path, "curope.cpp"),
24
+ os.path.join(curope_path, "kernels.cu"),
25
+ ]
26
+
27
+ # Get CUDA architectures
28
+ if torch.cuda.is_available():
29
+ all_cuda_archs = torch.cuda.get_gencode_flags().replace("compute=", "arch=").split()
30
+ else:
31
+ # Fallback architectures if CUDA info is not available
32
+ all_cuda_archs = [
33
+ '-gencode', 'arch=compute_70,code=sm_70',
34
+ '-gencode', 'arch=compute_75,code=sm_75',
35
+ '-gencode', 'arch=compute_80,code=sm_80',
36
+ '-gencode', 'arch=compute_86,code=sm_86'
37
+ ]
38
+
39
+ # Compile the extension
40
+ curope = load(
41
+ name="curope",
42
+ sources=sources,
43
+ extra_cflags=["-O3"],
44
+ extra_cuda_cflags=["-O3", "--ptxas-options=-v", "--use_fast_math"] + all_cuda_archs,
45
+ verbose=True,
46
+ build_directory="./curope_build" # Specify build directory
47
+ )
48
+
49
+ print("Successfully compiled curope CUDA extension")
50
+ return True
51
+
52
+ except Exception as e:
53
+ print(f"Failed to compile curope CUDA extension: {e}")
54
+ print("Will fall back to PyTorch implementation")
55
+ return False
56
 
57
  install_croco()
58