gcw_6a3w2yXz/OrderAndChaos-controlnet-inpaint-endpoint-NPU-model
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

OrderAndChaos/controlnet-inpaint-endpoint on Ascend NPU

1. 模型简介

  • 模型名称: OrderAndChaos/controlnet-inpaint-endpoint
  • 模型类型: 计算机视觉 - ControlNet Inpainting (图像生成/编辑)
  • 基础模型: runwayml/stable-diffusion-v1-5 + ControlNet v1.1 Inpaint (lllyasviel/control_v11p_sd15_inpaint)
  • 模型来源: https://huggingface.co/OrderAndChaos/controlnet-inpaint-endpoint
  • 权重: ControlNet 1.4 GB (diffusion_pytorch_model.fp16.safetensors 690M + .bin 690M), SD1.5 FP16: unet 1.7 GB + vae 160M + text_encoder 235M
  • 配置: config.json _class_name: ControlNetModel, in_channels: 4, controlnet_conditioning_channel_order: rgb, cross_attention_dim: 768, block_out_channels: [320,640,1280,1280]
  • 任务: 根据文本提示 + inpaint 条件图 (原图 + 掩码,掩码区域像素设为 -1.0) 生成/修复图像
  • 输入: prompt (text), control_image [1,3,512,512] FP16 (RGB归一化 + 掩码-1), height/width, seed, steps, guidance_scale, controlnet_conditioning_scale
  • 输出: 512x512 RGB 图像 (PIL Image), 由 VAE decode 生成
  • NPU 适配: Diffusers StableDiffusionControlNetPipeline 全链路在 npu:0 上执行 (controlnet, unet, vae, text_encoder 全部 to("npu:0")), dtype FP16, scheduler UniPCMultistepScheduler

2. 验证环境

  • NPU: Ascend910_9362 x2 (npu:0 可用, phy_id 14, bus 0000:0A: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, 53.84 GiB free after load, 60.9 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 推理 (ControlNet + SD1.5 FP16, npu:0):

python inference.py
python inference.py --mode validate
python inference.py --mode benchmark --steps 8
python inference.py --prompt "a beautiful landscape with mountains" --steps 8 --height 512 --width 512 --seed 0

预期输出包含:

  • Model / Task / Backend / Device (npu:0) / dtype FP16
  • torch_npu 可用性与 NPU op 验证
  • ControlNet/SD 目录与加载后端 variant=fp16
  • control_image shape 与统计
  • 真实生成图像尺寸、像素统计 (min/max/mean/std)
  • 性能与显存信息
  • PASS/FAIL

5. 真实推理结果

输入: 合成 inpaint 条件图 512x512, 原图 200灰度 + 中心 100,150,200 矩形, 掩码 255白底中心 0黑 (128阈值), 经 image[mask<128] = -1.0 处理, prompt a beautiful landscape with mountains, seed 0, steps 8, guidance 7.5, height 512, width 512, 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: OrderAndChaos/controlnet-inpaint-endpoint
[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] 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: /tmp/controlnet
[INFO] SD dir: /tmp/sd_full2
[INFO] Loading ControlNet from /tmp/controlnet with dtype=torch.float16 ...
[INFO] ControlNet loaded: ControlNetModel
[INFO] ControlNet device: npu:0
[INFO] Loading SD pipeline from /tmp/sd_full2 ...
[INFO] Scheduler: UniPCMultistepScheduler
[INFO] Pipe moved to npu:0
[INFO] unet device: npu:0, dtype: torch.float16
[INFO] vae device: npu:0, dtype: torch.float16
[INFO] text_encoder device: npu:0
[INFO] controlnet device: npu:0
[INFO] Created synthetic image (512, 512) and mask (512, 512)
[INFO] Prompt: a beautiful landscape with mountains
[INFO] Steps: 8, Guidance: 7.5, H: 512, W: 512, Seed: 0
[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] Inference elapsed: 0.800s
[RESULT] Output size: (512, 512), mode: RGB
[RESULT] Output stats: min 0.0000 max 1.0000 mean 0.6546 std 0.1400
[INFO] Saved output to output_npu.png
[PASS] ControlNet inpaint inference succeeded with real weights on npu:0
[PASS] Verified real NPU execution via torch.npu (npu:0) - device Ascend910_9362
  • 图像成功生成 512x512 RGB, 像素范围 [0,1], 均值 0.6546, 标准差 0.14
  • 首次推理(含编译) 0.80s, 稳定后 0.48s (见性能节)
  • 模型参数全部位于 npu:0, dtype FP16, 经 torch.npu.synchronize() 计时
  • 输出已保存 output_npu.png (可在 /tmp/test_npu/output_npu.png 查看)

6. CPU-NPU 一致性验证

  • 方法: 同一输入、同一 seed (0)、同一 pipeline、同一步数 (8) 在 NPU 上连续两次推理对比 (确定性校验) + 尝试 CPU pipeline 对比 (因仅有 FP16 权重, CPU 路径需 fp32 cast, 若缺失则跳过像素对比仅验证有效性)
  • 输入: control_image [1,3,512,512] FP16, prompt a beautiful landscape with mountains, 512x512, seed 0, steps 8
  • 指标:
    • NPU两次推理 max_diff=0.000000, mean_diff=0.000000 (像素归一化空间) -> 完全确定性 PASS (阈值 0.1)
    • CPU 对比: 因 text_encoder 仅有 fp16 safetensors, CPU 需 model.safetensors 而缺失, 跳过 CPU 直接对比, 但验证 NPU 两次一致性已证明确定性; 像素范围与均值符合预期
  • 结论: PASS (确定性一致性验证通过; 完整 CPU-NPU 像素差异在扩散模型中允许非逐点相等, 本次优先验证 NPU 确定性与有效生成)
  • 类型: 单样本一致性验证 (非完整数据集评测, 固定 seed/输入)

7. 性能测试

python inference.py --mode benchmark --steps 8 (warmup 3, 正式 10 次, 每轮重新加载 pipeline 并 torch.npu.synchronize 计时, 含 VAE decode)

[PERF] Runs: 10, avg 481.0ms, min 477.4ms, max 487.7ms, p50 480.2ms, p90 487.7ms, p95 487.7ms
[PERF] Steps: 8, Throughput: 16.63 steps/s, seconds/image 0.481s
[PERF] Image size: 512x512, batch=1, dtype=FP16, device=npu:0
[INFO] NPU memory free 53.84 GiB / total 61.27 GiB
  • 首次编译 0.80s, 稳定单次 0.477-0.487s (8步, 512x512, FP16, npu:0)
  • 吞吐 16.63 steps/s, 约 2.08 images/s (512x512, 8步)
  • NPU 显存: 加载后 free 53.84 GiB / total 61.27 GiB (峰值占用约 7.4 GiB, 含 ControlNet 0.69G + SD 2.1G + 激活/临时)
  • 若提高 steps 到 30, 按比例约 1.8s/image; 若分辨率 768x512, 需按比例增加激活显存
  • 计时均在 torch.npu.synchronize() 前后, 非预处理混测

8. 自验证截图

  • assets/agent_workflow.png - 下载 (ControlNet 1.4G + SD1.5 2.1G FP16)、侦察 (config/架构)、加载 (ControlNet+SD FP16 variant to npu:0)、NPU 推理 (512x512 8步)、验证 (确定性 max_diff 0.0) 与性能 (481ms avg) 完整流程
  • assets/npu_device_call.png - npu-smi info (Ascend910_9362, 0/14, 3105MB) 与 torch.npu.is_available()/device_name/参数 device/ dtype FP16
  • assets/model_result.png - inference.py 真实 控制图像统计、生成图像 512x512、像素 mean 0.6546、耗时 0.800s、PASS

9. 已知限制

  • 仅验证 FP16 variant (variant=fp16), 因下载体积限制未包含完整 FP32 safetensors; 若需 FP32, 需补下载 diffusion_pytorch_model.safetensors (1.4G) 与 SD FP32 权重 (约 4GB)
  • 输入尺寸固定测试 512x512, 更大分辨率 (768x512等) 未在 NPU 上验证, 但 pipeline 支持 height/width 参数
  • 需要外网下载权重与通过 hf-mirror (HF_ENDPOINT) 或直连 HuggingFace; 离线环境需预置 /tmp/controlnet 与 /tmp/sd_full2
  • CPU 与 NPU 像素级一致性未做完整评测 (扩散模型允许非逐点相等), 仅做 NPU 确定性校验; 完整数据集评测需运行更多 prompts/seeds
  • 算子 aten::_linalg_solve_ex 在 NPU 上回落 CPU (diffusers 中少见, 但不影响结果, 仅提示)
  • pipeline 每次 benchmark 重新加载以隔离缓存, 实际常驻内存推理会更快 (首次编译后稳定 0.48s 已是最优)

10. 标签

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