Refactor QKV Fusion Utilities to be LoRA-Aware#14047
Open
dg845 wants to merge 7 commits into
Open
Conversation
…e to original fusion state, module-level helpers to get Q,K,V in both fused and split cases
…duleMixin Tests are in tests/models/test_attention_mixins.py and cover the four minimal concrete AttentionModuleMixin fixtures (_MinimalSelfAttn, _MinimalCrossAttn, _MinimalAddedKVAttn, _MinimalAddedQKVAttn): TestAttentionModuleMixin (53 tests): - Idempotency of fuse_projections/unfuse_projections - Module attribute invariants for non-inplace and inplace paths - Weight/bias correctness: fused weight equals concatenation of split weights - Inplace round-trip weight preservation and storage-sharing (no copy on unfuse) - Cross-attention to_kv path, added-KV to_added_kv path (Wan-style), and added-QKV to_added_qkv path (Flux-style) - get_qkv and get_added_qkv numerical correctness in split and fused cases - LoRA guard: fuse_projections/unfuse_projections raise ValueError when PEFT-style lora_A/lora_B submodules are detected on split or fused projections TestAttentionMixin (6 tests): - fuse_qkv_projections/unfuse_qkv_projections propagate to all eligible blocks - restore_checkpoint_fusion_state respects _native_fused_projections=None/True/False per block, including mixed-state models Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
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.
What does this PR do?
This PR refactors the attention QKV fusion utilities in
AttentionMixinandAttentionModuleMixinto be more LoRA-aware. In particular, this PR adds guards when attempting to fuse/unfuse with a LoRA attached (because LoRAs cannot be easily transferred over when fusing/unfusing) and aninplaceoption to fuse without keeping copies of the split Q,K,V projections.Changelist
inplaceargument; ifinplace=True, the module is modified to have only the fused QKV projection (e.g.to_qkv) with the split Q,K,V projections (e.g.to_q/to_k/to_v) removed. (inplace=False, the default, retains the current behavior).add_k_projandadd_v_projare present withoutadd_q_projalso present (e.g. Wan models).get_qkvandget_added_qkvhelper methods inAttentionModuleMixinwhich handles getting the Q, K, V (and added Q,K,V, for second stream projections in MM-DiT-style models like Flux) in both the fused and split case. This is intended to make it easier for attention processors to support both fused and split QKV.restore_checkpoint_fusion_statemethod toAttentionMixinto put models back in the fusion state of the original model checkpoint. A new_native_fused_projectionsattribute onAttentionModuleMixinis added to allow this state to be described. (The motivation is to make it easier to support PEFT adapters which target the original checkpoint structure.)Partially addresses #14003.
Before submitting
.ai/review-rules.md?documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@sayakpaul
@DN6