1、如何使用相同的权重,达成在 MindSpore 910* 上推理结果与 PyTorch Diffusers A100 一致
- diffuser中openclip pool层 text_projection 操作实现与官方stability-ai不一致,本处follow官方实现; text_projection在两边实现中相差了一个转置操作,自行转换的时候需要检查注意下,本处提供的模型转换小工具中已带有转置操作
-
默认超参保持一致 (sampler、sample step、noise input、cfg 等)
-
ms中配置以下参数 (以 EulerAncestralSampler 为例):
--discretization DiffusersDDPMDiscretization
--precision_keep_origin_dtype True
python demo/sampling_without_streamlit.py \ --config configs/inference/sd_xl_base.yaml \ --weight checkpoints/sd_xl_base_1.0_ms.ckpt \ --prompt "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" \ --sampler EulerAncestralSampler \ --sample_step 40 \ --guidance_scale 5.0 \ --discretization DiffusersDDPMDiscretization \ --precision_keep_origin_dtype True
-
在diffusers运行过程中保存输入latent为
.npy文件
, 通过--init_latent_path
接口控制输入latent noise 固定输入噪声一致; -
(可选, 仅带随机采样的sampler需要配置) 在diffusers运行过程中保存每个sample step添加的随机噪声为
.npy文件
, 通过--init_noise_scheduler_path
配置固定每个sample step添加的随机噪声一致,当前仅EulerA采样器
生效;
2、如何使用相同的权重,达成在 MindSpore 910* 上推理结果与 PyTorch Stability-AI A100 一致
-
默认超参保持一致 (sampler、sample step、noise input、cfg 等)
python demo/sampling_without_streamlit.py \ --config configs/inference/sd_xl_base.yaml \ --weight checkpoints/sd_xl_base_1.0_ms.ckpt \ --prompt "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" \
- 在stability-ai代码运行过程中保存输入latent为
.npy文件
, 通过--init_latent_path
接口控制输入latent noise 固定输入噪声一致;
3、如何达成在 MindSpore 910* 上 vanilla finetune 训练结果与 PyTorch Diffusers A100 相近
⚠️ 注意:在了解了风险之后如果仍然希望使用fp16
权重进行训练,那么可以配合 fp32 vae cache 使用 或 配合vae-fp16-fix权重使用
该问题是因为网络原因无法连接到 huggingface,可以尝试手动下载后指定本地路径解决, issue 134;
(1) 在huggingface上下载clip patch
model:
target: gm.models.diffusion.DiffusionEngine
params:
...
conditioner_config:
target: gm.modules.GeneralConditioner
params:
emb_models:
- is_trainable: False
input_key: txt
target: gm.modules.embedders.modules.FrozenCLIPEmbedder
params:
layer: hidden
layer_idx: 11
version: /path/to/clip-vit-large-patch14
- ...
Mindspore 2.2 之后的版本,框架内的 FlashAttention
只支持Ascend 910*, 不支持Ascend 910。如果需要在 Ascend 910 上使用 flash attention,可以借助基于 Mindspore 框架和昇腾 CANN 软件栈开发的 transformer 加速库 acctransformer。具体使用方法如下:
(1) 按照 acctransformer 的 FlashAttention2安装指南安装whl包。
git clone https://github.com/mindspore-ai/acctransformer.git
cd train
bash build.sh
pip install dist/acctransformer-1.0.0-py3-none-any.whl
(2) 把examples/stable_diffusion_xl/gm/modules/attention.py文件 Mindspore 框架内的 FlashAttention
改成 acctransformer 的,具体修改如下:
https://github.com/mindspore-lab/mindone/blob/master/examples/stable_diffusion_xl/gm/modules/attention.py#L18 https://github.com/mindspore-lab/mindone/blob/master/examples/stable_diffusion_xl/gm/modules/attention.py#L117 https://github.com/mindspore-lab/mindone/blob/master/examples/stable_diffusion_xl/gm/modules/attention.py#L147
# L18
- from mindspore.nn.layer.flash_attention import FlashAttention
+ from acctransformer.flash_attention.nn.layer.flash_attention import FlashAttention
...
# L117
- self.flash_attention = FlashAttention(head_dim=dim_head, head_num=heads, high_precision=True)
+ self.flash_attention = FlashAttention(head_dim=dim_head)
# L147
- out = self.flash_attention(q.to(ms.float16), k.to(ms.float16), v.to(ms.float16), mask.to(ms.uint8))
+ out = self.flash_attention(q.to(ms.float16), k.to(ms.float16), v.to(ms.float16), mask.to(ms.float16))