Skip to content

perf(lora): accumulate the LoRA up projection into the base GEMM output and scale the rank-sized intermediate - #1772

Open
WhatGhost wants to merge 1 commit into
ModelTC:mainfrom
WhatGhost:perf/lora-addmm
Open

WhatGhost wants to merge 1 commit into
ModelTC:mainfrom
WhatGhost:perf/lora-addmm

Conversation

@WhatGhost

Copy link
Copy Markdown
Contributor

Summary

With lora_mode separate (and for the projections that consumer does not fuse), every LoRA linear runs add_lora(output, x) = (output + apply_lora(x)).to(output.dtype) with apply_lora(x) = lora_strength * lora_scale * ((x @ A^T) @ B^T). Besides the two small GEMMs this makes three extra passes over the [tokens, out] tensor: the up projection writes a new tensor, the scale multiply reads and writes it, and the add reads it together with the base output and writes the sum. For the fused FFN input projection of MiniMax-H3 (out = 28672) at 37.9k tokens each pass moves 2-6 GB.

  • apply_lora multiplies lora_strength * lora_scale into the [tokens, rank] intermediate instead of the [tokens, out] product (same arithmetic, one rounding; consumer mode benefits through project_parts).
  • add_lora accumulates the up projection into the base output with addmm_ (the GEMM's beta = 1 epilogue) when the output is a 2-D tensor with unit stride in the LoRA dtype; otherwise it keeps the previous expression. All call sites pass a fresh GEMM result, so accumulating in place is safe. Base GEMMs (including quantized ones) are not touched, and the scale stays a device tensor (no host sync).

Results

AMD Radeon AI PRO R9700 (gfx1201), ROCm 7.2.4, torch 2.10.0. LightX2V main 83a424a. MiniMax-H3 DiT shapes, M = 37869 tokens (768x1344x124), rank 128; mean of 10 calls after 3 warmup calls.

Linear (out x in) BF16 base + LoRA, before -> after FP8 (fp8-triton) base + LoRA, before -> after
q/k/v 7168 x 5376 31.5 -> 25.4 ms (1.24x) 25.9 -> 20.7 ms (1.25x)
attention out 5376 x 7168 29.5 -> 25.1 ms (1.18x) 25.4 -> 21.4 ms (1.18x)
FFN in 28672 x 5376 124.9 -> 102.7 ms (1.22x) 89.5 -> 68.3 ms (1.31x)
FFN out 5376 x 14336 57.4 -> 49.4 ms (1.16x) 45.6 -> 41.8 ms (1.09x)
LoRA cost per DiT block (6 linears) 80.7 -> 25.7 ms 72.9 -> 27.3 ms

End to end (MiniMax-H3 t2av, 768x1344x124, 4-step LoRA, block offload, same GPU, sequential runs):

Configuration denoising step end to end
main, fp8-triton DiT, lora_mode: separate 39.60 -> 38.35 s 199.6 -> 193.6 s (-3.0%)
e8ddf40, fp8-triton DiT, dynamic LoRA 39.72 -> 38.37 s 201.4 -> 195.6 s (-2.9%)
e8ddf40, BF16 DiT, dynamic LoRA 43.02 -> 41.67 s 213.7 -> 208.3 s (-2.5%)
main, fp8-triton DiT, lora_mode: consumer 39.29 -> 38.16 s (requests run out of memory on this 32 GB GPU with and without the patch)

Numerics: against an FP32 reference the LoRA linear error goes from 2.40e-3 to 2.34e-3 (one rounding less); the outputs differ from the previous ones by 1.5e-3. As with any rounding change, 4-step sampling then diverges in fine detail (video PSNR 20.5 dB against the previous output; a same-config rerun gives 47 dB and FP8 vs BF16 gives 20.4 dB), with no visible degradation.

NVIDIA

GeForce RTX 5090 (sm_120), torch 2.11.0+cu130, Triton 3.6.0; same shapes and method.

Linear (out x in) BF16 base + LoRA, before -> after FP8 (fp8-triton) base + LoRA, before -> after
q/k/v 7168 x 5376 17.4 -> 15.9 ms (1.10x) 10.8 -> 9.3 ms (1.17x)
attention out 5376 x 7168 17.3 -> 15.9 ms (1.08x) 10.9 -> 9.7 ms (1.12x)
FFN in 28672 x 5376 71.2 -> 64.9 ms (1.10x) 39.7 -> 33.4 ms (1.19x)
FFN out 5376 x 14336 33.2 -> 32.0 ms (1.04x) 20.4 -> 19.1 ms (1.07x)
LoRA cost per DiT block (6 linears) 20.2 -> 6.6 ms 21.8 -> 7.7 ms

The same linears without LoRA stay within 1% of their previous times, and the outputs differ from the previous ones by 1.5e-3, as on gfx1201.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant