wrhhh/laion-CLIP-convnext_base_w-laion2B-s13B-b82K-augreg-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

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

1. 模型简介

  • 模型名称: laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg
  • 架构: OpenCLIP 双塔 (dual-encoder) - 图像塔 ConvNeXt-Base (timm convnext_base, w variant with LayerNorm), 文本塔 Transformer (12 layers, 640 width, 10 heads, context 77)
  • 嵌入维度: 640, 图像尺寸 256x256, 权重格式 open_clip_pytorch_model.bin / open_clip_model.safetensors (717 MB 单文件, 共 1.34 GiB), 预处理 mean [0.4814,0.4578,0.4082] std [0.2686,0.2613,0.2757]
  • 训练数据: LAION-2B (s13B-b82K with AugReg), 任务为图文对比学习、零样本分类/图文检索
  • 来源: https://huggingface.co/laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg (open_clip_config.json model_cfg)
  • 适配方式: 本地权重通过 open_clip.create_model_and_transforms('convnext_base_w', pretrained=local_bin) 加载到 npu:0,使用 open_clip.get_tokenizer 分词,固定 256x256 合成图 + 6 候选文本验证 embedding 与 similarity,切实走 NPU 编译与推理

2. 验证环境

  • 硬件: Ascend910_9362 (910B), NPU 2, chip 4 / 5, HBM 64 GiB
  • 镜像: npu-smi 25.5.5, torch.npu.is_available()=True, device_count=2, device_name Ascend910_9362
  • 软件: Python 3.11, 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.19
  • 显存: 推理前 free ~62.3 GB total 62.7 GB, 推理后 free 61.48 GB (peak delta ~0.8 GB), batch=1, dtype float32, 权重显存约 0.68 GB (FP16) / 1.34 GB (FP32)
  • 复现: python inference.py 默认即 NPU float32 单样本推理;已通过 check_npu.py (passed=true)

3. 安装依赖

pip install -r requirements.txt
# requirements.txt 包含: torch, torch-npu, open-clip-torch, timm, torchvision, Pillow, safetensors, huggingface-hub

4. NPU 推理

# 默认 NPU 推理 (包含 CPU 对比 + benchmark)
python inference.py

# 仅验证一致性
python inference.py --mode validate

# 仅性能测试
python inference.py --mode benchmark

# 指定设备 / 精度
python inference.py --device npu:0 --dtype float32
python inference.py --device cpu --dtype float32
  • 模型加载: open_clip.create_model_and_transforms('convnext_base_w', pretrained="/opt/atomgit/.cache/huggingface/hub/.../open_clip_pytorch_model.bin") -> model.to('npu:0')
  • 预处理: Compose(Resize 256, CenterCrop 256, ToTensor, Normalize) 来自 open_clip
  • 输入契约: image [1,3,256,256] float32, text tokens [6,77] long
  • 输出契约: image_features [1,640], text_features [6,640], logits [1,6] = logit_scale.exp() * norm(image) @ norm(text).T, probs softmax dim=-1, ranking 完整排序

5. 真实推理结果

  • 固定合成图: 256x256 合成 RGB (梯度背景 + 中心绿色块 40,180,110 与浅块 220,210,180) via create_fixed_image(256)
  • 候选文本 (6, 顺序固定, 含正确语义冗余用于排序检验):
    1. a photo of a cat
    2. a photo of a dog
    3. a photo of a car
    4. a photo of a mountain
    5. a photo of food
    6. a photo of a beach
  • NPU (npu:0, float32, logit_scale 100.0):
    image_features shape [1,640] image_embeds[0][:5] [0.01503, 0.02018, 0.00094, 0.00963, 0.04081]
    text_features shape [6,640] text_embeds[0][:5] [0.01828, 0.00331, 0.01754, -0.02476, 0.02040]
    logits_per_image [[30.76327, 28.65554, 29.22905, 28.62244, 27.47962, 27.50110]]
    probs [[0.65338385, 0.07939548, 0.14088594, 0.07681035, 0.02449627, 0.02502811]]
    ranking:
      1. a photo of a cat logit=30.7633 prob=0.6534
      2. a photo of a car logit=29.2291 prob=0.1409
      3. a photo of a dog logit=28.6555 prob=0.0794
      4. a photo of a mountain logit=28.6224 prob=0.0768
      5. a photo of a beach logit=27.5011 prob=0.0250
      6. a photo of food logit=27.4796 prob=0.0245
  • 设备证明: param device npu:0 dtype torch.float32, image_features device npu:0, logits device npu:0, 单次同步后耗时 224.00 ms (含首次编译), 稳定端到端 19.92 ms
  • 说明: CLIP 的 softmax 是候选集合内分布 (非 SigLIP 的独立 sigmoid),此处分数为该分布;已通过 model.logit_scale.exp() 正确计算

6. CPU-NPU 一致性验证

  • 方法: 相同权重、相同固定图与 token、相同预处理,分别在 cpu 与 npu:0 上推理,对比归一化 embedding 与 logits (固定单样本验证,非全数据集评测)
  • 结果 (float32):
    image_embeds cosine 0.999988
    text_embeds cosine per text [0.999999, 0.999999, 0.999996, 0.999997, 0.999997, 0.999996] mean 0.999997
    logits max_AE 0.024584 mean_AE 0.012600
    Top-1 NPU=a photo of a cat CPU=a photo of a cat match=True
    ranking NPU [cat, car, dog, mountain, beach, food]
    ranking CPU [cat, car, dog, mountain, beach, food] full_match=True top3_overlap 1.00
  • 阈值: float32 要求 cosine >=0.9999, Top-1 必须一致, 候选<10 时需全排序一致 (或 top3 overlap >=0.9) -> PASS
  • 结论: 单样本一致性验证通过,证明 NPU 计算与 CPU 数值一致,未发现精度退化;完整精度评测需在对应下游数据集上另行执行

7. 性能测试

  • 计时方式: 区间前后 torch.npu.synchronize(), 预热 3 次, 正式 10 次 (取 avg/min/max/p50/p90/p95), batch=1, image 256x256, texts=6, dtype float32, device npu:0
  • 首次编译延迟: 224.00 ms (首次包含图编译与缓存)
  • 稳定推理 (10 次均值):
    e2e (image+text+similarity+softmax) avg 19.92 ms min 19.89 max 19.96 p50 19.92 p90 19.95 p95 19.95
    image_enc (encode_image only) avg 14.92 ms min 14.91 max 14.94 p50 14.92 p90 14.93
    text_enc (6 texts, encode_text) avg 6.86 ms min 6.80 max 7.01 p50 6.84
    similarity = logit_scale * image@text.T + softmax (包含在 e2e 内)
  • 吞吐: 约 50.2 images/s (e2e), 67.0 image_enc/s, 874 texts/s (6 texts per batch)
  • 显存: 推理后 HBM free 61482 MB / total 62740 MB, peak 预估权重 0.68-1.34 GiB + 激活 <2 GiB < 单卡可承载
  • 说明: 预处理与首次编译不计入稳定均值;不同文本数或图像尺寸会导致线性变化

8. 自验证截图

  • assets/agent_workflow.png - 完整工作流日志 (下载、侦察、加载、NPU 推理、验证、性能)
  • assets/npu_device_call.png - NPU 设备调用证据 (npu-smi, torch_npu 可用性, 设备名, 参数所在设备与 dtype)
  • assets/model_result.png - 模型推理结果 (默认 python inference.py 的真实输出、排序、验证 PASS/FAIL 与性能)

9. 已知限制

  • 仅验证 batch="1," 256x256 固定合成图, 6 文本的单样本一致性与延迟;未进行 ImageNet/下游数据集全量精度评测
  • 使用 float32 精度;FP16/BF16 未在本仓验证,可自行以 --dtype float16 测试但需重测一致性阈值 (>=0.999)
  • ConvNeXt-Base 体量约 88M 参数,单文件 717 MB,Huge/Giant 变体未测试;大模型需预判显存再加载
  • 依赖 timm 的 ConvNeXt 实现,torch_npu 与 timm/CANN 需版本兼容 (已验证 torch_npu 2.9.0 + CANN 8.5.1)
  • 分塔验证已合入端到端,但若显存不足可按 image/text 分塔验证并注明限制

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, Ascend910B, CLIP, OpenCLIP, ConvNeXt, dual-encoder, vision-language, zero-shot