|
| 1 | +import os |
| 2 | + |
| 3 | +import gradio as gr |
| 4 | +import torch |
| 5 | +import torch_tensorrt |
| 6 | +from diffusers import FluxPipeline, StableDiffusionPipeline |
| 7 | +from torch.export._trace import _export |
| 8 | + |
| 9 | +DEVICE = "cuda:0" |
| 10 | +pipe = FluxPipeline.from_pretrained( |
| 11 | + "black-forest-labs/FLUX.1-dev", |
| 12 | + torch_dtype=torch.float16, |
| 13 | +) |
| 14 | +pipe.to(DEVICE).to(torch.float16) |
| 15 | +backbone = pipe.transformer |
| 16 | + |
| 17 | + |
| 18 | +batch_size = 2 |
| 19 | +BATCH = torch.export.Dim("batch", min=1, max=8) |
| 20 | + |
| 21 | +# This particular min, max values for img_id input are recommended by torch dynamo during the export of the model. |
| 22 | +# To see this recommendation, you can try exporting using min=1, max=4096 |
| 23 | +dynamic_shapes = { |
| 24 | + "hidden_states": {0: BATCH}, |
| 25 | + "encoder_hidden_states": {0: BATCH}, |
| 26 | + "pooled_projections": {0: BATCH}, |
| 27 | + "timestep": {0: BATCH}, |
| 28 | + "txt_ids": {}, |
| 29 | + "img_ids": {}, |
| 30 | + "guidance": {0: BATCH}, |
| 31 | + "joint_attention_kwargs": {}, |
| 32 | + "return_dict": None, |
| 33 | +} |
| 34 | + |
| 35 | +settings = { |
| 36 | + "strict": False, |
| 37 | + "allow_complex_guards_as_runtime_asserts": True, |
| 38 | + "enabled_precisions": {torch.float32}, |
| 39 | + "truncate_double": True, |
| 40 | + "min_block_size": 1, |
| 41 | + "use_fp32_acc": True, |
| 42 | + "use_explicit_typing": True, |
| 43 | + "debug": False, |
| 44 | + "use_python_runtime": True, |
| 45 | + "immutable_weights": False, |
| 46 | +} |
| 47 | + |
| 48 | +trt_gm = torch_tensorrt.MutableTorchTensorRTModule(backbone, **settings) |
| 49 | +trt_gm.set_expected_dynamic_shape_range((), dynamic_shapes) |
| 50 | +pipe.transformer = trt_gm |
| 51 | + |
| 52 | + |
| 53 | +def generate_image(prompt, inference_step, batch_size=1): |
| 54 | + image = pipe( |
| 55 | + prompt, |
| 56 | + output_type="pil", |
| 57 | + num_inference_steps=inference_step, |
| 58 | + num_images_per_prompt=batch_size, |
| 59 | + ).images |
| 60 | + return image |
| 61 | + |
| 62 | + |
| 63 | +generate_image(["A golden retriever holding a sign to code"], 2) |
| 64 | + |
| 65 | + |
| 66 | +def model_change(model): |
| 67 | + if model == "Torch Model": |
| 68 | + pipe.transformer = backbone |
| 69 | + backbone.to(DEVICE) |
| 70 | + else: |
| 71 | + backbone.to("cpu") |
| 72 | + pipe.transformer = trt_gm |
| 73 | + torch.cuda.empty_cache() |
| 74 | + |
| 75 | + |
| 76 | +def load_lora(path): |
| 77 | + |
| 78 | + pipe.load_lora_weights( |
| 79 | + path, |
| 80 | + adapter_name="lora1", |
| 81 | + ) |
| 82 | + pipe.set_adapters(["lora1"], adapter_weights=[1]) |
| 83 | + pipe.fuse_lora() |
| 84 | + pipe.unload_lora_weights() |
| 85 | + print("LoRA loaded!") |
| 86 | + |
| 87 | + |
| 88 | +# Create Gradio interface |
| 89 | +with gr.Blocks(title="Flux Demo with Torch-TensorRT") as demo: |
| 90 | + gr.Markdown("# Flux Image Generation Demo Accelerated by Torch-TensorRT") |
| 91 | + |
| 92 | + with gr.Row(): |
| 93 | + with gr.Column(): |
| 94 | + # Input components |
| 95 | + prompt_input = gr.Textbox( |
| 96 | + label="Prompt", placeholder="Enter your prompt here...", lines=3 |
| 97 | + ) |
| 98 | + model_dropdown = gr.Dropdown( |
| 99 | + choices=["Torch Model", "Torch-TensorRT Accelerated Model"], |
| 100 | + value="Torch-TensorRT Accelerated Model", |
| 101 | + label="Model Variant", |
| 102 | + ) |
| 103 | + |
| 104 | + lora_upload_path = gr.Textbox( |
| 105 | + label="LoRA Path", |
| 106 | + placeholder="/home/TensorRT/examples/apps/NGRVNG.safetensors", |
| 107 | + lines=2, |
| 108 | + ) |
| 109 | + num_steps = gr.Slider( |
| 110 | + minimum=20, maximum=100, value=20, step=1, label="Inference Steps" |
| 111 | + ) |
| 112 | + batch_size = gr.Slider( |
| 113 | + minimum=1, maximum=8, value=1, step=1, label="Batch Size" |
| 114 | + ) |
| 115 | + |
| 116 | + generate_btn = gr.Button("Generate Image") |
| 117 | + load_lora_btn = gr.Button("Load LoRA") |
| 118 | + |
| 119 | + with gr.Column(): |
| 120 | + # Output component |
| 121 | + output_image = gr.Gallery(label="Generated Image") |
| 122 | + |
| 123 | + # Connect the button to the generation function |
| 124 | + model_dropdown.change(model_change, inputs=[model_dropdown]) |
| 125 | + generate_btn.click( |
| 126 | + fn=generate_image, |
| 127 | + inputs=[ |
| 128 | + prompt_input, |
| 129 | + num_steps, |
| 130 | + batch_size, |
| 131 | + ], |
| 132 | + outputs=output_image, |
| 133 | + ) |
| 134 | + load_lora_btn.click( |
| 135 | + fn=load_lora, |
| 136 | + inputs=[ |
| 137 | + lora_upload_path, |
| 138 | + ], |
| 139 | + ) |
| 140 | + |
| 141 | +# Launch the interface |
| 142 | +if __name__ == "__main__": |
| 143 | + demo.launch() |
0 commit comments