Skip to content

Commit 41d7b7f

Browse files
committed
[None][feat] add FLUX visual generation examples
Signed-off-by: Kanghwan Jang <861393+karljang@users.noreply.github.com>
1 parent e47f26e commit 41d7b7f

5 files changed

Lines changed: 240 additions & 0 deletions

File tree

examples/visual_gen/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ for feature details.
1919
python quickstart_example.py
2020
python models/wan_t2v.py
2121
python models/wan_i2v.py
22+
python models/flux1.py
23+
python models/flux2.py
2224

2325
# With engine config (quant, parallelism, etc.)
2426
python models/wan_t2v.py --visual_gen_args configs/wan2.2-t2v-fp4-1gpu.yaml
2527
python models/wan_i2v.py --visual_gen_args configs/wan2.2-i2v-fp4-1gpu.yaml --image /path/to/image.png
28+
python models/flux1.py --visual_gen_args configs/flux1-dev-fp4-1gpu.yaml
29+
python models/flux2.py --visual_gen_args configs/flux2-dev-fp4-1gpu.yaml
2630
```
2731

2832
Install deps from the repo root: `pip install -r requirements-dev.txt`.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# 1-GPU FLUX.1-dev with NVFP4 dynamic quantization.
17+
# Shared by offline examples (--visual_gen_args) and trtllm-serve.
18+
quant_config:
19+
quant_algo: NVFP4
20+
dynamic: true
21+
attention_config:
22+
backend: VANILLA
23+
parallel_config:
24+
cfg_size: 1
25+
ulysses_size: 1
26+
cuda_graph_config:
27+
enable: false
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# 1-GPU FLUX.2-dev with NVFP4 dynamic quantization.
17+
# Shared by offline examples (--visual_gen_args) and trtllm-serve.
18+
quant_config:
19+
quant_algo: NVFP4
20+
dynamic: true
21+
attention_config:
22+
backend: VANILLA
23+
parallel_config:
24+
cfg_size: 1
25+
ulysses_size: 1
26+
cuda_graph_config:
27+
enable: false
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python3
2+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
"""FLUX.1 text-to-image generation.
17+
18+
Usage:
19+
python flux1.py
20+
python flux1.py --visual_gen_args ../configs/flux1-dev-fp4-1gpu.yaml
21+
"""
22+
23+
import argparse
24+
from pathlib import Path
25+
26+
from tensorrt_llm import VisualGen, VisualGenArgs
27+
28+
29+
def _output_paths(output_path: str, num_images: int) -> str | list[str]:
30+
if num_images == 1:
31+
return output_path
32+
33+
path = Path(output_path)
34+
return [str(path.with_name(f"{path.stem}_{idx + 1}{path.suffix}")) for idx in range(num_images)]
35+
36+
37+
def main():
38+
parser = argparse.ArgumentParser(description="FLUX.1 Text-to-Image example")
39+
parser.add_argument(
40+
"--model",
41+
type=str,
42+
default="black-forest-labs/FLUX.1-dev",
43+
help="Model path or HuggingFace Hub ID",
44+
)
45+
parser.add_argument(
46+
"--visual_gen_args",
47+
"--extra_visual_gen_options",
48+
dest="visual_gen_args",
49+
type=str,
50+
default=None,
51+
help="Path to YAML config (same as trtllm-serve --visual_gen_args)",
52+
)
53+
parser.add_argument(
54+
"--prompt",
55+
type=str,
56+
default="A cat sitting on a windowsill, cinematic lighting, highly detailed",
57+
help="Text prompt for image generation",
58+
)
59+
parser.add_argument(
60+
"--num_images_per_prompt",
61+
type=int,
62+
default=1,
63+
help="Number of images to generate for the prompt",
64+
)
65+
parser.add_argument(
66+
"--output_path",
67+
type=str,
68+
default="flux1_output.png",
69+
help="Path to save the output image. For multiple images, an index is appended.",
70+
)
71+
args = parser.parse_args()
72+
if args.num_images_per_prompt < 1:
73+
raise ValueError("--num_images_per_prompt must be >= 1")
74+
75+
# Engine config from shared YAML (optional); model-specific defaults apply otherwise.
76+
extra_args = VisualGenArgs.from_yaml(args.visual_gen_args) if args.visual_gen_args else None
77+
visual_gen = VisualGen(model=args.model, args=extra_args)
78+
79+
# --- Model-specific: T2I request construction ---
80+
# Start from per-model defaults (resolution, steps, guidance, seed, etc.) and set image count.
81+
params = visual_gen.default_params
82+
params.num_images_per_prompt = args.num_images_per_prompt
83+
84+
output = visual_gen.generate(inputs=args.prompt, params=params)
85+
86+
saved = output.save(_output_paths(args.output_path, args.num_images_per_prompt))
87+
print(f"Saved: {saved}")
88+
89+
90+
if __name__ == "__main__":
91+
main()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python3
2+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
"""FLUX.2 text-to-image generation.
17+
18+
Usage:
19+
python flux2.py
20+
python flux2.py --visual_gen_args ../configs/flux2-dev-fp4-1gpu.yaml
21+
"""
22+
23+
import argparse
24+
from pathlib import Path
25+
26+
from tensorrt_llm import VisualGen, VisualGenArgs
27+
28+
29+
def _output_paths(output_path: str, num_images: int) -> str | list[str]:
30+
if num_images == 1:
31+
return output_path
32+
33+
path = Path(output_path)
34+
return [str(path.with_name(f"{path.stem}_{idx + 1}{path.suffix}")) for idx in range(num_images)]
35+
36+
37+
def main():
38+
parser = argparse.ArgumentParser(description="FLUX.2 Text-to-Image example")
39+
parser.add_argument(
40+
"--model",
41+
type=str,
42+
default="black-forest-labs/FLUX.2-dev",
43+
help="Model path or HuggingFace Hub ID",
44+
)
45+
parser.add_argument(
46+
"--visual_gen_args",
47+
"--extra_visual_gen_options",
48+
dest="visual_gen_args",
49+
type=str,
50+
default=None,
51+
help="Path to YAML config (same as trtllm-serve --visual_gen_args)",
52+
)
53+
parser.add_argument(
54+
"--prompt",
55+
type=str,
56+
default="A cat sitting on a windowsill, cinematic lighting, highly detailed",
57+
help="Text prompt for image generation",
58+
)
59+
parser.add_argument(
60+
"--num_images_per_prompt",
61+
type=int,
62+
default=1,
63+
help="Number of images to generate for the prompt",
64+
)
65+
parser.add_argument(
66+
"--output_path",
67+
type=str,
68+
default="flux2_output.png",
69+
help="Path to save the output image. For multiple images, an index is appended.",
70+
)
71+
args = parser.parse_args()
72+
if args.num_images_per_prompt < 1:
73+
raise ValueError("--num_images_per_prompt must be >= 1")
74+
75+
# Engine config from shared YAML (optional); model-specific defaults apply otherwise.
76+
extra_args = VisualGenArgs.from_yaml(args.visual_gen_args) if args.visual_gen_args else None
77+
visual_gen = VisualGen(model=args.model, args=extra_args)
78+
79+
# --- Model-specific: T2I request construction ---
80+
# Start from per-model defaults (resolution, steps, guidance, seed, etc.) and set image count.
81+
params = visual_gen.default_params
82+
params.num_images_per_prompt = args.num_images_per_prompt
83+
84+
output = visual_gen.generate(inputs=args.prompt, params=params)
85+
86+
saved = output.save(_output_paths(args.output_path, args.num_images_per_prompt))
87+
print(f"Saved: {saved}")
88+
89+
90+
if __name__ == "__main__":
91+
main()

0 commit comments

Comments
 (0)