Fix DeloraLinear.unmerge subtracting un-cast delta weight#3166
Open
Chessing234 wants to merge 1 commit into
Open
Fix DeloraLinear.unmerge subtracting un-cast delta weight#3166Chessing234 wants to merge 1 commit into
Chessing234 wants to merge 1 commit into
Conversation
DeloraLinear.merge converts the delta to the base layer's dtype/device
before applying it (lines 191-195):
delta_weight = (
self.get_delta_weight(active_adapter)
.detach()
.to(dtype=base_layer.weight.dtype, device=base_layer.weight.device)
)
base_layer.weight.data.add_(delta_weight)
unmerge omits the same conversion:
self.get_base_layer().weight.data -= self.get_delta_weight(active_adapter)
`get_delta_weight` returns the result of `_compute_delta`, whose
dtype/device follow the adapter parameters (delora_A/B/lambda/w_norm),
not the base weight. When the base layer is e.g. bf16 / int8 / on a
different device than the adapter (a common case after
prepare_model_for_kbit_training, FSDP, or DoRA-style mixed setups), the
in-place `-=` either raises a dtype/device mismatch or silently
casts/round-trips the residual incorrectly, so unmerge no longer
reverses merge.
Mirror the merge path: cast the delta to the base layer's dtype/device
before subtracting.
Member
|
Thanks for the PR.
What do you mean by that?
IIUC, due to floating point operations, that can never be avoided, even with explicit type promotion. |
|
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
`DeloraLinear.merge` converts the delta to the base layer's dtype / device before applying it (lines 191-195):
```python
delta_weight = (
self.get_delta_weight(active_adapter)
.detach()
.to(dtype=base_layer.weight.dtype, device=base_layer.weight.device)
)
...
base_layer.weight.data.add_(delta_weight)
```
`unmerge` omits the same conversion:
```python
self.get_base_layer().weight.data -= self.get_delta_weight(active_adapter)
```
Root cause
`get_delta_weight` returns the output of `_compute_delta`, whose dtype/device follow the adapter parameters (`delora_A`/`delora_B`/`delora_lambda`/`delora_w_norm`), not the base weight. When the base layer is in a different dtype (e.g. bf16/int8 after `prepare_model_for_kbit_training`) or on a different device than the adapter, the in-place `-=` either raises a dtype/device mismatch or silently round-trips the residual differently from how it was added — so `unmerge` no longer reverses `merge`.
Fix
Mirror the merge path: cast the delta to the base layer's dtype/device before subtracting. Behavior is unchanged when the adapter and base already share dtype/device.