本指导记录了 paint-by-example 在昇腾 NPU 上的完整迁移过程:推理,全参训练,Lora微调全部跑通)。
| 业务背景 | 版本 |
|---|---|
| 硬件环境 | Atlas 900 A2 SuperPoD Compute Node,昇腾 910B,单卡即可(验证使用 1 卡,容器可见 8 卡) |
| Python | 3.9(conda 环境 pbe) |
| Torch | 2.5.1 |
| Torch NPU | 2.5.1.post1 |
| CANN | 8.5.1(npu-smi 25.5.1) |
设备支持:
paint-by-example官网地址: https://github.com/Fantasy-Studio/Paint-by-Example 模型文件下载链接:https://modelscope.cn/models/iic/cv_stable-diffusion_paint-by-example/files
github地址 https://github.com/Fantasy-Studio/Paint-by-Example
为了适配NPU,修改如下几个文件
/workspace/Paint-by-Example-main
├── main.py
├── ldm/models/diffusion/ddpm.py
├── train_pbe_lora_npu.py
└── scripts/inference.py
## 基本功能测试
推理
```bash
python scripts/inference.py \
--plms --outdir results \
--config configs/v1.yaml \
--ckpt checkpoints/model.ckpt \
--image_path examples/image/example_1.png \
--mask_path examples/mask/example_1.png \
--reference_path examples/reference/example_1.jpg \
--seed 321 \
--scale 5
训练
```bash
torchrun main.py
--base configs/v1.yaml \
--pretrained_model /workspace/Paint-by-Example-main/pretrained_models/sd-v1-4-modified-9channel.ckpt \
--scale_lr False \
--name debug_10img_run \
--logdir logs/debug_10img \
--trainer.precision 32 -t
期望运行结果:
推理
Enjoy.
训练
923 M Trainable params
386 M Non-trainable params
1.3 B Total params
5,240.561 Total estimated model params size (MB)
904 Modules in train mode
509 Modules in eval mode
/root/miniconda3/envs/pbe/lib/python3.9/site-packages/pytorch_lightning/trainer/connectors/data_connector.py:424: The 'train_dataloader' does not have many workers which may be a bottleneck. Consider increasing the value of the num_workers argumenttonum_workers=191in theDataLoaderto improve performance. /root/miniconda3/envs/pbe/lib/python3.9/site-packages/pytorch_lightning/loops/fit_loop.py:298: The number of training batches (3) is smaller than the logging interval Trainer(log_every_n_steps=5). Set a lower value for log_every_n_steps if you want to see logs for the training epoch. /root/miniconda3/envs/pbe/lib/python3.9/site-packages/pytorch_lightning/trainer/connectors/data_connector.py:424: The 'val_dataloader' does not have many workers which may be a bottleneck. Consider increasing the value of thenum_workers argument to num_workers=191 in the DataLoader to improve performance.
Epoch 0: 0%| | 0/3 [00:00<?, ?it/s][DEBUG] x_start: npu:0, t: npu:0, logvar: npu:0
/root/miniconda3/envs/pbe/lib/python3.9/site-packages/torch_npu/utils/storage.py:38: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly. To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()
if self.device.type != 'cpu':
.Data shape for DDIM sampling is (4, 4, 64, 64), eta 1.0
Running DDIM Sampling with 200 timesteps
DDIM Sampler: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 200/200 [00:28<00:00, 6.93it/s]
Epoch 0: 33%|████████████████████ | 1/3 [00:52<01:44, 0.02it/s, v_num=0, train/loss_simple_step=0.236, train/loss_vlb_step=0.00272, train/loss_step=0.236, global_step=0.000, lr_abs=1e-11][DEBUG] x_start: npu:0, t: npu:0, logvar: npu:0
Data shape for DDIM sampling is (4, 4, 64, 64), eta 1.0
Running DDIM Sampling with 200 timesteps