Skip to content

Ensure the temb kwarg is not passed unexpectedly to attn - #6687

Closed
lsb wants to merge 2 commits into
huggingface:mainfrom
lsb:patch-2
Closed

Ensure the temb kwarg is not passed unexpectedly to attn#6687
lsb wants to merge 2 commits into
huggingface:mainfrom
lsb:patch-2

Conversation

@lsb

@lsblsb commented Jan 24, 2024

Copy link
Copy Markdown
Contributor

WIP.

Token embeddings can be passed to attention processors. Some attention processors expect this. In Python, an unexpected keyword causes an error. Using a sliced attention processor instead of the default, in SD, reveals this bug.

For example, note this call to the attention processor: https://github.com/huggingface/diffusers/blob/v0.25.0/src/diffusers/models/unet_2d_blocks.py#L624

This PR fixes one instance of one attention processor getting passed a token_embedding, by declaring an unused parameter that gets ignored. This was a temporary band-aid I made.

TODO: (patches welcome!)

[] Check all of the other attention processors for this same problem (a few other attention processors might have this bug, from a quick visual scan)
[] Throw an error in each attention processor that gets a non-None temb that wasn't expecting it (maybe? open to other design ideas)

What does this PR do?

Fixes # (issue)

Before submitting

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.

@lsb

lsb commented Jan 24, 2024

Copy link
Copy Markdown
ContributorAuthor

TODO: put this failing code into a test

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import SlicedAttnProcessor
pipe = DiffusionPipeline.from_pretrained(
"hf-internal-testing/tiny-stable-diffusion-torch",
)
pipe(prompt="a trout", num_inference_steps=1).images[0].save("trout1.png")
pipe.vae.set_attn_processor(SlicedAttnProcessor(1))
pipe(prompt="a trout", num_inference_steps=1).images[0].save("trout2.png")

@lsb

lsb commented Jan 24, 2024

Copy link
Copy Markdown
ContributorAuthor

The error message:

Traceback (most recent call last):
File "/Users/lsb/jupyterlab/sliced-attention-temb.py", line 12, in <module>
pipe(prompt="a trout", num_inference_steps=1).images[0].save("trout2.png")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py", line 1042, in __call__
image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False, generator=generator)[
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/diffusers/utils/accelerate_utils.py", line 46, in wrapper
return method(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/diffusers/models/autoencoders/autoencoder_kl.py", line 304, in decode
decoded = self._decode(z).sample
^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/diffusers/models/autoencoders/autoencoder_kl.py", line 275, in _decode
dec = self.decoder(z)
^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/diffusers/models/autoencoders/vae.py", line 333, in forward
sample = self.mid_block(sample, latent_embeds)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/diffusers/models/unet_2d_blocks.py", line 624, in forward
hidden_states = attn(hidden_states, temb=temb)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/lsb/jupyterlab/.direnv/python-3.11.7/lib/python3.11/site-packages/diffusers/models/attention_processor.py", line 527, in forward
return self.processor(
^^^^^^^^^^^^^^^
TypeError: SlicedAttnProcessor.__call__() got an unexpected keyword argument 'temb'

@github-actions

Copy link
Copy Markdown
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.

@github-actionsgithub-actionsBot added the stale Issues that haven't received updates label Mar 2, 2024
@yiyixuxu

Copy link
Copy Markdown
Collaborator

should have already fixed here #6915

@github-actionsgithub-actionsBot removed the stale Issues that haven't received updates label Mar 3, 2024
@lsb

lsb commented Mar 9, 2024

Copy link
Copy Markdown
ContributorAuthor

It is! Thanks for landing this

@lsblsb closed this Mar 9, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@lsb@yiyixuxu