aiqtech commited on
Commit
7619202
โ€ข
1 Parent(s): 92dcaec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -41
app.py CHANGED
@@ -82,12 +82,13 @@ torch.backends.cudnn.benchmark = True
82
 
83
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
84
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
85
- os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
86
  os.environ['SPCONV_ALGO'] = 'native'
87
  os.environ['SPARSE_BACKEND'] = 'native'
88
  os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
89
  os.environ['XFORMERS_FORCE_DISABLE_TRITON'] = '1'
90
  os.environ['XFORMERS_ENABLE_FLASH_ATTENTION'] = '1'
 
91
 
92
  # CUDA ์ดˆ๊ธฐํ™” ๋ฐฉ์ง€
93
  torch.set_grad_enabled(False)
@@ -205,6 +206,9 @@ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_stre
205
  return None, None
206
 
207
  try:
 
 
 
208
  if randomize_seed:
209
  seed = np.random.randint(0, MAX_SEED)
210
 
@@ -220,57 +224,82 @@ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_stre
220
 
221
  # GPU ์ž‘์—… ์‹œ์ž‘
222
  with torch.cuda.device(0):
223
- # ๋ชจ๋ธ์„ GPU๋กœ ์ด๋™
224
- g.trellis_pipeline.to('cuda')
225
-
226
- with torch.inference_mode(), torch.cuda.amp.autocast():
227
- outputs = g.trellis_pipeline.run(
228
- image,
229
- seed=seed,
230
- formats=["gaussian", "mesh"],
231
- preprocess_image=False,
232
- sparse_structure_sampler_params={
233
- "steps": ss_sampling_steps,
234
- "cfg_strength": ss_guidance_strength,
235
- },
236
- slat_sampler_params={
237
- "steps": slat_sampling_steps,
238
- "cfg_strength": slat_guidance_strength,
239
- },
240
- )
241
-
242
- # GPU์—์„œ ๋น„๋””์˜ค ๋ Œ๋”๋ง
243
- video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
244
- video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
245
-
246
- # CPU๋กœ ๋ฐ์ดํ„ฐ ์ด๋™
247
- video = [v.cpu().numpy() if torch.is_tensor(v) else v for v in video]
248
- video_geo = [v.cpu().numpy() if torch.is_tensor(v) else v for v in video_geo]
249
-
250
- video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
251
- new_trial_id = str(uuid.uuid4())
252
- video_path = f"{TMP_DIR}/{new_trial_id}.mp4"
253
- os.makedirs(os.path.dirname(video_path), exist_ok=True)
254
- imageio.mimsave(video_path, video, fps=15)
255
-
256
- # ์ƒํƒœ ์ €์žฅ ์ „์— CPU๋กœ ์ด๋™
257
- state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], new_trial_id)
258
 
259
- # ๋ชจ๋ธ์„ ๋‹ค์‹œ CPU๋กœ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  g.trellis_pipeline.to('cpu')
261
  torch.cuda.empty_cache()
262
-
263
- return state, video_path
264
 
265
  except Exception as e:
266
  print(f"Error in image_to_3d: {str(e)}")
267
- # ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ์—๋„ ๋ชจ๋ธ์„ CPU๋กœ ๋˜๋Œ๋ฆผ
268
  if hasattr(g.trellis_pipeline, 'to'):
269
  g.trellis_pipeline.to('cpu')
270
  torch.cuda.empty_cache()
 
271
  return None, None
272
 
273
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  @spaces.GPU
275
  def extract_glb(state: dict, mesh_simplify: float, texture_size: int) -> Tuple[str, str]:
276
  gs, mesh, trial_id = unpack_state(state)
 
82
 
83
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
84
  # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •
85
+ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512,garbage_collection_threshold:0.6"
86
  os.environ['SPCONV_ALGO'] = 'native'
87
  os.environ['SPARSE_BACKEND'] = 'native'
88
  os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
89
  os.environ['XFORMERS_FORCE_DISABLE_TRITON'] = '1'
90
  os.environ['XFORMERS_ENABLE_FLASH_ATTENTION'] = '1'
91
+ os.environ['TORCH_CUDA_MEMORY_ALLOCATOR'] = 'native'
92
 
93
  # CUDA ์ดˆ๊ธฐํ™” ๋ฐฉ์ง€
94
  torch.set_grad_enabled(False)
 
206
  return None, None
207
 
208
  try:
209
+ # CUDA ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ธฐํ™”
210
+ torch.cuda.empty_cache()
211
+
212
  if randomize_seed:
213
  seed = np.random.randint(0, MAX_SEED)
214
 
 
224
 
225
  # GPU ์ž‘์—… ์‹œ์ž‘
226
  with torch.cuda.device(0):
227
+ try:
228
+ # ๋ชจ๋ธ์„ GPU๋กœ ์ด๋™
229
+ g.trellis_pipeline.to('cuda')
230
+ torch.cuda.synchronize()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
+ with torch.inference_mode():
233
+ # ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„: 3D ์ƒ์„ฑ
234
+ outputs = g.trellis_pipeline.run(
235
+ image,
236
+ seed=seed,
237
+ formats=["gaussian", "mesh"],
238
+ preprocess_image=False,
239
+ sparse_structure_sampler_params={
240
+ "steps": ss_sampling_steps,
241
+ "cfg_strength": ss_guidance_strength,
242
+ },
243
+ slat_sampler_params={
244
+ "steps": slat_sampling_steps,
245
+ "cfg_strength": slat_guidance_strength,
246
+ },
247
+ )
248
+ torch.cuda.synchronize()
249
+
250
+ # ๋‘ ๋ฒˆ์งธ ๋‹จ๊ณ„: ๋น„๋””์˜ค ๋ Œ๋”๋ง
251
+ video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
252
+ torch.cuda.synchronize()
253
+
254
+ video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
255
+ torch.cuda.synchronize()
256
+
257
+ # CPU๋กœ ๋ฐ์ดํ„ฐ ์ด๋™ ๋ฐ ํ›„์ฒ˜๋ฆฌ
258
+ video = [v.cpu().numpy() if torch.is_tensor(v) else v for v in video]
259
+ video_geo = [v.cpu().numpy() if torch.is_tensor(v) else v for v in video_geo]
260
+
261
+ video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
262
+ new_trial_id = str(uuid.uuid4())
263
+ video_path = f"{TMP_DIR}/{new_trial_id}.mp4"
264
+ os.makedirs(os.path.dirname(video_path), exist_ok=True)
265
+ imageio.mimsave(video_path, video, fps=15)
266
+
267
+ # ์ƒํƒœ ์ €์žฅ
268
+ state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], new_trial_id)
269
+
270
+ return state, video_path
271
+
272
+ finally:
273
+ # ์ •๋ฆฌ ์ž‘์—…
274
  g.trellis_pipeline.to('cpu')
275
  torch.cuda.empty_cache()
276
+ torch.cuda.synchronize()
 
277
 
278
  except Exception as e:
279
  print(f"Error in image_to_3d: {str(e)}")
280
+ # ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ ์ •๋ฆฌ
281
  if hasattr(g.trellis_pipeline, 'to'):
282
  g.trellis_pipeline.to('cpu')
283
  torch.cuda.empty_cache()
284
+ torch.cuda.synchronize()
285
  return None, None
286
 
287
+ def clear_gpu_memory():
288
+ """GPU ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ •๋ฆฌํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜"""
289
+ if torch.cuda.is_available():
290
+ torch.cuda.empty_cache()
291
+ torch.cuda.synchronize()
292
+
293
+ def move_to_device(model, device):
294
+ """๋ชจ๋ธ์„ ์•ˆ์ „ํ•˜๊ฒŒ ๋””๋ฐ”์ด์Šค๋กœ ์ด๋™ํ•˜๋Š” ํ•จ์ˆ˜"""
295
+ try:
296
+ if hasattr(model, 'to'):
297
+ model.to(device)
298
+ if device == 'cuda':
299
+ torch.cuda.synchronize()
300
+ except Exception as e:
301
+ print(f"Error moving model to {device}: {str(e)}")
302
+
303
  @spaces.GPU
304
  def extract_glb(state: dict, mesh_simplify: float, texture_size: int) -> Tuple[str, str]:
305
  gs, mesh, trial_id = unpack_state(state)