andrew3d commited on
Commit
be08e83
Β·
verified Β·
1 Parent(s): d6b0d62

Update app.py: override CUDA defaults for CPU environment

Browse files
Files changed (1) hide show
  1. app.py +35 -2
app.py CHANGED
@@ -88,6 +88,8 @@ _ensure_hi3dgen_available()
88
  # ---------------------------------------------------------------------------
89
 
90
  def _ensure_xformers_stub():
 
 
91
  import sys
92
  # If xformers is already available, do nothing.
93
  if 'xformers.ops' in sys.modules:
@@ -116,9 +118,40 @@ def _ensure_xformers_stub():
116
  sys.modules['xformers'] = xformers_mod
117
  sys.modules['xformers.ops'] = ops_mod
118
 
119
- # Ensure the xformers stub is registered before importing Hi3DGen
120
  _ensure_xformers_stub()
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  from hi3dgen.pipelines import Hi3DGenPipeline
123
  import trimesh
124
  MAX_SEED = np.iinfo(np.int32).max
@@ -357,4 +390,4 @@ if __name__ == "__main__":
357
  normal_predictor = torch.hub.load("hugoycj/StableNormal", "StableNormal_turbo", trust_repo=True, yoso_version='yoso-normal-v1-8-1', local_cache_dir='./weights')
358
 
359
  # Launch the app
360
- demo.launch(share=False, server_name="0.0.0.0")
 
88
  # ---------------------------------------------------------------------------
89
 
90
  def _ensure_xformers_stub():
91
+
92
+ # ---------------------------------------------------------------------------
93
  import sys
94
  # If xformers is already available, do nothing.
95
  if 'xformers.ops' in sys.modules:
 
118
  sys.modules['xformers'] = xformers_mod
119
  sys.modules['xformers.ops'] = ops_mod
120
 
121
+ # Ensure the xformers stub is registered before importing Hi3DGen
122
  _ensure_xformers_stub()
123
 
124
+ # ---------------------------------------------------------------------------
125
+ # Monkey-patch Hi3DGen to run on CPU
126
+ #
127
+ # Some utility functions and classes in the Hi3DGen codebase assume the
128
+ # presence of a CUDA device by default. Specifically, the function
129
+ # `construct_dense_grid` in `hi3dgen.representations.mesh.utils_cube` uses
130
+ # `device='cuda'` as its default argument, and the class
131
+ # `EnhancedMarchingCubes` in `hi3dgen.representations.mesh.cube2mesh` has
132
+ # a constructor that defaults to `device="cuda"`. On CPU-only Spaces, these
133
+ # defaults cause runtime errors when PyTorch attempts to allocate tensors on
134
+ # a non-existent GPU. To avoid this, we override the default arguments for
135
+ # these functions to use the CPU instead. If the patch cannot be applied
136
+ # (for example, if the module structure changes in a future version), we
137
+ # catch any exceptions and log a warning without stopping execution.
138
+ try:
139
+ from hi3dgen.representations.mesh import utils_cube
140
+ if hasattr(utils_cube.construct_dense_grid, '__defaults__'):
141
+ _defaults = list(utils_cube.construct_dense_grid.__defaults__ or ())
142
+ if _defaults and _defaults[-1] == 'cuda':
143
+ _defaults[-1] = 'cpu'
144
+ utils_cube.construct_dense_grid.__defaults__ = tuple(_defaults)
145
+
146
+ from hi3dgen.representations.mesh.cube2mesh import EnhancedMarchingCubes
147
+ if hasattr(EnhancedMarchingCubes.__init__, '__defaults__'):
148
+ _mc_defaults = list(EnhancedMarchingCubes.__init__.__defaults__ or ())
149
+ if _mc_defaults and _mc_defaults[-1] == 'cuda':
150
+ _mc_defaults[-1] = 'cpu'
151
+ EnhancedMarchingCubes.__init__.__defaults__ = tuple(_mc_defaults)
152
+ except Exception as _e:
153
+ print(f"Warning: failed to apply CPU device overrides: {_e}")
154
+
155
  from hi3dgen.pipelines import Hi3DGenPipeline
156
  import trimesh
157
  MAX_SEED = np.iinfo(np.int32).max
 
390
  normal_predictor = torch.hub.load("hugoycj/StableNormal", "StableNormal_turbo", trust_repo=True, yoso_version='yoso-normal-v1-8-1', local_cache_dir='./weights')
391
 
392
  # Launch the app
393
+ demo.launch(share=False, server_name="0.0.0.0")