Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files290 unmodified lines291292293294295296297298120 unmodified lines419420421420421422422423424424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475290 unmodified lines quantization=QuantizationPolicy.fp8_cast(),)
pipeline_cache = {}
# Preload all models for ZeroGPU tensor packing.# >>> REPLACE the "Preload all models" block with this one:print("Preloading models (pinning decoders/encoders but leaving transformer dynamic)...")120 unmodified lines tiling_config = TilingConfig.default() video_chunks_number = get_video_chunks_number(num_frames, tiling_config)
# >>> RUNTIME LoRA application (robust, multi-fallback) # We cannot rely on mutating the original descriptor (some implementations are immutable), # so create a fresh runtime descriptor and try multiple ways to install it. # >>> NEW: deterministic pipeline-per-strength approach runtime_strength = float(lora_strength) replaced = False
# 1) Try simple approach: build a new LoraPathStrengthAndSDOps runtime_lora = LoraPathStrengthAndSDOps(lora_path, runtime_strength, LTXV_LORA_COMFY_RENAMING_MAP) print(f"[LoRA] attempting to apply runtime LoRA (strength={runtime_strength})")
# Try a few likely places to replace the descriptor used by the pipeline/ledger. try: # common attribute on pipeline if hasattr(pipeline, "loras"): try: pipeline.loras = [runtime_lora] replaced = True print("[LoRA] replaced pipeline.loras") except Exception as e: print(f"[LoRA] pipeline.loras assignment failed: {e}") except Exception: pass
try: # common attribute on the model ledger if hasattr(pipeline, "model_ledger") and hasattr(pipeline.model_ledger, "loras"): try: pipeline.model_ledger.loras = [runtime_lora] replaced = True print("[LoRA] replaced pipeline.model_ledger.loras") except Exception as e: print(f"[LoRA] pipeline.model_ledger.loras assignment failed: {e}") except Exception: pass
try: # some internals use a private _loras list if hasattr(pipeline, "model_ledger") and hasattr(pipeline.model_ledger, "_loras"): try: pipeline.model_ledger._loras = [runtime_lora] replaced = True print("[LoRA] replaced pipeline.model_ledger._loras") except Exception as e: print(f"[LoRA] pipeline.model_ledger._loras assignment failed: {e}") except Exception: pass
# 2) If we succeeded replacing the descriptor in-place, clear transformer cache so it will rebuild if replaced: try: if hasattr(pipeline.model_ledger, "_transformer"): pipeline.model_ledger._transformer = None # also clear potential caches named similar to 'transformer_cache' if present if hasattr(pipeline.model_ledger, "transformer_cache"): try: pipeline.model_ledger.transformer_cache = {} except Exception: pass print("[LoRA] in-place descriptor replacement done; transformer cache cleared") except Exception as e: print(f"[LoRA] replacement succeeded but cache clearing failed: {e}")
# 3) FINAL FALLBACK - if none of the in-place replacements worked, rebuild the pipeline if not replaced: print("[LoRA] in-place replacement FAILED; rebuilding pipeline with runtime LoRA (this is slow)") try: # Rebuild pipeline object with the new LoRA descriptor # NOTE: this replaces the global `pipeline`. We must declare global to reassign it. pipeline = LTX23DistilledA2VPipeline( distilled_checkpoint_path=checkpoint_path, spatial_upsampler_path=spatial_upsampler_path, gemma_root=gemma_root, loras=[runtime_lora], quantization=QuantizationPolicy.fp8_cast(), )
# After rebuilding, we *do not* re-run the original module-level preloads here, # because re-pinning may be complex; the rebuilt pipeline will construct its # own ledger as part of the first call. This is slower but reliable. # Clear any transformer caches if they exist on the new ledger as well. try: if hasattr(pipeline.model_ledger, "_transformer"): pipeline.model_ledger._transformer = None except Exception: pass
print("[LoRA] pipeline rebuilt with runtime LoRA") except Exception as e: print(f"[LoRA] pipeline rebuild FAILED: {e}")
# Finally, log memory then proceed log_memory("before pipeline call")
video, audio = pipeline( # round strength to avoid cache explosion (important) cache_key = round(runtime_strength, 2) if cache_key not in pipeline_cache: print(f"[LoRA] building new pipeline for strength={cache_key}") runtime_lora = LoraPathStrengthAndSDOps( lora_path, cache_key, LTXV_LORA_COMFY_RENAMING_MAP ) new_pipeline = LTX23DistilledA2VPipeline( distilled_checkpoint_path=checkpoint_path, spatial_upsampler_path=spatial_upsampler_path, gemma_root=gemma_root, loras=[runtime_lora], quantization=QuantizationPolicy.fp8_cast(), ) # OPTIONAL: minimal preload (safe components only) ledger = new_pipeline.model_ledger _video_encoder = ledger.video_encoder() _video_decoder = ledger.video_decoder() _audio_encoder = ledger.audio_encoder() _audio_decoder = ledger.audio_decoder() _vocoder = ledger.vocoder() _spatial_upsampler = ledger.spatial_upsampler() _text_encoder = ledger.text_encoder() _embeddings_processor = ledger.gemma_embeddings_processor() ledger.video_encoder = lambda: _video_encoder ledger.video_decoder = lambda: _video_decoder ledger.audio_encoder = lambda: _audio_encoder ledger.audio_decoder = lambda: _audio_decoder ledger.vocoder = lambda: _vocoder ledger.spatial_upsampler = lambda: _spatial_upsampler ledger.text_encoder = lambda: _text_encoder ledger.gemma_embeddings_processor = lambda: _embeddings_processor pipeline_cache[cache_key] = new_pipeline else: print(f"[LoRA] reusing cached pipeline (strength={cache_key})") active_pipeline = pipeline_cache[cache_key]
video, audio = active_pipeline( prompt=prompt, seed=current_seed, height=int(height),app.py+52-93