文件最后提交记录最后更新时间
!7437 fix sd attention api call Merge pull request !7437 from 李少鹏/flux_ff 7 个月前
文档整改,gitee->gitcode Co-authored-by: Lighters_c<zyh13227@163.com> # message auto-generated for no-merge-commit merge: !7469 merge ffffix into master 文档整改,gitee->gitcode Created-by: addsubmuldiv Commit-by: Lighters_c Merged-by: ascend-robot Description: ## Motivation Please describe the motivation of this PR and the goal you want to achieve through this PR. ## Modification Please briefly describe what modification is made in this PR. ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [ ] The new code needs to comply with the Clean Code specification. - [ ] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [ ] CLA has been signed and all committers have signed the CLA in this PR. - [ ] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!74695 个月前
!7119 flux魔乐代码同步到Modelzoo Merge pull request !7119 from 李少鹏/flux_zuixin 1 年前
!7119 flux魔乐代码同步到Modelzoo Merge pull request !7119 from 李少鹏/flux_zuixin 1 年前
!7358 fix no cache infer and 32g double card infer Merge pull request !7358 from 李少鹏/flux_fix 9 个月前
!7022 [MindIE-SD]迁移SD模型到MultiModal路径下 Merge pull request !7022 from 龙钢/master 1 年前
!7203 flux双卡显存优化 Merge pull request !7203 from 李少鹏/flux_double_Card 1 年前
!7119 flux魔乐代码同步到Modelzoo Merge pull request !7119 from 李少鹏/flux_zuixin 1 年前
README.md

模型推理指导

一、模型简介

Flux.1-DEV是一种文本到图像的扩散模型,能够在给定文本输入的情况下生成相符的图像。

本模型使用的优化手段如下: 等价优化:FA、ROPE、RMSnorm、TP并行(32G机器可选) 算法优化:FA、ROPE、RMSnorm、DiTCache、TP并行(32G机器可选)

二、环境准备

表 1 版本配套表

配套 版本 环境准备指导
Python 3.10.2 -
torch 2.1.0 -

2.1 获取安装包

2.2 CANN安装

# 增加软件包可执行权限,{version}表示软件版本号,{arch}表示CPU架构,{soc}表示昇腾AI处理器的版本。
chmod +x ./Ascend-cann-toolkit_{version}_linux-{arch}.run
chmod +x ./Ascend-cann-kernels-{soc}_{version}_linux.run
# 校验软件包安装文件的一致性和完整性
./Ascend-cann-toolkit_{version}_linux-{arch}.run --check
./Ascend-cann-kernels-{soc}_{version}_linux.run --check
# 安装
./Ascend-cann-toolkit_{version}_linux-{arch}.run --install
./Ascend-cann-kernels-{soc}_{version}_linux.run --install

# 设置环境变量
source /usr/local/Ascend/ascend-toolkit/set_env.sh

2.3 MindIE安装

# 增加软件包可执行权限,{version}表示软件版本号,{arch}表示CPU架构。
chmod +x ./Ascend-mindie_${version}_linux-${arch}.run
./Ascend-mindie_${version}_linux-${arch}.run --check

# 方式一:默认路径安装
./Ascend-mindie_${version}_linux-${arch}.run --install
# 设置环境变量
cd /usr/local/Ascend/mindie && source set_env.sh

# 方式二:指定路径安装
./Ascend-mindie_${version}_linux-${arch}.run --install --install-path=${AieInstallPath}
# 设置环境变量
cd ${AieInstallPath}/mindie && source set_env.sh

2.4 Torch_npu安装

安装pytorch框架 版本2.1.0 安装包下载

使用pip安装

# {version}表示软件版本号,{arch}表示CPU架构。
pip install torch-${version}-cp310-cp310-linux_${arch}.whl

下载 pytorch_v{pytorchversion}_py{pythonversion}.tar.gz

tar -xzvf pytorch_v{pytorchversion}_py{pythonversion}.tar.gz
# 解压后,会有whl包
pip install torch_npu-{pytorchversion}.xxxx.{arch}.whl

2.5 安装gcc、g++

# 若环境镜像中没有gcc、g++,请用户自行安装
yum install gcc
yum install g++

# 导入头文件路径
export CPLUS_INCLUDE_PATH=/usr/include/c++/12/:/usr/include/c++/12/aarch64-openEuler-linux/:$CPLUS_INCLUDE_PATH

2.6 下载本仓库

   git clone https://modelers.cn/MindIE/FLUX.1-dev.git

2.7 安装所需依赖

pip install -r requirements.txt

三、模型权重

3.1 权重下载

Flux.1-DEV权重下载地址

https://huggingface.co/black-forest-labs/FLUX.1-dev/tree/main

3.2 配置文件说明

修改权重配置文件:

vi ${model_path}/model_index.json

做如下修改:

{
  "_class_name": "FluxPipeline",
  "_diffusers_version": "0.30.0.dev0",
  "scheduler": [
    "diffusers",
    "FlowMatchEulerDiscreteScheduler"
  ],
  "text_encoder": [
    "transformers",
    "CLIPTextModel"
  ],
  "text_encoder_2": [
    "transformers",
    "T5EncoderModel"
  ],
  "tokenizer": [
    "transformers",
    "CLIPTokenizer"
  ],
  "tokenizer_2": [
    "transformers",
    "T5TokenizerFast"
  ],
  "transformer": [
    "FLUX1dev",
    "FluxTransformer2DModel"
  ],
  "vae": [
    "diffusers",
    "AutoencoderKL"
  ]
}

四、模型推理

4.1 Atlas-800I-A2-64g单卡推理性能测试

  1. 设置权重路径:
export model_path="your local flux model path"
  1. 执行命令:
# 在环境中导入以下环境变量提高推理性能
export CPU_AFFINITY_CONF=2
export TASK_QUEUE_ENABLE=2

python inference_flux.py \
       --path ${model_path} \
       --save_path "./res" \
       --device_id 0 \
       --device "npu" \
       --prompt_path "./prompts.txt" \
       --width 1024 \
       --height 1024 \
       --infer_steps 50 \
       --seed 42 \
       --use_cache \
       --device_type "A2-64g" \
       --batch_size 1

参数说明:

  • path: Flux本地模型权重路径,默认读取当前文件夹下的flux文件夹
  • save_path: 保存图像路径,默认当前文件夹下的res文件夹
  • device_id: 推理设备ID,默认值设置为0
  • device: 推理设备类型,默认为npu
  • prompt_path: 用于图像生成的文字描述提示的列表文件路径
  • width: 图像生成的宽度,默认1024
  • height: 图像生成的高度,默认1024
  • infer_steps: Flux图像推理步数,默认值为50
  • seed: 设置随机种子,默认值为42
  • use_cache: 是否开启dit cache近似优化
  • device_type: device类型,有A2-32g-single、A2-32g-dual、A2-64g三个选项
  • batch_size: 指定prompt的batch size,默认为1,大于1时以list形式送入pipeline

4.2 Atlas-800I-A2-32g单卡推理性能测试

  1. 设置权重路径:
export model_path="your local flux model path"
  1. 执行命令:
# 在环境中导入以下环境变量提高推理性能
export CPU_AFFINITY_CONF=2
export TASK_QUEUE_ENABLE=2

python inference_flux.py \
       --path ${model_path} \
       --save_path "./res" \
       --device_id 0 \
       --device "npu" \
       --prompt_path "./prompts.txt" \
       --width 1024 \
       --height 1024 \
       --infer_steps 50 \
       --seed 42 \
       --use_cache \
       --device_type "A2-32g-single"

参数说明参照Atlas-800I-A2-64g参数说明

4.3 Atlas-800I-A2-32g双卡推理性能测试

  1. 设置权重路径:
export model_path="your local flux model path"

2.执行命令进行权重切分

python3 tpsplit_weight.py --path ${model_path}

备注:权重切分成功后,会在模型权重目录生成'transformer_0'与'transformer_1'两个文件夹,两个文件夹下内容与初始transformer文件夹文件相同,但大小不同,执行du -sh,大小应为15G

3.修改transformer_0与transformer_1下的config文件,添加is_tp变量:

{
  "_class_name": "FluxTransformer2DModel",
  "_diffusers_version": "0.30.0.dev0",
  "_name_or_path": "../checkpoints/flux-dev/transformer",
  "attention_head_dim": 128,
  "guidance_embeds": true,
  "in_channels": 64,
  "joint_attention_dim": 4096,
  "num_attention_heads": 24,
  "num_layers": 19,
  "num_single_layers": 38,
  "patch_size": 1,
  "pooled_projection_dim": 768,
  "is_tp": true
}
  1. 执行命令:
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
ASCEND_RT_VISIBLE_DEVICES=0,1 torchrun --master_port=2002 --nproc_per_node=2 inference_flux.py --device_type "A2-32g-dual" --path ${model_path} --prompt_path "./prompts.txt" --width 1024 --height 1024 --infer_steps 50 --seed 42 --use_cache

参数说明:

  • ASCEND_RT_VISIBLE_DEVICES: shell环境变量,用以绑定推理时实际使用的NPU
  • mast_port:master节点端口号,torch_run命令变量设置
  • nproc_per_node:分布式推理使用的NPU数量,设置为2 其余参数说明参照Atlas-800I-A2-64g参数说明

4.4 精度测试

4.4.1 ClipScore测试

1.准备模型与数据集

# 下载Parti数据集
wget https://raw.githubusercontent.com/google-research/parti/main/PartiPrompts.tsv --no-check-certificate

# 下载clip模型
# 安装git-lfs
apt install git-lfs
git lfs install

git clone https://huggingface.co/laion/CLIP-ViT-H-14-laion2B-s32B-b79K

也可手动下载clip模型权重

2.推理Parti数据集,生成图像

# 单卡64G Flux 等价优化推理
python inference_flux.py \
       --path ${model_path} \
       --save_path "./clipscore_res_wocache" \
       --device_id 0 \
       --device "npu" \
       --prompt_path "./PartiPrompts.tsv" \
       --prompt_type "parti" \
       --num_images_per_prompt 4 \
       --info_file_save_path "./clip_info_wocache.json" \
       --width 1024 \
       --height 1024 \
       --infer_steps 50 \
       --seed 42 \
       --device_type "A2-64g"
# 单卡64G Flux 近似优化推理
python inference_flux.py \
       --path ${model_path} \
       --save_path "./clipscore_res_wcache" \
       --device_id 0 \
       --device "npu" \
       --prompt_path "./PartiPrompts.tsv" \
       --prompt_type "parti" \
       --num_images_per_prompt 4 \
       --info_file_save_path "./clip_info_wcache.json" \
       --width 1024 \
       --height 1024 \
       --infer_steps 50 \
       --seed 42 \
       --use_cache \
       --device_type "A2-64g"
# 双卡32G Flux等价优化推理
ASCEND_RT_VISIBLE_DEVICES=0,1 torchrun --master_port=2002 --nproc_per_node=2 inference_flux.py --device_type "A2-32g-dual" --path ${model_path} --prompt_path "./PartiPrompts.tsv" --prompt_type "parti" --num_images_per_prompt 4 --info_file_save_path "./clip_info_wocache.json" --width 1024 --height 1024 --infer_steps 50 --seed 42
# 双卡32G Flux近似优化推理
ASCEND_RT_VISIBLE_DEVICES=0,1 torchrun --master_port=2002 --nproc_per_node=2 inference_flux.py --device_type "A2-32g-dual" --path ${model_path} --prompt_path "./PartiPrompts.tsv" --prompt_type "parti" --num_images_per_prompt 4 --info_file_save_path "./clip_info_wcache.json" --width 1024 --height 1024 --infer_steps 50 --seed 42 --use_cache

3.执行推理脚本计算clipscore

# 等价优化
python clipscore.py \
       --device="cpu" \
       --image_info="clip_info_wocache.json" \
       --model_name="ViT-H-14" \
       --model_weights_path="./CLIP-ViT-H-14-laion2B-s32B-b79K/open_clip_pytorch_model.bin"
# 近似优化
python clipscore.py \
       --device="cpu" \
       --image_info="clip_info_wcache.json" \
       --model_name="ViT-H-14" \
       --model_weights_path="./CLIP-ViT-H-14-laion2B-s32B-b79K/open_clip_pytorch_model.bin"

参数说明

  • device: 推理设备,默认使用cpu做计算。
  • image_info: 上一步生成的json文件。
  • model_name: Clip模型名称。
  • model_weights_path: Clip模型权重文件路径。

4.4.2 Hpsv2精度测试

1.准备模型与数据集

hpsv2数据集获取

# 下载权重
wget https://huggingface.co/spaces/xswu/HPSv2/resolve/main/HPS_v2_compressed.pt --no-check-certificate

2.执行hpsv2数据集,生成图像

#单卡64G Flux等价优化推理
python inference_flux.py \
       --path ${model_path} \
       --save_path "./hpsv2_res_wocache" \
       --device_id 0 \
       --device "npu" \
       --prompt_type "hpsv2" \
       --num_images_per_prompt 1 \
       --info_file_save_path "./hpsv2_info_wocache.json" \
       --width 1024 \
       --height 1024 \
       --infer_steps 50 \
       --seed 42 \
       --device_type "A2-64g"
#单卡64G Flux近似优化推理
python inference_flux.py \
       --path ${model_path} \
       --save_path "./hpsv2_res_wcache" \
       --device_id 0 \
       --device "npu" \
       --prompt_type "hpsv2" \
       --num_images_per_prompt 1 \
       --info_file_save_path "./hpsv2_info_wcache.json" \
       --width 1024 \
       --height 1024 \
       --infer_steps 50 \
       --seed 42 \
       --use_cache \
       --device_type "A2-64g"
# 双卡32G Flux等价优化推理
ASCEND_RT_VISIBLE_DEVICES=0,1 torchrun --master_port=2002 --nproc_per_node=2 inference_flux.py --device_type "A2-32g-dual" --path ${model_path} --prompt_type "hpsv2" --num_images_per_prompt 1 --info_file_save_path "./hpsv2_info_wocache.json" --width 1024 --height 1024 --infer_steps 50 --seed 42
# 双卡32G Flux近似优化推理
ASCEND_RT_VISIBLE_DEVICES=0,1 torchrun --master_port=2002 --nproc_per_node=2 inference_flux.py --device_type "A2-32g-dual" --path ${model_path} --prompt_type "hpsv2" --num_images_per_prompt 1 --info_file_save_path "./hpsv2_info_wocache.json" --width 1024 --height 1024 --infer_steps 50 --seed 42 --use_cache

3.执行推理脚本计算hpsv2

python hpsv2_score.py \
       --image_info="hpsv2_info_wocache.json" \
       --HPSv2_checkpoint="./HPS_v2_compressed.pt" \
       --clip_checkpoint="./CLIP-ViT-H-14-laion2B-s32B-b79K/open_clip_pytorch_model.bin"
  • image_info: 上一步生成的json文件。
  • HPSv2_checkpoint: HPSv2模型权重文件路径。
  • clip_checkpointh: Clip模型权重文件路径。

五、推理结果参考

Flux.1-DEV性能数据

硬件形态 cpu规格 batch size 分辨率 迭代次数 优化手段 性能 采样器 备注
Atlas 800I A2(8*64G) 64核(arm) 1 1024*1024 50 with DiTCache 20.4s FlowMatchEuler 单卡运行
Atlas 800I A2(8*32G) 64核(arm) 1 1024*1024 50 with DiTCache 24.6s FlowMatchEuler 双卡运行

声明

  • 本代码仓提到的数据集和模型仅作为示例,这些数据集和模型仅供您用于非商业目的,如您使用这些数据集和模型来完成示例,请您特别注意应遵守对应数据集和模型的License,如您因使用数据集或模型而产生侵权纠纷,华为不承担任何责任。
  • 如您在使用本代码仓的过程中,发现任何问题(包括但不限于功能问题、合规问题),请在本代码仓提交issue,我们将及时审视并解答。