#NPU
laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg (ConvNeXt-Base w wide variant)/tmp/CLIP_convnext (snapshot_download via HF_ENDPOINT=https://hf-mirror.com, 无手动代理)open_clip_pytorch_model.bin 717,620,007 bytes + open_clip_model.safetensors 717,597,010 bytes (单权重重复打包, 实际加载其一), open_clip_config.json + tokenizer.json / merges.txttimm convnext_base (depth: stages 3/3/9/3, dims 96→192→384→768→1024, kernel 7, drop_path 0.1, timm_pool="", timm_proj=linear) + head proj 1024→640; Text: Transformer 12 layers, width 640, heads 10, vocab 49408, context_length 77, embed_dim 640; 逻辑 image_features (640) @ text_features.T *100 => logits[B,3,256,256] float32 normalized (mean [0.48145466,0.4578275,0.40821073] std [0.26862954,0.26130258,0.27577711]), text [B,77] int64 tokenizer (openai/clip-vit-large-patch14 style, BPE merges.txt)image_features [B,640], text_features [N,640] (L2-normalized), logits [B,N] (100*cos), probs softmaxopen_clip_torch 3.3.0 + timm 1.0.28 + torch 2.9.0+cpu + torch_npu 2.9.0.post1 + torchvision 0.24.0 + Pillow, 无 remote code (trust_remote_code=False)说明: 单卡
npu:0真实权重推理已验证, CPU vs NPU logitsmax_abs 9.0e-03(atol 0.05 rtol 1e-3通过), 无 CPU fallback。
| 项 | 值 |
|---|---|
| 任务 | zero-shot-image-classification (dual-encoder classification via logits, Top-1/Top-K) |
| 架构族 | CLIP ConvNeXt (vision) + Transformer (text) |
| 加载后端 | open_clip.create_model_and_transforms('convnext_base_w', pretrained=local_bin, device="cpu") -> .to("npu:0") + open_clip.get_tokenizer('convnext_base_w') |
| dtype | float32 (模型、输入、输出一致); int64 for text tokens |
| image_size | 256×256 |
| embed_dim | 640 |
| context_length | 77 |
| vocab_size | 49408 |
| vision cfg | timm_model_name convnext_base, timm_model_pretrained false, timm_drop 0, timm_drop_path 0.1, timm_pool "", timm_proj linear |
| text cfg | layers 12, width 640, heads 10 |
| 预处理 | Resize 256? via open_clip transform: Resize/Bilinear + CenterCrop 256 + ToTensor + Normalize(mean/std) => [B,3,256,256] |
| 输入 shape | image [1,3,256,256] float32, text [3,77] int64 (a photo of a cat/dog/car) |
| 输出 shape | image_features [1,640], text_features [3,640], logits [1,3], probs [1,3] |
| 频率/通道 | 无时间频率; 图像通道 3 (RGB), 文本 token 通道 77 |
| 缩放 | 无显式 scaling; 依赖 Norm + L2 normalize + 100×cosine |
| 预处理契约 | 输入为 PIL Image 256x256, 经 preprocess 产出 float32 tensor, 不改列顺序; 文本经 tokenizer BPE -> pad 77 |
| 缺失/异常 | 无缺失掩码; 输入固定为合成图像 (seed 42) |
| 验证策略 | CPU vs NPU logits/probabilities 对比 (classification) + argmax agreement |
| 输入示例 | 合成确定性图像 np.random.rand(256,256,3)*255 seed 42, SHA256 f98e90e8122eddb3 (前16), 均值 127.12, 首像素 [95,242,186]; tensor stats mean 0.182171 std 1.080439 |
| 输出示例 | NPU logits [[27.06119,27.57929,27.50230]] probs [[0.23622,0.39658,0.36719]] 预测 a photo of a dog (argmax 1); CPU logits [[27.05348,27.57516,27.49324]] 一致 |
| 轴顺序 | 图像 [B,C,H,W] 严格; 文本 [B,L] |
| 随机种子 | 42 (numpy + torch, 保证 CPU/NPU 同分布) |
Ascend910_9362 (npu:0), driver 25.5.5, 2 NPUs (0000:0B:00.0, 0000:0A:00.0), Health OK, HBM 64GB/卡, 已用 ~3GB/usr/local/Ascend/cann-8.5.1torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)==Ascend910_9362HF_ENDPOINT=https://hf-mirror.com (国内镜像, 未手动覆盖 http_proxy)/tmp/CLIP_convnext (open_clip_pytorch_model.bin 717 MB) -> create_model_and_transforms -> model.to("npu:0"), 所有 Tensor 显式 to(npu:0), 校验首参数 device npu:0 (无 fallback)npu:0 为唯一目标, torch.npu.synchronize() 包围计时, 首轮编译与稳定态分开报告pip install -r requirements.txt
# 首次运行若本地不存在 /tmp/CLIP_convnext,将自动从 HF 镜像下载 laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg (≈1.4GB, 含 .bin + .safetensors 双备份)依赖见 requirements.txt: torch, torch_npu, torchvision, open_clip_torch, timm, Pillow, numpy, huggingface_hub, safetensors, ftfy, regex。
注意: 系统已配置
HF_ENDPOINT=https://hf-mirror.com与 pip 镜像, 无需手动设置代理。
默认命令完成真实 NPU 推理、同步计时、CPU 对比与校验:
python inference.py流程:
torch.npu.is_available() 并锁定 npu:0, 打印 npu-smi info。/tmp/CLIP_convnext), 加载 open_clip_config.json + open_clip_pytorch_model.bin。open_clip.create_model_and_transforms('convnext_base_w', pretrained=bin, device="cpu") -> model.to("npu:0"), 断言首参数 device==npu:0。open_clip.get_tokenizer('convnext_base_w')。[1,3,256,256] (seed 42, 256x256, uint8->preprocess) 与 3 条文本 token [3,77], 均迁移至 npu:0 并校验 device。torch.npu.synchronize() 计时, 验证输出 device npu:0。max_abs / mean_abs / argmax_agreement 并保存 working/npu_output.npy / working/cpu_output.npy。无 CPU fallback: 首参数与 image_features/logits device 均校验为 npu:0, 无固定 device="cpu" 字符串, 显式 .to("npu:0")。
在 npu:0 (batch 1, image 256x256, 3 prompts, seed 42, float32) 执行 python inference.py:
[INFO] model_name=laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg
[INFO] task_type=zero-shot-image-classification architecture=CLIP-ConvNeXt-Base backend=open_clip_torch+timm+torch_npu
[INFO] image_size=256 embed_dim=640 dtype=float32 prompts=['a photo of a cat', 'a photo of a dog', 'a photo of a car']
[INFO] torch=2.9.0+cpu torch_npu=2.9.0.post1 npu_available=True device_count=2
[INFO] target device npu:0 name Ascend910_9362
[INFO] HF_ENDPOINT=https://hf-mirror.com
[INFO] npu-smi exit 0
[INFO] loading model from /tmp/CLIP_convnext
[INFO] config .../open_clip_config.json exists True
[INFO] model_cfg embed_dim=640 vision convnext_base image_size 256
[INFO] text_cfg layers=12 width=640 heads=10
[INFO] creating model convnext_base_w with pretrained /tmp/CLIP_convnext/open_clip_pytorch_model.bin
[INFO] model created, visual TimmModel text Transformer
[INFO] preprocess mean [0.48145466, 0.4578275, 0.40821073] std [0.26862954, 0.26130258, 0.27577711]
[INFO] first param device npu:0 dtype torch.float32 numel 179385345
[CHECK] param device verified npu:0, no fallback
[INFO] tokenizer loaded for convnext_base_w
[INFO] input image shape (1, 3, 256, 256) dtype torch.float32 device cpu (before npu)
[INFO] input image preview first pixel [95, 242, 186] mean 127.12 std 73.58
[INFO] input image SHA256 f98e90e8122eddb3
[INFO] image tensor stats mean 0.182171 std 1.080439 min -1.792263 max 2.131677
[INFO] text tokens shape (3, 77) preview [49406, 320, 1125, 539, 320, 2368, 49407] prompts [...]
[INFO] image_npu shape (1, 3, 256, 256) device npu:0 dtype torch.float32
[INFO] text_npu shape (3, 77) device npu:0 dtype torch.int64
[INFO] warmup 1 run (compile)...
[INFO] warmup done compile/first-run 223.88 ms
[CHECK] output image_features device npu:0 text_features device npu:0 logits device npu:0 fallback False
[RESULT] image_features shape (1, 640) device npu:0 dtype torch.float32
[RESULT] text_features shape (3, 640) device npu:0 dtype torch.float32
[RESULT] logits shape (1, 3) device npu:0
[RESULT] logits [[27.061193466186523, 27.57929801940918, 27.502300262451172]]
[RESULT] probs [[0.23622404038906097, 0.3965829908847809, 0.36719298362731934]]
[RESULT] predicted class a photo of a dog argmax 1
[RESULT] image_features preview first5 [0.0502989..., 0.03041..., 0.01435..., 0.02099..., 0.01704...]
[RESULT] text_features preview first5 [0.01828..., 0.00331..., 0.01754..., -0.02476..., 0.02040...]
[PERF] compile/first-run 223.88 ms
[PERF] stable 10 runs avg 20.46 ms min 20.00 max 21.13 p50 20.22 p90 21.13 p95 21.13
[PERF] throughput 48.87 samples/s (batch 1, image 256x256, 3 prompts)
[PERF] batch size 1 shape [1,3,256,256] dtype float32
[COMPARE] logits cpu [[27.05348,27.57516,27.49324]] npu [[27.06119,27.57929,27.50230]]
[COMPARE] max_abs 9.056091e-03 mean_abs 6.965002e-03 max_if 8.06e-04 passed True atol 0.05 rtol 0.001
[RESULT] probs cpu [[0.2360065,0.3976343,0.3663591]] probs npu [[0.2362240,0.3965829,0.3671929]]
[RESULT] output SHA256 cpu c1f7e8e4346b94c2 npu 3859e6bd82ec17ea
[SUCCESS] NPU inference completed on npu:0 with CLIP convnext_base_w zero-shot classification同步保存 working/npu_output.npy (logits [1,3]) 与 working/cpu_output.npy。
同权重、同输入 (seed 42 synthetic 256x256)、预处理、dtype、seed 下 CPU vs NPU logits 对比 (classification):
python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_output.npy --npu working/npu_output.npy --task classification --atol 0.05 --rtol 0.001输出:
{
"task": "classification",
"cpu_shape": [1,3],
"npu_shape": [1,3],
"finite": true,
"atol": 0.05, "rtol": 0.001,
"max_abs_error": 0.00905609130859375,
"mean_abs_error": 0.0069650014241536455,
"passed": true,
"argmax_agreement": 1.0
}阈值 atol=0.05, rtol=1e-3 适用于 FP32 ConvNext dual-encoder logits (scale ~27, softmax 输出 0-1); 实测 max_abs 9e-03 < 0.05, argmax_agreement 1.0, 无 fallback, shape 一致, finite 校验通过。同进程内复现, 输入与模型权重完全一致。图像特征 max_abs 8.0e-04 更小。
npu:0 (Ascend910_9362, batch 1, 256x256, 3 prompts, float32, torch.npu.synchronize() 包围):
[1,3,256,256], dtype float32, 预处理 (PIL->tensor->normalize) 不计入 NPU 计时 (仅 encode 阶段)synchronize() 要求三张 PNG 由 xterm.js 根据本次真实日志渲染 (scripts/render_xterm_evidence.mjs --style raw), 非 Pillow 模拟, 深灰背景白色等宽字体, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 为展示标签:
assets/agent_workflow.png — 侦察、环境检查、模型下载、加载、NPU 推理、一致性与性能 full workflow (来源 logs/workflow.log)assets/npu_device_call.png — npu-smi info + torch.npu.is_available()==True + device npu:0 + 首参/输出 device 校验 (来源 logs/device.log)assets/model_result.png — 默认 python inference.py 的真实完整输出, 含输入/输出摘要、logits/probs、耗时与 SUCCESS (来源 logs/inference.log)

npu:0; 更大 batch / 384×384 高分辨率或 AMP 需另测merges.txt/tokenizer.json 本地加载, 首次需联网下载 (已缓存至 /tmp/CLIP_convnext), 正式推理已为本地加载laion/CLIP-convnext_base_w-laion2B-s13B-b82K-augreg 权重, 未使用 dummy)pip install -r requirements.txt
python inference.py
# 预期: [SUCCESS] NPU inference completed on npu:0 ... max_abs <0.05 通过