Spaces:
Runtime error
Runtime error
Jordan Legg
commited on
Commit
Β·
a7d057d
1
Parent(s):
d2b0012
check latent chapes before multiplication
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import random
|
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
| 7 |
from torchvision import transforms
|
| 8 |
-
from diffusers import DiffusionPipeline
|
| 9 |
|
| 10 |
# Constants
|
| 11 |
dtype = torch.bfloat16
|
|
@@ -13,27 +13,43 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 13 |
MAX_SEED = np.iinfo(np.int32).max
|
| 14 |
MAX_IMAGE_SIZE = 2048
|
| 15 |
|
| 16 |
-
# Load
|
| 17 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
| 18 |
pipe.enable_model_cpu_offload()
|
| 19 |
pipe.vae.enable_slicing()
|
| 20 |
pipe.vae.enable_tiling()
|
| 21 |
|
| 22 |
-
vae = AutoencoderKL.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="vae").to(device)
|
| 23 |
-
|
| 24 |
def preprocess_image(image, image_size):
|
| 25 |
preprocess = transforms.Compose([
|
| 26 |
transforms.Resize((image_size, image_size), interpolation=transforms.InterpolationMode.LANCZOS),
|
| 27 |
transforms.ToTensor(),
|
| 28 |
transforms.Normalize([0.5], [0.5])
|
| 29 |
])
|
| 30 |
-
image = preprocess(image).unsqueeze(0).to(device, dtype=
|
| 31 |
return image
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
@spaces.GPU()
|
| 39 |
def infer(prompt, init_image=None, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
|
@@ -56,15 +72,21 @@ def infer(prompt, init_image=None, seed=42, randomize_seed=False, width=1024, he
|
|
| 56 |
# img2img case
|
| 57 |
init_image = init_image.convert("RGB")
|
| 58 |
init_image = preprocess_image(init_image, 1024) # Using 1024 as FLUX VAE sample size
|
| 59 |
-
latents = encode_image(init_image)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
latents = torch.nn.functional.interpolate(latents, size=(height // 8, width // 8), mode='bilinear')
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
image = pipe(
|
| 70 |
prompt=prompt,
|
|
@@ -81,30 +103,5 @@ def infer(prompt, init_image=None, seed=42, randomize_seed=False, width=1024, he
|
|
| 81 |
print(f"Error during inference: {e}")
|
| 82 |
return Image.new("RGB", (width, height), (255, 0, 0)), seed # Red fallback image
|
| 83 |
|
| 84 |
-
# Gradio interface setup
|
| 85 |
-
with gr.Blocks() as demo:
|
| 86 |
-
with gr.Row():
|
| 87 |
-
prompt = gr.Textbox(label="Prompt")
|
| 88 |
-
init_image = gr.Image(label="Initial Image (optional)", type="pil")
|
| 89 |
-
|
| 90 |
-
with gr.Row():
|
| 91 |
-
generate = gr.Button("Generate")
|
| 92 |
-
|
| 93 |
-
with gr.Row():
|
| 94 |
-
result = gr.Image(label="Result")
|
| 95 |
-
seed_output = gr.Number(label="Seed")
|
| 96 |
-
|
| 97 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 98 |
-
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42)
|
| 99 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 100 |
-
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 101 |
-
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
| 102 |
-
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=4)
|
| 103 |
-
|
| 104 |
-
generate.click(
|
| 105 |
-
infer,
|
| 106 |
-
inputs=[prompt, init_image, seed, randomize_seed, width, height, num_inference_steps],
|
| 107 |
-
outputs=[result, seed_output]
|
| 108 |
-
)
|
| 109 |
|
| 110 |
demo.launch()
|
|
|
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
| 7 |
from torchvision import transforms
|
| 8 |
+
from diffusers import DiffusionPipeline
|
| 9 |
|
| 10 |
# Constants
|
| 11 |
dtype = torch.bfloat16
|
|
|
|
| 13 |
MAX_SEED = np.iinfo(np.int32).max
|
| 14 |
MAX_IMAGE_SIZE = 2048
|
| 15 |
|
| 16 |
+
# Load FLUX model
|
| 17 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype).to(device)
|
| 18 |
pipe.enable_model_cpu_offload()
|
| 19 |
pipe.vae.enable_slicing()
|
| 20 |
pipe.vae.enable_tiling()
|
| 21 |
|
|
|
|
|
|
|
| 22 |
def preprocess_image(image, image_size):
|
| 23 |
preprocess = transforms.Compose([
|
| 24 |
transforms.Resize((image_size, image_size), interpolation=transforms.InterpolationMode.LANCZOS),
|
| 25 |
transforms.ToTensor(),
|
| 26 |
transforms.Normalize([0.5], [0.5])
|
| 27 |
])
|
| 28 |
+
image = preprocess(image).unsqueeze(0).to(device, dtype=dtype)
|
| 29 |
return image
|
| 30 |
|
| 31 |
+
def check_shapes(latents):
|
| 32 |
+
# Get the shape of the latents
|
| 33 |
+
latent_shape = latents.shape
|
| 34 |
+
print(f"Latent shape: {latent_shape}")
|
| 35 |
+
|
| 36 |
+
# Get the expected shape for the transformer input
|
| 37 |
+
expected_shape = (1, latent_shape[1] * latent_shape[2], latent_shape[3])
|
| 38 |
+
print(f"Expected transformer input shape: {expected_shape}")
|
| 39 |
+
|
| 40 |
+
# Get the shape of the transformer's weight matrix
|
| 41 |
+
if hasattr(pipe.transformer, 'text_model'):
|
| 42 |
+
weight_shape = pipe.transformer.text_model.encoder.layers[0].self_attn.q_proj.weight.shape
|
| 43 |
+
else:
|
| 44 |
+
weight_shape = pipe.transformer.encoder.layers[0].self_attn.q_proj.weight.shape
|
| 45 |
+
print(f"Transformer weight shape: {weight_shape}")
|
| 46 |
+
|
| 47 |
+
# Check if the shapes are compatible for matrix multiplication
|
| 48 |
+
if expected_shape[1] == weight_shape[1]:
|
| 49 |
+
print("Shapes are compatible for matrix multiplication.")
|
| 50 |
+
else:
|
| 51 |
+
print("Warning: Shapes are not compatible for matrix multiplication.")
|
| 52 |
+
print(f"Expected: {expected_shape[1]}, Got: {weight_shape[1]}")
|
| 53 |
|
| 54 |
@spaces.GPU()
|
| 55 |
def infer(prompt, init_image=None, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 72 |
# img2img case
|
| 73 |
init_image = init_image.convert("RGB")
|
| 74 |
init_image = preprocess_image(init_image, 1024) # Using 1024 as FLUX VAE sample size
|
|
|
|
| 75 |
|
| 76 |
+
# Encode the image using FLUX VAE
|
| 77 |
+
latents = pipe.vae.encode(init_image).latent_dist.sample() * 0.18215
|
| 78 |
+
|
| 79 |
+
# Ensure latents are the correct shape
|
| 80 |
latents = torch.nn.functional.interpolate(latents, size=(height // 8, width // 8), mode='bilinear')
|
| 81 |
|
| 82 |
+
# Check shapes before reshaping
|
| 83 |
+
check_shapes(latents)
|
| 84 |
+
|
| 85 |
+
# Reshape latents to match the expected input shape of the transformer
|
| 86 |
+
latents = latents.permute(0, 2, 3, 1).contiguous().view(1, -1, pipe.vae.config.latent_channels)
|
| 87 |
|
| 88 |
+
# Check shapes after reshaping
|
| 89 |
+
check_shapes(latents)
|
| 90 |
|
| 91 |
image = pipe(
|
| 92 |
prompt=prompt,
|
|
|
|
| 103 |
print(f"Error during inference: {e}")
|
| 104 |
return Image.new("RGB", (width, height), (255, 0, 0)), seed # Red fallback image
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
demo.launch()
|