Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio diffusion: change LoRA weights without reloading the adapter files (#16310)

Daniel Hancd14d764e40f
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index e9567ff0ad9..bea9f497cf1 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -533,6 +533,36 @@ def _negative_prompt_guidance_threshold(family: DiffusionFamily) -> Optional[flo
return None
+# ``{path: (size, mtime_ns)}`` of the adapter files the generation-time path last loaded onto a pipe.
+_LORA_FILE_SIGS_ATTR = "_unsloth_lora_file_sigs"
+
+
+def _lora_file_sigs(loras: Any) -> Optional[dict]:
+ """Size and mtime of each adapter file, or None when one cannot be read."""
+ sigs: dict = {}
+ for _name, path, _weight in loras:
+ try:
+ st = os.stat(path)
+ except OSError:
+ return None
+ sigs[path] = (st.st_size, st.st_mtime_ns)
+ return sigs
+
+
+def _lora_weights_only_change(state: Any, pipe: Any, current: Any, desired: Any) -> bool:
+ """Same adapter names and files in the same order, each file unchanged on disk since its load, and no
+ block-streamed graph: ``set_adapters`` alone then reaches the scales a full reload would set."""
+ if not current or len(current) != len(desired):
+ return False
+ if any((n, p) != (dn, dp) for (n, p, _w), (dn, dp, _dw) in zip(current, desired)):
+ return False
+ loaded = getattr(pipe, _LORA_FILE_SIGS_ATTR, None)
+ if not loaded or loaded != _lora_file_sigs(desired):
+ return False
+ handles = getattr(state, "cuda_graphs", ()) or ()
+ return not any(getattr(getattr(h, "placement", None), "mode", None) == "group" for h in handles)
+
+
def _refuse_weight_change_on_streamed_graph(state: Any) -> None:
"""Recapturing a block-streamed graph after a LoRA weight change fails, and the eager fallback then fails every
later generate, so such a load keeps its baked weights until a reload."""
@@ -12929,6 +12959,7 @@ class DiffusionBackend:
except Exception: # noqa: BLE001 -- best-effort clear
pass
pipe._unsloth_loras = ()
+ setattr(pipe, _LORA_FILE_SIGS_ATTR, None)
cuda_graph.reset_all(getattr(state, "cuda_graphs", ()))
invalidate_cfg_replica(pipe, "the LoRA adapters changed", logger)
return
@@ -12952,17 +12983,30 @@ class DiffusionBackend:
hf_token = state.hf_token,
cancel = cancel,
)
- uniq = list(desired)
if desired == current:
return
+ names = [name for name, _p, _w in desired]
+ weights = [w for _n, _p, w in desired]
+ if _lora_weights_only_change(state, pipe, current, desired):
+ try:
+ pipe.set_adapters(names, adapter_weights = weights)
+ except Exception: # noqa: BLE001 -- the full reload below sets the same scales or fails cleanly
+ pass
+ else:
+ pipe._unsloth_loras = desired
+ # A captured graph bakes the scales in.
+ cuda_graph.reset_all(getattr(state, "cuda_graphs", ()))
+ invalidate_cfg_replica(pipe, "the LoRA adapters changed", logger)
+ return
+ # Read before loading, so a file rewritten during the load reads as changed next time.
+ file_sigs = _lora_file_sigs(desired)
try:
if current:
pipe.unload_lora_weights()
- for name, path, _weight in uniq:
+ setattr(pipe, _LORA_FILE_SIGS_ATTR, None)
+ for name, path, _weight in desired:
pipe.load_lora_weights(path, adapter_name = name)
- pipe.set_adapters(
- [name for name, _p, _w in uniq], adapter_weights = [w for _n, _p, w in uniq]
- )
+ pipe.set_adapters(names, adapter_weights = weights)
except Exception as exc: # noqa: BLE001 -- surface as a clean 400
try:
pipe.unload_lora_weights()
@@ -12974,6 +13018,7 @@ class DiffusionBackend:
invalidate_cfg_replica(pipe, "the LoRA adapters changed", logger)
raise ValueError(f"Failed to apply LoRA: {exc}") from exc
pipe._unsloth_loras = desired
+ setattr(pipe, _LORA_FILE_SIGS_ATTR, file_sigs)
# load_lora_weights can re-materialise parameters the graphs baked pointers to.
cuda_graph.reset_all(getattr(state, "cuda_graphs", ()))
invalidate_cfg_replica(pipe, "the LoRA adapters changed", logger)
diff --git a/studio/backend/tests/test_diffusion_lora.py b/studio/backend/tests/test_diffusion_lora.py
index cb309efd0ec..66807d58160 100644
--- a/studio/backend/tests/test_diffusion_lora.py
+++ b/studio/backend/tests/test_diffusion_lora.py
@@ -896,3 +896,163 @@ def test_pinned_lora_change_also_sets_a_text_encoder_embedding_scale(monkeypatch
assert diff._pin_baked_lora_scales(pipe, "cpu") == 2
assert diff._set_pinned_lora_scales(pipe, {"a": 0.5}) is True
assert emb.scaling == {"a": 1.0} # 0.5 * alpha / r
+
+
+def _real_file_specs(monkeypatch, root):
+ """resolve_specs over real files under ``root``, so the adapter files can be stat'ed and rewritten."""
+ for stem in ("styleA", "styleB"):
+ (root / f"{stem}.safetensors").write_bytes(stem.encode() * 8)
+ monkeypatch.setattr(
+ dl,
+ "resolve_specs",
+ lambda specs, **_: [
+ dl.ResolvedLora(
+ i, dl.sanitize_alias(i), str(root / f"{i}.safetensors"), "safetensors", w
+ )
+ for i, w in specs
+ ],
+ )
+
+
+def test_diffusers_weight_only_change_sets_scales_without_reloading(monkeypatch, tmp_path):
+ import threading
+
+ from core.inference import diffusion as diffusion_mod
+
+ _real_file_specs(monkeypatch, tmp_path)
+ resets: list = []
+ invalidated: list = []
+ monkeypatch.setattr(
+ diffusion_mod.cuda_graph, "reset_all", lambda handles: resets.append(handles)
+ )
+ monkeypatch.setattr(
+ diffusion_mod, "invalidate_cfg_replica", lambda pipe, why, log: invalidated.append(why)
+ )
+ pipe = _FakePipe()
+ b = _backend()
+ state = _fake_state(pipe)
+ b._apply_loras(state, [("styleA", 1.0), ("styleB", 0.5)], threading.Event())
+ loaded = list(pipe.loaded)
+ resets.clear()
+ invalidated.clear()
+
+ b._apply_loras(state, [("styleA", 0.6), ("styleB", 0.5)], threading.Event())
+
+ assert pipe.unloaded == 0
+ assert pipe.loaded == loaded
+ assert pipe.active == (["styleA", "styleB"], [0.6, 0.5])
+ assert [(n, w) for n, _p, w in pipe._unsloth_loras] == [("styleA", 0.6), ("styleB", 0.5)]
+ # Scales are Python values a captured graph or a CFG replica bakes in, so both are still dropped.
+ assert len(resets) == 1 and len(invalidated) == 1
+
+
+def test_diffusers_weight_only_change_matches_a_fresh_load(monkeypatch, tmp_path):
+ import threading
+
+ _real_file_specs(monkeypatch, tmp_path)
+ fast, fresh = _FakePipe(), _FakePipe()
+ b = _backend()
+ b._apply_loras(_fake_state(fast), [("styleA", 1.0)], threading.Event())
+ b._apply_loras(_fake_state(fast), [("styleA", 0.3)], threading.Event())
+ b._apply_loras(_fake_state(fresh), [("styleA", 0.3)], threading.Event())
+ assert fast.active == fresh.active
+ assert fast.loaded == fresh.loaded
+ assert fast._unsloth_loras == fresh._unsloth_loras
+
+
+def test_diffusers_weight_change_reloads_a_rewritten_adapter_file(monkeypatch, tmp_path):
+ import threading
+
+ _real_file_specs(monkeypatch, tmp_path)
+ pipe = _FakePipe()
+ b = _backend()
+ b._apply_loras(_fake_state(pipe), [("styleA", 1.0)], threading.Event())
+ target = tmp_path / "styleA.safetensors"
+ st = target.stat()
+ target.write_bytes(b"retrained adapter, same name")
+ os.utime(target, ns = (st.st_atime_ns, st.st_mtime_ns + 1_000_000_000))
+
+ b._apply_loras(_fake_state(pipe), [("styleA", 0.5)], threading.Event())
+
+ assert pipe.unloaded == 1
+ assert pipe.loaded == [(str(target), "styleA")]
+ assert pipe.active == (["styleA"], [0.5])
+
+
+ "first, second",
+ [
+ ([("styleA", 1.0), ("styleB", 0.5)], [("styleB", 0.5), ("styleA", 0.6)]),
+ ([("styleA", 1.0), ("styleB", 0.5)], [("styleA", 0.6)]),
+ ([("styleA", 1.0)], [("styleB", 0.6)]),
+ ],
+)
+def test_diffusers_adapter_set_change_still_reloads(monkeypatch, tmp_path, first, second):
+ import threading
+
+ _real_file_specs(monkeypatch, tmp_path)
+ pipe = _FakePipe()
+ b = _backend()
+ b._apply_loras(_fake_state(pipe), first, threading.Event())
+ b._apply_loras(_fake_state(pipe), second, threading.Event())
+ assert pipe.unloaded == 1
+ assert [n for _p, n in pipe.loaded] == [i for i, _w in second]
+ assert pipe.active == ([i for i, _w in second], [w for _i, w in second])
+
+
+def test_diffusers_weight_change_after_clear_reloads(monkeypatch, tmp_path):
+ import threading
+
+ _real_file_specs(monkeypatch, tmp_path)
+ pipe = _FakePipe()
+ b = _backend()
+ b._apply_loras(_fake_state(pipe), [("styleA", 1.0)], threading.Event())
+ b._apply_loras(_fake_state(pipe), [], threading.Event())
+ b._apply_loras(_fake_state(pipe), [("styleA", 0.5)], threading.Event())
+ assert pipe.unloaded == 1
+ assert pipe.loaded == [(str(tmp_path / "styleA.safetensors"), "styleA")]
+ assert pipe.active == (["styleA"], [0.5])
+
+
+def test_diffusers_weight_change_on_a_streamed_graph_keeps_the_reload(monkeypatch, tmp_path):
+ import threading
+
+ _real_file_specs(monkeypatch, tmp_path)
+ pipe = _FakePipe()
+ b = _backend()
+ state = _fake_state(pipe)
+ b._apply_loras(state, [("styleA", 1.0)], threading.Event())
+ state.cuda_graphs = (types.SimpleNamespace(placement = types.SimpleNamespace(mode = "group")),)
+ b._apply_loras(state, [("styleA", 0.5)], threading.Event())
+ assert pipe.unloaded == 1
+ assert pipe.active == (["styleA"], [0.5])
+
+
+def test_diffusers_weight_change_falls_back_to_reload_when_set_adapters_fails(
+ monkeypatch, tmp_path
+):
+ import threading
+
+ _real_file_specs(monkeypatch, tmp_path)
+
+ class _FlakyPipe(_FakePipe):
+ fail_next = False
+
+ def set_adapters(
+ self,
+ names,
+ adapter_weights = None,
+ ):
+ if self.fail_next:
+ self.fail_next = False
+ raise RuntimeError("adapter missing")
+ super().set_adapters(names, adapter_weights)
+
+ pipe = _FlakyPipe()
+ b = _backend()
+ b._apply_loras(_fake_state(pipe), [("styleA", 1.0)], threading.Event())
+ pipe.fail_next = True
+ b._apply_loras(_fake_state(pipe), [("styleA", 0.5)], threading.Event())
+ assert pipe.unloaded == 1
+ assert pipe.active == (["styleA"], [0.5])
+ assert [(n, w) for n, _p, w in pipe._unsloth_loras] == [("styleA", 0.5)]