lalopenguin commited on
Commit
e4119ec
·
1 Parent(s): a7df645

Memory optimizations for Zero GPU

Browse files
Files changed (1) hide show
  1. app.py +38 -21
app.py CHANGED
@@ -155,7 +155,7 @@ input[type="range"] {
155
  # ============================================
156
  # VIDEO GENERATION FUNCTION
157
  # ============================================
158
- @spaces.GPU(duration=120)
159
  def generate_video(
160
  prompt,
161
  negative_prompt,
@@ -168,45 +168,62 @@ def generate_video(
168
  fps,
169
  progress=gr.Progress(track_tqdm=True)
170
  ):
 
 
171
  if not prompt or prompt.strip() == "":
172
  gr.Warning("Please enter a prompt to generate a video.")
173
  return None
174
 
175
  progress(0, desc="Loading model...")
176
 
177
- # Load model inside GPU context
 
 
 
 
178
  pipe = LTXPipeline.from_pretrained(
179
  "Lightricks/LTX-Video",
180
- torch_dtype=torch.bfloat16
 
 
181
  )
182
- pipe.to("cuda")
183
- pipe.enable_model_cpu_offload()
 
 
 
184
 
185
  # Handle seed
186
  if seed == -1:
187
  seed = random.randint(0, 2**32 - 1)
188
 
189
- generator = torch.Generator(device="cuda").manual_seed(int(seed))
190
 
191
  progress(0.1, desc="Generating video frames...")
192
 
193
- # Generate video
194
- output = pipe(
195
- prompt=prompt,
196
- negative_prompt=negative_prompt if negative_prompt else None,
197
- num_frames=int(num_frames),
198
- guidance_scale=float(guidance_scale),
199
- num_inference_steps=int(num_inference_steps),
200
- generator=generator,
201
- height=int(height),
202
- width=int(width),
203
- )
 
204
 
205
  progress(0.9, desc="Exporting video...")
206
 
207
  # Export to video file
208
  video_path = export_to_video(output.frames[0], fps=int(fps))
209
 
 
 
 
 
 
210
  progress(1.0, desc="Complete!")
211
 
212
  return video_path
@@ -285,8 +302,8 @@ def create_ui():
285
  num_frames = gr.Slider(
286
  label="Frames",
287
  minimum=9,
288
- maximum=97,
289
- value=41,
290
  step=8,
291
  info="More frames = longer video"
292
  )
@@ -305,8 +322,8 @@ def create_ui():
305
  num_inference_steps = gr.Slider(
306
  label="Inference Steps",
307
  minimum=10,
308
- maximum=50,
309
- value=30,
310
  step=5,
311
  info="More steps = higher quality"
312
  )
 
155
  # ============================================
156
  # VIDEO GENERATION FUNCTION
157
  # ============================================
158
+ @spaces.GPU(duration=180)
159
  def generate_video(
160
  prompt,
161
  negative_prompt,
 
168
  fps,
169
  progress=gr.Progress(track_tqdm=True)
170
  ):
171
+ import gc
172
+
173
  if not prompt or prompt.strip() == "":
174
  gr.Warning("Please enter a prompt to generate a video.")
175
  return None
176
 
177
  progress(0, desc="Loading model...")
178
 
179
+ # Clear memory before loading
180
+ gc.collect()
181
+ torch.cuda.empty_cache() if torch.cuda.is_available() else None
182
+
183
+ # Load model with memory optimizations
184
  pipe = LTXPipeline.from_pretrained(
185
  "Lightricks/LTX-Video",
186
+ torch_dtype=torch.float16,
187
+ low_cpu_mem_usage=True,
188
+ use_safetensors=True,
189
  )
190
+
191
+ # Use CPU offload for memory efficiency (don't call .to("cuda") with this)
192
+ pipe.enable_sequential_cpu_offload()
193
+ pipe.vae.enable_slicing()
194
+ pipe.vae.enable_tiling()
195
 
196
  # Handle seed
197
  if seed == -1:
198
  seed = random.randint(0, 2**32 - 1)
199
 
200
+ generator = torch.Generator(device="cpu").manual_seed(int(seed))
201
 
202
  progress(0.1, desc="Generating video frames...")
203
 
204
+ # Generate video with memory-efficient settings
205
+ with torch.inference_mode():
206
+ output = pipe(
207
+ prompt=prompt,
208
+ negative_prompt=negative_prompt if negative_prompt else None,
209
+ num_frames=int(num_frames),
210
+ guidance_scale=float(guidance_scale),
211
+ num_inference_steps=int(num_inference_steps),
212
+ generator=generator,
213
+ height=int(height),
214
+ width=int(width),
215
+ )
216
 
217
  progress(0.9, desc="Exporting video...")
218
 
219
  # Export to video file
220
  video_path = export_to_video(output.frames[0], fps=int(fps))
221
 
222
+ # Cleanup
223
+ del pipe
224
+ gc.collect()
225
+ torch.cuda.empty_cache() if torch.cuda.is_available() else None
226
+
227
  progress(1.0, desc="Complete!")
228
 
229
  return video_path
 
302
  num_frames = gr.Slider(
303
  label="Frames",
304
  minimum=9,
305
+ maximum=57,
306
+ value=25,
307
  step=8,
308
  info="More frames = longer video"
309
  )
 
322
  num_inference_steps = gr.Slider(
323
  label="Inference Steps",
324
  minimum=10,
325
+ maximum=40,
326
+ value=20,
327
  step=5,
328
  info="More steps = higher quality"
329
  )