Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -79,15 +79,25 @@ def measure_time(func):
|
|
79 |
# GPU ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ ํจ์
|
80 |
def clear_gpu_memory():
|
81 |
"""๊ฐ๋ ฅํ GPU ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ"""
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
gc.collect()
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
188 |
-
logger.info("
|
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 |
-
|
|
|
|
|
|
|
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) # ๋์ ์คํ ์ ํ
|