liguang0115 commited on
Commit
226e278
·
1 Parent(s): 531a707

Enhance install_croco function in app.py by adding checks for source file existence, improved CUDA architecture handling, and a cleanup process for the build directory. Implement an alternative compilation method using setup.py if the primary method fails, ensuring robust error handling and better user feedback during the installation of the curope CUDA extension.

Browse files
Files changed (1) hide show
  1. app.py +87 -18
app.py CHANGED
@@ -13,46 +13,115 @@ def install_croco():
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
 
 
13
  """
14
  try:
15
  import os
16
+ import shutil
17
  from torch.utils.cpp_extension import load
18
 
19
  # Path to the curope source files
20
  curope_path = "./extern/CUT3R/src/croco/models/curope"
21
 
22
+ # Check if source files exist
23
+ cpp_file = os.path.join(curope_path, "curope.cpp")
24
+ cu_file = os.path.join(curope_path, "kernels.cu")
25
+
26
+ if not os.path.exists(cpp_file) or not os.path.exists(cu_file):
27
+ print(f"Source files not found: {cpp_file}, {cu_file}")
28
+ return False
29
+
30
+ print(f"Found source files: {cpp_file}, {cu_file}")
31
+
32
  # Define source files
33
+ sources = [cpp_file, cu_file]
 
 
 
34
 
35
+ # Get CUDA architectures - be more conservative
36
  if torch.cuda.is_available():
37
+ try:
38
+ # Get the current GPU architecture
39
+ capability = torch.cuda.get_device_capability()
40
+ arch_code = f"compute_{capability[0]}{capability[1]}"
41
+ sm_code = f"sm_{capability[0]}{capability[1]}"
42
+ all_cuda_archs = ['-gencode', f'arch={arch_code},code={sm_code}']
43
+ print(f"Using CUDA architecture: {arch_code}")
44
+ except:
45
+ # Fallback to common architectures
46
+ all_cuda_archs = [
47
+ '-gencode', 'arch=compute_70,code=sm_70',
48
+ '-gencode', 'arch=compute_80,code=sm_80',
49
+ ]
50
  else:
51
+ print("CUDA not available!")
52
+ return False
53
+
54
+ # Create build directory in current working directory
55
+ build_dir = os.path.abspath("./curope_build_temp")
56
+ if os.path.exists(build_dir):
57
+ shutil.rmtree(build_dir)
58
+ os.makedirs(build_dir, exist_ok=True)
59
+ print(f"Using build directory: {build_dir}")
60
 
61
  # Compile the extension
62
+ print("Starting CUDA compilation...")
63
  curope = load(
64
  name="curope",
65
  sources=sources,
66
  extra_cflags=["-O3"],
67
+ extra_cuda_cflags=["-O3", "--use_fast_math"] + all_cuda_archs,
68
  verbose=True,
69
+ build_directory=build_dir,
70
+ with_cuda=True
71
  )
72
 
73
  print("Successfully compiled curope CUDA extension")
74
+
75
+ # Clean up build directory
76
+ try:
77
+ shutil.rmtree(build_dir)
78
+ except:
79
+ pass
80
+
81
  return True
82
 
83
  except Exception as e:
84
+ print(f"Method 1 failed: {e}")
85
+ print("Trying alternative compilation method...")
86
+
87
+ # Alternative method: use setup.py build_ext --inplace
88
+ try:
89
+ import subprocess
90
+ import sys
91
+ import os
92
+
93
+ # Change to curope directory
94
+ original_cwd = os.getcwd()
95
+ curope_path = "./extern/CUT3R/src/croco/models/curope"
96
+ os.chdir(curope_path)
97
+
98
+ # Set CUDA environment variables to ensure they're available
99
+ env = os.environ.copy()
100
+ if torch.cuda.is_available():
101
+ cuda_home = os.environ.get('CUDA_HOME') or os.environ.get('CUDA_PATH')
102
+ if cuda_home:
103
+ env['CUDA_HOME'] = cuda_home
104
+ env['CUDA_PATH'] = cuda_home
105
+
106
+ # Run setup.py build_ext --inplace within the GPU context
107
+ result = subprocess.run([
108
+ sys.executable, "setup.py", "build_ext", "--inplace"
109
+ ], env=env, capture_output=True, text=True)
110
+
111
+ os.chdir(original_cwd)
112
+
113
+ if result.returncode == 0:
114
+ print("Successfully compiled curope using setup.py")
115
+ return True
116
+ else:
117
+ print(f"Setup.py compilation failed: {result.stderr}")
118
+ return False
119
+
120
+ except Exception as e2:
121
+ print(f"Alternative method also failed: {e2}")
122
+ import traceback
123
+ traceback.print_exc()
124
+ return False
125
 
126
  install_croco()
127