|
| 1 | +# %% |
| 2 | +# Import the following libraries |
| 3 | +# ----------------------------- |
| 4 | +import modelopt.torch.opt as mto |
| 5 | +import modelopt.torch.quantization as mtq |
| 6 | +import torch |
| 7 | +import torch_tensorrt |
| 8 | +from diffusers import FluxPipeline |
| 9 | +from modelopt.torch.quantization.utils import export_torch_mode |
| 10 | +from torch.export._trace import _export |
| 11 | +from transformers import AutoModelForCausalLM |
| 12 | + |
| 13 | +# Load the ModelOpt-modified model architecture and weights using Huggingface APIs |
| 14 | +# model = AutoModelForCausalLM.from_pretrained("/home/other/quantization/quantized_flux.pt") |
| 15 | + |
| 16 | +# %% |
| 17 | +DEVICE = "cuda:0" |
| 18 | +pipe = FluxPipeline.from_pretrained( |
| 19 | + "black-forest-labs/FLUX.1-dev", |
| 20 | + torch_dtype=torch.float32, |
| 21 | +) |
| 22 | +pipe.to(DEVICE).to(torch.float32) |
| 23 | +# Store the config and transformer backbone |
| 24 | +config = pipe.transformer.config |
| 25 | +# global backbone |
| 26 | +backbone = pipe.transformer |
| 27 | +backbone.eval() |
| 28 | + |
| 29 | + |
| 30 | +def generate_image(pipe, prompt, image_name): |
| 31 | + seed = 42 |
| 32 | + image = pipe( |
| 33 | + prompt, |
| 34 | + output_type="pil", |
| 35 | + num_inference_steps=20, |
| 36 | + generator=torch.Generator("cuda").manual_seed(seed), |
| 37 | + ).images[0] |
| 38 | + image.save(f"{image_name}.png") |
| 39 | + print(f"Image generated using {image_name} model saved as {image_name}.png") |
| 40 | + |
| 41 | + |
| 42 | +generate_image(pipe, ["A golden retriever holding a sign to code"], "dog_code") |
| 43 | + |
| 44 | +# %% |
| 45 | +# Quantization |
| 46 | + |
| 47 | + |
| 48 | +def do_calibrate( |
| 49 | + pipe, |
| 50 | + prompt: str, |
| 51 | +) -> None: |
| 52 | + """ |
| 53 | + Run calibration steps on the pipeline using the given prompts. |
| 54 | + """ |
| 55 | + image = pipe( |
| 56 | + prompt, |
| 57 | + output_type="pil", |
| 58 | + num_inference_steps=20, |
| 59 | + generator=torch.Generator("cuda").manual_seed(0), |
| 60 | + ).images[0] |
| 61 | + |
| 62 | + |
| 63 | +def forward_loop(mod): |
| 64 | + # Switch the pipeline's backbone, run calibration |
| 65 | + pipe.transformer = mod |
| 66 | + do_calibrate( |
| 67 | + pipe=pipe, |
| 68 | + prompt="test", |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +# mto.restore(backbone, "/home/other/quantization/quantized_flux.pt") |
| 73 | +ptq_config = mtq.INT8_DEFAULT_CFG |
| 74 | +backbone = mtq.quantize(backbone, ptq_config, forward_loop) |
| 75 | + |
| 76 | + |
| 77 | +# %% |
| 78 | +# Export the backbone using torch.export |
| 79 | +# -------------------------------------------------- |
| 80 | +# Define the dummy inputs and their respective dynamic shapes. We export the transformer backbone with dynamic shapes with a ``batch_size=2`` |
| 81 | +# 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>`_ |
| 82 | + |
| 83 | +batch_size = 2 |
| 84 | +BATCH = torch.export.Dim("batch", min=1, max=2) |
| 85 | +SEQ_LEN = torch.export.Dim("seq_len", min=1, max=512) |
| 86 | +# This particular min, max values for img_id input are recommended by torch dynamo during the export of the model. |
| 87 | +# To see this recommendation, you can try exporting using min=1, max=4096 |
| 88 | +IMG_ID = torch.export.Dim("img_id", min=3586, max=4096) |
| 89 | +dynamic_shapes = { |
| 90 | + "hidden_states": {0: BATCH}, |
| 91 | + "encoder_hidden_states": {0: BATCH, 1: SEQ_LEN}, |
| 92 | + "pooled_projections": {0: BATCH}, |
| 93 | + "timestep": {0: BATCH}, |
| 94 | + "txt_ids": {0: SEQ_LEN}, |
| 95 | + "img_ids": {0: IMG_ID}, |
| 96 | + "guidance": {0: BATCH}, |
| 97 | + "joint_attention_kwargs": {}, |
| 98 | + "return_dict": None, |
| 99 | +} |
| 100 | +# The guidance factor is of type torch.float32 |
| 101 | +dummy_inputs = { |
| 102 | + "hidden_states": torch.randn((batch_size, 4096, 64), dtype=torch.float16).to( |
| 103 | + DEVICE |
| 104 | + ), |
| 105 | + "encoder_hidden_states": torch.randn( |
| 106 | + (batch_size, 512, 4096), dtype=torch.float16 |
| 107 | + ).to(DEVICE), |
| 108 | + "pooled_projections": torch.randn((batch_size, 768), dtype=torch.float16).to( |
| 109 | + DEVICE |
| 110 | + ), |
| 111 | + "timestep": torch.tensor([1.0, 1.0], dtype=torch.float16).to(DEVICE), |
| 112 | + "txt_ids": torch.randn((512, 3), dtype=torch.float16).to(DEVICE), |
| 113 | + "img_ids": torch.randn((4096, 3), dtype=torch.float16).to(DEVICE), |
| 114 | + "guidance": torch.tensor([1.0, 1.0], dtype=torch.float32).to(DEVICE), |
| 115 | + "joint_attention_kwargs": {}, |
| 116 | + "return_dict": False, |
| 117 | +} |
| 118 | + |
| 119 | +# This will create an exported program which is going to be compiled with Torch-TensorRT |
| 120 | +with export_torch_mode(): |
| 121 | + ep = _export( |
| 122 | + backbone, |
| 123 | + args=(), |
| 124 | + kwargs=dummy_inputs, |
| 125 | + # dynamic_shapes=dynamic_shapes, |
| 126 | + strict=False, |
| 127 | + allow_complex_guards_as_runtime_asserts=True, |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +trt_gm = torch_tensorrt.dynamo.compile( |
| 132 | + ep, |
| 133 | + inputs=dummy_inputs, |
| 134 | + enabled_precisions={torch.int8}, |
| 135 | + truncate_double=True, |
| 136 | + min_block_size=1, |
| 137 | + # use_fp32_acc=True, |
| 138 | + # use_explicit_typing=True, |
| 139 | + debug=False, |
| 140 | + use_python_runtime=True, |
| 141 | + immutable_weights=True, |
| 142 | + offload_module_to_cpu=False, |
| 143 | +) |
| 144 | + |
| 145 | + |
| 146 | +del ep |
| 147 | +pipe.transformer = trt_gm |
| 148 | +pipe.transformer.config = config |
| 149 | + |
| 150 | + |
| 151 | +# %% |
| 152 | +trt_gm.device = torch.device(DEVICE) |
| 153 | +# Function which generates images from the flux pipeline |
| 154 | + |
| 155 | +for _ in range(2): |
| 156 | + generate_image(pipe, ["A golden retriever holding a sign to code"], "dog_code") |
0 commit comments