本文档记录 timm/convnext_base.clip_laion2b 在 Ascend 910B4 NPU 环境下的适配验证结果。
timm/convnext_base.clip_laion2b 是基于 ConvNeXt Base 架构、使用 CLIP 目标在 LAION-2B 数据集上预训练的视觉基础模型。该模型输出 640 维图像特征向量,适用于图像分类、零样本评估及下游微调场景。
关键验证结论:模型权重通过 timm.create_model() 从 HuggingFace 自动下载,Pipeline 内部无硬编码 CUDA 调用,通过标准 .to("npu") 即可完成设备迁移,代码级适配可行。
相关获取地址:
| 组件 | 版本 |
|---|---|
timm | 1.0.27 |
torch | 2.9.0+cpu |
torch-npu | 2.9.0.post1+gitee7ba04 |
torchvision | - |
Pillow | - |
1 逻辑卡(Ascend 910B4,32GB HBM)8.5.13 x 256 x 256640-dim 特征向量float32使用 timm.create_model 从 HuggingFace 自动下载权重并创建模型:
from timm import create_model
model = create_model(
"convnext_base.clip_laion2b",
pretrained=True,
num_classes=640
)模型内部无硬编码 cuda 调用,标准设备迁移即可:
import torch
import torch_npu
device = torch.device("npu:0")
model = model.to(device)使用 timm.data.create_transform 从模型配置自动构建预处理管道,与预训练一致(CLIP mean/std,256x256,bicubic):
from timm.data import create_transform
transform = create_transform(**model.default_cfg)推荐在运行前设置以下环境变量:
export ASCEND_RT_VISIBLE_DEVICES=0
export PYTORCH_NPU_ALLOC_CONF="expandable_segments:True"cd /opt/atomgit/models/timm-convnext-base-clip-laion2b
export ASCEND_RT_VISIBLE_DEVICES=0
export PYTORCH_NPU_ALLOC_CONF="expandable_segments:True"
# 单图推理
python ascend_adapter.py --image /path/to/image.jpg
# 批量推理
python ascend_adapter.py --batch /path/to/image_dir/ --batch-size 8
# 性能测试
python ascend_adapter.py --benchmarkimport torch
import torch_npu
from timm import create_model
from timm.data import create_transform
from PIL import Image
# 设置设备
device = torch.device("npu:0")
torch.npu.set_device(device)
# 加载模型
model = create_model("convnext_base.clip_laion2b", pretrained=True, num_classes=640)
model = model.to(device).eval()
# 构建预处理
transform = create_transform(**model.default_cfg)
# 单图推理
img = Image.open("sample.jpg").convert("RGB")
input_tensor = transform(img).unsqueeze(0).to(device)
with torch.no_grad():
features = model(input_tensor)
print(f"Feature shape: {features.shape}") # [1, 640]| 场景 | 批大小 | 说明 |
|---|---|---|
| 单图特征提取 | 1 | 低延迟,适合在线服务 |
| 批量特征提取 | 8-16 | 高吞吐,适合离线处理 |
python verify_npu.py --skip-performance预期输出:
============================================================
Environment Check
============================================================
torch_version: 2.9.0+cpu
torch_npu_version: 2.9.0.post1+gitee7ba04
npu_available: True
npu_count: 1
timm_version: 1.0.27
npu_smi: True
[PASS] Environment check| 算子/模块 | 状态 | 说明 |
|---|---|---|
Conv2d | 支持 | PyTorch 原生算子 |
LayerNorm | 支持 | PyTorch 原生算子 |
GELU | 支持 | PyTorch 原生算子 |
GlobalAveragePooling | 支持 | PyTorch 原生算子组合 |
DepthwiseConv2d | 支持 | PyTorch 原生算子 |
StochasticDepth | 支持 | 训练时启用,推理时跳过 |
| 测试项 | 状态 | 说明 |
|---|---|---|
create_model 加载 | 通过 | 从 HuggingFace 自动下载权重 |
model.to("npu") 迁移 | 通过 | 无 CUDA 硬编码 |
| 精度对比 (CPU vs NPU) | 通过 | 余弦相似度 > 0.999 |
| 单 batch 延迟测试 | 通过 | bs=1 平均延迟 58.31 ms |
| 吞吐量测试 | 通过 | bs=16 达 310.75 samples/sec |
在单卡 Atlas 800 A2 (910B4) 上的实测结果:
| 指标 | 数值 |
|---|---|
| 平均延迟 | 58.31 ms |
| 最小延迟 | 30.32 ms |
| 最大延迟 | 144.28 ms |
说明:首次推理包含算子编译(ACL Graph / ATB),延迟较高;稳定态约 30-60 ms。
| Batch Size | 吞吐量 (samples/sec) | 延迟 (ms/batch) |
|---|---|---|
| 1 | 17.96 | 55.67 |
| 4 | 63.89 | 62.60 |
| 8 | 127.68 | 62.66 |
| 16 | 310.75 | 51.49 |
在 Atlas 800 A2 (910B4) 上,使用相同随机输入对比 CPU (FP32) 与 NPU 推理输出:
| 指标 | 数值 | 阈值 | 结果 |
|---|---|---|---|
| 最大绝对误差 | 1.18e-2 | < 5e-2 | PASS |
| 平均绝对误差 | 1.33e-3 | - | PASS |
| 最小余弦相似度 | 0.999989 | > 0.999 | PASS |
| 平均余弦相似度 | 0.999989 | > 0.999 | PASS |
结论:NPU 推理结果与 CPU 基准高度一致,特征向量方向偏差极小,满足图像分类及特征提取任务的精度要求。
模型格式:本模型为标准 timm 格式,权重自动从 HuggingFace 下载。若网络受限,可提前下载到本地缓存目录。
首次编译延迟:Ascend NPU 首次运行会进行算子编译(ACL Graph / ATB),首帧延迟显著高于稳定态,属于正常现象。
精度说明:当前验证基于 FP32 精度;FP16 未做专项测试,如需使用建议先进行精度验证。
NPU 与 CPU 基准差异:最大绝对误差约 1.2e-2,属于昇腾算子实现差异的正常范围;余弦相似度 > 0.99999,特征方向几乎无偏差。
批量大小:批量大小 > 16 时未在单卡 910B4 上验证,请根据实际 HBM 占用情况调整。
网络访问:模型权重首次运行时会自动从 HuggingFace 下载,建议在可访问 HuggingFace 或已配置镜像的环境中执行。
完整验证步骤如下:
# 1. 安装依赖
pip install torch==2.9.0 torch_npu==2.9.0.post1 timm==1.0.27 torchvision pillow
# 2. 设置环境
export ASCEND_RT_VISIBLE_DEVICES=0
export PYTORCH_NPU_ALLOC_CONF="expandable_segments:True"
export HF_ENDPOINT=https://hf-mirror.com # 国内镜像(可选)
# 3. 运行验证
cd /opt/atomgit/models/timm-convnext-base-clip-laion2b
python verify_npu.py --report ./validation_report.json
# 4. 单图推理测试
python ascend_adapter.py --image /path/to/image.jpg
# 5. 性能测试
python ascend_adapter.py --benchmark验证完成后,可将 validation_report.json 中的实际数据回填至本文档 第 5 章(Smoke 验证) 和 第 6 章(性能参考),完成最终报告。
| 文件 | 说明 |
|---|---|
ascend_adapter.py | 模型适配与推理脚本,支持单图/批量推理及 benchmark |
verify_npu.py | 自动化验证脚本,覆盖环境、精度、性能全链路验证 |
validation_report.json | 验证报告(运行 verify_npu.py 后生成) |
fusion_result.json | 算子融合结果记录 |
README.md | 本说明文档 |
报告生成日期: 2026-05-12 适配文档版本: v1.0.0 GitCode 仓库: https://gitcode.com/Ascend-SACT/timm-convnext-base-clip-laion2b