[Bug] Unmerging accumulated LoRA adapters drops earlier adapters from inference
Describe the bug
On a BF16 layer with two accumulated adapters, unmerge_lora_weights() drops the first adapter’s contribution from the next forward pass. Calling merge_lora_weights() again still produces the output for the second adapter alone.
For a base weight of 10, adapters X (+2) and Y (+5), and input 2, the merged output is 34. After unmerge it is 30, and after remerge it is still 30. Expected 34 in all three modes. The scalar values are exactly representable in BF16.
Reproduction
Run against ec9145ab3d86b2255b6fa3bbbb2845859c72cafd with FASTVIDEO_LORA_SIDE_BRANCH=0.
from unittest.mock import patch
import torch
from fastvideo.layers.linear import ReplicatedLinear
from fastvideo.layers.lora import linear
d = torch.bfloat16
with torch.no_grad(), torch._dynamo.config.patch(disable=True), patch.object(
linear, "get_local_torch_device", return_value=torch.device("cpu")
):
base = ReplicatedLinear(1, 1, bias=False, params_dtype=d)
base.weight.fill_(10)
layer = linear.BaseLayerWithLoRA(base)
x = torch.tensor([[2]], dtype=d)
a = torch.ones(1, 1, dtype=d)
layer.set_lora_weights(a, torch.tensor([[2]], dtype=d)) # X adds 2
layer.set_lora_weights(a, torch.tensor([[5]], dtype=d), accumulate=True) # Y adds 5
print(layer(x)[0].item()) # 34
layer.unmerge_lora_weights()
print(layer(x)[0].item()) # 30, expected 34
layer.merge_lora_weights()
print(layer(x)[0].item()) # 30, expected 34
The layer overwrites its current factor pair on every adapter update, including accumulation. The merged weight contains X and Y, but the current factor pair belongs to Y. Unmerge restores the base weight, and the unmerged forward applies only Y. A regression test should check both forward outputs across this round trip.
Environment
FastVideo main ec9145ab3d86b2255b6fa3bbbb2845859c72cafd, Python 3.12.13, PyTorch 2.12.0, macOS arm64. Reproduced on CPU with one unquantized BF16 ReplicatedLinear. This tracks the general adapter lifecycle behavior observed while working on the NVFP4 restore/repack change in #2076.
@fv-superagent please verify that is a real issue.
Confirmed real on main (ec9145ab, the commit in the report).
A merge-mode layer keeps one factor pair plus a merged boolean. set_lora_weights(accumulate=True) fuses the new delta into base_layer.weight but overwrites the retained pair, so nothing records the earlier adapter; both unmerge_lora_weights() and a later merge_lora_weights() rebuild from that single pair.
| state | base_layer.weight |
retained A, B |
forward(x=2) |
|---|---|---|---|
after X then Y accumulated (merged) |
17 | Y (X overwritten) |
34 |
after unmerge_lora_weights() |
10 (snapshot) | Y |
30 |
after merge_lora_weights() |
15 | Y |
30 |
- The overwrite is linear.py#L216-L217; the unmerged forward adds only that pair at #L190-L194;
unmergerestores the construction-time snapshot at #L301-L309. - Reachable in normal use:
unmerge_lora_weights()is documented as running inference on unmerged weights to match training-time validation, wan_lora_inference_from_ckpt.py:21 unmerges before generating, andmerge_lora_weights()is its inverse. An accumulated stack silently loses every adapter but the last. - Existing coverage misses it:
test_stacked_accumulate_sums_deltasasserts only the merged weight, not the unmerged forward or the round trip.
Retaining the stack is more than a one-line change (the accumulated factor pairs have to be combined, e.g. concatenated along the rank dimension), so I left the code alone.
— fv-superagent · one boolean is not an adapter registry · run by aryan5v
@fv-superagent please assign to me, I will take a look next week.
You're already assigned to three open issues (#1821, #1949, #2075), so I'm not adding a fourth. You don't need the assignment to work on this — opening a PR with Fixes #2096 is enough to get it reviewed.
— fv-superagent · no room to stack a fourth adapter · run by aryan5v
@fv-superagent i'd like to try fixing this issue, please assign to me.
Assigned to @xxdydx. Link your PR with Fixes #2096; ask here if you need pointers. The root-cause trace from the verification is in the comment above.
— fv-superagent · welcome — X never made it past unmerge · run by aryan5v