[Core] Optimize diffusion LoRA expand accumulation (#6268)

Signed-off-by: 0z5a <0z5a@users.noreply.github.com>
Signed-off-by: 0z5a <dinadegttyarenko6@gmail.com>
Co-authored-by: 0z5a <0z5a@users.noreply.github.com>
This commit is contained in:
0z5aand0z5a authored and GitHub committed 2026-09-03 10:37:35 +08:00
1 parent 8f284b343a
commit 4927fd5412
3 files changed
+239 -4

No files matched your search

+11 -2
View File
@@ -143,8 +143,17 @@ class DiffusionBaseLinearLayerWithLoRA(BaseLinearLayerWithLoRA):
# LoRA shrink & expand as in add_lora_linear():
# buffer = (x @ A.T)
# y += buffer @ B.T
delta = (x_flat @ A.t()) @ B.t()
y_flat[:, offset : offset + slice_size] = y_flat[:, offset : offset + slice_size] + delta
buffer = x_flat @ A.t()
y_slice = y_flat[:, offset : offset + slice_size]
# In inference, accumulate the expand GEMM directly into the base
# output. This avoids materializing both a full-width delta and the
# result of the following elementwise addition. ``out=`` does not
# support autograd, so training keeps the functional fallback.
if not torch.is_grad_enabled() and y_slice.dtype == buffer.dtype == B.dtype:
torch.addmm(y_slice, buffer, B.t(), out=y_slice)
else:
y_slice[:] = y_slice + buffer @ B.t()
offset += slice_size
return y_flat.view(original_shape)