Video LoRA: skip adapters incompatible with the video model

An SD/SDXL image LoRA loaded onto a Wan video transformer matches no keys, so
set_adapters() raised "not in the list of present adapters: set()" and aborted
the whole request. Now each adapter is checked against the pipe's PEFT-capable
components after load; ones that registered nothing (wrong architecture) are
skipped with a clear message and generation proceeds with whatever is
compatible (or no LoRA). The request signature is still cached so the futile
load isn't retried every clip.
Co-Authored-By: 's avatarClaude Opus 4.8 <noreply@anthropic.com>
parent c9c0871b
...@@ -1218,6 +1218,26 @@ def _unload_video_loras(pipe): ...@@ -1218,6 +1218,26 @@ def _unload_video_loras(pipe):
pass pass
def _present_adapters(pipe) -> set:
"""Adapter names actually registered on the pipe's PEFT-capable components.
After load_lora_weights, an adapter only appears here if the LoRA state dict
had keys matching that component. An SD/SDXL UNet LoRA loaded onto a video DiT
(e.g. WanTransformer3DModel) matches nothing, so its name never shows up.
"""
present = set()
for attr in ('transformer', 'transformer_2', 'unet', 'prior',
'text_encoder', 'text_encoder_2'):
comp = getattr(pipe, attr, None)
pc = getattr(comp, 'peft_config', None)
if pc:
try:
present |= set(pc.keys())
except Exception:
pass
return present
def _lora_signature(loras) -> tuple: def _lora_signature(loras) -> tuple:
"""Normalized identity of a requested LoRA set: ((model, name, weight), ...). """Normalized identity of a requested LoRA set: ((model, name, weight), ...).
...@@ -1261,20 +1281,41 @@ def _sync_video_loras(pipe, loras) -> None: ...@@ -1261,20 +1281,41 @@ def _sync_video_loras(pipe, loras) -> None:
if not hasattr(pipe, 'load_lora_weights'): if not hasattr(pipe, 'load_lora_weights'):
print(" [video][lora] pipeline does not support LoRA — skipping") print(" [video][lora] pipeline does not support LoRA — skipping")
return return
names, weights = [], [] # Remember this request as the active set even if some adapters turn out
try: # incompatible, so identical follow-up clips don't re-attempt the same load.
for model, name, w in desired: pipe._coderai_active_loras = desired
model_cls = type(getattr(pipe, 'transformer', None)
or getattr(pipe, 'unet', None) or pipe).__name__
loaded = [] # (name, weight) that actually registered on the model
before = _present_adapters(pipe)
for model, name, w in desired:
try:
pipe.load_lora_weights(model, adapter_name=name) pipe.load_lora_weights(model, adapter_name=name)
names.append(name) except Exception as e:
weights.append(w) print(f" [video][lora] failed to load '{name}': {e}")
if names: continue
pipe.set_adapters(names, weights) now = _present_adapters(pipe)
pipe._coderai_active_loras = desired if name in now and name not in before:
print(f" [video][lora] applied: {names} weights={weights}") loaded.append((name, w))
before = now
else:
# Keys matched nothing on this architecture (e.g. an SD/SDXL image
# LoRA on a Wan video transformer). Skip it; it would not affect output.
print(f" [video][lora] '{name}' has no weights matching {model_cls} "
f"— skipping (incompatible LoRA for this video model)")
if not loaded:
print(" [video][lora] no compatible adapters — generating without LoRA")
_unload_video_loras(pipe)
pipe._coderai_active_loras = desired # _unload reset it; restore for dedup
return
try:
pipe.set_adapters([n for n, _ in loaded], [w for _, w in loaded])
print(f" [video][lora] applied: {[n for n, _ in loaded]} "
f"weights={[w for _, w in loaded]}")
except Exception as e: except Exception as e:
print(f" [video][lora] could not apply LoRA weights: {e}") print(f" [video][lora] could not activate LoRA weights: {e}")
# Partial load — clear so the next request starts clean.
_unload_video_loras(pipe) _unload_video_loras(pipe)
pipe._coderai_active_loras = desired
def _run_pipeline(pipe, kw: dict): def _run_pipeline(pipe, kw: dict):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment