w
gcw_uQ09W7jl/laion-CLIP-convnext_base_w-laion2B-s13B-b82K-augreg-NPU
模型介绍
文件和版本
Pull Requests
讨论
分析

laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg on Ascend NPU

1. 模型简介

  • 模型名称: laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg
  • 模型类型: 计算机视觉 / 双塔图文检索 (CLIP)
  • 架构: OpenCLIP convnext_base_w (timm ConvNeXt-Base vision + Transformer text width 640, 12 layers, heads 10, embed_dim 640, image_size 256)
  • 官方地址: https://huggingface.co/laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg
  • 配置: vision_cfg: timm_model_name=convnext_base, timm_pool="", timm_proj=linear, image_size=256 ; text_cfg: vocab_size 49408, context_length 77, width 640, heads 10, layers 12 ; embed_dim 640 ; preprocess mean [0.48145466,0.4578275,0.40821073] std [0.26862954,0.26130258,0.27577711]
  • 权重格式: open_clip_model.safetensors (717 MB) + open_clip_pytorch_model.bin (717 MB), 1.337 GiB 合计, 实际加载 safetensors via open_clip
  • 任务: 零样本图像分类 / 图文检索 (embedding + similarity ranking)
  • 适配精度: float32 (原始权重 float32, 单卡 Ascend910B 实测)
  • 加载后端: open_clip_torch create_model_and_transforms('convnext_base_w', pretrained=local) + open_clip.get_tokenizer, 不依赖运行时下载

2. 验证环境

  • 硬件: Ascend910_9362 (npu:0), 2× Ascend910, HBM 64 GB
  • NPU 驱动: npu-smi 25.5.5, Health OK, Power ~160W, Temp 43-44°C
  • 软件: torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, CANN 8.5.1, open_clip_torch 3.3.0, timm 1.0.28, Pillow, numpy, safetensors
  • 系统: Linux aarch64, Python 3.11.14
  • 检查命令:
    npu-smi info
    python -c "import torch; import torch_npu; print(torch.npu.is_available()); print(torch.npu.get_device_name(0))"
    输出: torch.npu.is_available()=True, device_name=Ascend910_9362, device_count=2, passed=true
  • 模型路径: /opt/atomgit/.cache/huggingface/hub/models--laion--CLIP-convnext_base_w-laion2B-s13B-b82K-augreg/snapshots/f231e88547aac0dd86ad224432612fcf91aa4226
  • 显存预估: 权重 717 MB (FP32) + 激活/编译 ~500 MB, 峰值 <2 GB, 实测 NPU 已用 ~1202 MB, 满足单卡 64GB

3. 安装依赖

pip install -r requirements.txt
# requirements 包含 torch, torch-npu, open_clip_torch, timm, torchvision, Pillow, numpy, safetensors, huggingface_hub, ftfy, regex, tqdm
# 已在环境中安装 torch-npu 与 CANN, 无需手动设置代理或 HF_ENDPOINT (已配置 hf-mirror)

4. NPU 推理

默认命令 (真实 NPU):

python inference.py
# 或显式指定本地权重目录
python inference.py --model-dir /opt/atomgit/.cache/huggingface/hub/models--laion--CLIP-convnext_base_w-laion2B-s13B-b82K-augreg/snapshots/f231e88547aac0dd86ad224432612fcf91aa4226

其他模式:

python inference.py --mode validate   # 完整验证 (含 CPU-NPU 一致性)
python inference.py --mode benchmark  # 性能测试 (预热3次, 测试10次, torch.npu.synchronize 计时)

输入契约 (固定可复现):

  • 图像: 1×3×256×256 RGB 合成图 (256x256, 淡色背景, 中心红色方块 60% 尺寸 + 白色边框 + 内层高亮, 确定性 Pillow 生成)
  • 文本: 5 条候选, 按固定顺序:
    1. a photo of a cat
    2. a photo of a dog
    3. a red square
    4. a photo of a bird
    5. a photo of a car
  • 预处理: open_clip preprocess (resize 256, center_crop, rescale, normalize mean/std 同上), tokenizer context_length 77
  • 设备: npu:0, dtype float32, batch 1

5. 真实推理结果

以下为本次在 npu:0 上 python inference.py 的真实输出 (首轮含编译):

Model: laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg
Task: zero-shot image classification / image-text retrieval (dual-encoder CLIP ConvNeXt)
Device: npu:0  torch="2.9.0+cpu"  torch_npu="2.9.0.post1"  NPU name="Ascend910_9362"
Requested dtype: torch.float32  batch_size="1"  image_size="256"
Input: 1×3×256×256 synthetic red-square image + 5 texts
Output contract:
  image_embeds: (1, 640)  dtype="torch.float32"  device="npu:0"
  text_embeds:  (5, 640)  dtype="torch.float32"  device="npu:0"
  logits (similarity): (1, 5)  dtype="torch.float32"
Inference latency (single, synchronized): 130.36 ms  (first run includes NPU compilation)
Similarity (logits) raw scores (CLIP):
  [0] a photo of a cat               logit="26.8776"  softmax="0.0000"
  [1] a photo of a dog               logit="25.8744"  softmax="0.0000"
  [2] a red square                   logit="37.6738"  softmax="1.0000"
  [3] a photo of a bird              logit="26.2200"  softmax="0.0000"
  [4] a photo of a car               logit="25.8334"  softmax="0.0000"
Ranking (desc by logit):
  Rank 1: [2] a red square  logit="37.6738"  prob="1.0000"
  Rank 2: [0] a photo of a cat  logit="26.8776"  prob="0.0000"
  Rank 3: [3] a photo of a bird  logit="26.2200"  prob="0.0000"
  Rank 4: [1] a photo of a dog  logit="25.8744"  prob="0.0000"
  Rank 5: [4] a photo of a car  logit="25.8334"  prob="0.0000"
Top-1: "a red square"
Note: CLIP uses softmax over candidates (not SigLIP sigmoid).

说明:

  • 采用模型原生 logit_scale.exp() (100.0) 乘以归一化 embedding 点积得到 logits, 并对候选集合做 softmax 得到分布
  • 合成红方块与文本 a red square 强相关, Top-1 正确且置信度 1.0, 排序完整记录可复现
  • 所有数值来自本次 npu:0 真实执行, 设备与 dtype 已验证 (param device="npu:0)"

6. CPU-NPU 一致性验证

相同权重、相同输入、相同预处理分别在 CPU 与 NPU 上运行, 报告:

image_embeds cosine: 0.999984
text_embeds cosine mean: 0.999997 min: 0.999996 per-text: ['0.999999', '0.999999', '0.999996', '0.999997', '0.999996']
logits max_abs_err: 0.035534 mean_abs_err: 0.015661
Top-1 CPU: [2] a red square  NPU: [2] a red square  (一致)
Ranking CPU: [2, 0, 3, 1, 4]  NPU: [2, 0, 3, 1, 4]  Top3 overlap: 3/3

阈值与判定 (float32):

  • image/text embedding cosine >= 0.999 (实测 0.99998/0.99999, 通过)
  • logits max_abs <= 0.1 (实测 0.035, 通过)
  • Top-1 必须一致 (通过)
  • 候选数 5 <10, 排序应全部一致 (实测全一致, 通过)

结论: [PASS] CPU-NPU consistency within tolerance

验证方法为单样本 smoke consistency (固定合成样例), 非完整数据集评测, 已在 inference.py 默认流程中自动执行。

7. 性能测试

计时区间前后均调用 torch.npu.synchronize(), 预热 3 次, 正式测试 10 次, batch 1, 256x256, 5 texts, float32, npu:0:

Full end-to-end: avg=24.16ms min=23.50ms max=24.72ms p50=24.37ms p90=24.48ms p95=24.60ms
Image encoder: avg=15.64ms min=15.59ms max=15.73ms
Text encoder (5 texts): avg=7.76ms min=7.34ms max=8.14ms
Similarity: avg=0.76ms
Throughput: 41.39 inferences/s  (batch 1, 256x256, 5 texts, dtype torch.float32, device npu:0)
NPU memory used: 1202.3 MB / 62740 MB
First run (cold, with compilation): 130.36 ms
Stable (warm): ~23.50ms (min) to 24.16ms (avg)

说明:

  • 端到端含图像编码、文本编码、相似度计算 (含 normalize 与 matmul)
  • 首次编译延迟单独标注, 稳定延迟为预热后均值
  • 图像/文本编码分别计时, 文本编码含 5 条文本的 batch 处理

复现:

python inference.py --mode benchmark

8. 自验证截图

  • assets/agent_workflow.png — 阶段日志 (下载、侦察、加载、NPU推理、验证、性能)
  • assets/npu_device_call.png — npu-smi info、torch.npu.is_available()、设备名、模型参数设备与 dtype
  • assets/model_result.png — python inference.py 完整输出、任务结果、计时与 PASS/FAIL

图片由 scripts/render_xterm_evidence.mjs 从本次真实日志渲染 (--style raw), 提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$, 不代表实际主机名。

9. 已知限制

  • 合成固定图非真实自然图像, 仅用于可复现的 smoke 验证; 真实场景建议替换为业务图像并重新计时
  • 当前为 float32; ConvNeXt-Base 717 MB 在 FP32 下已满足单卡显存与速度 (24ms), 暂无需 FP16/BF16
  • 文本长度固定为 tokenizer context 77, 超长文本会被截断
  • 首次 NPU 编译约 130ms, 冷启动与热启动差异已在 README 明确区分
  • 未做完整数据集精度评测 (如 ImageNet zero-shot), 仅提供单样本 CPU-NPU 一致性; 如需数据集评测需额外脚本

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, CLIP, ConvNeXt, open_clip, vision, dual-encoder, zero-shot

11. 目录结构

laion-CLIP-convnext_base_w-laion2B-s13B-b82K-augreg-NPU/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png

12. 复现命令汇总

# 环境检查
npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available()); print(torch.npu.get_device_name(0))"

# 默认推理 (NPU)
python inference.py

# 验证 (含 CPU-NPU 一致性)
python inference.py --mode validate

# 性能
python inference.py --mode benchmark