|
1 | 1 | """
|
2 |
| -.. _refit_engine_example: |
| 2 | +.. _mutable_torchtrt_module_example: |
3 | 3 |
|
4 |
| -Refit TenorRT Graph Module with Torch-TensorRT |
| 4 | +Mutable Torch TensorRT Module |
5 | 5 | ===================================================================
|
6 | 6 |
|
7 |
| -We are going to demonstrate how a compiled TensorRT Graph Module can be refitted with updated weights. |
| 7 | +We are going to demonstrate how we can easily use Mutable Torch TensorRT Module to compile, interact, and modify the TensorRT Graph Module. |
8 | 8 |
|
9 |
| -In many cases, we frequently update the weights of models, such as applying various LoRA to Stable Diffusion or constant A/B testing of AI products. |
10 |
| -That poses challenges for TensorRT inference optimizations, as compiling the TensorRT engines takes significant time, making repetitive compilation highly inefficient. |
11 |
| -Torch-TensorRT supports refitting TensorRT graph modules without re-compiling the engine, considerably accelerating the workflow. |
| 9 | +Compiling a Torch-TensorRT module is straightforward, but modifying the compiled module can be challenging, especially when it comes to maintaining the state and connection between the PyTorch module and the corresponding Torch-TensorRT module. |
| 10 | +In Ahead-of-Time (AoT) scenarios, integrating Torch TensorRT with complex pipelines, such as the Hugging Face Stable Diffusion pipeline, becomes even more difficult. |
| 11 | +The Mutable Torch TensorRT Module is designed to address these challenges, making interaction with the Torch-TensorRT module easier than ever. |
12 | 12 |
|
13 | 13 | In this tutorial, we are going to walk through
|
14 |
| -1. Compiling a PyTorch model to a TensorRT Graph Module |
15 |
| -2. Save and load a graph module |
16 |
| -3. Refit the graph module |
| 14 | +1. Sample workflow of Mutable Torch TensorRT Module with ResNet 18 |
| 15 | +2. Save a Mutable Torch TensorRT Module |
| 16 | +3. Integration with Huggingface pipeline in LoRA use case |
17 | 17 | """
|
18 | 18 |
|
19 | 19 | import numpy as np
|
|
26 | 26 | inputs = [torch.rand((1, 3, 224, 224)).to("cuda")]
|
27 | 27 |
|
28 | 28 | # %%
|
29 |
| -# Compile the module for the first time and save it. |
| 29 | +# Initialize the Mutable Torch TensorRT Module with settings. |
30 | 30 | # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
31 |
| -kwargs = { |
| 31 | +settings = { |
32 | 32 | "use_python": False,
|
33 | 33 | "enabled_precisions": {torch.float32},
|
34 | 34 | "make_refitable": True,
|
35 | 35 | }
|
36 | 36 |
|
37 | 37 | model = models.resnet18(pretrained=False).eval().to("cuda")
|
38 |
| -model2 = models.resnet18(pretrained=True).eval().to("cuda") |
39 |
| -mutable_module = torch_trt.MutableTorchTensorRTModule(model, **kwargs) |
| 38 | +mutable_module = torch_trt.MutableTorchTensorRTModule(model, **settings) |
| 39 | +# You can use the mutable module just like the original pytorch module. The compilation happens while you first call the mutable module. |
40 | 40 | mutable_module(*inputs)
|
41 | 41 |
|
| 42 | +# %% |
| 43 | +# Make modifications to the mutable module. |
| 44 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 45 | + |
| 46 | +# %% |
| 47 | +# Making changes to mutable module can trigger refit or re-compilation. For example, loading a different state_dict and setting new weight values will trigger refit, and adding a module to the model will trigger re-compilation. |
| 48 | +model2 = models.resnet18(pretrained=True).eval().to("cuda") |
42 | 49 | mutable_module.load_state_dict(model2.state_dict())
|
43 | 50 |
|
44 | 51 |
|
45 | 52 | # Check the output
|
| 53 | +# The refit happens while you call the mutable module again. |
46 | 54 | expected_outputs, refitted_outputs = model2(*inputs), mutable_module(*inputs)
|
47 | 55 | for expected_output, refitted_output in zip(expected_outputs, refitted_outputs):
|
48 | 56 | assert torch.allclose(
|
49 | 57 | expected_output, refitted_output, 1e-2, 1e-2
|
50 | 58 | ), "Refit Result is not correct. Refit failed"
|
51 | 59 |
|
52 | 60 | print("Refit successfully!")
|
| 61 | + |
| 62 | +# %% |
| 63 | +# Saving Mutable Torch TensorRT Module |
| 64 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 65 | + |
| 66 | +# Currently, saving is only enabled for C++ runtime, not python runtime. |
53 | 67 | torch_trt.MutableTorchTensorRTModule.save(mutable_module, "mutable_module.pkl")
|
54 | 68 | reload = torch_trt.MutableTorchTensorRTModule.load("mutable_module.pkl")
|
| 69 | + |
| 70 | +# %% |
| 71 | +# Stable Diffusion with Huggingface |
| 72 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 73 | + |
| 74 | +# The LoRA checkpoint is from https://civitai.com/models/12597/moxin |
| 75 | + |
| 76 | +from diffusers import DiffusionPipeline |
| 77 | + |
| 78 | +with torch.no_grad(): |
| 79 | + kwargs = { |
| 80 | + "use_python_runtime": True, |
| 81 | + "enabled_precisions": {torch.float16}, |
| 82 | + "debug": True, |
| 83 | + "make_refitable": True, |
| 84 | + } |
| 85 | + |
| 86 | + model_id = "runwayml/stable-diffusion-v1-5" |
| 87 | + device = "cuda:0" |
| 88 | + |
| 89 | + prompt = "portrait of a woman standing, shuimobysim, wuchangshuo, best quality" |
| 90 | + negative = "(worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, skin spots, acnes, skin blemishes, age spot, glans, (watermark:2)," |
| 91 | + |
| 92 | + pipe = DiffusionPipeline.from_pretrained( |
| 93 | + model_id, revision="fp16", torch_dtype=torch.float16 |
| 94 | + ) |
| 95 | + pipe.to(device) |
| 96 | + |
| 97 | + # The only extra line you need |
| 98 | + pipe.unet = torch_trt.MutableTorchTensorRTModule(pipe.unet, **kwargs) |
| 99 | + |
| 100 | + image = pipe(prompt, negative_prompt=negative, num_inference_steps=30).images[0] |
| 101 | + image.save("./without_LoRA_mutable.jpg") |
| 102 | + |
| 103 | + # Standard Huggingface LoRA loading procedure |
| 104 | + pipe.load_lora_weights("./moxin.safetensors", adapter_name="lora1") |
| 105 | + pipe.set_adapters(["lora1"], adapter_weights=[1]) |
| 106 | + pipe.fuse_lora() |
| 107 | + pipe.unload_lora_weights() |
| 108 | + |
| 109 | + # Refit triggered |
| 110 | + image = pipe(prompt, negative_prompt=negative, num_inference_steps=30).images[0] |
| 111 | + image.save("./with_LoRA_mutable.jpg") |
0 commit comments