Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Model Optimizer Changelog (Linux)
- Add support for multi-node PTQ and export with FSDP2 in ``examples/llm_ptq/multinode_ptq.py``. See `examples/llm_ptq/README.md <https://github.com/NVIDIA/TensorRT-Model-Optimizer/tree/main/examples/llm_ptq#multi-node-post-training-quantization-with-fsdp2>`_ for more details.
- Add support for Nemotron Nano VL v1 & v2 models in FP8/NVFP4 PTQ workflow.
- Add flags ``nodes_to_include`` and ``op_types_to_include`` in AutoCast to force-include nodes in low precision, even if they would otherwise be excluded by other rules.
- Add support for ``torch.compile`` and benchmarking in ``examples/diffusers/quantization/diffusion_trt.py``.

**Documentation**

Expand Down
9 changes: 7 additions & 2 deletions examples/diffusers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,16 @@ Generate images for the quantized checkpoint with the following [Script](./quant
python diffusion_trt.py \
--model {sdxl-1.0|sdxl-turbo|sd3-medium|flux-dev} \
--prompt "A cat holding a sign that says hello world" \
[--override-model-path /path/to/model] \
[--restore-from ./{MODEL}_fp8.pt] \
[--onnx-load-path {ONNX_DIR}] \
[--trt-engine-load-path {ENGINE_DIR}] \
[--dq_only] \
[--torch]
[--dq-only] \
[--torch] \
[--save-image-as /path/to/image] \
[--benchmark] \
[--torch-compile] \
[--skip-image]
```

This script will save the output image as `./{MODEL}.png` and report the latency of the TensorRT backbone.
Expand Down
31 changes: 12 additions & 19 deletions examples/diffusers/quantization/diffusion_trt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
"flux-schnell": ModelType.FLUX_SCHNELL,
}

dtype_map = {
"Half": torch.float16,
"BFloat16": torch.bfloat16,
"Float": torch.float32,
DTYPE_MAP = {
"sdxl-1.0": torch.float16,
"sdxl-turbo": torch.float16,
"sd3-medium": torch.float16,
"flux-dev": torch.bfloat16,
"flux-schnell": torch.bfloat16,
}


Expand All @@ -60,7 +62,7 @@ def generate_image(pipe, prompt, image_name):


def benchmark_model(
pipe, prompt, num_warmup=10, num_runs=50, num_inference_steps=20, model_dtype="Half"
pipe, prompt, num_warmup=10, num_runs=50, num_inference_steps=20, model_dtype=torch.float16
):
"""Benchmark the backbone model inference time."""
backbone = pipe.transformer if hasattr(pipe, "transformer") else pipe.unet
Expand All @@ -83,7 +85,7 @@ def forward_hook(_module, _input, _output):
try:
print(f"Starting warmup: {num_warmup} runs")
for _ in tqdm(range(num_warmup), desc="Warmup"):
with torch.amp.autocast("cuda", dtype=dtype_map[model_dtype]):
with torch.amp.autocast("cuda", dtype=model_dtype):
_ = pipe(
prompt,
output_type="pil",
Expand All @@ -95,7 +97,7 @@ def forward_hook(_module, _input, _output):

print(f"Starting benchmark: {num_runs} runs")
for _ in tqdm(range(num_runs), desc="Benchmark"):
with torch.amp.autocast("cuda", dtype=dtype_map[model_dtype]):
with torch.amp.autocast("cuda", dtype=model_dtype):
_ = pipe(
prompt,
output_type="pil",
Expand Down Expand Up @@ -126,13 +128,6 @@ def main():
default=None,
help="Path to the model if not using default paths in MODEL_ID mapping.",
)
parser.add_argument(
"--model-dtype",
type=str,
default="Half",
choices=["Half", "BFloat16", "Float"],
help="Precision used to load the model.",
)
parser.add_argument(
"--restore-from", type=str, default=None, help="Path to the modelopt quantized checkpoint"
)
Expand Down Expand Up @@ -167,10 +162,11 @@ def main():
args = parser.parse_args()

image_name = args.save_image_as if args.save_image_as else f"{args.model}.png"
model_dtype = DTYPE_MAP[args.model]

pipe = PipelineManager.create_pipeline_from(
MODEL_ID[args.model],
dtype_map[args.model_dtype],
torch_dtype=model_dtype,
override_model_path=args.override_model_path,
)

Expand All @@ -189,9 +185,6 @@ def main():
mto.restore(backbone, args.restore_from)

if args.torch_compile:
assert args.model_dtype in ["BFloat16", "Float", "Half"], (
"torch.compile() only supports BFloat16 and Float"
)
print("Compiling backbone with torch.compile()...")
backbone = torch.compile(backbone, mode="max-autotune")

Expand All @@ -203,7 +196,7 @@ def main():
pipe.to("cuda")

if args.benchmark:
benchmark_model(pipe, args.prompt, model_dtype=args.model_dtype)
benchmark_model(pipe, args.prompt, model_dtype=model_dtype)

if not args.skip_image:
generate_image(pipe, args.prompt, image_name)
Expand Down