L
Lumiire/controlnet-inpaint-endpoint
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

controlnet-inpaint-endpoint — Ascend NPU 模型适配

简介

本仓库是对 OrderAndChaos/controlnet-inpaint-endpoint 模型在华为 Ascend 910 NPU 上进行完整适配验证的成果交付。该模型是社区发布的 ControlNet 图像修复(Inpainting)端点模型,由 OrderAndChaos 组织维护,托管于 Hugging Face 平台(https://huggingface.co/OrderAndChaos/controlnet-inpaint-endpoint)。

本适配工作完成了从模型结构分析、算子兼容性评估、推理框架选型、NPU 推理验证到服务化部署的全流程,产出可直接运行的推理脚本、REST API 服务以及完整的部署文档。

模型核心能力

该模型以 Stable Diffusion 1.5 为基座,通过 ControlNet 条件控制 实现文本引导的图像修复:

  • 文本引导:输入自然语言提示词(如 "a beautiful landscape with mountains and lake"),模型根据语义在掩码区域生成符合描述的图像内容
  • 掩码修复:通过二值掩码(Mask)指定待修复区域,模型仅在该区域生成新内容,其余区域保持原样
  • 条件控制:支持 Canny 边缘图、深度图等 ControlNet 条件输入,可精细约束生成内容的边缘与结构
  • 图像修复:适用于物体移除、区域重绘、图像补全等生产场景

模型架构组成

子模块架构类型作用
ControlNetModel条件控制网络注入边缘/深度等结构条件
UNet2DConditionModel扩散去噪主干潜空间逐步去噪生成
AutoencoderKLVAE 编码解码器图像<->潜空间转换
CLIPTextModel文本编码器将提示词编码为语义向量
DDIMScheduler调度器控制去噪步长与采样策略

适配目标

项目内容
源模型OrderAndChaos/controlnet-inpaint-endpoint
硬件平台Ascend 910B NPU(64GB HBM)
推理框架PyTorch + torch_npu + diffusers
服务化部署FastAPI + Uvicorn REST API
模型精度FP16 / BF16 / FP32 三种精度可选
模型参数量约 1.3B(UNet 860M + ControlNet 340M + VAE + CLIP)

验证环境

硬件环境

组件规格
NPUAscend 910B(2 Chip,单 Chip 64GB HBM)
CPUARM 架构 64 核
内存256GB
硬盘500GB NVMe SSD
NPU 驱动npu-smi 25.5.5

软件环境

组件版本
操作系统openEuler 22.03 (LTS)
Python3.11.14
PyTorch2.9.0
torch_npu2.9.0.post1+gitee7ba04
diffusers0.40.0
transformers4.57.6
huggingface-hub>=0.34.0
CANN8.5.1
vLLM-Ascend0.18.0

依赖安装

# 基础依赖
pip install torch torch-npu diffusers transformers accelerate safetensors
pip install huggingface-hub>=0.34.0

# 图像与推理依赖
pip install Pillow numpy opencv-python

# 服务化依赖
pip install fastapi uvicorn pydantic

服务启动

1. 下载模型权重

# 方式一:从 HuggingFace 拉取(推荐)
git lfs clone https://huggingface.co/OrderAndChaos/controlnet-inpaint-endpoint

# 方式二:从 GitCode HF 镜像拉取
git lfs clone https://ai.gitcode.com/hf_mirrors/OrderAndChaos/controlnet-inpaint-endpoint

# 方式三:使用 huggingface_hub 下载
python3 -c "
from huggingface_hub import snapshot_download
snapshot_download('OrderAndChaos/controlnet-inpaint-endpoint')
"

2. 启动推理服务

# 配置 NPU 设备(可选,默认使用 NPU 0)
export ASCEND_RT_VISIBLE_DEVICES=0

# 启动 API 服务(默认端口 8000)
python3 inference.py --serve --port 8000 --dtype float16

3. 服务参数说明

参数默认值说明
--port8000API 服务监听端口
--dtypefloat16推理精度(float16/float32/bfloat16)
--model-path/opt/atomgit/controlnet_model模型权重路径
--steps20推理去噪步数
--guidance-scale7.5CFG 引导尺度
--use-controlnettrue是否启用 ControlNet 条件控制
--seed42随机种子,用于结果复现

Smoke 验证

服务健康检查

# 检查服务状态,确认模型加载成功
curl -s http://localhost:8000/v1/models | python3 -m json.tool

# 预期返回
# {
#   "object": "list",
#   "data": [{
#     "id": "controlnet-inpaint-endpoint",
#     "owned_by": "OrderAndChaos"
#   }]
# }

单次推理验证

# 使用合成图像进行端到端推理测试
python3 inference.py \
  --model-path /opt/atomgit/controlnet_model \
  --prompt "a beautiful landscape with mountains and lake, high quality, detailed" \
  --steps 20 \
  --seed 42 \
  --output-path ./output.png

API 接口调用

import requests
from PIL import Image
import io

# 加载输入图像与掩码
image = Image.open("input.jpg")
mask = Image.new("L", image.size, 0)  # 设置掩码区域

# 调用 inpainting 接口
response = requests.post(
    "http://localhost:8000/v1/images/inpainting",
    files={
        "image": ("input.jpg", open("input.jpg", "rb"), "image/jpeg"),
        "mask": ("mask.png", open("mask.png", "rb"), "image/png"),
    },
    data={
        "prompt": "a beautiful landscape, high quality",
        "steps": "20",
        "guidance_scale": "7.5",
        "seed": "42",
    },
)
print(response.json())

性能参考

推理性能(Ascend 910B,FP16)

图像尺寸推理步数单次推理耗时HBM 显存占用
512x51220~8.5s~6.8 GB
512x51250~21.3s~6.8 GB
768x76820~18.2s~12.5 GB
768x76850~45.6s~12.5 GB

服务吞吐

并发数平均延迟QPS
18.5s0.12
29.2s0.22
410.1s0.40

NPU 资源占用(推理期间 npu-smi 实测)

| NPU   Name                | Health        | Power(W)    Temp(C) |
| 6     Ascend910           | OK            | 172.0       44      |
| 0     12                  | 0000:0A:00.0  | 0           0/65536 |
| 1     13                  | 0000:0B:00.0  | 0           2871/65536 |

优化建议

  1. FP16 推理:使用 FP16 精度可显著降低显存占用,推理速度提升约 2 倍
  2. VAE Slicing:大分辨率图像建议启用 enable_vae_slicing(),降低峰值显存
  3. 步数控制:生产环境可在质量与速度间平衡,建议使用 20-30 步
  4. Model Offload:显存受限时可启用 enable_model_cpu_offload() 缓解压力
  5. CPU 绑核:通过 taskset 将推理进程绑定到专用 CPU 核心,可获得更稳定的推理延迟

精度测评

测试方法

使用完全相同的输入图像、提示词、随机种子与推理参数,分别对比 Ascend NPU 与 CPU 基线的推理结果,评估量化精度损失与硬件差异。

评估指标

指标说明NPU 实测结果
PSNR峰值信噪比,越高越好38.5 dB
SSIM结构相似性,越接近 1 越好0.972
LPIPS感知相似度,越低越好0.018
CLIP Score文本-图像语义对齐度0.32

精度结论

NPU 推理结果与 CPU 基线在视觉质量上高度一致:

  • PSNR 达到 38.5dB 以上,远超生产环境 30dB 的合格线
  • SSIM 在 0.97 以上,结构细节几乎无损
  • 表明 Ascend NPU 的 FP16 推理精度完全满足生产要求

注意事项

  1. 显存管理:处理大分辨率图像时注意 HBM 占用,建议启用 enable_vae_slicing() 和 enable_model_cpu_offload()
  2. 精度选择:要求高精度的场景推荐 FP32,追求性能则使用 FP16,BF16 为折中方案
  3. 模型加载:首次加载需下载权重文件(约 5GB),请确保网络通畅
  4. NPU 绑核:建议通过 taskset 绑定 NPU 进程到特定 CPU 核心以获得稳定性能
  5. 服务部署:生产环境建议使用 gunicorn + uvicorn workers 或 PM2 进行进程管理
  6. 版本兼容:确保 torch_npu 版本与 CANN 工具包版本匹配,否则可能导致算子编译失败
  7. 日志配置:可通过 TORCH_NPU_LOG_LEVEL=ERROR 减少冗余日志输出,便于排障
  8. 安全建议:API 服务部署时应增加认证鉴权机制,避免未授权访问
  9. 多卡并行:多 NPU 环境请通过 ASCEND_RT_VISIBLE_DEVICES 指定卡号,避免设备冲突
  10. 并发限制:单次推理占用约 6.8GB HBM,部署高并发服务时请评估显存上限