Skip to content

Improve textual inversion compatibility #10949

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/diffusers/loaders/textual_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,13 @@ def load_textual_inversion(
# 5. Extend tokens and embeddings for multi vector
tokens, embeddings = self._extend_tokens_and_embeddings(tokens, embeddings, tokenizer)

# 6. Make sure all embeddings have the correct size
# 6. Adjust all embeddings to match the expected dimension
expected_emb_dim = text_encoder.get_input_embeddings().weight.shape[-1]
if any(expected_emb_dim != emb.shape[-1] for emb in embeddings):
raise ValueError(
"Loaded embeddings are of incorrect shape. Expected each textual inversion embedding "
"to be of shape {input_embeddings.shape[-1]}, but are {embeddings.shape[-1]} "
)
for i, embedding in enumerate(embeddings):
if embedding.shape[-1] != expected_emb_dim:
linear = nn.Linear(embedding.shape[-1], expected_emb_dim)
embeddings[i] = linear(embedding)
logger.info(f"Changed embedding dimension from {embedding.shape[-1]} to {expected_emb_dim}")
Comment on lines +405 to +409
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add a test case to cover this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add a test case to cover this?

What should we do with this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @suzukimain. The test would load an embedding to an incompatible model and check for the log "Changed embedding dimension...".
Also, do you have any example outputs to share?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, @hlky
The following log is what I was able to get.

The loaded token: emb_params is overwritten by the passed token EasyNegative.
Changed embedding dimension from 768 to 1024
Changed embedding dimension from 768 to 1024
Changed embedding dimension from 768 to 1024
Changed embedding dimension from 768 to 1024
Changed embedding dimension from 768 to 1024
Changed embedding dimension from 768 to 1024
Changed embedding dimension from 768 to 1024
Changed embedding dimension from 768 to 1024
Loaded textual inversion embedding for EasyNegative.
Loaded textual inversion embedding for EasyNegative_1.
Loaded textual inversion embedding for EasyNegative_2.
Loaded textual inversion embedding for EasyNegative_3.
Loaded textual inversion embedding for EasyNegative_4.
Loaded textual inversion embedding for EasyNegative_5.
Loaded textual inversion embedding for EasyNegative_6.
Loaded textual inversion embedding for EasyNegative_7.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello. Do you need any other information?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @suzukimain, apologies for the delay, last week was the Diffusers team offsite.

Changed embedding dimension from 768 to 1024

This text is what we would check for in the test, either just Changed embedding dimension from or including the original + new dimensions depending on how existing TI tests are set up. Would you like assistance adding the test? happy to take over if needed.

Do you need any other information?

Example outputs from a model using an incompatible TI would be useful. cc @asomoza Is this something you've tested before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @suzukimain, apologies for the delay, last week was the Diffusers team offsite.

Changed embedding dimension from 768 to 1024

This text is what we would check for in the test, either just or including the original + new dimensions depending on how existing TI tests are set up. Would you like assistance adding the test? happy to take over if needed.Changed embedding dimension from

Do you need any other information?

Example outputs from a model using an incompatible TI would be useful. cc @asomoza Is this something you've tested before?

Hello @hlky, if possible, could you please add a test?


# 7. Now we can be sure that loading the embedding matrix works
# < Unsafe code:
Expand Down