[Perf][Diffusion] Keep LoRA weights resident across activation cycles (#7195)

Signed-off-by: princepride <wangzhipeng628@gmail.com>
This commit is contained in:
汪志鹏 authored and GitHub committed 2026-09-07 14:27:24 +00:00
1 parent 30d6a0b4e6
commit 9b6a308914
4 files changed
+211 -4

No files matched your search

@@ -25,6 +25,9 @@ class DiffusionBaseLinearLayerWithLoRA(BaseLinearLayerWithLoRA):
lora_a_stacked: tuple[torch.Tensor, ...]
lora_b_stacked: tuple[torch.Tensor, ...]
# Mask saved while the adapter is suspended; None means "not suspended".
_diffusion_lora_suspended_slices: tuple[bool, ...] | None = None
def _move_lora_buffers(self, device: torch.device) -> None:
self.lora_a_stacked = tuple(tensor.to(device=device) for tensor in self.lora_a_stacked)
self.lora_b_stacked = tuple(tensor.to(device=device) for tensor in self.lora_b_stacked)
@@ -54,11 +57,14 @@ class DiffusionBaseLinearLayerWithLoRA(BaseLinearLayerWithLoRA):
object.__setattr__(self, "_diffusion_base_layer_ref", base_layer)
n_slices = getattr(self, "n_slices", 1)
self._diffusion_lora_active_slices = (False,) * int(n_slices)
self._diffusion_lora_suspended_slices = None
def reset_lora(self, index: int):
super().reset_lora(index)
n_slices = getattr(self, "n_slices", 1)
self._diffusion_lora_active_slices = (False,) * int(n_slices)
# The upload is gone, so a saved mask no longer describes the buffers.
self._diffusion_lora_suspended_slices = None
def set_lora(
self,
@@ -82,6 +88,30 @@ class DiffusionBaseLinearLayerWithLoRA(BaseLinearLayerWithLoRA):
# Single-slice layer.
self._diffusion_lora_active_slices = (True,)
def suspend_lora(self) -> None:
"""Gate every slice off without dropping the uploaded weights.
`apply()` reads the stacked buffers only for slices this mask marks
active, so a suspended adapter costs nothing while staying resident.
A saved mask means the layer is already suspended; keep the original
rather than overwriting it with the all-False mask now in place.
"""
if self._diffusion_lora_suspended_slices is not None:
return
self._diffusion_lora_suspended_slices = self._diffusion_lora_active_slices
self._diffusion_lora_active_slices = (False,) * len(self._diffusion_lora_active_slices)
def resume_lora(self) -> None:
"""Restore the mask captured by `suspend_lora()`.
A layer registered after the suspend, or one whose buffers were torn
down meanwhile, has no mask to restore and keeps its own inactive one.
"""
saved = self._diffusion_lora_suspended_slices
if saved is not None:
self._diffusion_lora_active_slices = saved
self._diffusion_lora_suspended_slices = None
def apply(self, x: torch.Tensor, bias: torch.Tensor | None = None) -> torch.Tensor:
"""
override: Use simple matmul instead of punica_wrapper.add_lora_linear().
+31 -3
View File
@@ -48,6 +48,11 @@ class DiffusionLoRAManager:
# Valid max allowed ranks for LoRA in vLLM
_VALID_MAX_RANKS: list[int] = sorted(get_args(MaxLoRARanks))
# Adapter whose weights are still uploaded while gated off. Class-level so
# a manager built without __init__ (some tests use object.__new__) reads as
# "nothing suspended" instead of raising.
_suspended_adapter_id: int | None = None
def __init__(
self,
pipeline: nn.Module,
@@ -493,9 +498,11 @@ class DiffusionLoRAManager:
fully_sharded_loras=False,
)
# Recreate per-layer buffers with the new maximum rank.
# Recreate per-layer buffers with the new maximum rank. The previous
# upload is gone, so nothing may be re-armed afterwards.
for lora_layer in self._lora_modules.values():
lora_layer.create_lora_weights(max_loras=1, lora_config=lora_config, model_config=None)
self._suspended_adapter_id = None
# Re-apply active adapter if needed (buffers were reset).
if self._active_adapter_id is not None:
@@ -679,18 +686,32 @@ class DiffusionLoRAManager:
def _reset_lora_layers(self) -> None:
for lora_layer in self._lora_modules.values():
lora_layer.reset_lora(0)
self._suspended_adapter_id = None
def _activate_adapter(self, adapter_id: int, scale: float) -> None:
if self._is_active_at_scale(adapter_id, scale):
logger.debug("Adapter %d already active at scale %.3f skipping", adapter_id, scale)
return
if self._suspended_adapter_id == adapter_id and self._adapter_scales.get(
adapter_id
) == DiffusionLoRAManager._get_rounded_scale(scale):
# Weights are still uploaded from an earlier activation; re-arming
# the masks avoids rebuilding and re-uploading every layer.
for lora_layer in self._lora_modules.values():
lora_layer.resume_lora()
self._suspended_adapter_id = None
self._active_adapter_id = adapter_id
logger.debug("Re-armed suspended adapter %d", adapter_id)
return
logger.info("Activating adapter: id=%d", adapter_id)
lora_model = self._registered_adapters[adapter_id]
# Binding overwrites slot 0 incrementally. Invalidate the fast-path
# state before the first mutation and leave every wrapper inactive if
# any set_lora() call or model validator fails.
self._active_adapter_id = None
self._suspended_adapter_id = None
try:
self._bind_adapter_weights(lora_model, scale)
except Exception:
@@ -704,8 +725,10 @@ class DiffusionLoRAManager:
if self._active_adapter_id is None:
logger.debug("All adapters already inactive")
return
logger.info("Deactivating all adapters: %d layers", len(self._lora_modules))
self._reset_lora_layers()
logger.info("Suspending all adapters: %d layers", len(self._lora_modules))
for lora_layer in self._lora_modules.values():
lora_layer.suspend_lora()
self._suspended_adapter_id = self._active_adapter_id
self._active_adapter_id = None
logger.debug("All adapters deactivated")
@@ -772,6 +795,11 @@ class DiffusionLoRAManager:
if self._active_adapter_id == adapter_id:
self._deactivate_all_adapters()
if self._suspended_adapter_id == adapter_id:
# The adapter is going away, so the upload can never be resumed.
# Tear it down instead of leaving it in the stacked buffers.
self._reset_lora_layers()
del self._registered_adapters[adapter_id]
self._adapter_scales.pop(adapter_id, None)
self._adapter_access_order.pop(adapter_id, None)