LeapAlign: Post-Training Flow Matching Models at Any Generation Step by Building Two-Step Trajectories
Official PyTorch Implementation
LeapAlign: Post-Training Flow Matching Models at Any Generation Step by Building Two-Step Trajectories
Zhanhao Liang1,2,*, Tao Yang2,*,†, Jie Wu2, Chengjian Feng2, Liang Zheng1
1ANU, 2ByteDance Seed
*Equal contribution, †Project Lead
LeapAlign is a post-training method for aligning flow matching models with human preferences by backpropagating reward gradients through the differentiable generation process. Since backpropagation through the full sampling trajectory is memory-intensive and prone to gradient explosion, early generation steps that determine the global structure and layout of the final image are difficult to optimize. LeapAlign addresses this by constructing a two-step leap trajectory and backpropagating gradients through it instead of the full sampling trajectory, enabling gradient propagation to any generation step.
conda create --name leapalign python=3.11 -y
conda activate leapalign./setup_env.shThis script installs the Python dependencies for LeapAlign, installs python3-tk, and clones and installs HPSv2.
mkdir -p data
huggingface-cli download black-forest-labs/FLUX.1-dev --local-dir ./data/flux
mkdir -p hps_ckpt
huggingface-cli download xswu/HPSv2 HPS_v2.1_compressed.pt --local-dir ./hps_ckpt
huggingface-cli download laion/CLIP-ViT-H-14-laion2B-s32B-b79K open_clip_pytorch_model.bin --local-dir ./hps_ckptThe preprocessing scripts use 8 GPUs by default.
Prepare HPDv2 prompt embeddings:
bash scripts/preprocess/preprocess_flux_hpdv2_rl_embeddings.shPrepare GenEval prompt embeddings:
bash scripts/preprocess/preprocess_flux_geneval_rl_embeddings.shThe provided training scripts are configured for a 2x8 setup by default, i.e., 2 nodes with 8 GPUs each.
Launch on node 0:
MASTER_ADDR=<node0_host> MASTER_PORT=29500 NNODES=2 NODE_RANK=0 NPROC_PER_NODE=8 \
bash scripts/finetune/train_flux_leapalign_hpdv2_hpsv2.shLaunch on node 1:
MASTER_ADDR=<node0_host> MASTER_PORT=29500 NNODES=2 NODE_RANK=1 NPROC_PER_NODE=8 \
bash scripts/finetune/train_flux_leapalign_hpdv2_hpsv2.shLaunch on node 0:
MASTER_ADDR=<node0_host> MASTER_PORT=29500 NNODES=2 NODE_RANK=0 NPROC_PER_NODE=8 \
bash scripts/finetune/train_flux_leapalign_geneval_hpsv2.shLaunch on node 1:
MASTER_ADDR=<node0_host> MASTER_PORT=29500 NNODES=2 NODE_RANK=1 NPROC_PER_NODE=8 \
bash scripts/finetune/train_flux_leapalign_geneval_hpsv2.shThe example below uses 8 GPUs.
torchrun --nproc_per_node=8 fastvideo/evaluation/sample_flux_images.py \
--model_path <path_to_finetuned_transformer> \
--output_dir ./output/flux_leapalign_hpsv2--model_path should point to a checkpoint directory saved during fine-tuning.
python fastvideo/evaluation/evaluate_reward_values.py \
--results_dir ./output/flux_leapalign_hpsv2 \
--output_dir ./output/rewards_logThe script prints the average HPSv2.1 score and writes <results_dir_basename>_rewards_log.json to --output_dir.
The example below uses 8 GPUs.
torchrun --nproc_per_node=8 fastvideo/evaluation/sample_flux_images_geneval.py \
--model_path <path_to_finetuned_transformer> \
--outdir ./output/flux_leapalign_genevalFollow the setup and scoring instructions in the official GenEval repo and run their evaluator against the directory produced in step 1.
LeapAlign builds upon several excellent open-source projects. We would like to thank the authors and contributors of:
If you find this code useful in your research, please consider citing:
@article{liang2026leapalign,
title={LeapAlign: Post-Training Flow Matching Models at Any Generation Step by Building Two-Step Trajectories},
author={Liang, Zhanhao and Yang, Tao and Wu, Jie and Feng, Chengjian and Zheng, Liang},
journal={arXiv preprint arXiv:2604.15311},
year={2026}
}
