seawolf2357 commited on
Commit
fde0767
ยท
verified ยท
1 Parent(s): 05707ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -20
app.py CHANGED
@@ -79,15 +79,25 @@ def measure_time(func):
79
  # GPU ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ ํ•จ์ˆ˜
80
  def clear_gpu_memory():
81
  """๊ฐ•๋ ฅํ•œ GPU ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ"""
82
- if torch.cuda.is_available():
83
- torch.cuda.empty_cache()
84
- torch.cuda.ipc_collect()
85
  gc.collect()
86
-
87
- # GPU ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ ๋กœ๊น…
88
- allocated = torch.cuda.memory_allocated() / 1024**3
89
- reserved = torch.cuda.memory_reserved() / 1024**3
90
- logger.info(f"GPU Memory - Allocated: {allocated:.2f}GB, Reserved: {reserved:.2f}GB")
 
 
 
 
 
 
 
 
 
 
91
 
92
  # ๋ชจ๋ธ ๊ด€๋ฆฌ์ž (์‹ฑ๊ธ€ํ†ค ํŒจํ„ด)
93
  class ModelManager:
@@ -125,7 +135,8 @@ class ModelManager:
125
  clear_gpu_memory()
126
 
127
  # ๋ชจ๋ธ ์ปดํฌ๋„ŒํŠธ ๋กœ๋“œ (๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ) - autocast ์ˆ˜์ •
128
- if torch.cuda.is_available():
 
129
  with torch.amp.autocast('cuda', enabled=False): # ์ˆ˜์ •๋œ ๋ถ€๋ถ„
130
  image_encoder = CLIPVisionModel.from_pretrained(
131
  config.model_id,
@@ -141,18 +152,18 @@ class ModelManager:
141
  low_cpu_mem_usage=True
142
  )
143
  else:
144
- # CPU ํ™˜๊ฒฝ
145
  image_encoder = CLIPVisionModel.from_pretrained(
146
  config.model_id,
147
  subfolder="image_encoder",
148
- torch_dtype=torch.float32,
149
  low_cpu_mem_usage=True
150
  )
151
 
152
  vae = AutoencoderKLWan.from_pretrained(
153
  config.model_id,
154
  subfolder="vae",
155
- torch_dtype=torch.float32,
156
  low_cpu_mem_usage=True
157
  )
158
 
@@ -160,7 +171,7 @@ class ModelManager:
160
  config.model_id,
161
  vae=vae,
162
  image_encoder=image_encoder,
163
- torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
164
  low_cpu_mem_usage=True,
165
  use_safetensors=True
166
  )
@@ -184,12 +195,16 @@ class ModelManager:
184
 
185
  # GPU ์ตœ์ ํ™” ์„ค์ •
186
  if hasattr(spaces, 'GPU'): # Zero GPU ํ™˜๊ฒฝ
187
- self._pipe.enable_model_cpu_offload()
188
- logger.info("CPU offload enabled for Zero GPU")
189
  elif config.enable_model_cpu_offload and torch.cuda.is_available():
190
  self._pipe.enable_model_cpu_offload()
 
191
  elif torch.cuda.is_available():
192
  self._pipe.to("cuda")
 
 
 
193
 
194
  if config.enable_vae_slicing:
195
  self._pipe.enable_vae_slicing()
@@ -297,8 +312,8 @@ class VideoGenerator:
297
  if height > 832 or width > 832: # ํ•œ ๋ณ€์˜ ์ตœ๋Œ€ ๊ธธ์ด
298
  return False, "๐Ÿ“ In Zero GPU environment, maximum dimension is 832 pixels"
299
 
300
- # GPU ๋ฉ”๋ชจ๋ฆฌ ์ฒดํฌ
301
- if torch.cuda.is_available():
302
  try:
303
  free_memory = torch.cuda.get_device_properties(0).total_memory - torch.cuda.memory_allocated()
304
  required_memory = (height * width * 3 * 8 * duration * self.config.fixed_fps) / (1024**3)
@@ -378,6 +393,10 @@ def generate_video(input_image, prompt, height, width,
378
  raise gr.Error("โณ Another video is being generated. Please wait...")
379
 
380
  try:
 
 
 
 
381
  progress(0.1, desc="๐Ÿ” Validating inputs...")
382
 
383
  # Zero GPU ํ™˜๊ฒฝ์—์„œ ์ถ”๊ฐ€ ๊ฒ€์ฆ
@@ -417,7 +436,9 @@ def generate_video(input_image, prompt, height, width,
417
  progress(0.4, desc="๐ŸŽฌ Generating video frames...")
418
 
419
  # ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ธ ์ƒ์„ฑ
420
- if torch.cuda.is_available():
 
 
421
  with torch.inference_mode(), torch.amp.autocast('cuda', enabled=True): # ์ˆ˜์ •๋œ ๋ถ€๋ถ„
422
  try:
423
  output_frames_list = pipe(
@@ -857,8 +878,11 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
857
  )
858
 
859
  if __name__ == "__main__":
860
- # ์ดˆ๊ธฐ ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ
861
- clear_gpu_memory()
 
 
 
862
 
863
  # ์•ฑ ์‹คํ–‰
864
  demo.queue(concurrency_count=1) # ๋™์‹œ ์‹คํ–‰ ์ œํ•œ
 
79
  # GPU ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ ํ•จ์ˆ˜
80
  def clear_gpu_memory():
81
  """๊ฐ•๋ ฅํ•œ GPU ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ"""
82
+ # Zero GPU ํ™˜๊ฒฝ์—์„œ๋Š” ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์—์„œ CUDA ์ดˆ๊ธฐํ™” ๋ฐฉ์ง€
83
+ if hasattr(spaces, 'GPU'):
84
+ # Zero GPU ํ™˜๊ฒฝ์—์„œ๋Š” @spaces.GPU ๋‚ด์—์„œ๋งŒ GPU ์ž‘์—… ์ˆ˜ํ–‰
85
  gc.collect()
86
+ return
87
+
88
+ if torch.cuda.is_available():
89
+ try:
90
+ torch.cuda.empty_cache()
91
+ torch.cuda.ipc_collect()
92
+ gc.collect()
93
+
94
+ # GPU ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ ๋กœ๊น…
95
+ allocated = torch.cuda.memory_allocated() / 1024**3
96
+ reserved = torch.cuda.memory_reserved() / 1024**3
97
+ logger.info(f"GPU Memory - Allocated: {allocated:.2f}GB, Reserved: {reserved:.2f}GB")
98
+ except Exception as e:
99
+ logger.warning(f"GPU memory clear failed: {e}")
100
+ gc.collect()
101
 
102
  # ๋ชจ๋ธ ๊ด€๋ฆฌ์ž (์‹ฑ๊ธ€ํ†ค ํŒจํ„ด)
103
  class ModelManager:
 
135
  clear_gpu_memory()
136
 
137
  # ๋ชจ๋ธ ์ปดํฌ๋„ŒํŠธ ๋กœ๋“œ (๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ) - autocast ์ˆ˜์ •
138
+ if torch.cuda.is_available() and not hasattr(spaces, 'GPU'):
139
+ # ์ผ๋ฐ˜ GPU ํ™˜๊ฒฝ
140
  with torch.amp.autocast('cuda', enabled=False): # ์ˆ˜์ •๋œ ๋ถ€๋ถ„
141
  image_encoder = CLIPVisionModel.from_pretrained(
142
  config.model_id,
 
152
  low_cpu_mem_usage=True
153
  )
154
  else:
155
+ # CPU ํ™˜๊ฒฝ ๋˜๋Š” Zero GPU ํ™˜๊ฒฝ
156
  image_encoder = CLIPVisionModel.from_pretrained(
157
  config.model_id,
158
  subfolder="image_encoder",
159
+ torch_dtype=torch.float16 if hasattr(spaces, 'GPU') else torch.float32,
160
  low_cpu_mem_usage=True
161
  )
162
 
163
  vae = AutoencoderKLWan.from_pretrained(
164
  config.model_id,
165
  subfolder="vae",
166
+ torch_dtype=torch.float16 if hasattr(spaces, 'GPU') else torch.float32,
167
  low_cpu_mem_usage=True
168
  )
169
 
 
171
  config.model_id,
172
  vae=vae,
173
  image_encoder=image_encoder,
174
+ torch_dtype=torch.bfloat16 if (torch.cuda.is_available() or hasattr(spaces, 'GPU')) else torch.float32,
175
  low_cpu_mem_usage=True,
176
  use_safetensors=True
177
  )
 
195
 
196
  # GPU ์ตœ์ ํ™” ์„ค์ •
197
  if hasattr(spaces, 'GPU'): # Zero GPU ํ™˜๊ฒฝ
198
+ # Zero GPU ํ™˜๊ฒฝ์—์„œ๋Š” ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ๋จ
199
+ logger.info("Model loaded for Zero GPU environment")
200
  elif config.enable_model_cpu_offload and torch.cuda.is_available():
201
  self._pipe.enable_model_cpu_offload()
202
+ logger.info("CPU offload enabled")
203
  elif torch.cuda.is_available():
204
  self._pipe.to("cuda")
205
+ logger.info("Model moved to CUDA")
206
+ else:
207
+ logger.info("Running on CPU")
208
 
209
  if config.enable_vae_slicing:
210
  self._pipe.enable_vae_slicing()
 
312
  if height > 832 or width > 832: # ํ•œ ๋ณ€์˜ ์ตœ๋Œ€ ๊ธธ์ด
313
  return False, "๐Ÿ“ In Zero GPU environment, maximum dimension is 832 pixels"
314
 
315
+ # GPU ๋ฉ”๋ชจ๋ฆฌ ์ฒดํฌ (Zero GPU ํ™˜๊ฒฝ์ด ์•„๋‹ ๋•Œ๋งŒ)
316
+ if torch.cuda.is_available() and not hasattr(spaces, 'GPU'):
317
  try:
318
  free_memory = torch.cuda.get_device_properties(0).total_memory - torch.cuda.memory_allocated()
319
  required_memory = (height * width * 3 * 8 * duration * self.config.fixed_fps) / (1024**3)
 
393
  raise gr.Error("โณ Another video is being generated. Please wait...")
394
 
395
  try:
396
+ # Zero GPU ํ™˜๊ฒฝ์—์„œ๋Š” ์ด์ œ GPU ์‚ฌ์šฉ ๊ฐ€๋Šฅ
397
+ if hasattr(spaces, 'GPU') and torch.cuda.is_available():
398
+ logger.info("GPU initialized in Zero GPU environment")
399
+
400
  progress(0.1, desc="๐Ÿ” Validating inputs...")
401
 
402
  # Zero GPU ํ™˜๊ฒฝ์—์„œ ์ถ”๊ฐ€ ๊ฒ€์ฆ
 
436
  progress(0.4, desc="๐ŸŽฌ Generating video frames...")
437
 
438
  # ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ธ ์ƒ์„ฑ
439
+ device = "cuda" if torch.cuda.is_available() else "cpu"
440
+
441
+ if device == "cuda":
442
  with torch.inference_mode(), torch.amp.autocast('cuda', enabled=True): # ์ˆ˜์ •๋œ ๋ถ€๋ถ„
443
  try:
444
  output_frames_list = pipe(
 
878
  )
879
 
880
  if __name__ == "__main__":
881
+ # Zero GPU ํ™˜๊ฒฝ ์ฒดํฌ ๋กœ๊น…
882
+ if hasattr(spaces, 'GPU'):
883
+ logger.info("Running in Zero GPU environment")
884
+ else:
885
+ logger.info("Running in standard environment")
886
 
887
  # ์•ฑ ์‹คํ–‰
888
  demo.queue(concurrency_count=1) # ๋™์‹œ ์‹คํ–‰ ์ œํ•œ