冬
gcw_IDzXRVNw/controlnet-inpaint-endpoint-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

controlnet-inpaint-endpoint 昇腾 NPU 适配部署文档

1. 模型简介

ControlNet 来自论文 Adding Conditional Control to Text-to-Image Diffusion Models,是一种神经网络结构,用于通过额外条件来控制扩散模型。

本模型 (OrderAndChaos/controlnet-inpaint-endpoint) 是 ControlNet v1.1 Inpaint 版本,专为 Stable Diffusion 1.5 图像修复训练。

模型架构

属性值
模型类型ControlNetModel (v1.1 Inpaint)
参数量361.28 M
基础模型Stable Diffusion 1.5
条件通道顺序RGB
注意力头维度8
块输出通道[320, 640, 1280, 1280]
输入通道4 (RGB + Mask)
条件嵌入通道[16, 32, 96, 256]

模型特点

  • Inpaint 专用: 专为图像修复任务设计
  • Conditioning Embedding: 专用模块处理条件图像
  • 多尺度输出: 4 个下采样块 + 中间块
  • Cross Attention: 与 Stable Diffusion UNet 配合使用

输入输出格式

  • 输入:
    • sample: 噪声图像张量 (batch, 4, H, W)
    • timestep: 扩散时间步
    • encoder_hidden_states: 文本嵌入 (batch, seq_len, 768)
    • controlnet_cond: RGB 条件图像 (batch, 3, H, W)
  • 输出:
    • down_block_res_samples: 4 个下采样块输出
    • mid_block_res_sample: 中间块输出

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 4 GB NPU 显存(ControlNet 推理峰值约 2-3 GB)
  • 本次实测使用 910B2(单卡 64 GB)

2.2 软件依赖

依赖项版本要求说明
操作系统CentOS 7.6+ / Ubuntu 18.04+支持主流 Linux 发行版
CANN5.0.RC2+华为昇腾计算架构
Python3.10 / 3.12推荐 3.10+
PyTorch2.0.0+推荐 2.12
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展
diffusers>=0.16.0ControlNet 模型加载
transformers>=4.30.0文本编码器

2.3 安装方式

# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100

# 2) 安装依赖(使用华为云源)
pip install diffusers transformers -i https://repo.huaweicloud.com/repository/pypi/simple

# 3) 验证环境
python -c "
import torch, torch_npu
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"

3. 分步推理操作流程

步骤 1:准备工作目录

# 进入适配目录
cd /workspace/agent4/controlnet-inpaint-endpoint-ascend

# 确认上游权重存在
ls -la /workspace/agent3/OrderAndChaos/controlnet-inpaint-endpoint/

# 期望输出:config.json diffusion_pytorch_model.fp16.safetensors (~722MB) ...

步骤 2:检查 NPU 环境

# 检查 NPU 设备状态
npu-smi info

# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"

步骤 3:运行推理脚本

# 基本用法:默认参数 (512x512)
python inference.py --device npu:0

# 指定其他 NPU 卡
python inference.py --device npu:1

# 自定义图像尺寸
python inference.py --device npu:0 --height 768 --width 768

4. 完整测试用例

测试用例 1:ControlNet 推理(NPU:0)

conda activate pt2100
cd /workspace/agent4/controlnet-inpaint-endpoint-ascend
python inference.py --device npu:0

实际输出:

[W823 12:21:xx.xxxxxxx FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
[W823 12:21:xx.xxxxxxx NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size...
============================================================
ControlNet Inpaint 昇腾 NPU 推理
============================================================

[Device] npu:0
[Model Path] /workspace/agent3/OrderAndChaos/controlnet-inpaint-endpoint

[Config]
  _class_name: ControlNetModel
  attention_head_dim: 8
  block_out_channels: [320, 640, 1280, 1280]
  in_channels: 4
  cross_attention_dim: 768

[Memory Before Model Load] Allocated: 0.00 GB, Reserved: 0.00 GB

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 95.4        41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          3424 / 65536         |
+===========================+===============+====================================================+


[Loading] ControlNet model weights...
[Loading] ControlNet weights from ...diffusion_pytorch_model.fp16.safetensors
  Loaded 340 keys
  Total parameters: 361.28 M

[Moving] Weights to npu:0...

[Model Loaded] Allocated: 0.68 GB, Reserved: 0.71 GB

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 104.2       41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          4217 / 65536         |
+========================---+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1694322       | python                   | 848                     |
+===========================+===============+====================================================+


[Running] ControlNet forward pass simulation...

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 104.4       41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          4362 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1694322       | python                   | 992                     |
+========================---+============---+====================================================+


[Results]
  Number of down block outputs: 4
    down_block_res_samples[0]: torch.Size([1, 320, 256, 256])
    down_block_res_samples[1]: torch.Size([1, 640, 128, 128])
    down_block_res_samples[2]: torch.Size([1, 1280, 64, 64])
    down_block_res_samples[3]: torch.Size([1, 1280, 32, 32])
  mid_block_res_sample: torch.Size([1, 1280, 32, 32])

[Weight Statistics]
  Sample weight 'controlnet_cond_embedding.blocks.0.bias': shape=torch.Size([16]), mean=0.0179

[Memory After Inference] Allocated: 0.75 GB, Reserved: 0.86 GB

[Done]

性能指标

指标值
模型参数量361.28 M
权重文件diffusion_pytorch_model.fp16.safetensors (~722 MB)
推理显存峰值~2-3 GB
输出块数4 个下采样块 + 1 个中间块

5. 注意事项

  1. Diffusers 版本兼容性: 该模型配置为 diffusers 0.16.0.dev0,部分 API 可能与稳定版有差异
  2. 完整 Pipeline: 完整推理需要配合 Stable Diffusion UNet 和 VAE 使用
  3. Inpaint 用法: ControlNet 接收修复遮罩条件,掩码区域在 RGB 通道中设为 -1

6. 参考链接

  • ControlNet 论文 (arXiv:2302.05543)
  • ControlNet GitHub
  • Diffusers ControlNet 文档
  • HuggingFace 模型页