借助 NVIDIA Blackwell GPU 加速 FLUX 3 Image Klein 图像生成
原题Speed Up Image Generation with FLUX 3 Image Klein on NVIDIA Blackwell GPUs
读前速览
本文展示如何使用 TensorRT LLM VisualGen 优化 NVIDIA Blackwell GPU 上的 FLUX 3 Image Klein,通过 NVFP4 精度、GEMM 内核优化、量化注意力、步骤缓存和多 GPU 并行,实现最高 3.46 倍的文生图加速和 4.45 倍的图生图加速。
NVFP4 精度在 Blackwell GPU 上可实现最高 1.62 倍的生成速度提升,相较于 BF16。
NVFP4 使用 4 位 E2M1 元素和 FP8 缩放因子,在保持准确性的同时缩小内存占用;配合 TensorRT LLM VisualGen 对预量化检查点的支持,可实现更快推理。
VC-Attention FP8 相较于 VANILLA BF16 注意力实现 1.18 倍加速,同时只造成极小的感知质量损失。
通过使用 ρ=0.005 的修复预算来校正量化误差较大的 token,VC-Attention FP8 相较于 cuDNN FP8 实现了更好的速度与质量平衡;Base 版本的 LPIPS 分数为 0.086,蒸馏版 Klein 为 0.151。
Cache-DiT 通过在不同去噪步骤间复用中间 Transformer 输出,将推理延迟几乎减半。
在残差差异阈值为 0.24 时,Cache-DiT 在 2048×2048 分辨率下使 Base 版本提速 1.94 倍、蒸馏版 Klein 提速 1.20 倍,LPIPS 分数分别为 0.033 和 0.022。
结合 Ulysses 序列并行和 CFG 扩展的多 GPU 并行可显著降低延迟。
从 1 张扩展到 8 张 B200 GPU 后,Base 版本的 T2I 中位延迟从 14.90 秒降至 3.00 秒,蒸馏版 Klein 从 2.73 秒降至 1.08 秒;去噪速度分别提升 6.01 倍和 4.44 倍。
多种优化组合后,单张 B200 GPU 上的 T2I 最高提速 3.46 倍,I2I 最高提速 4.45 倍。
编译、NVFP4 与 PrimsTS GEMM、Sol-Attention 以及 Cache-DiT 组合使用后,单张 B200 GPU 上的 T2I 延迟从 22.36 秒降至 6.46 秒,I2I 延迟从 54.64 秒降至 12.28 秒。
完整摘要
本文提供了一份全面的教程,介绍如何使用 TensorRT LLM VisualGen 优化 Black Forest Labs 的扩散模型 FLUX 3 Image Klein,使其适用于 NVIDIA Blackwell GPU。文章详细介绍了多种优化技术,包括 NVFP4 精度支持、FlashInfer PrimsTS GEMM 内核、量化注意力方法(cuDNN FP8 和 VC-Attention FP8)、稀疏 Sol-Attention、用于步骤缓存的 Cache-DiT,以及借助 Ulysses 序列并行实现多 GPU 扩展。基准测试显示,性能延迟显著降低:在单张 B200 GPU 上,与未编译的 BF16 推理相比,文生图最高提速 3.46 倍,图生图最高提速 4.45 倍;多 GPU 配置和 CUDA Graphs 还能进一步提升性能。
目录 · 15 节
在设计工具、图像编辑器和视觉制作工作流中部署扩散模型时,开发者必须考虑推理延迟。生成图像需要依次多次运行一个拥有数十亿参数的模型,而更高的分辨率会增加注意力计算的成本。When deploying diffusion models in design tools, image editors, and visual production workflows, developers must account for inference latency. Generating an image requires multiple sequential passes through a model with billions of parameters, and higher resolutions increase attention costs.
FLUX 3 Image Klein 是 Black Forest Labs (BFL) 推出的图像编辑与生成模型,基于 NVIDIA GB200 NVL72 训练,并针对 NVIDIA GPU 进行了优化。它提供 Base 和蒸馏版检查点,以满足不同的延迟和保真度需求。FLUX 3 Image Klein is an image editing and generation model from Black Forest Labs (BFL) trained on NVIDIA GB200 NVL72 and optimized for NVIDIA GPUs. It offers Base and distilled checkpoints for different latency and fidelity requirements.
本教程介绍如何在 NVIDIA TensorRT LLM VisualGen 中配置 FLUX 3 Image Klein,以利用融合内核、多 GPU 并行、FP8/NVFP4 支持、缓存和注意力优化。教程还会比较各项优化对延迟和生成结果的影响。在报告的单块 NVIDIA B200 GPU 上进行的 2048×2048 测试中,与未经编译的 BF16 推理相比,组合优化使 FLUX 3 Image Klein Base 的文生图(T2I)生成速度最高提升至 3.46 倍,图生图(I2I)生成速度最高提升至 4.45 倍。本教程还包含部署说明和 YAML 配置,帮助你将这些优化应用于自己的工作负载。This tutorial shows how to configure FLUX 3 Image Klein in NVIDIA TensorRT LLM VisualGen to take advantage of fused kernels, multi-GPU parallelism, FP8/NVFP4 support, caching, and attention optimizations. It compares latency and changes in generated outputs across optimizations. In the reported 2048×2048 tests on a single NVIDIA B200 GPU, combined optimizations delivered up to 3.46x faster text-to-image (T2I) and 4.45x faster image-to-image (I2I) generation for FLUX 3 Image Klein Base compared with uncompiled BF16 inference. The tutorial includes deployment instructions and YAML configurations for applying these optimizations to your workloads.
使用 TensorRT LLM VisualGen 进行 FLUX 3 推理FLUX 3 inference with TensorRT LLM VisualGen
FLUX 3 Image Klein 将 Qwen3-VL-4B-Instruct 用于文本条件输入,将扩散 Transformer(DiT)用于细化图像潜变量,并使用自动编码器将潜变量解码为图像,以及对 I2I 参考输入进行编码。Klein 会先分别处理文本、带噪目标图像和可选的参考图像流,再通过联合注意力将它们组合起来。FLUX 3 Image Klein combines Qwen3-VL-4B-Instruct for text conditioning, a diffusion transformer (DiT) to refine image latents, and an autoencoder to decode latents into images and encode I2I reference inputs. Klein processes text, noisy target-image, and optional reference-image streams independently before combining them through joint attention.
TensorRT LLM VisualGen 是 TensorRT LLM 中的专用运行时,发布时支持 BF16、FP8 和 NVFP4 变体。在 NVIDIA Blackwell GPU 上,与 BF16 相比,NVFP4 的 T2I 生成速度最高可提升 1.62 倍,I2I 生成速度最高可提升 1.58 倍。图 2 对比了生成延迟,图 1 则对比了示例输出。以下章节将以 NVFP4 为起点,进一步进行优化。TensorRT LLM VisualGen, a dedicated runtime within TensorRT LLM, supported BF16, FP8, and NVFP4 variants at launch. On an NVIDIA Blackwell GPU, NVFP4 delivered up to 1.62x faster T2I and 1.58x faster I2I generation than BF16. Figure 2 compares generation latency, while Figure 1 compares example outputs. The following sections use NVFP4 as the starting point for further optimization.
图 1 比较了 2048×2048 分辨率下的输出。概述后的 T2I 提示词描述了一幅厨房静物画;概述后的 I2I 提示词要求对太空场景进行修改,包括一个红色气态巨行星、一条银色巨龙和一块雕刻铭牌。Figure 1 compares outputs at 2048×2048. The summarized T2I prompt describes a kitchen still life; the summarized I2I prompt requests changes to a space scene, including a red gas giant, a silver dragon, and an engraved plaque.
以 NVFP4 为起点,VisualGen 的优化方向转向去噪,因为去噪占据了 B200 上整体生成时间的大部分。因此,以下各节将重点介绍如何加速 FLUX 3 的 DiT。With NVFP4 as the starting point, VisualGen optimizations turned to denoising, which dominates overall generation time on B200. Accelerating FLUX 3’s DiT is therefore the focus of the following sections.
表 1 展示了在一块 NVIDIA B200 GPU 上使用 NVFP4、VANILLA attention 和 torch.compile 时,2048×2048 分辨率下的流水线延迟。结果为 10 次请求的平均值,测量前进行了 2 次预热请求。Base 使用 50 个去噪步骤,classifier-free guidance (CFG) scale 为 3.5;蒸馏版 Klein 使用 16 个步骤,不使用 CFG。Table 1 shows pipeline latency at 2048×2048 on one NVIDIA B200 GPU using NVFP4, VANILLA attention, and torch.compile. Results are averaged over 10 requests after two warmup requests. Base uses 50 denoising steps with a classifier-free guidance (CFG) scale of 3.5; the distilled Klein uses 16 steps without CFG.
| Task | Model | Text + preparation | Denoising | Decode + postprocess | Total | Denoising share |
|---|---|---|---|---|---|---|
| T2I | Base | 47 ms | 14.24 s | 416 ms | 14.70 s | 96.8% |
| T2I | Distilled | 24 ms | 2.28 s | 410 ms | 2.71 s | 84.0% |
| I2I | Base | 468 ms | 39.40 s | 417 ms | 40.29 s | 97.8% |
| I2I | Distilled | 407 ms | 6.65 s | 418 ms | 7.48 s | 89.0% |
在 TensorRT LLM VisualGen 中优化 FLUX 3Optimizing FLUX 3 in TensorRT LLM VisualGen
TensorRT LLM VisualGen 通过优化的通用矩阵乘法(GEMM)内核和尾声融合、低精度与稀疏注意力、缓存以及多 GPU 执行,加速 NVFP4 DiT 推理。除非另有说明,以下基准测试均使用 torch.compile 和随机种子 0。Base 使用 50 个去噪步骤和 3.5 的 CFG scale;蒸馏版 Klein 使用 16 个去噪步骤。TensorRT LLM VisualGen accelerates NVFP4 DiT inference with optimized general matrix multiplication (GEMM) kernels and epilogue fusion, low-precision and sparse attention, caching, and multi-GPU execution. Unless otherwise noted, the following benchmarks use torch.compile and seed 0. Base uses 50 denoising steps and a CFG scale of 3.5; the distilled Klein uses 16 denoising steps.
NVFP4 推理NVFP4 inference
VisualGen 支持适用于 FLUX 3 Image Klein 和 FLUX 3 Image Klein Base 的 BFL 预量化 NVFP4 DiT 检查点。要使用这些检查点,请在 pipeline_config 中将 variant 设置为 nvfp4:VisualGen supports BFL’s prequantized NVFP4 DiT checkpoints for FLUX 3 Image Klein and FLUX 3 Image Klein Base. To use these checkpoints, set variant to nvfp4 in pipeline_config:
pipeline_config: variant: nvfp4
GEMM 内核优化GEMM kernel optimization
TensorRT LLM VisualGen 使用 FlashInfer 的 PrimsTS FP8/NVFP4 GEMM 内核,这些内核基于 CUTLASS 原语和任务调度构建,可加速量化推理。它们针对 FLUX 3 张量形状进行了调优,并默认启用;通过将优化后的 GEMM 与融合 epilogue 相结合,支持包括带 SwiGLU 的 GEMM,以及带查询/键归一化和旋转位置嵌入(RoPE)的查询、键和值(QKV)投影。详情请参阅 FlashInfer PR #5758。TensorRT LLM VisualGen uses FlashInfer’s PrimsTS FP8/NVFP4 GEMM kernels, built with CUTLASS Primitives and Task Scheduling to accelerate quantized inference. Tuned for FLUX 3 tensor shapes and enabled by default, they combine optimized GEMMs with fused epilogues, including GEMM with SwiGLU and query, key, and value (QKV) projections with query/key normalization and rotary position embeddings (RoPE). See FlashInfer PR #5758 for more details.
在发布时,固定的 NVFP4 PrimsTS 内核在 B200 上的 T2I 生成速度比经过自动调优的 TensorRT LLM NVFP4 GEMM 实现快 1.02–1.10 倍。At launch, the pinned NVFP4 PrimsTS kernels delivered 1.02–1.10x faster T2I generation on B200 than the autotuned TensorRT LLM NVFP4 GEMM implementation.
量化与稀疏注意力Quantized and sparse attention
在高分辨率图像生成中,注意力是一个主要瓶颈。在 B200 上报告的 2048×2048 NVFP4 T2I 测试中,无论是 Base 还是蒸馏版 Klein,原生 BF16 注意力都约占 DiT GPU 内核耗时的 60%。VisualGen 支持两种降低这项开销的方法:使用 cuDNN FP8 或 VC-Attention FP8 的量化注意力,以及使用 Sol-Attention 的稀疏注意力。VC-Attention FP8 是基于 VC-Attention 论文中所述方法的 TensorRT LLM 变体。Attention is a major bottleneck in high-resolution image generation. In the reported 2048×2048 NVFP4 T2I tests on B200, VANILLA BF16 attention accounted for approximately 60% of DiT GPU kernel time for both Base and distilled Klein. VisualGen supports two approaches to reducing this cost: quantized attention with cuDNN FP8 or VC-Attention FP8, and sparse attention with Sol-Attention. VC-Attention FP8 is a TensorRT LLM variant of the method described in the VC-Attention paper.
学习感知图像块相似度(LPIPS)用于衡量生成图像之间的感知差异。在以下对比中,分数越低,表示与相应的 VANILLA 基线输出越相似。Learned Perceptual Image Patch Similarity (LPIPS) measures perceptual differences between generated images. In the following comparisons, lower scores indicate greater similarity to the corresponding VANILLA baseline output.
与 VANILLA BF16 注意力相比,cuDNN FP8 的生成速度在 Base 上提升了 1.08 倍,在 16 步蒸馏版 Klein 上提升了 1.09 倍。对应的 LPIPS 分数分别为 0.072 和 0.161。VC-Attention FP8 的生成速度在 Base 上提升了 1.18 倍,在蒸馏版 Klein 上提升了 1.16 倍,对应的 LPIPS 分数分别为 0.086 和 0.151。Compared with VANILLA BF16 attention, cuDNN FP8 delivered generation speedups of 1.08x for Base and 1.09x for the 16-step distilled Klein. The corresponding LPIPS scores were 0.072 and 0.161. VC-Attention FP8 delivered speedups of 1.18x for Base and 1.16x for distilled Klein, with corresponding LPIPS scores of 0.086 and 0.151.
图 4 比较了启用 CUDA Graphs 时 FLUX 3 Image Klein Base NVFP4 在 2048×2048 分辨率下的输出。Figure 4 compares FLUX 3 Image Klein Base NVFP4 outputs at 2048×2048 with CUDA Graphs enabled.
这些测量结果表明,在 B200 上进行 2048×2048 量化注意力时,建议使用 VC-Attention FP8。测试配置使用 ρ=0.005 的修复预算,选取量化误差最高的一部分 token 进行残差校正。要使用量化注意力,请将 attention_config 替换为以下配置之一:These measurements suggest using VC-Attention FP8 for quantized attention at 2048×2048 on B200. The tested configuration uses a repair budget of ρ=0.005 to select the fraction of tokens with the highest quantization error for residual correction. To use quantized attention, replace attention_config with one of the following configurations:
对于 cuDNN FP8:For cuDNN FP8:
attention_config:
backend: CUDNN
quant_attention_config:
qk_dtype: fp8
v_dtype: fp8
对于 VC-Attention FP8:For VC-Attention FP8:
attention_config:
backend: TRTLLM
quant_attention_config:
qk_dtype: fp8
v_dtype: fp8
vc_attention: true
vc_repair_budget: 0.005
图 4. BF16、cuDNN FP8 和 VC-Attention FP8 示例输出Figure 4. BF16, cuDNN FP8, and VC-Attention FP8 example outputs
Sol-Attention 通过由 τ 控制的稀疏性降低 DiT 的计算量。报告中的测试使用 TensorRT LLM 中基于 PrimsTS 的通用块稀疏注意力内核。按如下方式配置 Sol-Attention:Sol-Attention reduces DiT computation with sparsity controlled by τ. The reported tests use the PrimsTS-based general block-sparse attention kernel in TensorRT LLM. Configure Sol-Attention as follows:
attention_config:
backend: TRTLLM
sparse_attention_config:
algorithm: sol_attn
tau: 1.0
disabled_until_timestep: 0.96
在 B200 上以 2048×2048 分辨率运行 NVFP4 FLUX 3 Image Klein Base 时,τ=1.0 的 Sol-Attention 相较于采用 16 个初始稠密步骤的已编译稠密 VANILLA,在去噪和自动编码器解码方面快 1.24 倍;若采用 12 个初始稠密步骤,则快 1.30 倍。For NVFP4 FLUX 3 Image Klein Base at 2048×2048 on B200, Sol-Attention with τ=1.0 delivered 1.24x faster denoising plus autoencoder decoding than compiled dense VANILLA with 16 initial dense steps, or 1.30x with 12.
这些测量结果表明,若优先考虑保真度,可采用 16 个初始密集步骤;若优先考虑速度,则可采用 12 个。较高的 τ 会降低输出质量,却几乎不能进一步提升速度。These measurements suggest using 16 initial dense steps to prioritize fidelity or 12 to prioritize speed. Higher τ reduced output quality with little additional speedup.
图 5 对比了在启用 CUDA Graphs 且 τ=1.0 时,NVFP4 FLUX 3 Image Klein Base 在 2048×2048 分辨率下的输出。Figure 5 compares NVFP4 FLUX 3 Image Klein Base outputs at 2048×2048 with CUDA Graphs enabled and τ=1.0.
图 5. 使用 16 或 12 个初始稠密步骤时,稠密注意力和 Sol-Attention 的输出Figure 5. Dense attention and Sol-Attention outputs with 16 or 12 initial dense steps
步骤缓存Step caching
Cache-DiT 通过在去噪步骤之间复用 Transformer 中间输出,降低 FLUX 3 Image Klein 的推理延迟,且无需重新训练或修改模型权重。残差差异阈值用于控制何时复用缓存输出,因此你可以调整推理速度与输出质量之间的平衡。使用所需参数配置 Cache-DiT:Cache-DiT reduces FLUX 3 Image Klein inference latency by reusing intermediate transformer outputs across denoising steps without retraining or modifying model weights. A residual-difference threshold controls when cached outputs are reused, so you can adjust the balance between inference speed and output quality. Configure Cache-DiT with your desired parameters:
cache_config: cache_backend: cache_dit Fn_compute_blocks: 1 Bn_compute_blocks: 0 max_warmup_steps: 4 max_continuous_cached_steps: 3 residual_diff_threshold: 0.24
在 0.24 的阈值下,Cache-DiT 在测试的两种分辨率下,几乎将 NVFP4 Base T2I 的延迟减半;蒸馏版 Klein 的提升则较小。在 1024×1024 分辨率下,Base 和蒸馏版 Klein 的加速比分别为 1.97x 和 1.26x;相对于各自未使用缓存的输出,对应的 LPIPS 分数分别为 0.038 和 0.036。在 2048×2048 分辨率下,Base 和蒸馏版 Klein 的加速比分别为 1.94x 和 1.20x(图 6)。At a threshold of 0.24, Cache-DiT nearly halved NVFP4 Base T2I latency at both tested resolutions, with smaller gains for the distilled Klein. At 1024×1024, speedups were 1.97x for Base and 1.26x for distilled Klein, with corresponding LPIPS scores of 0.038 and 0.036 against their respective uncached outputs. At 2048×2048, speedups were 1.94x for Base and 1.20x for distilled Klein (Figure 6).
多 GPU 推理Multi-GPU inference
TensorRT LLM 中的 VisualGen 运行时通过无分类器引导(CFG)和 Ulysses 序列并行扩展 FLUX 3 Image Klein。Base 将正向/负向分支分配到不同的 GPU 组,并使用 Ulysses 对每个分支的序列进行分区;蒸馏版不使用 CFG,仅使用 Ulysses。The VisualGen runtime in TensorRT LLM scales FLUX 3 Image Klein using classifier-free guidance (CFG) and Ulysses sequence parallelism. Base assigns positive/negative branches to separate GPU groups and partitions each branch’s sequence with Ulysses; distilled runs without CFG and uses Ulysses alone.
VisualGen 负责序列拆分、分布式注意力和输出汇总。对于八卡 NVFP4 Base 推理,请使用以下配置:VisualGen handles sequence splitting, distributed attention, and output gathering. For eight-GPU NVFP4 Base inference, use the following configuration:
parallel_config:
cfg_size: 2
ulysses_size: 4
对于蒸馏模型,将 cfg_size 设置为 1,ulysses_size 设置为 8,并使用 flux-3-image-klein 检查点。For the distilled model, set cfg_size: 1, ulysses_size: 8, and use the flux-3-image-klein checkpoint.
在 2048×2048 分辨率下,将 NVIDIA DGX B200 上的 GPU 数量从 1 块扩展到 8 块后,Base 的 T2I 中位延迟从 14.90 秒降至 3.00 秒,蒸馏版则从 2.73 秒降至 1.08 秒。去噪速度提升更显著:Base 为 6.01 倍,蒸馏版 Klein 为 4.44 倍;但 8-GPU 蒸馏版生成时间中仍有约一半花在去噪之外,从而限制了端到端性能提升。At 2048×2048, scaling from one to eight GPUs on an NVIDIA DGX B200 reduced median T2I latency from 14.90 to 3.00 seconds for Base and 2.73 to 1.08 seconds for distilled. Denoising speedups were larger, at 6.01x for Base and 4.44x for distilled Klein, but roughly half of eight-GPU distilled generation time remained outside denoising, limiting end-to-end gains.
在较小的分辨率下,随着每块 GPU 承担的工作量减少,CUDA Graphs 可通过降低内核启动开销来补充并行化。在另一项采用 VANILLA attention 的 1024×1024 NVFP4 实验中,使用八块 B200 GPU,将 CUDA Graphs 纳入编译后,Base 延迟从 1.823 秒降至 0.809 秒(提升 2.25 倍;CFG 跨两个 GPU 组,每组四块 GPU 使用 Ulysses),蒸馏延迟则从 0.791 秒降至 0.366 秒(提升 2.16 倍;Ulysses 跨八块 GPU)。使用以下配置启用 CUDA Graphs:At smaller resolutions, CUDA Graphs complement parallelism by reducing kernel-launch overhead as work per GPU shrinks. In a separate 1024×1024 NVFP4 experiment on eight B200s with VANILLA attention, adding CUDA Graphs to compilation reduced Base latency from 1.823 to 0.809 seconds (2.25x, CFG across two GPU groups with Ulysses across four GPUs per group) and distilled latency from 0.791 to 0.366 seconds (2.16x, Ulysses across eight GPUs). Enable CUDA Graphs with the following configuration:
cuda_graph_config:
enable: true
FLUX 3 端到端性能FLUX 3 end-to-end performance
在 2048×2048 分辨率下,图 8 对比了 TensorRT LLM VisualGen 各阶段优化后端到端生成速度相较于未编译 BF16 VANILLA 推理的提升。At 2048×2048, Figure 8 compares end-to-end generation speedups across successive TensorRT LLM VisualGen optimization stages against uncompiled BF16 VANILLA inference.
在单块 B200 上,编译优化、搭配 PrimsTS GEMM 的 NVFP4、Sol-Attention 和 Cache-DiT,使 T2I 生成速度提升了 3.46 倍,延迟从 22.36 秒降至 6.46 秒;I2I 速度提升了 4.45 倍,延迟从 54.64 秒降至 12.28 秒。16 步蒸馏版 Klein 的端到端速度最高提升了 2.34 倍。On a single B200, compilation, NVFP4 with PrimsTS GEMMs, Sol-Attention, and Cache-DiT delivered a 3.46x generation speedup for T2I, reducing latency from 22.36 to 6.46 seconds, and a 4.45x speedup for I2I, reducing latency from 54.64 to 12.28 seconds. The 16-step distilled Klein achieved up to 2.34x end-to-end speedups.
在单块 NVIDIA RTX PRO 6000 Blackwell Server Edition GPU 上,编译、采用融合 CuTe DSL 内核的 NVFP4 以及 Cache-DiT 带来了 1.73–3.25 倍的生成速度提升,并将性能收益扩展到了两个平台。图 9 和图 10 对比了不同优化阶段的示例输出。On a single NVIDIA RTX PRO 6000 Blackwell Server Edition GPU, compilation, NVFP4 with fused CuTe DSL kernels, and Cache-DiT delivered 1.73-3.25x generation speedups, extending the gains across both platforms. Figures 9 and 10 compare example outputs across optimization stages.
图 9 和图 10 中的示例是在单个 B200 GPU 上生成的。The examples in Figures 9 and 10 were generated on a single B200 GPU.
编辑提示要求让独木舟呈现出饱经风霜的外观,同时保留其形状、位置和周围场景。The editing prompt requests a weathered appearance for the canoe while preserving its shape, position, and surrounding scene.
要将这些优化应用于你自己的工作负载,请按照以下各节中的运行时设置、推理命令和 YAML 配置进行操作,以进行离线生成或在线服务。To apply these optimizations to your own workloads, follow the runtime setup, inference commands, and YAML configurations in the following sections for offline generation or online serving.
先决条件Prerequisites
使用以下命令启动预构建的 TensorRT LLM 容器。要从 feat/flux3-image-klein 分支构建该容器,请参阅 FLUX 3 部署指南。Start the prebuilt TensorRT LLM container with the following command. To build it from the feat/flux3-image-klein branch, follow the FLUX 3 deployment guide.
docker run --rm -it --ipc=host --gpus all -p 8000:8000 \
--name tensorrt_llm \
-e TLLM_FMHA_LIBS=+prims_ts \
-e HF_TOKEN=<your_hf_token> \
-v ~/.cache:/root/.cache:rw \
nvcr.io/nvidia/tensorrt-llm/release:feat-flux3-image-klein-1.4.0a0.dev202610280436
TLLM_FMHA_LIBS=+prims_ts 可启用 VC-Attention 所需的 PrimsTS 注意力内核。本文中的优化和结果聚焦于 NVIDIA Blackwell 数据中心 GPU 和 NVIDIA RTX PRO 6000 Blackwell Server Edition GPU。TLLM_FMHA_LIBS=+prims_ts enables the PrimsTS attention kernels VC-Attention requires. The optimizations and results in this post focus on NVIDIA Blackwell data center GPUs and NVIDIA RTX PRO 6000 Blackwell Server Edition GPUs.
开始使用 TensorRT LLM VisualGenGet started with TensorRT LLM VisualGen
运行时环境准备就绪后,你可以离线运行 FLUX 3,也可以通过图像生成端点提供服务。使用以下章节中的示例 YAML 配置,应用此前报告的结果所采用的优化设置。With the runtime environment ready, you can run FLUX 3 offline or serve it through an image generation endpoint. Use the sample YAML configurations in the following sections to apply the optimization settings used for the results reported earlier.
这些示例不会扩写提示词。如果使用 BFL 的提示词上采样器,请在推理前单独运行。报告的延迟不包括提示词上采样所需的时间。These examples do not expand prompts. If you use BFL’s prompt upsampler, run it separately before inference. Reported latencies exclude prompt upsampling.
离线推理Offline inference
在容器内运行 FLUX 3 示例:Inside the container, run the FLUX 3 example:
cd /app/tensorrt_llm/examples/visual_gen python models/flux3.py \ --model black-forest-labs/flux-3-image-klein-base \ --visual_gen_args configs/flux3-images-1gpu.yaml \ --prompt "A small robot tending a rooftop garden at sunrise" \ --height 2048 --width 2048 --seed 0 \ --output_path flux3.png
对于 Blackwell 上的 NVFP4,添加 --variant nvfp4;对于逐行 FP8,使用 --variant fp8r;对于 BF16,则省略 --variant。蒸馏模型请选择 black-forest-labs/flux-3-image-klein。要进行编辑,请添加编辑提示词和 --image /path/to/reference.png;有多个参考图时,重复使用 --image。For NVFP4 on Blackwell, add --variant nvfp4; use --variant fp8r for row-wise FP8 or omit --variant for BF16. Select black-forest-labs/flux-3-image-klein for the distilled model. For editing, add an editing prompt and --image /path/to/reference.png; repeat --image for multiple references.
在线服务端点Online serving endpoint
在同一个容器目录中,启动兼容 OpenAI 的 /v1/images/generations 端点:From the same container directory, launch the OpenAI-compatible /v1/images/generations endpoint:
cd /app/tensorrt_llm/examples/visual_gen trtllm-serve black-forest-labs/flux-3-image-klein-base \ --host 0.0.0.0 --port 8000 \ --visual_gen_args configs/flux3-images-1gpu.yaml
服务器准备就绪后,打开第二个终端,进入正在运行的容器,并运行客户端:Once the server is ready, open a second terminal, enter the running container, and run the client:
docker exec -it tensorrt_llm bash python examples/visual_gen/serve/sync_image_gen.py \ --base-url http://localhost:8000/v1 \ --model black-forest-labs/flux-3-image-klein-base \ --size 2048x2048 \ --prompt "A small robot tending a rooftop garden at sunrise" \ --output flux3-served.png
对于 NVFP4 服务,在启动服务器前,将 pipeline_config: {variant: nvfp4} 添加到 YAML 中;FP8 则使用 fp8r。For NVFP4 serving, add pipeline_config: {variant: nvfp4} to the YAML before starting the server; use fp8r for FP8.
经过优化的单 GPU 部署Optimized single-GPU deployment
对于在一张 B200 上进行 2048×2048 推理,请将以下内容保存为 configs/flux3-nvfp4-vc-fp8-1gpu.yaml,该配置结合了 NVFP4 权重、torch.compile 和 VC-Attention FP8。For 2048×2048 inference on one B200, save the following as configs/flux3-nvfp4-vc-fp8-1gpu.yaml, which combines NVFP4 weights, torch.compile, and VC-Attention FP8.
pipeline_config:
variant: nvfp4
torch_compile_config:
enable: true
compilation_config:
resolutions: [[2048, 2048]]
num_frames: [1]
modes: [t2i, i2i]
attention_config:
backend: TRTLLM
quant_attention_config:
qk_dtype: fp8
v_dtype: fp8
vc_attention: true
vc_repair_budget: 0.005
对于 Sol-Attention 和 Cache-DiT,将此文件复制到 configs/flux3-nvfp4-sol-cache-1gpu.yaml,替换 attention_config,并添加 cache_config:For Sol-Attention and Cache-DiT, copy this file to configs/flux3-nvfp4-sol-cache-1gpu.yaml, replace attention_config, and add cache_config:
pipeline_config:
variant: nvfp4
torch_compile_config:
enable: true
compilation_config:
resolutions: [[2048, 2048]]
num_frames: [1]
modes: [t2i, i2i]
attention_config:
backend: TRTLLM
sparse_attention_config:
algorithm: sol_attn
tau: 1.0
disabled_until_timestep: 0.96
cache_config:
cache_backend: cache_dit
Fn_compute_blocks: 1
Bn_compute_blocks: 0
max_warmup_steps: 4
max_continuous_cached_steps: 3
residual_diff_threshold: 0.24
时间步截止阈值 0.96 会让 50 步的 FLUX 3 Image Klein Base 在前 12 个去噪步骤中保持密集计算,并让 16 步蒸馏版 Klein 在前四个步骤中保持密集计算,以上均使用打包的 α=6.93 调度。对于上文建议的 16 个密集步骤设置,请设为 disabled_until_timestep: 0.94。该截止阈值指定的是噪声水平,而非步数。移除 cache_config 即可在不使用 Cache-DiT 的情况下使用 Sol-Attention。The timestep cutoff of 0.96 keeps the first 12 denoising steps dense for 50-step FLUX 3 Image Klein Base and the first four steps dense for 16-step distilled Klein under the packaged α=6.93 schedule. For the 16-dense-step setting recommended above, set disabled_until_timestep: 0.94. The cutoff specifies a noise level rather than a number of steps. Remove cache_config to use Sol-Attention without Cache-DiT.
在离线推理或服务命令中,将 --visual_gen_args 设置为所选 YAML 文件的路径。将图像尺寸设为 2048×2048,以匹配性能示例。Set --visual_gen_args in the offline inference or serving commands to the path of your selected YAML file. Set the image size to 2048×2048 to match the performance examples.
结论与后续步骤Conclusion and next steps
TensorRT LLM VisualGen 支持 FLUX 3 Image Klein 推理,并提供可配置的性能优化。NVFP4、融合内核、编译、多 GPU 执行和 CUDA Graphs 可加速执行;量化注意力、稀疏注意力和缓存则可进一步降低延迟,但会对输出质量产生可测量的影响。与未编译的 BF16 VANILLA 推理相比,这些优化组合可在 NVIDIA DGX B200 上使用单块 GPU 实现最高 3.46 倍的 T2I 加速和 4.45 倍的 I2I 加速。TensorRT LLM VisualGen supports FLUX 3 Image Klein inference with configurable performance optimizations. NVFP4, fused kernels, compilation, multi-GPU execution, and CUDA Graphs accelerate execution; quantized and sparse attention and caching provide further latency reductions with measurable effects on output quality. Together, they deliver up to 3.46x faster T2I and 4.45x faster I2I generation using a single GPU on an NVIDIA DGX B200, compared with uncompiled BF16 VANILLA inference.
开始使用:Get started:
- 运行端到端 FLUX 3 示例Run the end-to-end FLUX 3 example
- 探索 TensorRT LLM VisualGen 运行时和优化工具集Explore the TensorRT LLM VisualGen runtime and optimization utilities
- 在 TensorRT LLM 的 GitHub 上提交问题或分享结果File issues or share results on the TensorRT LLM GitHub
致谢Acknowledgements
尼基塔·科罗博夫、哈里森·张、帕梅拉·彭、张康焕、何宇航和夏洛特·马洛Nikita Korobov, Harrison Zhang, Pamela Peng, Kanghwan Jang, Yuhang He, and Charlotte Mallo



























