File size: 2,035 Bytes
730c74d
 
 
ce5737c
0b9c220
730c74d
 
 
 
ce5737c
fa7c3b0
730c74d
 
 
ce5737c
 
 
 
 
730c74d
ce5737c
 
 
 
 
 
730c74d
ce5737c
 
 
 
730c74d
306edbc
730c74d
 
 
 
 
 
 
 
ce5737c
 
 
 
 
 
730c74d
ce5737c
 
730c74d
 
 
 
 
 
 
 
 
 
 
ce5737c
730c74d
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import gradio as gr
import torch
import spaces
from diffusers import FluxPipeline
from torchao.quantization import autoquant



# normal FluxPipeline
pipeline_normal = FluxPipeline.from_pretrained(
    "sayakpaul/FLUX.1-merged",
    torch_dtype=torch.bfloat16
).to("cuda")

# # optimized FluxPipeline
# pipeline_optimized = DiffusionPipeline.from_pretrained(
#     "sayakpaul/FLUX.1-merged",
#     torch_dtype=torch.bfloat16
# ).to("cuda")

# pipeline_optimized.transformer.to(memory_format=torch.channels_last)
# pipeline_optimized.transformer = torch.compile(
#     pipeline_optimized.transformer,
#     mode="max-autotune",
#     fullgraph=True
# )

# pipeline_optimized.transformer = autoquant(
#     pipeline_optimized.transformer,
#     error_on_unseen=False
# )

@spaces.GPU(duration=120)
def generate_images(prompt, guidance_scale, num_inference_steps):
    # generate image with normal pipeline
    image_normal = pipeline_normal(
        prompt=prompt,
        guidance_scale=guidance_scale,
        num_inference_steps=int(num_inference_steps)
    ).images[0]
    
    # # generate image with optimized pipeline
    # image_optimized = pipeline_optimized(
    #     prompt=prompt,
    #     guidance_scale=guidance_scale,
    #     num_inference_steps=int(num_inference_steps)
    # ).images[0]
    
    # return image_normal, image_optimized
    return image_normal

# set up Gradio interface
demo = gr.Interface(
    fn=generate_images,
    inputs=[
        gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"),
        gr.Slider(1.0, 10.0, step=0.5, value=3.5, label="Guidance Scale"),
        gr.Slider(10, 100, step=1, value=50, label="Number of Inference Steps")
    ],
    outputs=[
        gr.Image(type="pil", label="Normal FluxPipeline"),
        # gr.Image(type="pil", label="Optimized FluxPipeline")
    ],
    title="FluxPipeline Comparison",
    description="Compare images generated by the normal FluxPipeline and the optimized one using torchao and torch.compile()."
)

demo.launch()