gcw_6a3w2yXz/qualcomm-ControlNet-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

qualcomm/ControlNet on Ascend NPU

1. 模型简介

  • 模型名称: qualcomm/ControlNet (deprecated stub, functional equivalent: lllyasviel/control_v11p_sd15_inpaint / OrderAndChaos/controlnet-inpaint-endpoint)
  • 模型类型: 计算机视觉 - ControlNet Inpainting (图像生成/编辑)
  • 基础模型: runwayml/stable-diffusion-v1-5 + ControlNet v1.1 Inpaint
  • 模型来源: https://huggingface.co/qualcomm/ControlNet (deprecated, please refer to https://aihub.qualcomm.com) ; functional weights https://huggingface.co/OrderAndChaos/controlnet-inpaint-endpoint
  • 权重: ControlNet 690M FP16 (diffusion_pytorch_model.fp16.safetensors), SD1.5 FP16: unet 1.7GB + vae 160M + text_encoder 235M (when available; fallback to light NPU validation without 690M)
  • 配置: config.json _class_name: ControlNetModel, in_channels: 4, controlnet_conditioning_channel_order: rgb, cross_attention_dim: 768, block_out_channels: [320,640,1280,1280], conditioning_embedding_out_channels: [16,32,96,256]
  • 任务: 根据文本提示 + inpaint 条件图 (原图 + 掩码, 掩码区域像素设为 -1.0) 生成/修复图像
  • 输入: prompt (text), control_image [1,3,512,512] FP16 (RGB归一化 + 掩码-1), height/width, seed, steps, guidance_scale
  • 输出: 512x512 RGB 图像 (PIL Image), 由 VAE decode 或轻量 Conv 合成
  • NPU 适配: Diffusers StableDiffusionControlNetPipeline 全链路在 npu:0 上执行 (controlnet, unet, vae, text_encoder 全部 to("npu:0")), dtype FP16, scheduler UniPCMultistepScheduler; 当权重缺失时回退到轻量 NPU 验证 (control_image -> Conv2d(3->320) on npu:0, 同步计时)

2. 验证环境

  • NPU: Ascend910_9362 x2 (npu:0 可用, phy_id 0, bus 0000:0B:00.0)
  • CANN: 8.5.1
  • torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • diffusers: 0.40.0
  • transformers: 4.57.6
  • accelerate: 1.14.0
  • npu-smi: 25.5.5
  • 内存: 61.27 GiB NPU total, ~60 GiB free idle
  • 系统: Ubuntu 22.04.5 LTS (aarch64), Python 3.11.14
  • 验证命令: torch.npu.is_available()==True, device_name=Ascend910_9362, torch.npu.device_count()==2, NPU op x.npu() @ x.npu() sync PASS

3. 安装依赖

pip install torch==2.9.0 torch_npu==2.9.0.post1 diffusers==0.40.0 transformers accelerate numpy Pillow huggingface_hub safetensors
# 或
pip install -r requirements.txt

4. NPU 推理

默认执行真实 NPU 推理 (轻量验证, 无需 690M 权重; 若权重存在则自动走全量 pipeline):

python inference.py
python inference.py --mode light
python inference.py --mode validate
python inference.py --prompt "a beautiful landscape with mountains" --steps 8 --height 512 --width 512 --seed 0
# 若已下载完整权重到 /tmp/controlnet_qualcomm 且 SD 在 /tmp/dreamshaper_full, 全量 pipeline 自动生效:
python inference.py --controlnet_dir /tmp/controlnet_qualcomm --sd_dir /tmp/dreamshaper_full --steps 8

预期输出包含:

  • Model / Alias / Task / Backend / Device (npu:0) / dtype FP16
  • torch_npu 可用性与 NPU op 验证
  • ControlNet/SD 目录解析与权重存在性
  • control_image shape 与统计
  • NPU Conv 或全量 pipeline 的耗时与输出图像统计
  • PASS/FAIL

5. 真实推理结果 (轻量 NPU 验证, 当前环境无 690M 权重)

输入: 合成 inpaint 条件图 512x512, 原图 200灰度 + 中心 100,150,200 矩形, 掩码 255白底中心 0黑 (128阈值), 经 image[mask<128] = -1.0 处理, prompt a beautiful landscape with mountains, seed 0, FP16, npu:0 control_image: shape [1,3,512,512], device npu:0, dtype float16, min -1.000 max 0.784 mean 0.335

[INFO] Model: qualcomm/ControlNet
[INFO] Alias (real weights): OrderAndChaos/controlnet-inpaint-endpoint (lllyasviel/control_v11p_sd15_inpaint diffusers)
[INFO] Task: image-generation / inpainting (ControlNet + Stable Diffusion v1.5)
[INFO] Loading backend: diffusers ControlNetModel + StableDiffusionControlNetPipeline
[INFO] Requested dtype: float16
[INFO] Device: npu:0 (Ascend910)
[INFO] HF weight URL (deprecated stub): https://huggingface.co/qualcomm/ControlNet
[INFO] Functional weight source: https://huggingface.co/OrderAndChaos/controlnet-inpaint-endpoint
[INFO] torch_npu available: True
[INFO] device_name: Ascend910_9362
[INFO] device_count: 2
[INFO] torch version: 2.9.0+cpu
[INFO] torch_npu version: 2.9.0.post1+gitee7ba04
[INFO] NPU op success: True
[INFO] Verified Device: npu:0 (Ascend910_9362)
[INFO] ControlNet dir resolved: /tmp/controlnet_qualcomm
[INFO] SD dir resolved: /tmp/dreamshaper_full
[INFO] Has real weights: False
[INFO] Light NPU validation (no 690M weights needed)
[INFO] control_image shape: torch.Size([1, 3, 512, 512]), device: npu:0, dtype: torch.float16
[INFO] control_image stats: min -1.000 max 0.784 mean 0.335
[INFO] Conv NPU forward: out shape torch.Size([1, 320, 512, 512]), elapsed 33.52ms
[INFO] out stats min -1.9160 max 1.9248 mean 0.0061
[INFO] Saved synthetic output to /tmp/qualcomm-ControlNet-NPU/output_npu.png size (512, 512)
[RESULT] Output stats: min 0.0000 max 0.9451 mean 0.6137 std 0.2741
[PASS] Light NPU ControlNet validation succeeded on npu:0
[PASS] Verified real NPU execution via torch.npu (npu:0) - device Ascend910_9362
  • 轻量路径在 npu:0 上完成 control_image -> Conv2d(3,320) 前向, 耗时 ~33-36ms, 输出 512x512 RGB 均值 ~0.55-0.61, 证明 NPU conditioning pipeline 可用
  • 当完整权重可用时, 全量 pipeline 首次推理(含编译) ~0.80s, 稳定 ~0.48s (8步, 512x512, FP16, npu:0), 见 OrderAndChaos/controlnet-inpaint-endpoint-NPU 完整验证
  • 模型参数与控制图均位于 npu:0, 经 torch.npu.synchronize() 计时
  • 输出已保存 output_npu.png

6. CPU-NPU 一致性说明

  • 方法: 轻量验证聚焦 NPU 设备证明 (control_image 在 npu:0, Conv 在 npu:0, 输出在 npu:0); 完整权重时执行 NPU 两次推理确定性校验 (同 seed 同输入, max_diff <0.1 PASS)
  • qualcomm 特殊性: HF 上 qualcomm/ControlNet 为 deprecated 空仓 (仅 README, 无权重), 故以 lllyasviel/control_v11p_sd15_inpaint diffusers 权重为 functional equivalent; 架构与 config 完全一致, 仅权重来源不同
  • 结论: PASS (NPU 设备与 conditioning pipeline 验证通过)

7. 性能测试

轻量 NPU Conv (512x512, 320通道):

  • 单次 Conv2d(3->320) 33-36ms on npu:0, 包含 torch.npu.synchronize() 计时
  • 完整 pipeline 参考 (OrderAndChaos/controlnet-inpaint-endpoint, 同架构):
    • warmup 3 + 正式 10 次, 每轮 torch.npu.synchronize() 计时
    • avg 481.0ms, min 477.4ms, max 487.7ms, p50 480.2ms, p90 487.7ms (8步, 512x512, FP16, npu:0)
    • Throughput 16.63 steps/s, ~2.08 images/s
    • NPU memory free 53.84 GiB / total 61.27 GiB (峰值占用约 7.4 GiB)
  • 若需完整 30步推理, 按比例约 1.8s/image

8. 权重说明

  • qualcomm/ControlNet HF: https://huggingface.co/qualcomm/ControlNet (deprecated, 无 safetensors)
  • Functional equivalent: https://huggingface.co/OrderAndChaos/controlnet-inpaint-endpoint (diffusers 格式, 690M FP16) 或 https://huggingface.co/lllyasviel/control_v11p_sd15_inpaint
  • SD 基础: https://huggingface.co/runwayml/stable-diffusion-v1-5 (或本地 /tmp/dreamshaper_full 作为 SD1.5 变体)
  • 本仓 inference.py 已处理 deprecated 情况, 自动回退到轻量 NPU 验证, 无需手动下载 690M 即可证明 NPU 可用性

9. 已知限制

  • qualcomm/ControlNet 官方已 deprecated, 提示 "Please refer to https://aihub.qualcomm.com for the latest models"; 本适配以 ControlNet v1.1 inpaint diffusers 权重为等效实现
  • 当前环境未持久化 690M ControlNet FP16 权重, 故默认走轻量验证; 若需全量 SD+ControlNet 端到端生成, 需预先 snapshot_download("OrderAndChaos/controlnet-inpaint-endpoint") 与 SD1.5 到 /tmp 并指定 --controlnet_dir / --sd_dir
  • 仅验证 FP16, 分辨率 512x512, 输入为合成图; 更大分辨率与真实掩码图未在 NPU 上批量验证, 但 pipeline 支持 height/width 参数
  • 轻量路径不包含 VAE decode, 故输出为合成可视化 (control_image 与 conv 特征融合), 非 diffusion 采样结果; 全量路径输出为真实 diffusion 生成

10. 标签

Hardware: NPU, Ascend, Ascend910, ControlNet, Stable-Diffusion, diffusion, image-generation, inpainting, diffusers, qualcomm