feat: support pipefusion for flux2 - #1268
Conversation
new file: lightx2v/common/distributed/__init__.py new file: lightx2v/common/distributed/pipeline_comm.py new file: lightx2v/common/distributed/pipeline_state.py modified: lightx2v/models/networks/base_model.py new file: lightx2v/models/networks/flux2/infer/pipefusion/__init__.py new file: lightx2v/models/networks/flux2/infer/pipefusion/pipeline_driver.py new file: lightx2v/models/networks/flux2/infer/pipefusion/transformer_infer.py modified: lightx2v/models/networks/flux2/infer/pre_infer.py modified: lightx2v/models/networks/flux2/infer/transformer_infer.py modified: lightx2v/models/networks/flux2/model.py modified: lightx2v/models/networks/flux2/weights/transformer_weights.py modified: lightx2v/models/runners/flux2/flux2_runner.py modified: lightx2v/models/schedulers/flux2/scheduler.py modified: lightx2v/pipeline.py modified: lightx2v/utils/set_config.py
There was a problem hiding this comment.
Code Review
This pull request introduces PipeFusion-based pipeline parallelism for Flux2, adding a pipeline driver, stale-KV caching transformer inference, and distributed communication helpers to orchestrate sync and async denoising loops. Key feedback from the review highlights critical data corruption risks due to globally shared pre-allocated K/V buffers across transformer blocks, which should be stored in block-specific dictionaries instead. Additionally, the reviewer suggested performance and code quality improvements, including increasing the pending isend queue limit to prevent CPU blocking, avoiding redundant memory allocations and CPU-GPU synchronization bottlenecks, and utilizing the newly added step_post_patch scheduler method for better encapsulation.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
…fer.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…fer.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…r.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…fer.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…r.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…r.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
modified: lightx2v/models/runners/flux2/flux2_runner.py
lightx2v/models/networks/flux2/weights/transformer_weights.py
modified: lightx2v/models/networks/flux2/weights/transformer_weights.py
lightx2v/models/networks/flux2/infer/transformer_infer.py
modified: lightx2v/common/distributed/pipeline_state.py modified: lightx2v/common/ops/attn/flash_attn.py modified: lightx2v/models/networks/flux2/infer/pipefusion/transformer_infer.py modified: lightx2v/models/networks/flux2/infer/pre_infer.py modified: lightx2v/models/networks/flux2/model.py modified: lightx2v/models/networks/flux2/weights/transformer_weights.py modified: lightx2v/models/runners/flux2/flux2_runner.py
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
…void OOM modified: lightx2v/models/runners/flux2/flux2_runner.py
modified: lightx2v/utils/set_config.py
Support PipeFusion parallel inference for Flux2
Summary
Adds pipeline parallelism (PipeFusion) support for the Flux2 model in LightX2V, enabling multi-GPU inference with per-patch stale-KV caching across pipeline stages.
Key Components
New modules
lightx2v/common/distributed/— pipeline parallel infrastructure:pipeline_comm.py(PipelineComm): P2P communication manager between adjacent stages. Wrapsdist.isend/dist.irecvwith pre-allocated recv buffers, a FIFO task queue, and asyncrecv_next()prefetch.pipeline_state.py(PipelineRuntimeState): patch metadata management and stage-identification helpers (is_pipeline_first_stage,get_pipeline_parallel_rank, etc.).lightx2v/models/networks/flux2/infer/pipefusion/— Flux2 PipeFusion driver:pipeline_driver.py(Flux2PipelineDriver): orchestrates the denoising loop across stages — sync pipeline for warmup, async pipeline for the main loop with per-patch compute/comm overlap.transformer_infer.py(Flux2PipeFusionTransformerInfer): subclassesFlux2TransformerInferto run only the current stage's block subset and apply stale-KV caching (image KV cached across patches, text KV stays fresh).Comparison
14.40s (rank7, incl. VAE)
11.62s (rank0, incl. VAE)
20.05s (rank7, slowest)
20.92s (rank0, slowest)
PP
{ "model_cls": "flux2_klein", "task": "t2i", "infer_steps": 20, "sample_guide_scale": 1.0, "vae_scale_factor": 16, "feature_caching": "None", "enable_cfg": false, "patch_size": 2, "tokenizer_max_length": 512, "rope_type": "flashinfer_rope", "attn_type": "flash_attn2", "parallel": { "cfg_p_size": 1, "pp_size": 8, "seq_p_size": 1, "num_pipeline_patch": 8, "pipeline_warmup_steps": 2 } }torchrun --nproc_per_node=8 -m lightx2v.infer \ --model_cls flux2_klein \ --task t2i \ --target_shape 2048 2048 \ --model_path $model_path \ --prompt "A cat running in a garden" \ --save_result_path "save_results/flux2_klein_4b_pp8.png" \ --config_json "${lightx2v_path}/configs/flux2/flux2_klein_pp8.json" \ 2>&1 | tee "$log_file"USP
{ "model_cls": "flux2_klein", "task": "t2i", "infer_steps": 20, "sample_guide_scale": 1.0, "vae_scale_factor": 16, "feature_caching": "None", "enable_cfg": false, "patch_size": 2, "tokenizer_max_length": 512, "rope_type": "flashinfer_rope", "attn_type": "flash_attn2", "parallel": { "seq_p_size": 8, "seq_p_attn_type": "ulysses" } }torchrun --nproc_per_node=8 -m lightx2v.infer \ --model_cls flux2_klein \ --task t2i \ --target_shape 2048 2048 \ --model_path $model_path \ --prompt "A cat running in a garden" \ --save_result_path "save_results/flux2_klein_4b_usp8.png" \ --config_json "${lightx2v_path}/configs/flux2/flux2_klein_usp8.json" \ 2>&1 | tee "$log_file"