[Perf][Diffusion] Keep LoRA weights resident across activation cycles (#7195)
Signed-off-by: princepride <wangzhipeng628@gmail.com>
This commit is contained in:
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().
|
||||
|
||||
Reference in new issue
Block a user