Skip to content

Add flux.1 to diffusion backend#832

Draft
zirui wants to merge 27 commits into
mainfrom
dev/zirui/flux
Draft

Add flux.1 to diffusion backend#832
zirui wants to merge 27 commits into
mainfrom
dev/zirui/flux

Conversation

@zirui

@zirui zirui commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR adds FLUX.1-dev text-to-image training support on top of the Primus diffusion backend introduced in #779.
The goal is to make FLUX T2I pretraining runnable through the existing Primus training entrypoint, reusing the diffusion FSDP2 trainer, config system, checkpoint flow, logging, and launch infrastructure.


Motivation

FLUX is a primary diffusion workload for text-to-image training. This integration enables:

  • Unified Primus training interface for FLUX and Wan diffusion models
  • FLUX.1-dev T2I pretraining with FSDP2
  • Support for both precomputed encodings and raw image-text data bring-up
  • Reuse of existing Primus infra for launch, optimizer, checkpointing, logging, and hooks

Scope

Included

  • FLUX.1-dev model wrapper and registration
  • FLUX training pipeline with flow-matching objective
  • Precomputed dataset path using T5 encodings, CLIP encodings, and VAE latent statistics
  • Raw image-text dataset path with frozen T5 / CLIP / autoencoder encoding
  • Diffusion data registry/generalized dataset processing
  • MI355X example configs for precomputed and raw FLUX pretraining
  • Preflight/dependency updates for FLUX assets and datasets
  • Focused unit tests for config conversion, dataset processing, and tiny FLUX loss computation

Not included / future work

  • FLUX sequence parallelism; FLUX currently requires sp_size=1
  • Multi-node validation
  • Inference pipeline
  • Full production-scale convergence validation
  • Kernel-level performance optimization

Current Status

Completed

  • FLUX.1-dev model integration
  • FLUX model/data registration in the diffusion backend
  • Precomputed encoding dataset support
  • Raw image-text dataset support
  • Single-node 8-GPU smoke validation
  • Focused FLUX backend unit tests

In Progress / Follow-up

  • Multi-node training validation
  • Longer-running convergence validation
  • Performance tuning for larger batch / production runs
  • FLUX-specific sequence parallelism support, if needed

Testing

Unit Tests

  • python -m pytest tests/unit_tests/backends/diffusion/test_flux_backend.py
    • Result: 8 passed

Single-node Validation

Validated on 8x AMD Instinct MI355X with torchrun --standalone --nproc_per_node=8:

  • FLUX precomputed encoding mode: local batch sizes 1 / 8 / 16 / 24
  • FLUX raw image-text mode: local batch sizes 1 / 8 / 16 / 24
  • Additional 8-step smoke runs for:
    • Precomputed mode, local batch size 24
    • Raw mode, local batch size 16

Known Issues / Risks

  • Multi-node stability has not been validated yet
  • FLUX sequence parallelism is not implemented; configs should keep sp_size=1
  • Full convergence quality still needs longer training runs with production data
  • Current ROCm validation used gradient_checkpointing=false; enabling it needs more investigation with FSDP2

Next Steps

  1. Complete multi-node validation
  2. Run longer convergence checks on real FLUX pretraining data
  3. Add performance benchmark notes for recommended batch sizes
  4. Investigate gradient checkpointing and FLUX-specific SP support

Notes

This is a WIP draft PR building on the diffusion backend and Wan training support from #779.

Copilot AI review requested due to automatic review settings July 1, 2026 04:09
@zirui zirui changed the title Add flux to diffusion backend Add flux.1 to diffusion backend Jul 1, 2026
Comment thread primus/backends/diffusion/models/wan/wan_dit.py Fixed
Comment thread primus/backends/diffusion/models/wan/vae2_1.py Fixed
Comment thread primus/backends/diffusion/models/wan/vae2_1.py Fixed
Comment thread primus/backends/diffusion/models/wan/vae2_1.py Fixed
Comment thread primus/backends/diffusion/models/wan/vae2_1.py Fixed
Comment thread primus/backends/diffusion/models/wan/vae2_1.py Fixed
Comment thread primus/backends/diffusion/models/wan/vae2_1.py Fixed
Comment thread primus/backends/diffusion/models/wan/vae2_1.py Fixed
Comment thread primus/backends/diffusion/models/wan/vae2_1.py Fixed
Comment thread primus/backends/diffusion/models/wan/vae2_1.py Fixed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends Primus’s in-tree diffusion backend to support FLUX.1-dev text-to-image pretraining, reusing the existing diffusion training entrypoint, FSDP2 trainer, config normalization, and hook/launch infrastructure (building on the WAN diffusion backend introduced in #779).

Changes:

  • Added FLUX model + training pipeline (flow-matching) with support for both precomputed-encoding datasets and raw image-text datasets (online frozen encoders).
  • Expanded diffusion backend infrastructure: registries, distributed utilities (DTCP checkpoints + mesh/SP helpers), attention backends, optimizer variant(s), and prepare hooks + requirements.
  • Added example configs and focused unit tests validating config conversion, dataset processing, and a tiny FLUX loss computation.

Reviewed changes

Copilot reviewed 79 out of 80 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
tests/unit_tests/backends/diffusion/test_flux_backend.py Unit tests for FLUX arg mapping, datasets/processors, and tiny loss.
runner/helpers/hooks/train/pretrain/diffusion/requirements-diffusion.txt Python deps for diffusion hook environments.
runner/helpers/hooks/train/pretrain/diffusion/prepare.py Pretrain diffusion config validation/prepare hook (WAN + FLUX).
runner/helpers/hooks/train/pretrain/diffusion/00_install_requirements.sh Hook step to install diffusion requirements with cache.
runner/helpers/hooks/train/posttrain/diffusion/prepare.py Posttrain wrapper that reuses the pretrain diffusion prepare hook.
primus/configs/modules/diffusion/pre_trainer.yaml Adds diffusion module preset (pretrain stage).
primus/configs/modules/diffusion/post_trainer.yaml Adds diffusion module preset (posttrain stage).
primus/configs/models/diffusion/wan2.2_ti2v_5b.yaml WAN2.2 model preset for diffusion backend.
primus/configs/models/diffusion/wan2.2_ti2v_5b_sft.yaml WAN2.2 SFT/posttrain preset override.
primus/configs/models/diffusion/wan2.1_t2v_1.3b.yaml WAN2.1 model preset for diffusion backend.
primus/configs/models/diffusion/wan2.1_t2v_1.3b_sft.yaml WAN2.1 SFT/posttrain preset override.
primus/configs/models/diffusion/flux.1_dev_t2i.yaml FLUX.1-dev T2I model preset (encoders + scaling params).
primus/backends/diffusion/utils/train_utils.py Training utilities (dtype/seed/state_dict helpers).
primus/backends/diffusion/utils/log.py Loguru proxy to ensure Primus-bound extras are present.
primus/backends/diffusion/utils/data_utils.py Video frame-count utility helpers (WAN path).
primus/backends/diffusion/utils/init.py Diffusion utils package metadata/docstring.
primus/backends/diffusion/trainers/init.py Diffusion trainer exports.
primus/backends/diffusion/schedulers/flow_match.py Flow-matching scheduler used by diffusion training.
primus/backends/diffusion/registry.py Central registry for diffusion model/dataset/trainer builders.
primus/backends/diffusion/optim/adamw_fp32_state.py FP32-state AdamW optimizer for bf16/fp16 params.
primus/backends/diffusion/models/wan/train_pipeline.py WAN flow-matching training pipeline implementation.
primus/backends/diffusion/models/wan/t5.py WAN T5 encoder implementation + checkpoint loader.
primus/backends/diffusion/models/wan/configuration_wanvideo.py WAN config shim for training.
primus/backends/diffusion/models/wan/components.py Container type for WAN model components.
primus/backends/diffusion/models/wan/attention_backend.py WAN attention backend wrapper with CPU fallback.
primus/backends/diffusion/models/wan/adapter.py WAN training adapter implementing Primus model interface.
primus/backends/diffusion/models/wan/init.py WAN model exports.
primus/backends/diffusion/models/registrations/wan.py WAN model builder + checkpoint loading logic.
primus/backends/diffusion/models/registrations/flux.py FLUX model builder + checkpoint/HF download logic.
primus/backends/diffusion/models/registrations/init.py Package marker for model registrations.
primus/backends/diffusion/models/interface.py Abstract diffusion model interface for trainer integration.
primus/backends/diffusion/models/flux/utils.py FLUX latent utilities (sampling, packing, position ids).
primus/backends/diffusion/models/flux/train_pipeline.py FLUX flow-matching loss for precomputed + raw modes.
primus/backends/diffusion/models/flux/model.py FLUX DiT backbone implementation (+ checkpointing toggles).
primus/backends/diffusion/models/flux/math.py FLUX attention math + RoPE helpers.
primus/backends/diffusion/models/flux/layers.py FLUX transformer blocks and embeddings.
primus/backends/diffusion/models/flux/configuration_flux.py Simple config container for FLUX training.
primus/backends/diffusion/models/flux/conditioner.py HF-based CLIP/T5 text embedders for raw-mode training.
primus/backends/diffusion/models/flux/autoencoder.py FLUX autoencoder implementation + checkpoint resolver.
primus/backends/diffusion/models/flux/adapter.py Primus adapter around FLUX backbone + pipeline.
primus/backends/diffusion/models/flux/init.py FLUX model exports.
primus/backends/diffusion/models/init.py Diffusion model package exports.
primus/backends/diffusion/distributed/ulysses.py Ulysses sequence-parallel primitives.
primus/backends/diffusion/distributed/mesh.py Device-mesh creation and distributed setup.
primus/backends/diffusion/distributed/checkpoint.py DTCP save/load helpers for sharded checkpoints.
primus/backends/diffusion/distributed/init.py Distributed utilities exports.
primus/backends/diffusion/diffusion_pretrain_trainer.py Primus trainer wrapper wiring diffusion registry builders.
primus/backends/diffusion/diffusion_adapter.py Backend adapter converting Primus module params to diffusion args.
primus/backends/diffusion/data/registrations/wan.py WAN dataset/processor builder registration.
primus/backends/diffusion/data/registrations/flux.py FLUX dataset/processor builder registration.
primus/backends/diffusion/data/registrations/init.py Package marker for dataset registrations.
primus/backends/diffusion/data/processor.py WAN video processor implementation (tokenize + video preprocess).
primus/backends/diffusion/data/flux_precomputed.py FLUX precomputed + raw datasets and processors.
primus/backends/diffusion/data/dataset.py WAN video dataset implementation (video backends).
primus/backends/diffusion/data/config.py Pydantic dataset config schema (WAN path).
primus/backends/diffusion/data/collator.py Raw and vision collators used by diffusion dataloaders.
primus/backends/diffusion/data/init.py Diffusion data package exports.
primus/backends/diffusion/attention/flex.py FlexAttention backend wrapper with compile fallback.
primus/backends/diffusion/attention/attention.py Unified attention API + backend selection.
primus/backends/diffusion/attention/aiter.py AITER flash-attn backend integration.
primus/backends/diffusion/attention/_flash_common.py Shared flash-attn backend glue (fixed/varlen paths).
primus/backends/diffusion/attention/init.py Attention package exports.
primus/backends/diffusion/init.py Registers diffusion backend adapter.
examples/diffusion/README.md Example usage docs for diffusion training entrypoints.
examples/diffusion/configs/MI355X/wan2.2_ti2v_5b-pretrain.yaml MI355X WAN2.2 pretrain example config.
examples/diffusion/configs/MI355X/wan2.2_ti2v_5b-posttrain.yaml MI355X WAN2.2 posttrain example config.
examples/diffusion/configs/MI355X/wan2.1_t2v_1.3b-pretrain.yaml MI355X WAN2.1 pretrain example config.
examples/diffusion/configs/MI355X/wan2.1_t2v_1.3b-posttrain.yaml MI355X WAN2.1 posttrain example config.
examples/diffusion/configs/MI355X/flux.1_dev_t2i-raw-pretrain.yaml MI355X FLUX raw image-text pretrain example config.
examples/diffusion/configs/MI355X/flux.1_dev_t2i-pretrain.yaml MI355X FLUX precomputed pretrain example config.
.gitignore Ensures diffusion data package isn’t inadvertently ignored.

Comment thread primus/backends/diffusion/distributed/checkpoint.py
Comment thread primus/backends/diffusion/schedulers/flow_match.py
Comment thread primus/backends/diffusion/utils/__init__.py Outdated
Comment thread primus/backends/diffusion/trainers/__init__.py Outdated
Comment thread primus/backends/diffusion/models/__init__.py Outdated
Comment thread primus/backends/diffusion/diffusion_pretrain_trainer.py Outdated
Comment thread primus/backends/diffusion/diffusion_pretrain_trainer.py
Comment thread examples/diffusion/README.md Outdated
Copilot finished work on behalf of zirui July 6, 2026 11:21
Copilot AI review requested due to automatic review settings July 8, 2026 00:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review is ineligible. To be eligible to request a review, you need a paid Copilot license, or your organization must enable Copilot code review.

Comment thread primus/backends/diffusion/data/processing_wanvideo.py Fixed
Comment thread primus/backends/diffusion/data/dataset.py Fixed
Comment thread primus/backends/diffusion/data/processing_wanvideo.py Fixed
Comment thread primus/backends/diffusion/data/processing_wanvideo.py Fixed
zirui and others added 19 commits July 8, 2026 01:33
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Remove redundant VAE checkpoint loading, harden AdamW optimizer fallback, and add focused tests for Wan argument normalization and optimizer fallback behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>
zirui and others added 7 commits July 8, 2026 01:33
Add missing diffusion dependency checks, normalize file:// video paths across readers, report accumulated loss correctly, and validate Ulysses all-to-all divisibility before collectives.

Co-authored-by: Cursor <cursoragent@cursor.com>
Add FLUX.1-dev pretraining support for precomputed and raw image-text data so the diffusion backend can run FLUX workloads without TorchTitan framework dependencies.

Co-authored-by: Cursor <cursoragent@cursor.com>
Remove local asset assumptions from FLUX configs/docs, preserve AMD and BFL attribution, and tighten checkpoint/dataset validation.

Co-authored-by: Cursor <cursoragent@cursor.com>
Normalize diffusion backend formatting after running pre-commit across the repository.

Co-authored-by: Cursor <cursoragent@cursor.com>
Route FLUX attention through the diffusion backend dispatcher and align the default runtime backend with ROCm Wan defaults while preserving SDPA coverage in tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
…icts

The CI was failing with a pytest collection error:
  ERROR collecting tests/unit_tests/tools/test_utils.py
  import file mismatch: imported module 'test_utils' has this __file__ attribute:
    tests/unit_tests/core/patches/test_utils.py
  HINT: remove __pycache__ / .pyc files and/or use a unique basename for
  your test file modules

Root cause: two files named test_utils.py existed in different
sub-directories of tests/unit_tests/ (core/patches/ and tools/),
but their parent directories lacked __init__.py files. Without
__init__.py, pytest imports every test file as a top-level module
using just its basename, so both were imported as 'test_utils',
causing the conflict.

Fix: add empty __init__.py to all test sub-directories that were
missing one, making each directory a proper Python package. pytest
then imports tests with fully-qualified package paths
(e.g. tests.unit_tests.core.patches.test_utils vs
tests.unit_tests.tools.test_utils), eliminating the name collision.
Clarify diffusion-generic docs and error messages, tighten Wan preprocessing quality issues, and fail fast for missing exponential shift parameters.

Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI review requested due to automatic review settings July 8, 2026 01:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review is ineligible. To be eligible to request a review, you need a paid Copilot license, or your organization must enable Copilot code review.

Avoid tying the shared diffusion utils package description to a specific video backend.

Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI review requested due to automatic review settings July 8, 2026 03:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review is ineligible. To be eligible to request a review, you need a paid Copilot license, or your organization must enable Copilot code review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants