- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.5k
Fix Flux multiple Lora loading bug #10388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            yiyixuxu
  merged 10 commits into
  huggingface:main
from
maxs-kan:flux-lora-base_layer-check
  
      
      
   
  Jan 2, 2025 
      
    
  
     Merged
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      f099b2f
              
                check for base_layer key in transformer state dict
              
              
                maxs-kan db010ae
              
                Merge branch 'main' into flux-lora-base_layer-check
              
              
                hlky bdc5de5
              
                Merge branch 'main' into flux-lora-base_layer-check
              
              
                sayakpaul 3a4f8a4
              
                test_lora_expansion_works_for_absent_keys
              
              
                hlky da00c8d
              
                Merge branch 'main' into flux-lora-base_layer-check
              
              
                hlky a2cdcda
              
                check
              
              
                hlky c8d4a1c
              
                Update tests/lora/test_lora_layers_flux.py
              
              
                hlky 75268c0
              
                check
              
              
                hlky 08ea124
              
                test_lora_expansion_works_for_absent_keys/test_lora_expansion_works_f…
              
              
                hlky 5a7997b
              
                absent->extra
              
              
                hlky File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -2460,13 +2460,17 @@ def _maybe_expand_lora_state_dict(cls, transformer, lora_state_dict): | |
| if unexpected_modules: | ||
| logger.debug(f"Found unexpected modules: {unexpected_modules}. These will be ignored.") | ||
|  | ||
| is_peft_loaded = getattr(transformer, "peft_config", None) is not None | ||
| transformer_base_layer_keys = { | ||
| k[: -len(".base_layer.weight")] for k in transformer_state_dict.keys() if ".base_layer.weight" in k | ||
| } | ||
| for k in lora_module_names: | ||
| if k in unexpected_modules: | ||
| continue | ||
|  | ||
| base_param_name = ( | ||
| f"{k.replace(prefix, '')}.base_layer.weight" if is_peft_loaded else f"{k.replace(prefix, '')}.weight" | ||
| f"{k.replace(prefix, '')}.base_layer.weight" | ||
| if k in transformer_base_layer_keys | ||
| else f"{k.replace(prefix, '')}.weight" | ||
| ) | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. base_param_name = f"{k.replace(prefix, '')}.weight"
base_layer_name = f"{k.replace(prefix, '')}.base_layer.weight"
if is_peft_loaded and base_layer_name in transformer_state_dict:
    base_param_name = base_layer_nameSomething like this might be better. | ||
| base_weight_param = transformer_state_dict[base_param_name] | ||
| lora_A_param = lora_state_dict[f"{prefix}{k}.lora_A.weight"] | ||
|  | ||
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
base_layersubstring can only be present when the underlying pipeline has at least one LoRA loaded that affects the layer under consideration. So, perhaps it's better to have anis_peft_loadedcheck?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In your PR description you mention:
Note that we may also have an opposite situation i.e., the first LoRA ckpt may have the params while the second LoRA may not. This is what I considered in #10388.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if is_peft_loaded and ".base_layer.weight" in kmight be clearer that this is something when a lora is already loaded.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The case where the first LoRA has extra weights than the second is ok on
mainHyper-FLUX.1-dev-8steps-lora.safetensorsPurz/choose-your-own-adventureor
alimama-creative/FLUX.1-Turbo-AlphaTTPlanet/Migration_Lora_fluxIn this case
base_param_nameis set to f"{k.replace(prefix, '')}.base_layer.weight" for the 2nd LoRA and all keys exist.If loaded in the reverse order
f"{k.replace(prefix, '')}.base_layer.weight"doesn't exist for the extra weights.Purz/choose-your-own-adventureHyper-FLUX.1-dev-8steps-lora.safetensorsor
TTPlanet/Migration_Lora_fluxalimama-creative/FLUX.1-Turbo-AlphaKeyError context_embedder.base_layer.weightSo for the extra weights we use
f"{k.replace(prefix, '')}.weight". If another LoRA were loaded withcontext_embedderit would then usecontext_embedder.base_layer.weight.We could
continueiff"{k.replace(prefix, '')}.base_layer.weight"is not found but the extra weights may need to be expanded.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, we are considering that LoRA params for certain modules exist in the first checkpoint while they don't exist in the second checkpoint (or any other subsequent checkpoint).
In this case, we don't want to expand no? Or am I missing something? Perhaps better expressed through a short test case like the one I added here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test case passes on
main, the test case should be in the reverse order:I think we still want to check whether the param needs to be expanded
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, I understand it better now. Thanks!
Might be better to ship this PR with proper testing then. Okay with me.