Skip to content

dreambooth lora: text encoder monkey-patching not working #3445

Closed

Description

@rvorias

Describe the bug

In the dreambooth lora script, the lora layers are monkey patched into the text encoder as the text encoder comes from transformers. However, this has some undesirable results:

image

This is before any weight is updated. Vertically, the images share the same exact seed.

I suspect these lines of code:

def new_forward(x):
return old_forward(x) + lora_layer(x)

Commenting out the lora term produces the same distortions. This means that wrapping the original ref to the forward function and injecting it via monkey patching produces extra undesirable results.

My current guess is that this is some pass-by-ref issue.

Reproduction

train_text_encoder: True and check images at epoch=0 before training.

Logs

No response

System Info

Python 3.8.10

accelerate==0.19.0
diffusers @ git+https://github.com/huggingface/diffusers.git@7a32b6beeb0cfdefed645253dce23d9b0a78597f
transformers==4.29.1

Activity

  1. rvorias commented on May 16, 2023

    @rvorias
    Author

    This seems to alleviate the ref issue

    def create_new_forward(old_forward):
        def new_forward(x):
            return old_forward(x)
        return new_forward
    
    # Monkey-patch.
    module.forward = create_new_forward(old_forward)

    So finally:

    ...
    old_forward = module.forward
    
    def create_new_forward(old_forward):
        def new_forward(x):
            return old_forward(x) + lora_layer(x)
        return new_forward
    
    # Monkey-patch.
    module.forward = create_new_forward(old_forward)
  2. patrickvonplaten commented on May 16, 2023

    @patrickvonplaten
    Contributor

    cc @sayakpaul here

  3. Mr-Philo commented on May 17, 2023

    @Mr-Philo
    Contributor

    Same problems here! Adding --train_text_encoder will lead to broken text encoder: the generated images won't follow the input text:

    image

    When training after 2500steps, the model will fit the training images but still ignores the input prompt, llike here "the sunflowers":

    image

    When disable --train_text_encoder, everything goes smoothly.

    Experiment Setting

    transformers==4.28.1
    accelerate==0.19.0
    diffusers=0.17.0.dev0 (latest)
  4. sayakpaul commented on May 17, 2023

    @sayakpaul
    Member

    Being worked in #3437. Thanks for your patience.

  5. github-actions commented on Jun 15, 2023

    @github-actions
    Contributor

    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.

    Please note that issues that do not follow the contributing guidelines are likely to be ignored.

  6. patrickvonplaten commented on Jun 16, 2023

    @patrickvonplaten
    Contributor

    Think we can close this as it's solved, please leave a message if it's not solved for you

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingstaleIssues that haven't received updates

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions