Skip to content
Draft
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
7 changes: 7 additions & 0 deletions docsrc/getting_started/windows_arm64.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ Native build on Windows ARM64
python -m pip install C:\path\to\the\generated-win_arm64.whl
python -c "import torch_tensorrt; print(torch_tensorrt.__version__)"

For FP16 Flux and SDXL examples, see the repository's
`application examples <https://github.com/pytorch/TensorRT/tree/main/examples/apps>`_.
Install their pipeline dependencies in this native ARM64 environment without
the optional ``modelopt`` extra. The reduced
`Flux smoke test <https://github.com/pytorch/TensorRT/tree/main/tools/perf/Flux>`_
checks transformer compilation without downloading pretrained weights.

Cross-build on Windows x64
--------------------------

Expand Down
97 changes: 97 additions & 0 deletions examples/apps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,100 @@ You can combine these options as needed. For example:
```bash
python flux_demo.py --dtype fp8 --dynamic_shapes --low_vram_mode
```

## Windows ARM64 Flux example

Build and install the native ARM64 Torch-TensorRT-RTX wheel following the
[Windows ARM64 guide](https://docs.pytorch.org/TensorRT/getting_started/windows_arm64.html).
Use that same native ARM64 CUDA PyTorch environment for the examples, and
install the dependencies listed above without ModelOpt. Run FP16 from the
repository root:

```bash
python examples/apps/flux_demo.py --dtype fp16 --low_vram_mode --output flux.png
```

This generates `flux.png` before launching the Gradio app. The full Flux
pipeline downloads pretrained weights and needs substantially more memory than
the [reduced Flux smoke test](../../tools/perf/Flux/README.md). For gated model
weights, accept the model license and authenticate with Hugging Face first.
FP4, FP8, and INT8 modes require ModelOpt and are not supported on Windows ARM64.

## SDXL image generation

`sdxl_demo.py` loads the pretrained SDXL base pipeline and saves one generated
image. It runs the text encoders, denoising loop, and VAE decoder end to end.
The default mode accelerates the UNet with Torch-TensorRT; `--pytorch` runs the
pipeline with PyTorch only. Both modes use FP16 weights and require a CUDA GPU.
ModelOpt, xformers, Gradio, LoRA weights, and the SDXL refiner are not required.

On Windows ARM64, install native ARM64 CUDA PyTorch and a matching
[Torch-TensorRT-RTX build](https://docs.pytorch.org/TensorRT/getting_started/windows_arm64.html)
before running the TensorRT mode. The PyTorch mode does not import Torch-TensorRT.
Install the pipeline dependencies into that same environment:

```bash
python -m pip install --upgrade diffusers transformers "peft>=0.21.2" accelerate safetensors
```

From the repository root, first generate a PyTorch baseline:

```bash
python examples/apps/sdxl_demo.py --pytorch --output sdxl_pytorch.png
```

Then generate an image with the TensorRT UNet:

```bash
python examples/apps/sdxl_demo.py --output sdxl_trt.png
```

The first run downloads the model weights. The first TensorRT generation also
compiles the UNet and takes longer than subsequent denoising steps. Outputs
default to 1024x1024 with 30 inference steps; for a smaller initial run, add
`--height 512 --width 512 --num_inference_steps 10`. Change the scene with
`--prompt "a red panda sitting in a bamboo forest"`. `--model` also accepts a
local Diffusers SDXL base checkpoint directory containing the FP16 variant.

### Timing and comparing with PyTorch

Run both backends with the same prompt, seed, resolution, and inference steps:

```bash
python examples/apps/sdxl_demo.py --compare --height 512 --width 512 --num_inference_steps 10 --runs 3 --timings_json sdxl_timings.json
```

This loads the pipeline once, benchmarks native PyTorch first, then wraps the
same UNet with Torch-TensorRT. It saves `sdxl_pytorch.png` and
`sdxl_tensorrt.png`. In comparison mode, `--output scene.png` changes those names
to `scene_pytorch.png` and `scene_tensorrt.png`.

Each backend reports its first generation separately, followed by `--runs`
measured generations (default: 1). The first generation is excluded from the
warmed mean, median, and speedup. `--warmup N` adds N more unmeasured generations
after the first. Normal single-backend invocations also report these timings.

The stage timings are synchronized wall-clock seconds:

- Pipeline imports, model download/loading, GPU transfer, TensorRT import, and
wrapper construction are setup costs, reported separately.
- `compile/export` measures the mutable module's compilation method, including
graph export, lowering, engine building, and source-model offloading.
- Text encoding, UNet calls excluding compilation, VAE decoding, and other
pipeline work add up to generation time. Other work includes the scheduler,
latent preparation, and PIL conversion. UNet timing includes dispatch and
mutable-module checks as well as inference. Any first-execution engine
initialization or TensorRT-RTX runtime JIT is also included in UNet time,
rather than the separate mutable-module `compile/export` measurement.
- Image saving is reported separately and excluded from generation time.

The comparison uses the median warmed end-to-end generation time. If compilation
occurs again in measured runs, the script flags it. Stage measurements synchronize
CUDA at each boundary, adding some diagnostic overhead to both backends. The
TensorRT first generation follows the PyTorch runs and shares their initialized
CUDA context; its compile timing remains measured directly.

The earlier SDXL example is in
[`examples/dynamo/mutable_torchtrt_module_example.py`](../dynamo/mutable_torchtrt_module_example.py),
under "Stable Diffusion with Huggingface". That tutorial additionally runs
ResNet, dynamic shapes, LoRA refitting, and engine caching examples.
7 changes: 6 additions & 1 deletion examples/apps/flux_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import sys
import time
from pathlib import Path

import gradio as gr
import torch
Expand Down Expand Up @@ -148,7 +149,10 @@ def forward_loop(mod):
generator=torch.Generator("cuda").manual_seed(seed),
).images
print(f"generated {len(image)} images")
image[0].save("/tmp/forest.png")
output = Path(args.output)
output.parent.mkdir(parents=True, exist_ok=True)
image[0].save(output)
print(f"Saved image to {output.resolve()}")

torch.cuda.empty_cache()

Expand Down Expand Up @@ -264,6 +268,7 @@ def main(args):
parser = argparse.ArgumentParser(
description="Run Flux quantization with different dtypes"
)
parser.add_argument("--output", type=Path, default=Path("forest.png"))
parser.add_argument(
"--model",
default="black-forest-labs/FLUX.1-dev",
Expand Down
Loading
Loading