|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +"""Run full Flux2 text-to-image generation through FastVideo. |
| 3 | +
|
| 4 | +User story: |
| 5 | + "I have a local or HF Diffusers-format full Flux2 checkpoint and want a |
| 6 | + minimal text-to-image generation command that uses embedded guidance." |
| 7 | +""" |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from fastvideo import VideoGenerator |
| 13 | +from fastvideo.api import ( |
| 14 | + ComponentConfig, |
| 15 | + EngineConfig, |
| 16 | + GenerationRequest, |
| 17 | + GeneratorConfig, |
| 18 | + OffloadConfig, |
| 19 | + OutputConfig, |
| 20 | + ParallelismConfig, |
| 21 | + PipelineSelection, |
| 22 | + SamplingConfig, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def parse_args() -> argparse.Namespace: |
| 27 | + parser = argparse.ArgumentParser(description="Run full Flux2 text-to-image generation.") |
| 28 | + parser.add_argument( |
| 29 | + "--model-path", |
| 30 | + default="black-forest-labs/FLUX.2-dev", |
| 31 | + help="HF id or local diffusers-format full Flux2 weights directory.", |
| 32 | + ) |
| 33 | + parser.add_argument( |
| 34 | + "--output", |
| 35 | + default="outputs/flux2/flux2.png", |
| 36 | + help="Output PNG path.", |
| 37 | + ) |
| 38 | + parser.add_argument( |
| 39 | + "--prompt", |
| 40 | + default="a photo of a banana on a wooden table, studio lighting", |
| 41 | + help="Text prompt.", |
| 42 | + ) |
| 43 | + parser.add_argument("--height", type=int, default=1024) |
| 44 | + parser.add_argument("--width", type=int, default=1024) |
| 45 | + parser.add_argument("--steps", type=int, default=50) |
| 46 | + parser.add_argument("--guidance-scale", type=float, default=4.0) |
| 47 | + parser.add_argument("--max-sequence-length", type=int, default=None) |
| 48 | + parser.add_argument("--seed", type=int, default=0) |
| 49 | + parser.add_argument("--num-gpus", type=int, default=1) |
| 50 | + parser.add_argument("--tp-size", type=int, default=None) |
| 51 | + parser.add_argument("--sp-size", type=int, default=None) |
| 52 | + parser.add_argument( |
| 53 | + "--backend", |
| 54 | + default=None, |
| 55 | + help="Set FASTVIDEO_ATTENTION_BACKEND, for example TORCH_SDPA.", |
| 56 | + ) |
| 57 | + return parser.parse_args() |
| 58 | + |
| 59 | + |
| 60 | +def main() -> None: |
| 61 | + args = parse_args() |
| 62 | + if args.backend: |
| 63 | + os.environ["FASTVIDEO_ATTENTION_BACKEND"] = args.backend |
| 64 | + |
| 65 | + output = Path(args.output) |
| 66 | + output.parent.mkdir(parents=True, exist_ok=True) |
| 67 | + tp_size = args.tp_size if args.tp_size is not None else ( |
| 68 | + args.num_gpus if args.num_gpus > 1 else 1 |
| 69 | + ) |
| 70 | + sp_size = args.sp_size if args.sp_size is not None else ( |
| 71 | + 1 if args.num_gpus > 1 else args.num_gpus |
| 72 | + ) |
| 73 | + |
| 74 | + generator_config = GeneratorConfig( |
| 75 | + model_path=args.model_path, |
| 76 | + engine=EngineConfig( |
| 77 | + num_gpus=args.num_gpus, |
| 78 | + parallelism=ParallelismConfig(tp_size=tp_size, sp_size=sp_size), |
| 79 | + use_fsdp_inference=False, |
| 80 | + offload=OffloadConfig( |
| 81 | + dit=False, |
| 82 | + vae=True, |
| 83 | + text_encoder=True, |
| 84 | + pin_cpu_memory=False, |
| 85 | + ), |
| 86 | + ), |
| 87 | + pipeline=PipelineSelection( |
| 88 | + workload_type="t2i", |
| 89 | + components=ComponentConfig(override_pipeline_cls_name="Flux2Pipeline"), |
| 90 | + ), |
| 91 | + ) |
| 92 | + |
| 93 | + generator = VideoGenerator.from_config(generator_config) |
| 94 | + try: |
| 95 | + sampling = SamplingConfig( |
| 96 | + height=args.height, |
| 97 | + width=args.width, |
| 98 | + num_frames=1, |
| 99 | + fps=1, |
| 100 | + num_inference_steps=args.steps, |
| 101 | + guidance_scale=args.guidance_scale, |
| 102 | + seed=args.seed, |
| 103 | + ) |
| 104 | + extensions = {} |
| 105 | + if args.max_sequence_length is not None: |
| 106 | + extensions["max_sequence_length"] = args.max_sequence_length |
| 107 | + |
| 108 | + request = GenerationRequest( |
| 109 | + prompt=args.prompt, |
| 110 | + sampling=sampling, |
| 111 | + output=OutputConfig( |
| 112 | + output_path=str(output), |
| 113 | + save_video=True, |
| 114 | + return_frames=False, |
| 115 | + ), |
| 116 | + extensions=extensions, |
| 117 | + ) |
| 118 | + generator.generate(request) |
| 119 | + finally: |
| 120 | + generator.shutdown() |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments