atlasleong/controlnet-inpaint-endpoint-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

OrderAndChaos/controlnet-inpaint-endpoint 昇腾 NPU 适配

模型简介

OrderAndChaos/controlnet-inpaint-endpoint 是 ControlNet v1.1(control_v11p_sd15_inpaint) 的 diffusers 格式检查点,配合 Stable Diffusion v1.5 完成 图像局部重绘(image inpainting control)。 本仓库使用 torch_npu 在昇腾 Ascend 910B4-1 上完成真实端到端推理: ControlNet、UNet、VAE、TextEncoder 及全部中间张量均放置在 npu:0,禁止 CPU 静默回退。

  • 推理任务:image inpainting control
  • 引擎:torch_npu
  • 验收设备:npu:0
  • 采样器:UniPCMultistepScheduler(与上游 endpoint 保持一致)

原模型仓库 / 固定版本

  • 原仓库地址:https://huggingface.co/OrderAndChaos/controlnet-inpaint-endpoint
  • 固定版本(revision):f687f36ddb6403221e37e06a5ddc0ce66f89a906
  • 上游许可证:openrail(CreativeML OpenRAIL-M)
  • 基础模型:runwayml/stable-diffusion-v1-5(revision 451f4fe16113bff5a5d2269ed5ad43b0592e9a14,fp16)
  • 适配来源:diffusers 格式的 lllyasviel/control_v11p_sd15_inpaint

环境依赖

  • Python 3.11.14
  • CANN 8.5.1,驱动 25.5.1(npu-smi 25.5.1)
  • torch 2.9.0+cpu 与 torch_npu 2.9.0(由容器提供,不能用普通 PyPI 轮子替换)
  • NPU:Ascend910B4-1(8 卡),本适配使用 npu:0
  • 依赖安装使用阿里云镜像:https://mirrors.aliyun.com/pypi/simple/

安装依赖(在任务目录外创建带 --system-site-packages 的虚拟环境,仅安装非 torch 上层依赖):

python3 -m venv --system-site-packages /path/to/venv-controlnet-inpaint
source /path/to/venv-controlnet-inpaint/bin/activate
export PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
export PIP_TRUSTED_HOST=mirrors.aliyun.com
pip install -r requirements.txt

requirements.txt 固定版本(已实际解析验证):

diffusers==0.39.0
transformers==4.57.1
accelerate==1.6.0
safetensors==0.8.0
huggingface_hub==0.36.2
numpy==1.26.4
Pillow==12.3.0

分步推理操作流程

  1. 准备本地固定版本权重:
    • ControlNet:/work/pipeline/models/next8/controlnet-inpaint-endpoint(diffusion_pytorch_model.fp16.safetensors)
    • 基础模型:stable-diffusion-v1-5 本地 fp16 diffusers 快照
  2. 加载 CANN 环境:
    source /usr/local/Ascend/ascend-toolkit/set_env.sh
  3. 进入本仓库根目录,执行(默认 --device npu:0 --steps 20 --seed 0):
    MODEL_DIR=/path/to/controlnet-inpaint-endpoint \
    BASE_MODEL_DIR=/path/to/stable-diffusion-v1-5 \
    python inference.py --device npu:0 --steps 20 --seed 0
  4. 脚本校验 NPU 可用性、张量/模型放置,失败时返回非零退出码;结构化结果写入 logs/npu_result.json, 推理日志写入 logs/npu_inference.log,npu-smi 实时进程证据写入 logs/npu_device_call.log。
  5. 生成输出图像:assets/inpaint_output.png。

完整测试用例和输出

  • 模型版本:f687f36ddb6403221e37e06a5ddc0ce66f89a906
  • 测试输入:test_input/original.png + test_input/mask.png(512×512,来自固定模型快照,原为 lllyasviel/control_v11p_sd15_inpaint,CreativeML OpenRAIL-M)
  • 输入 SHA256:
    • original.png:6362b8d29696684d1ced03a8e1f90ee9ac71655270d657100f9b273da9d6e9db
    • mask.png:a1b30c1a38d55c35fef6e7cf3c1f45b6b880515fc7030e9122dd48a20091b4cb
  • 测试提示词:A blue cat sitting on a park bench
  • 负向提示词:lowres, bad anatomy, bad hands, cropped, worst quality
  • 执行命令:
    MODEL_DIR=/work/pipeline/models/next8/controlnet-inpaint-endpoint \
    BASE_MODEL_DIR=/work/pipeline/jobs/next8/controlnet-inpaint-endpoint/stable-diffusion-v1-5 \
    python inference.py --device npu:0 --steps 20 --seed 0
  • 严格加载证据:strict_load num_tensors=340 missing=0 unexpected=0 strict_ok=True
  • 设备放置:input_device=npu:0, model_device=npu:0, output_device=npu:0, logits_device=npu:0, decoded_image_device=npu:0, control_image_device=npu:0
  • 耗时:denoise_seconds=28.0442, total_seconds=51.9666
  • 输出图像:assets/inpaint_output.png(512×512 RGB,470,433 字节)
    • SHA256:1b83a343dc612465ee75224564fab17577ea40d051ad681ce3c1db51f8acdc54
  • 退出码:0,status=success

结果与证据截图

截图说明
assets/agent_workflow.png本次 ModelAgent 适配工作流(1440×1800)
assets/npu_device_call.pngnpu:0 设备调用与 npu-smi 进程关联证据(1440×900)
assets/model_result.png模型推理结果与运行指标(1440×900)
assets/modelagent_live_session.pngModelAgent 实时会话(去敏,1440×900)

npu-smi PID 关联证据:npu-smi 报告进程在设备上持有 NPU 时的宿主机侧 PID,而容器内只能看到自身的 命名空间 PID(实测二者不同,/proc 的 NSpid 仅一个值)。因此通过生命周期窗口(进程驻留时设备行出现、 进程退出后消失,见 logs/npu_device_call.log 首尾证据)以及显存占用匹配(torch.npu.memory_reserved() 与 npu-smi 进程行内存同步增长:110 → 3048 → 3432 → 4312 MB)将容器 PID 与 npu-smi 进程行精确关联: container_pid=191093 ↔ npu_smi(host) pid=2211643,pid_correlated=true。

限制

  • torch_npu 未实现 aten::_linalg_solve_ex;UniPC 采样器在每步会求解小的系数线性系统。本仓库在 inference.py 中将其替换为 NPU 原生小矩阵求解器(Gauss-Jordan),保证无任何算子回退到 CPU。
  • 模型输出未做安全审核(safety_checker=None,与上游 endpoint 一致)。请遵守 Stable Diffusion / ControlNet 的 CreativeML OpenRAIL-M 许可证,勿在公开场景暴露未过滤结果。
  • 本仓库为昇腾 NPU 适配版本;为保持与上游一致使用 UniPCMultistepScheduler,固定 seed=0 保证确定性输出。

许可证

模型与基础模型均为 CreativeML OpenRAIL-M(见 LICENSE.md)。