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] |
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: 中间块输出| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| 操作系统 | CentOS 7.6+ / Ubuntu 18.04+ | 支持主流 Linux 发行版 |
| CANN | 5.0.RC2+ | 华为昇腾计算架构 |
| Python | 3.10 / 3.12 | 推荐 3.10+ |
| PyTorch | 2.0.0+ | 推荐 2.12 |
| torch_npu | 与 PyTorch 版本匹配 | 昇腾 PyTorch 扩展 |
| diffusers | >=0.16.0 | ControlNet 模型加载 |
| transformers | >=4.30.0 | 文本编码器 |
# 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())
"# 进入适配目录
cd /workspace/agent4/controlnet-inpaint-endpoint-ascend
# 确认上游权重存在
ls -la /workspace/agent3/OrderAndChaos/controlnet-inpaint-endpoint/
# 期望输出:config.json diffusion_pytorch_model.fp16.safetensors (~722MB) ...# 检查 NPU 设备状态
npu-smi info
# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"# 基本用法:默认参数 (512x512)
python inference.py --device npu:0
# 指定其他 NPU 卡
python inference.py --device npu:1
# 自定义图像尺寸
python inference.py --device npu:0 --height 768 --width 768conda 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 个中间块 |