Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files415 unmodified lines416417418419419420421422423424425426427428429430421422423424425426427428431432433434435436437438439440441442430431432433434435436443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513415 unmodified lines tiling_config = TilingConfig.default() video_chunks_number = get_video_chunks_number(num_frames, tiling_config)
# >>> SET the LoRA descriptor strength at runtime # >>> 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. 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: # lora_descriptor was created at module scope during init lora_descriptor.strength = float(lora_strength) print(f"[LoRA] set strength = {lora_descriptor.strength}") except Exception as e: # Defensive: if the descriptor isn't present, continue quietly print(f"[LoRA] could not set strength: {e}")
# >>> FORCE transformer rebuild so LoRA strength updates # 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: # Clear cached transformer so new LoRA strength is applied if hasattr(pipeline.model_ledger, "_transformer"): pipeline.model_ledger._transformer = None print("[LoRA] transformer cache cleared") except Exception as e: print(f"[LoRA] transformer reset failed: {e}") # 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. global pipeline 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(app.py+90-16