Skip to content
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

[PixArt-Alpha] fix mask_feature so that precomputed embeddings work with a batch size > 1 #5677

Merged
merged 4 commits into from
Nov 7, 2023

Conversation

sayakpaul
Copy link
Member

So that the following works:

from transformers import T5EncoderModel
from diffusers import PixArtAlphaPipeline
import torch
import gc


def flush():
    gc.collect()
    torch.cuda.empty_cache()

def bytes_to_giga_bytes(bytes):
    return bytes / 1024 / 1024 / 1024

# Loading in 8 bits needs `bitsandbytes`.
text_encoder = T5EncoderModel.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS",
    subfolder="text_encoder",
    torch_dtype=torch.float16
).to("cuda")


pipe = PixArtAlphaPipeline.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS",
    text_encoder=text_encoder, # pass the previously instantiated 8bit text encoder
    transformer=None,
).to("cuda")


prompt = "cute cat"
with torch.no_grad(): prompt_embeds, negative_embeds = pipe.encode_prompt(prompt)


del text_encoder
del pipe
flush()


pipe = PixArtAlphaPipeline.from_pretrained(
    "PixArt-alpha/PixArt-XL-2-1024-MS",
    text_encoder=None,
    torch_dtype=torch.float16
)
pipe.enable_model_cpu_offload()

images = pipe(
    negative_prompt=None, 
    prompt_embeds=prompt_embeds, 
    negative_prompt_embeds=negative_embeds,
    num_images_per_prompt=4
).images[0]

print(
    f"Max memory allocated: {bytes_to_giga_bytes(torch.cuda.max_memory_allocated())} GB"
)

@patrickvonplaten patrickvonplaten merged commit a8523bf into main Nov 7, 2023
13 checks passed
@patrickvonplaten patrickvonplaten deleted the fix/pixart-embeds branch November 7, 2023 16:12
patrickvonplaten pushed a commit that referenced this pull request Nov 7, 2023
… with a batch size > 1 (#5677)

* fix embeds

* remove todo

* add: test

* better name
kashif pushed a commit to kashif/diffusers that referenced this pull request Nov 11, 2023
… with a batch size > 1 (huggingface#5677)

* fix embeds

* remove todo

* add: test

* better name
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
… with a batch size > 1 (huggingface#5677)

* fix embeds

* remove todo

* add: test

* better name
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
… with a batch size > 1 (huggingface#5677)

* fix embeds

* remove todo

* add: test

* better name
Sign up for free to 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.

None yet

2 participants