|
14 | 14 | 1. Sample workflow of Mutable Torch TensorRT Module with ResNet 18
|
15 | 15 | 2. Save a Mutable Torch TensorRT Module
|
16 | 16 | 3. Integration with Huggingface pipeline in LoRA use case
|
| 17 | +4. Usage of dynamic shape with Mutable Torch TensorRT Module |
17 | 18 | """
|
18 | 19 |
|
19 | 20 | import numpy as np
|
|
63 | 64 | # Saving Mutable Torch TensorRT Module
|
64 | 65 | # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
65 | 66 |
|
66 |
| -# Currently, saving is only enabled for C++ runtime, not python runtime. |
| 67 | +# Currently, saving is only when "use_python" = False in settings |
67 | 68 | torch_trt.MutableTorchTensorRTModule.save(mutable_module, "mutable_module.pkl")
|
68 | 69 | reload = torch_trt.MutableTorchTensorRTModule.load("mutable_module.pkl")
|
69 | 70 |
|
70 | 71 | # %%
|
71 | 72 | # Stable Diffusion with Huggingface
|
72 | 73 | # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
73 | 74 |
|
74 |
| -# The LoRA checkpoint is from https://civitai.com/models/12597/moxin |
75 |
| - |
76 | 75 | from diffusers import DiffusionPipeline
|
77 | 76 |
|
78 | 77 | with torch.no_grad():
|
|
111 | 110 | # Refit triggered
|
112 | 111 | image = pipe(prompt, negative_prompt=negative, num_inference_steps=30).images[0]
|
113 | 112 | image.save("./with_LoRA_mutable.jpg")
|
| 113 | + |
| 114 | + |
| 115 | +# %% |
| 116 | +# Use Mutable Torch TensorRT module with dynamic shape |
| 117 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 118 | +class Model(torch.nn.Module): |
| 119 | + def __init__(self): |
| 120 | + super().__init__() |
| 121 | + |
| 122 | + def forward(self, a, b, c={}): |
| 123 | + x = torch.matmul(a, b) |
| 124 | + x = torch.matmul(c["a"], c["b"].T) |
| 125 | + print(c["b"][0]) |
| 126 | + x = 2 * c["b"] |
| 127 | + return x |
| 128 | + |
| 129 | + |
| 130 | +device = "cuda:0" |
| 131 | +model = Model().eval().to(device) |
| 132 | +inputs = (torch.rand(10, 3).to(device), torch.rand(3, 30).to(device)) |
| 133 | +kwargs = { |
| 134 | + "c": {"a": torch.rand(10, 30).to(device), "b": torch.rand(10, 30).to(device)}, |
| 135 | +} |
| 136 | +dim_0 = torch.export.Dim("dim", min=1, max=50) |
| 137 | +dim_1 = torch.export.Dim("dim", min=1, max=50) |
| 138 | +dim_2 = torch.export.Dim("dim2", min=1, max=50) |
| 139 | +args_dynamic_shapes = ({1: dim_1}, {0: dim_0}) |
| 140 | +kwarg_dynamic_shapes = { |
| 141 | + "c": {"a": {}, "b": {0: dim_2}}, |
| 142 | +} |
| 143 | +# Export the model first with custom dynamic shape constraints |
| 144 | +model = torch_trt.MutableTorchTensorRTModule(model, debug=True, min_block_size=1) |
| 145 | +model.set_expected_dynamic_shape_range(args_dynamic_shapes, kwarg_dynamic_shapes) |
| 146 | +# Compile |
| 147 | +model(*inputs, **kwargs) |
| 148 | +# Change input shape |
| 149 | +inputs_2 = (torch.rand(10, 5).to(device), torch.rand(10, 30).to(device)) |
| 150 | +kwargs_2 = { |
| 151 | + "c": {"a": torch.rand(10, 30).to(device), "b": torch.rand(5, 30).to(device)}, |
| 152 | +} |
| 153 | +# Run without recompiling |
| 154 | +model(*inputs_2, **kwargs_2) |
0 commit comments