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 |
本模型使用的优化手段如下:
表 2 版本配套表
| 配套 | 版本 | 环境准备指导 |
|---|---|---|
| Python | 3.10.2 | - |
| torch | 2.9.0 | - |
注意:
# 增加软件包可执行权限,{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# 增加软件包可执行权限,{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安装pytorch框架 版本2.9.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# 若环境镜像中没有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_PATHgit clone https://modelers.cn/MindIE/hunyuan_dit.gitpip install -r requirements.txt
# 若要使用hpsv2验证精度,则还需要按照以下步骤安装hpsv2
git clone https://github.com/tgxs002/HPSv2.git
pip install -e HPSv2https://huggingface.co/Tencent-Hunyuan/HunyuanDiT-v1.2/tree/main/t2i{
"_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
}|----hunyuan_dit
| |---- ckpts
| | |---- t2i
| | | |---- clip_text_encoder
| | | |---- model
| | | | |---- config.json
| | | | |---- 模型权重
| | | |---- mt5
| | | |---- sdxl-vae-fp16-fix
| | | |---- tokenizerpath="ckpts/t2i"修改权重和prompts文件夹的权限为安全权限
chmod -R 640 ckpts/t2i/
chmod -R 640 prompts/python inference_hydit.py \
--path ${path} \
--device_id 0 \
--prompt "渔舟唱晚" \
--input_size 1024 1024 \
--seed 42 \
--infer_steps 100参数说明:
执行完成后在results目录下生成一张推理图像。
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/example_prompts.txt" \
--input_size 1024 1024 \
--seed 42 \
--infer_steps 100参数说明:
执行完成后在results目录下生成推理图像,图像生成顺序与prompt顺序保持一致,并在终端显示推理时间。
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/example_prompts.txt" \
--use_attentioncache \
--input_size 1024 1024 \
--seed 42 \
--infer_steps 100参数说明:
执行完成后在results目录下生成推理图像,图像生成顺序与prompt顺序保持一致,并在终端显示推理时间。
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/example_prompts.txt" \
--use_attentioncache \
--input_size 1024 1024 \
--batch_size 2 \
--seed 42 \
--infer_steps 100参数说明:
执行完成后在results目录下生成推理图像,图像生成顺序与prompt顺序保持一致,并在终端显示推理时间。
由于生成的图像存在随机性,提供两种精度验证方法:
注意:由于要生成的图片数量较多,进行完整的精度验证需要耗费很长的时间。
# 下载Parti数据集
wget https://raw.githubusercontent.com/google-research/parti/main/PartiPrompts.tsv --no-check-certificate建议将PartiPrompts.tsv和hpsv2_benchmark_prompts.json文件放到prompts/路径下。
# 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权重放到当前路径
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参数说明:
执行完成后在./results_parti目录下生成推理图像。在当前目录下生成一个image_info_parti.json文件,记录着图像和prompt的对应关系,并在终端显示推理时间。
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参数说明:
执行完成后在./results_hpsv2目录下生成推理图像。在当前目录下生成一个image_info_hpsv2.json文件,记录着图像和prompt的对应关系,并在终端显示推理时间。
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"参数说明:
image_info_parti.json文件。clip_score.py脚本可参考SDXL,执行完成后会在屏幕打印出精度计算结果。
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_hpsv2.json文件。hpsv2_score.py脚本可参考SDXL,执行完成后会在屏幕打印出精度计算结果。
| 硬件形态 | 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