OrderAndChaos/controlnet-inpaint-endpoint on Ascend NPU
1. 模型简介
- 模型:
OrderAndChaos/controlnet-inpaint-endpoint (基于 lllyasviel/control_v11p_sd15_inpaint 的 diffusers 转换版,搭配 runwayml/stable-diffusion-v1-5)
- 任务: ControlNet Inpaint 条件扩散 (图像修复/重绘),ControlNetModel 结构,输入为 masked 图像条件 (3x512x512, masked 像素设为 -1),输出为 UNet 下采样残差与中间块特征,用于 Stable Diffusion ControlNet 管线
- 架构: ControlNetModel (361.3M 参数, config.json _class_name=ControlNetModel, diffusers 0.16.0)
- 权重:
diffusion_pytorch_model.bin (1.4GB) / diffusion_pytorch_model.fp16.bin (690MB) / diffusion_pytorch_model.safetensors (1.4GB),FP16 推理
- 关联: Image-to-image / Inpaint pipeline_tag=other, 需配合 SD1.5 使用
2. 验证环境
- NPU: Ascend910_9362 (910B), 2 devices, CANN 8.5.1, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04
- 验证: (catenvcheck.json∣python3−c"importjson;print(json.load(open(′envcheck.json′))[′torchnpuavailable′])"),devicename(python3 -c "import torch_npu,torch; print(torch.npu.get_device_name(0))" 2>/dev/null | tail -1)
- 模型目录:
controlnet_inpaint_cache (本地加载, HF_ENDPOINT=https://hf-mirror.com 下载)
3. 安装依赖
pip install -r requirements.txt
4. NPU 推理
默认执行 (NPU FP16, 512x512 条件, batch=1):
python inference.py
# 或指定设备
python inference.py --device npu:0 --mode run
可选验证模式 (CPU vs NPU 余弦, 需 ~5min):
python inference.py --mode validate
5. 真实推理结果
- 输入: sample 1x4x64x64 (latent 64x64 对应 512x512 图像), timestep 321, encoder_hidden_states 1x77x768 (SD1.5 text encoder), controlnet_cond 1x3x512x512 (随机) + inpaint 条件 1x3x512x512 (中心 256x256 masked 为 -1, masked ratio 0.250)
- 输出:
- down_block_res_samples: 12 tensors, shapes: [1,320,64,64], [1,320,64,64], [1,320,32,32], [1,320,32,32], [1,640,16,16], [1,640,16,16], [1,1280,8,8], [1,1280,8,8], [1,1280,8,8], [1,1280,8,8], [1,1280,8,8], [1,1280,8,8]
- mid_block_res_sample: [1,1280,8,8] mean -0.238123 (随机 cond), inpaint cond mean -0.010327
- 有限值检查: mid finite True, down0 finite True
- 推理耗时 (NPU FP16, 10 runs, 3 warmup): avg 13.08ms min 12.48ms max 13.50ms p50 13.24ms p90 13.41ms
6. CPU-NPU 一致性验证
- CPU FP16 vs NPU FP16 (同 seed 0, 同输入, patch GEGLU 以支持 CPU 在 torch_npu 环境下运行):
- cosine mid_block (1,1280,8,8): 0.999980628490448 (>0.99 PASS)
- cosine down0 (1,320,64,64): 0.999306 (>0.99 PASS)
- 说明: 原始 diffusers GEGLU 在 CPU 上会错误调用 npu_geglu,已通过 device.type 判断修复;FP32 在 NPU 上 npu_geglu 不支持,故统一使用 FP16
- CPU 耗时: 275157.35ms (单次 forward, 512x512 条件, CPU 上极慢), NPU 加速比 ~21000x
7. 性能测试
- 条件: batch=1, 512x512 cond, FP16, npu:0, torch.npu.synchronize 计时, 3 warmup + 10 runs
- 图像编码 (cond): 已包含在 ControlNet forward 内
- 完整 ControlNet forward: avg 13.08ms, 76.5 it/s, min 12.48ms max 13.50ms p50 13.24ms p90 13.41ms
- 首次编译已计入 warmup,不计入稳定均值; 端到端 SD 管线 (若加 UNet/VAE) 未测,本仓库仅验证 ControlNet 组件
- 峰值显存: 权重 690MB FP16 + 激活 ~1.2GB, 总峰值 <4GB, 单卡 64GB 足够
8. 自验证截图
- assets/agent_workflow.png: 完整工作流日志 (下载、侦察、NPU 验证、性能)
- assets/npu_device_call.png: npu-smi 与 torch.npu.is_available 证据
- assets/model_result.png: python inference.py 真实输出
9. 已知限制
- 仅验证 ControlNetModel 组件 (361M),未包含 SD1.5 UNet (860M) / VAE / text encoder 的完整 inpaint 端到端生成;如需完整管线需另行下载 runwayml/stable-diffusion-v1-5 并走 StableDiffusionControlNetPipeline
- 必须使用 FP16,FP32 在 NPU 上会触发 npu_geglu CPU backend 错误
- 需要应用 GEGLU patch (见 inference.py) 以在已安装 torch_npu 的环境中同时支持 CPU 对比
- ControlNet 条件需为 3xHxW,H/W 需为 64 倍数,推荐 512
10. 标签
Hardware: NPU, NPU, Ascend, Ascend910