文件最后提交记录最后更新时间
HunyuanDiT适配mindiesd仓中已删除的接口 Co-authored-by: LoganJane<longgang2@huawei.com> # message auto-generated for no-merge-commit merge: !7483 merge master into master HunyuanDiT适配mindiesd仓中已删除的接口 Created-by: LoganJane Commit-by: LoganJane Merged-by: ascend-robot Description: ## Motivation 适配mindiesd仓中已删除的接口 ## Modification 1、将mindiesd中原有的接口ConfigMixin、DiffusionScheduler新增到HunyuanDiT模型仓中 2、修改HunyuanDiT模型仓中调用接口方式 ## Self-test (Optional) 修改后验证模型功能性能精度正常 ![image.png](https://raw.gitcode.com/user-images/assets/8112803/324cdc9a-af8c-4176-8ea3-3d11f49e7e17/image.png 'image.png') ## 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**: - [x] The new code needs to comply with the Clean Code specification. - [x] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [x] CLA has been signed and all committers have signed the CLA in this PR. - [x] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!74834 个月前
!7022 [MindIE-SD]迁移SD模型到MultiModal路径下 Merge pull request !7022 from 龙钢/master 1 年前
!7022 [MindIE-SD]迁移SD模型到MultiModal路径下 Merge pull request !7022 from 龙钢/master 1 年前
文档整改,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 个月前
!7163 【MindIE SD】HunyuanDiT 增加broadcast方法 Merge pull request !7163 from 龙钢/master 1 年前
!7127 【MindIE SD】HunyuanDiT模型更新接口调用和README Merge pull request !7127 from 龙钢/master 1 年前
README.md

模型推理指导

一、模型简介

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

表 1 本模型当前支持的分辨率

比例(H:W) 1:1 1:1 4:3 4:3 4:3 3:4 3:4 3:4 5:3 3:5
分辨率 1024×1024 1280×1280 1024×768 1152×864 1280×960 768×1024 864×1152 960×1280 1280×768 768×1280

本模型使用的优化手段如下:

  • 等价优化:FA、RoPE、Linear
  • 算法优化:FA、RoPE、Linear、Cache

二、环境准备

表 2 版本配套表

配套 版本 环境准备指导
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://gitcode.com/ascend/ModelZoo-PyTorch.git

2.7 安装模型所需依赖

pip install -r requirements.txt

# 若要使用hpsv2验证精度,则还需要按照以下步骤安装hpsv2
git clone https://github.com/tgxs002/HPSv2.git
pip install -e HPSv2

三、模型权重

3.1 权重下载

https://huggingface.co/Tencent-Hunyuan/HunyuanDiT-v1.2/tree/main/t2i

3.2 配置文件说明

  1. 在t2i/model路径下,新增HunyuanDiT模型权重的配置文件,命名为config.json
{
  "_class_name": "HunyuanDiT2DModel",
  "_mindiesd_version": "2.0.RC1",
  "input_size": [
    null,
    null
  ],
  "patch_size": 2,
  "in_channels": 4,
  "hidden_size": 1408,
  "depth": 40,
  "num_heads": 16,
  "mlp_ratio": 4.3637,
  "text_states_dim": 1024,
  "text_states_dim_t5": 2048,
  "text_len": 77,
  "text_len_t5": 256,
  "size_cond": null,
  "use_style_cond": false
}
  1. 各模型的配置文件、权重文件的路径层级样例如下所示。
|----hunyuan_dit
|    |---- ckpts
|    |    |---- t2i
|    |    |    |---- clip_text_encoder
|    |    |    |---- model
|    |    |    |    |---- config.json
|    |    |    |    |---- 模型权重
|    |    |    |---- mt5
|    |    |    |---- sdxl-vae-fp16-fix
|    |    |    |---- tokenizer

四、模型推理

4.1 单卡推理适配性能测试

  1. 设置权重路径:
path="ckpts/t2i"

修改权重和prompts文件夹的权限为安全权限

chmod -R 640 ckpts/t2i/
chmod -R 640 prompts/
  1. 执行命令:
python inference_hydit.py \
       --path ${path} \
       --device_id 0 \
       --prompt "渔舟唱晚" \
       --input_size 1024 1024 \
       --seed 42 \
       --infer_steps 100

参数说明:

  • path:权重路径,包含clip_text_encoder、model、mt5、sdxl-vae-fp16-fix、tokenizer的权重及配置文件。
  • device_id:推理设备ID。
  • prompt:用于图像生成的文字描述提示。
  • input_size:生成的图像尺寸,宽高要求是8的倍数。
  • seed:设置随机种子,默认值为42。
  • infer_steps:推理迭代步数,默认值为100。

执行完成后在results目录下生成一张推理图像。

4.2 单卡等价优化推理性能测试

  1. 设置权重路径
path="ckpts/hydit"

修改权重和prompts文件夹的权限为安全权限

chmod -R 640 ckpts/t2i/
chmod -R 640 prompts/
  1. 执行命令:
python inference_hydit.py \
       --path ${path} \
       --device_id 0 \
       --test_acc \
       --prompt_file "prompts/example_prompts.txt" \
       --input_size 1024 1024 \
       --seed 42 \
       --infer_steps 100

参数说明:

  • path:权重路径,包含clip_text_encoder、model、mt5、sdxl-vae-fp16-fix、tokenizer的权重及配置文件。
  • device_id:推理设备ID。
  • test_acc:使用 --test_acc 开启prompt_file列表中的图像生成,用于性能/精度测试。
  • prompt_file:用于图像生成的文字描述提示的列表文件路径。
  • input_size:生成的图像尺寸,宽高要求是8的倍数。
  • seed:设置随机种子,默认值为42。
  • infer_steps:推理迭代步数,默认值为100。

执行完成后在results目录下生成推理图像,图像生成顺序与prompt顺序保持一致,并在终端显示推理时间。

4.3 单卡算法优化推理性能测试

  1. 设置权重路径
path="ckpts/hydit"

修改权重和prompts文件夹的权限为安全权限

chmod -R 640 ckpts/t2i/
chmod -R 640 prompts/
  1. 执行命令:
python inference_hydit.py \
       --path ${path} \
       --device_id 0 \
       --test_acc \
       --prompt_file "prompts/example_prompts.txt" \
       --use_attentioncache \
       --input_size 1024 1024 \
       --seed 42 \
       --infer_steps 100

参数说明:

  • path:权重路径,包含clip_text_encoder、model、mt5、sdxl-vae-fp16-fix、tokenizer的权重及配置文件。
  • device_id:推理设备ID。
  • test_acc:使用 --test_acc 开启prompt_file列表中的图像生成,用于性能/精度测试。
  • prompt_file:用于图像生成的文字描述提示的列表文件路径。
  • use_attentioncache:使用 --use_attentioncache 开启算法策略优化的测试。
  • input_size:生成的图像尺寸,宽高要求是8的倍数。
  • seed:设置随机种子,默认值为42。
  • infer_steps:推理迭代步数,默认值为100。

执行完成后在results目录下生成推理图像,图像生成顺序与prompt顺序保持一致,并在终端显示推理时间。

4.4 单卡多batch推理性能测试

  1. 设置权重路径
path="ckpts/hydit"

修改权重和prompts文件夹的权限为安全权限

chmod -R 640 ckpts/t2i/
chmod -R 640 prompts/
  1. 执行命令:
python inference_hydit.py \
       --path ${path} \
       --device_id 0 \
       --test_acc \
       --prompt_file "prompts/example_prompts.txt" \
       --use_attentioncache \
       --input_size 1024 1024 \
       --batch_size 2 \
       --seed 42 \
       --infer_steps 100

参数说明:

  • path:权重路径,包含clip_text_encoder、model、mt5、sdxl-vae-fp16-fix、tokenizer的权重及配置文件。
  • device_id:推理设备ID。
  • test_acc:使用 --test_acc 开启prompt_file列表中的图像生成,用于性能/精度测试。
  • prompt_file:用于图像生成的文字描述提示的列表文件路径。
  • use_attentioncache:使用 --use_attentioncache 开启算法策略优化的测试。
  • input_size:生成的图像尺寸,宽高要求是8的倍数。
  • batch_size:每个prompt生成的图像数量,根据设备显存,batch_size最大设置为2。
  • seed:设置随机种子,默认值为42。
  • infer_steps:推理迭代步数,默认值为100。

执行完成后在results目录下生成推理图像,图像生成顺序与prompt顺序保持一致,并在终端显示推理时间。

4.5 精度测试

由于生成的图像存在随机性,提供两种精度验证方法:

  • CLIP-score(文图匹配度量):评估图片和输入文本的相关性,分数的取值范围为[-1, 1],越高越好。使用Parti数据集进行验证
  • HPSv2(图片美学度量):评估生成图片的人类偏好评分,分数的取值范围为[0, 1],越高越好。使用HPSv2数据集进行验证

注意:由于要生成的图片数量较多,进行完整的精度验证需要耗费很长的时间。

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

hpsv2数据集下载链接:https://gitcode.com/ascend/ModelZoo-PyTorch/blob/master/MindIE/MindIE-Torch/built-in/foundation/stable_diffusion_xl/hpsv2_benchmark_prompts.json

建议将PartiPrompts.tsvhpsv2_benchmark_prompts.json文件放到prompts/路径下。

  1. 下载模型权重
# Clip Score和HPSv2均需要使用的权重
# 安装git-lfs
apt install git-lfs
git lfs install

# Clip Score权重
git clone https://huggingface.co/laion/CLIP-ViT-H-14-laion2B-s32B-b79K

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

也可手动下载权重 将权重放到CLIP-ViT-H-14-laion2B-s32B-b79K目录下,手动下载HPSv2权重放到当前路径

  1. 使用推理脚本读取Parti数据集,生成图片 设置权重路径
path="ckpts/hydit"

修改权重和prompts文件夹的权限为安全权限

chmod -R 640 ckpts/t2i/
chmod -R 640 prompts/

执行命令:

# 使用算法优化
python inference_hydit.py \
       --path ${path} \
       --device_id 0 \
       --test_acc \
       --prompt_file "prompts/PartiPrompts.tsv" \
       --prompt_file_type parti \
       --max_num_prompts 0 \
       --info_file_save_path ./image_info_parti.json \
       --save_result_path ./results_parti \
       --use_attentioncache \
       --input_size 1024 1024 \
       --seed 42 \
       --infer_steps 100

参数说明:

  • path:权重路径,包含clip_text_encoder、model、mt5、sdxl-vae-fp16-fix、tokenizer的权重及配置文件。
  • device_id:推理设备ID。
  • test_acc:使用 --test_acc 开启prompt_file列表中的图像生成,用于性能/精度测试。
  • prompt_file:用于图像生成的文字描述提示的列表文件路径。
  • prompt_file_type:prompt文件类型,用于指定读取方式,可选范围:plain,parti,hpsv2。默认值为plain。
  • max_num_prompts:限制prompt数量为前X个,0表示不限制。
  • info_file_save_path:生成图像信息的json文件路径。
  • save_result_path:生成图像的存放目录。
  • use_attentioncache:使用 --use_attentioncache 开启算法策略优化的测试。
  • input_size:生成的图像尺寸,宽高要求是8的倍数。
  • seed:设置随机种子,默认值为42。
  • infer_steps:推理迭代步数,默认值为100。

执行完成后在./results_parti目录下生成推理图像。在当前目录下生成一个image_info_parti.json文件,记录着图像和prompt的对应关系,并在终端显示推理时间。

  1. 使用推理脚本读取hpsv2数据集,生成图片 设置权重路径
path="ckpts/hydit"

修改权重和prompts文件夹的权限为安全权限

chmod -R 640 ckpts/t2i/
chmod -R 640 prompts/

执行命令:

# 使用算法优化
python inference_hydit.py \
       --path ${path} \
       --device_id 0 \
       --test_acc \
       --prompt_file "prompts/hpsv2_benchmark_prompts.json" \
       --prompt_file_type hpsv2 \
       --max_num_prompts 0 \
       --info_file_save_path ./image_info_hpsv2.json \
       --save_result_path ./results_hpsv2 \
       --use_attentioncache \
       --input_size 1024 1024 \
       --seed 42 \
       --infer_steps 100

参数说明:

  • path:权重路径,包含clip_text_encoder、model、mt5、sdxl-vae-fp16-fix、tokenizer的权重及配置文件。
  • device_id:推理设备ID。
  • test_acc:使用 --test_acc 开启prompt_file列表中的图像生成,用于性能/精度测试。
  • prompt_file:用于图像生成的文字描述提示的列表文件路径。
  • prompt_file_type:prompt文件类型,用于指定读取方式,可选范围:plain,parti,hpsv2。默认值为plain。
  • max_num_prompts:限制prompt数量为前X个,0表示不限制。
  • info_file_save_path:生成图像信息的json文件路径。
  • save_result_path:生成图像的存放目录。
  • use_attentioncache:使用 --use_attentioncache 开启算法策略优化的测试。
  • input_size:生成的图像尺寸,宽高要求是8的倍数。
  • seed:设置随机种子,默认值为42。
  • infer_steps:推理迭代步数,默认值为100。

执行完成后在./results_hpsv2目录下生成推理图像。在当前目录下生成一个image_info_hpsv2.json文件,记录着图像和prompt的对应关系,并在终端显示推理时间。

  1. 计算精度指标
    1. CLIP-score

      python clip_score.py \
             --device=cpu \
             --image_info="image_info_parti.json" \
             --model_name="ViT-H-14" \
             --model_weights_path="./CLIP-ViT-H-14-laion2B-s32B-b79K/open_clip_pytorch_model.bin"
      

      参数说明:

      • device: 推理设备,默认为"cpu",如果是cuda设备可设置为"cuda"。
      • image_info: 上一步生成的image_info_parti.json文件。
      • model_name: Clip模型名称。
      • model_weights_path: Clip模型权重文件路径。

      clip_score.py脚本可参考SDXL,执行完成后会在屏幕打印出精度计算结果。

    2. HPSv2

      python hpsv2_score.py \
            --image_info="image_info_hpsv2.json" \
            --HPSv2_checkpoint="./HPS_v2_compressed.pt" \
            --clip_checkpoint="./CLIP-ViT-H-14-laion2B-s32B-b79K/open_clip_pytorch_model.bin"
      

      参数说明:

      • image_info: 上一步生成的image_info_hpsv2.json文件。
      • HPSv2_checkpoint: HPSv2模型权重文件路径。
      • clip_checkpointh: Clip模型权重文件路径。

      hpsv2_score.py脚本可参考SDXL,执行完成后会在屏幕打印出精度计算结果。

五、推理结果参考

HunyuanDiT性能 & 精度数据

硬件形态 cpu规格 分辨率 batch size 迭代次数 优化方式 平均耗时 CLIP-score HPSv2-score
Atlas 800I A2(8*32G) 64核(arm) 1024×1024 1 100 等价优化 42.413s 0.339 0.2843779
Atlas 800I A2(8*32G) 64核(arm) 1024×1024 1 100 算法优化 25.373s 0.339 0.2825251

注意:性能测试需要独占npu和cpu

声明

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