MeanFlow

haidog-yaqubMIT活跃维护

MeanFlow 与 iMF 的 PyTorch 实现,用一步生成建模替代多步扩散采样,让图像生成推理更快。

PyTorch implementation of MeanFlow & iMF (one-step generative modeling).

暂无版本1,213star68fork同步于 2026-11-10 23:50 UTC

怎么上手

由 README 提炼,执行前请确认
pip install torch accelerate torchvision einops tqdm diffusers
python train.py --config configs/mnist.py
硬件要求按 README 说明

README 未说明显存与 GPU 型号要求;支持单卡与 Accelerate 多卡训练,ImageNet 配置使用 VAE latent。

多卡 / 张量并行
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

MeanFlow 与 iMF 一步生成模型的 PyTorch 非官方实现,可直接训练出单步采样生成器

MeanFlow 与 iMF 的 PyTorch 实现,用一步生成建模替代多步扩散采样,让图像生成推理更快。

核心能力

  • 支持 meanflow 与 i-meanflow 两种训练模式
  • 双头 DiT 同时输出 u 与 v 两路预测
  • 把 CFG scale 作为模型输入做引导蒸馏
  • JVP 在 no_grad 下只算 dudt,另开带梯度前向
  • 提供 MNIST、CIFAR-10、ImageNet latent 配置

容易踩的坑

jvp 与 PyTorch 原生 Flash Attention 不兼容,仓库用 no_grad JVP 加独立前向绕开;ImageNet latent 配置需要额外 VAE 权重;Triton JVP 与 Flash Attention 尚未支持。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    MeanFlow

    PyTorch implementation of Mean Flows for One-step Generative Modeling (MeanFlow) and Improved Mean Flows (iMF).

    Note: Unofficial implementation, based on the papers above and the official JAX repo imeanflow.

    Contributions and feedback are welcome! Feel free to open an issue or pull request.

    Updates

    2026.06.13

    • MeanFlow and iMF training (meanflow.mode: "meanflow" / "i-meanflow")
    • Dual-head DiT: u for MeanFlow, v for flow matching
    • CFG scale as model input with CFG distillation (cfg_scale; None to disable)
    • JVP under no_grad for dudt only; separate grad-enabled forward pass for optimization
    • Config-based training for MNIST, CIFAR-10, and ImageNet latent

    Usage

    i-meanflow (meanflow.mode: "i-meanflow") is more stable and recommended for your projects. meanflow mode is kept for reference.

    pip install torch accelerate torchvision einops tqdm diffusers
    
    # single GPU
    python train.py --config configs/mnist.py
    
    # custom run name
    python train.py --config configs/cifar10.py --run_suffix exp1
    
    # multi-GPU
    accelerate launch --num_processes 2 train.py --config configs/mnist.py
    
    ConfigDataset
    configs/mnist.pyMNIST
    configs/cifar10.pyCIFAR-10
    configs/imagenet_latent.pyImageNet (latent, VAE)

    Common config fields: n_steps, batch_size, grad_clip, mixed_precision, meanflow.mode, meanflow.cfg_scale.

    Training logs are saved to logs/{run_name}/:

    logs/{run_name}/
    ├── config.py
    ├── train.log      # loss, FM/MF loss, MF_V_MSE, grad norm, LR
    ├── images/
    └── ckpts/
    

    Examples

    MNIST — 10k steps, 1-step sample:

    MNIST

    MNIST — 6k steps, 1-step CFG (w=2.0):

    MNIST-cfg

    CIFAR-10 — 200k steps, 1-step CFG (w=2.0):

    CIFAR-10-cfg

    TODO

    • Basic training and inference
    • Multi-GPU via Accelerate
    • Classifier-Free Guidance
    • Latent image training
    • Improved MeanFlow (iMF)
    • Triton JVP + Flash Attention

    Known Issues

    • jvp is currently incompatible with PyTorch's native Flash Attention (scaled_dot_product_attention).

    • Solution in this repo:

      • Run the JVP pass under no_grad (for dudt only) without Flash Attention.
      • Run a separate gradient-enabled forward pass for optimization with Flash Attention.
    • Other advanced solutions:

      • Triton-based kernels with JVP support (e.g. rcm) will allow Flash Attention within JVP.
      • Such kernels may offer additional speed and memory benefits, but are not currently supported in this repository.

    Acknowledgement

    Building upon Just-a-DiT and EzAudio.

    iMF support is based on the official JAX repo imeanflow. Thanks to the authors for releasing their code.

    Research projects built on this repository

    Like This Project?

    If you find this repo helpful, consider dropping a ⭐, it really helps!

    Citation

    If you find this repository useful in your research or projects, please consider citing the original MeanFlow papers as well as this implementation.

    MeanFlow

    @article{geng2025meanflow,
      title={Mean Flows for One-step Generative Modeling},
      author={Geng, Zhengyang and Shechtman, Eli and Kolter, J. Zico and He, Kaiming},
      journal={arXiv preprint arXiv:2505.13447},
      year={2025}
    }
    

    Improved MeanFlow

    @article{geng2025improved,
      title={Improved Mean Flows: On the Challenges of Fastforward Generative Models},
      author={Geng, Zhengyang and Lu, Yiyang and Wu, Zongze and Shechtman, Eli and Kolter, J. Zico and He, Kaiming},
      journal={arXiv preprint arXiv:2512.02012},
      year={2025}
    }
    

    This Repository

    @misc{meanflow_pytorch,
      title={MeanFlow: Unofficial PyTorch Implementation},
      author={haidog-yaqub},
      year={2025},
      howpublished={\url{https://github.com/haidog-yaqub/MeanFlow}},
    }