|
| 1 | +# %% |
| 2 | +# Import the following libraries |
| 3 | +# ----------------------------- |
| 4 | +import re |
| 5 | + |
| 6 | +import modelopt.torch.opt as mto |
| 7 | +import modelopt.torch.quantization as mtq |
| 8 | +import torch |
| 9 | +import torch_tensorrt |
| 10 | +from diffusers import FluxPipeline |
| 11 | +from diffusers.models.attention_processor import Attention |
| 12 | +from diffusers.models.transformers.transformer_flux import FluxTransformer2DModel |
| 13 | +from modelopt.torch.quantization.utils import export_torch_mode |
| 14 | +from torch.export._trace import _export |
| 15 | +from transformers import AutoModelForCausalLM |
| 16 | + |
| 17 | +# %% |
| 18 | +DEVICE = "cuda:0" |
| 19 | +pipe = FluxPipeline.from_pretrained( |
| 20 | + "black-forest-labs/FLUX.1-dev", |
| 21 | + torch_dtype=torch.float32, |
| 22 | +) |
| 23 | +pipe.transformer = FluxTransformer2DModel( |
| 24 | + num_layers=1, num_single_layers=1, guidance_embeds=True |
| 25 | +) |
| 26 | + |
| 27 | +pipe.to(DEVICE).to(torch.float32) |
| 28 | +# Store the config and transformer backbone |
| 29 | +config = pipe.transformer.config |
| 30 | +# global backbone |
| 31 | +backbone = pipe.transformer |
| 32 | +backbone.eval() |
| 33 | + |
| 34 | + |
| 35 | +def filter_func(name): |
| 36 | + pattern = re.compile( |
| 37 | + r".*(time_emb_proj|time_embedding|conv_in|conv_out|conv_shortcut|add_embedding|pos_embed|time_text_embed|context_embedder|norm_out|x_embedder).*" |
| 38 | + ) |
| 39 | + return pattern.match(name) is not None |
| 40 | + |
| 41 | + |
| 42 | +def generate_image(pipe, prompt, image_name): |
| 43 | + seed = 42 |
| 44 | + image = pipe( |
| 45 | + prompt, |
| 46 | + output_type="pil", |
| 47 | + num_inference_steps=20, |
| 48 | + generator=torch.Generator("cuda").manual_seed(seed), |
| 49 | + ).images[0] |
| 50 | + image.save(f"{image_name}.png") |
| 51 | + print(f"Image generated using {image_name} model saved as {image_name}.png") |
| 52 | + |
| 53 | + |
| 54 | +generate_image(pipe, ["A golden retriever holding a sign to code"], "dog_code") |
| 55 | + |
| 56 | +# %% |
| 57 | +# Quantization |
| 58 | + |
| 59 | + |
| 60 | +def do_calibrate( |
| 61 | + pipe, |
| 62 | + prompt: str, |
| 63 | +) -> None: |
| 64 | + """ |
| 65 | + Run calibration steps on the pipeline using the given prompts. |
| 66 | + """ |
| 67 | + image = pipe( |
| 68 | + prompt, |
| 69 | + output_type="pil", |
| 70 | + num_inference_steps=20, |
| 71 | + generator=torch.Generator("cuda").manual_seed(0), |
| 72 | + ).images[0] |
| 73 | + |
| 74 | + |
| 75 | +def forward_loop(mod): |
| 76 | + # Switch the pipeline's backbone, run calibration |
| 77 | + pipe.transformer = mod |
| 78 | + do_calibrate( |
| 79 | + pipe=pipe, |
| 80 | + prompt="test", |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +ptq_config = mtq.FP8_DEFAULT_CFG |
| 85 | +backbone = mtq.quantize(backbone, ptq_config, forward_loop) |
| 86 | +mtq.disable_quantizer(backbone, filter_func) |
| 87 | + |
| 88 | + |
| 89 | +# %% |
| 90 | +# Export the backbone using torch.export |
| 91 | +# -------------------------------------------------- |
| 92 | +# Define the dummy inputs and their respective dynamic shapes. We export the transformer backbone with dynamic shapes with a ``batch_size=2`` |
| 93 | +# due to `0/1 specialization <https://docs.google.com/document/d/16VPOa3d-Liikf48teAOmxLc92rgvJdfosIy-yoT38Io/edit?fbclid=IwAR3HNwmmexcitV0pbZm_x1a4ykdXZ9th_eJWK-3hBtVgKnrkmemz6Pm5jRQ&tab=t.0#heading=h.ez923tomjvyk>`_ |
| 94 | + |
| 95 | +batch_size = 2 |
| 96 | +BATCH = torch.export.Dim("batch", min=1, max=2) |
| 97 | +SEQ_LEN = torch.export.Dim("seq_len", min=1, max=512) |
| 98 | +# This particular min, max values for img_id input are recommended by torch dynamo during the export of the model. |
| 99 | +# To see this recommendation, you can try exporting using min=1, max=4096 |
| 100 | +IMG_ID = torch.export.Dim("img_id", min=3586, max=4096) |
| 101 | +dynamic_shapes = { |
| 102 | + "hidden_states": {0: BATCH}, |
| 103 | + "encoder_hidden_states": {0: BATCH, 1: SEQ_LEN}, |
| 104 | + "pooled_projections": {0: BATCH}, |
| 105 | + "timestep": {0: BATCH}, |
| 106 | + "txt_ids": {0: SEQ_LEN}, |
| 107 | + "img_ids": {0: IMG_ID}, |
| 108 | + "guidance": {0: BATCH}, |
| 109 | + "joint_attention_kwargs": {}, |
| 110 | + "return_dict": None, |
| 111 | +} |
| 112 | +# The guidance factor is of type torch.float32 |
| 113 | +dummy_inputs = { |
| 114 | + "hidden_states": torch.randn((batch_size, 4096, 64), dtype=torch.float32).to( |
| 115 | + DEVICE |
| 116 | + ), |
| 117 | + "encoder_hidden_states": torch.randn( |
| 118 | + (batch_size, 512, 4096), dtype=torch.float32 |
| 119 | + ).to(DEVICE), |
| 120 | + "pooled_projections": torch.randn((batch_size, 768), dtype=torch.float32).to( |
| 121 | + DEVICE |
| 122 | + ), |
| 123 | + "timestep": torch.tensor([1.0, 1.0], dtype=torch.float32).to(DEVICE), |
| 124 | + "txt_ids": torch.randn((512, 3), dtype=torch.float32).to(DEVICE), |
| 125 | + "img_ids": torch.randn((4096, 3), dtype=torch.float32).to(DEVICE), |
| 126 | + "guidance": torch.tensor([1.0, 1.0], dtype=torch.float32).to(DEVICE), |
| 127 | + "joint_attention_kwargs": {}, |
| 128 | + "return_dict": False, |
| 129 | +} |
| 130 | + |
| 131 | +# This will create an exported program which is going to be compiled with Torch-TensorRT |
| 132 | +with export_torch_mode(): |
| 133 | + ep = _export( |
| 134 | + backbone, |
| 135 | + args=(), |
| 136 | + kwargs=dummy_inputs, |
| 137 | + dynamic_shapes=dynamic_shapes, |
| 138 | + strict=False, |
| 139 | + allow_complex_guards_as_runtime_asserts=True, |
| 140 | + ) |
| 141 | + |
| 142 | +with torch_tensorrt.logging.debug(): |
| 143 | + trt_gm = torch_tensorrt.dynamo.compile( |
| 144 | + ep, |
| 145 | + inputs=dummy_inputs, |
| 146 | + enabled_precisions={torch.float8_e4m3fn}, |
| 147 | + truncate_double=True, |
| 148 | + min_block_size=1, |
| 149 | + debug=False, |
| 150 | + use_python_runtime=True, |
| 151 | + immutable_weights=True, |
| 152 | + offload_module_to_cpu=True, |
| 153 | + ) |
| 154 | + |
| 155 | + |
| 156 | +del ep |
| 157 | +pipe.transformer = trt_gm |
| 158 | +pipe.transformer.config = config |
| 159 | + |
| 160 | + |
| 161 | +# %% |
| 162 | +trt_gm.device = torch.device(DEVICE) |
| 163 | +# Function which generates images from the flux pipeline |
| 164 | + |
| 165 | +for _ in range(2): |
| 166 | + generate_image(pipe, ["A golden retriever holding a sign to code"], "dog_code") |
| 167 | + |
| 168 | +# For this dummy model, the fp16 engine size is around 1GB, fp32 engine size is around 2GB |
0 commit comments