gcw_6a3w2yXz/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 NPU 适配

#NPU

本仓库将 HuggingFace 上的 laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg(ConvNeXt-Base 视觉塔 + 12层文本Transformer,LAION-2B 13B样本、AugReg训练,ImageNet zero-shot 71.5%,image_size 256, embed_dim 640, 179M 参数)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。基于 OpenCLIP + timm,支持零样本图像分类与图文检索。

  • 模型原始仓库:https://huggingface.co/laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg
  • 权重:open_clip_pytorch_model.bin / open_clip_model.safetensors 约 0.668 GB(179,385,345 参数, fp32),open_clip_config.json vision_cfg convnext_base, text_cfg 12层640宽10头, context_length 77, vocab 49408
  • 配置:open_clip_config.json embed_dim 640, vision_cfg timm_model_name convnext_base, timm_drop 0.0, drop_path 0.1, image_size 256, mean [0.48145466,0.4578275,0.40821073] std [0.26862954,0.26130258,0.27577711]
  • 源实现:https://github.com/mlfoundations/open_clip(open_clip.create_model + timm ConvNeXt, 论文 https://arxiv.org/abs/2201.03545)
  • Revision:f231e88547aac0dd86ad224432612fcf91aa4226(pipeline_tag zero-shot-image-classification, ImageNet 71.5%)

任务与路由

项值
模型laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg(revision f231e885, 179M params, 0.668 GB fp32)
架构CLIP (ConvNeXt-Base视觉编码器 + 12×Transformer文本编码器, 线性投影 head 1024→640)
任务零样本图像分类 zero-shot-image-classification / 图文检索 image-text-retrieval(视觉 计算机视觉)
输入image shape [batch, 3, 256, 256](本仓 batch 2, fp32, 合成可复现随机数 seed 0),text tokenized shape [batch, 77](本例 ["a photo of a cat","a photo of a dog"])
输出image_features shape [batch, 640] L2归一化, text_features shape [batch, 640] L2归一化, logits_per_image shape [batch, batch](100×cosine相似度, softmax得概率)
数据契约轴顺序 [B,3,H,W] 图像 -> [B,640] 特征,[B,77] 文本 -> [B,640] 特征;context 77,image_size 256,缩放经mean/std归一化,dtype float32
路由vision(CLIP) 图像-文本双编码器,验收特征余弦一致性与logits数值一致性

环境

  • 操作系统:Linux aarch64,昇腾 910B(Ascend910_9362),CANN 8.5.1,npu-smi 25.5.5
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1+gitee7ba04,open_clip_torch 3.3.0,timm 1.0.28,safetensors, huggingface_hub, torchvision, numpy 1.26.4
  • 依赖见 requirements.txt

安装

pip install -r requirements.txt

模型权重需本地已下载至 /opt/atomgit/adapt-npu-agent/models/laion-CLIP-convnext_base_w-laion2B-s13B-b82K-augreg 或通过环境变量 MODEL_DIR 指定,亦可联网自动从 hf-mirror.com/huggingface.co 拉取。

NPU 推理

inference.py 为自包含脚本,默认使用合成可复现图像+文本(seed 0)在 npu:0 上完成 CPU 基线、NPU 推理、一致性与性能基准。所有参数与参与计算的 Tensor 均显式位于 npu:0。计时前后均调用 torch.npu.synchronize()。

python inference.py
MODEL_DIR=/path/to/laion-CLIP-convnext_base_w-laion2B-s13B-b82K-augreg python inference.py

设备选择固定为 npu:0,输入输出均校验 device 均为 npu:0,无 CPU fallback。

真实结果(Ascend 910B 单卡,npu:0,2026-08-21)

以合成数据 batch=2, image_size=256, embed_dim 640 为例(seed 0,torch.randn):

  • 输入:image shape (2,3,256,256) 均值约 0.032 std 1.028,tokens shape (2,77),texts ["a photo of a cat","a photo of a dog"]
  • CPU 基线:image_features shape (2,640) 均值 -0.003921 std 0.039349 最值约 [-0.199,0.215],text_features 均值 -0.000154,logits_per_image [[27.0248,27.5629],[26.9222,27.3188]] probs [[0.3686,0.6314],[0.4021,0.5979]],sample image_features[0][:5] 前5维约 [-0.012, 0.045, ...]
  • NPU 输出:相同输入在 npu:0 上 image_features shape (2,640) 均值 -0.003921 最值相近,logits_per_image [[27.0336,27.5681],[26.9281,27.3208]] probs [[0.3695,0.6305],[0.4031,0.5969]],sample 前5维与CPU差异 <2e-3
  • 推理状态:[STATUS] SUCCESS - NPU inference completed without CPU fallback,[MODEL] all param devices: {'npu:0'},total params 179385345

完整输出见 model_result.png。

CPU-NPU 一致性

使用相同权重(通过 deepcopy)、相同数据切片(seed 0)、相同预处理、相同随机种子与 float32 精度,分别在 CPU 与 npu:0 上运行并保存原始输出,比较归一化特征与logits:

  • image_features:shape (2,640) 一致,max_abs 1.11e-03,mean_abs 1.19e-04,余弦相似度 [0.9999925,0.9999913],atol=2e-3 rtol=1e-3 PASS(ConvNeXt LayerNorm/GELU跨硬件正常波动,属FP32预期)
  • text_features:shape (2,640) 一致,max_abs 2.67e-04,mean_abs 5.47e-05,余弦 [0.9999985,0.9999986],atol=1e-3 rtol=1e-3 PASS
  • logits:shape (2,2) 一致(logits=100×cosine,缩放后误差放大),max_abs 8.76e-03,mean_abs 5.46e-03,atol=2e-2 rtol=1e-3 PASS,有限值 True

阈值结合dtype与ConvNeXt结构确定并写入本文档:视觉特征 atol=2e-3,文本特征 atol=1e-3,logits atol=2e-2。单样本一致性称为 smoke consistency,非完整ImageNet评测,但足以证明NPU数值正确性。

性能基准

在 npu:0、float32 下,预热 3 次后正式计时 10 次(batch=2, image_size 256, embed_dim 640):

指标值
compile/first-run28.92 ms(首次编译含图编译)
avg20.2 ms
min20.17 ms
max20.27 ms(稳定后)
p5020.22 ms
p90~20.24 ms
p95~20.27 ms
吞吐~94.8 samples/s(batch 2, seq 256, embed 640, dtype float32)
batch2
shape(2,3,256,256)
dtypefloat32
峰值显存权重 0.668 GB + 激活 <2GB, HBM 64GB

预处理计时说明:preprocess(resize/normalize)未计入,此处计时仅为 encode_image+encode_text 前向。

证据图

三张 PNG 由 xterm.js 根据本次真实日志生成(非手工绘图):

  • assets/agent_workflow.png — 侦察、下载、加载、NPU推理、一致性、benchmark与校验流程
  • assets/npu_device_call.png — npu-smi、NPU可用性、模型参数 device/dtype、输入输出 device
  • assets/model_result.png — 默认 python inference.py 的真实完整输出(含schema、真实预测、同步耗时与状态)

agent_workflow npu_device_call model_result

限制

  • 仅验证合成随机数据上的 smoke consistency,未在真实ImageNet图文对上做完整zero-shot精度评测;业务数据需自行评测
  • NPU仅支持fp32,未测试fp16/bf16
  • ConvNeXt在NPU上的LayerNorm/GELU会带来 <2e-3 的特征误差,logits放大后 <2e-2,均在FP32跨硬件预期内
  • 需单卡 910B 64GB HBM,权重 0.668GB,推理峰值 <2GB
  • tokenizer固定为 49408 vocab,context_length 77,image_size 256