gcw_6a3w2yXz/facebook-PE-Core-S16-384-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

facebook/PE-Core-S16-384 on Ascend NPU

1. 模型简介

Perception Encoder (PE) Core S16-384 是 Meta 提出的视觉-语言双塔对比学习模型,基于 ViT-Small (patch 16, width 384, depth 12, heads 6) 视觉塔与 12 层文本 Transformer,经图像预训练与合成视频数据微调。支持 384px 零样本图文检索与分类,CLIP 维度 512,上下文长度 32。该 S 尺寸为从 G 教师蒸馏而来,相比 B/L/G 更轻量,适合单卡 Ascend910B 端对端推理。

  • 模型:facebook/PE-Core-S16-384 (Hugging Face: https://huggingface.co/facebook/PE-Core-S16-384)
  • 权重:PE-Core-S16-384.pt (333 MB, safetensors/pt 格式)
  • 任务:zero-shot-image-classification / image-text retrieval
  • 架构:VisionTransformer (patch 16, width 384, layers 12, pool=attn) + TextTransformer (width 512, layers 12, vocab 49408)
  • 官方代码:https://github.com/facebookresearch/perception_models (core.vision_encoder.pe.CLIP)

2. 验证环境

  • 硬件:Ascend910B (Ascend910_9362) x2, 64GB HBM, npu-smi 25.5.5
  • 驱动/CANN:cann-8.5.1, Ascend-toolkit 8.5.1
  • Python:3.11.14
  • PyTorch:2.9.0+cpu, torch_npu 2.9.0.post1
  • 依赖:timm==1.0.28, ftfy, regex, einops, Pillow, huggingface_hub, modelscope (下载)
  • NPU 可用性:torch.npu.is_available()=True, device_count=2, device_name=Ascend910_9362, npu:0
  • 验证方式:本地权重 /tmp/models/facebook/PE-Core-S16-384/PE-Core-S16-384.pt, pretrained=True, perception_models 源码适配

3. 安装依赖

pip install -r requirements.txt
# requirements.txt 已包含 torch, torch_npu, timm, ftfy, regex, einops, Pillow, torchvision
# 需配合 CANN 8.5.1 与 Ascend 驱动

4. NPU 推理

默认命令(真实 NPU 推理,包含下载/加载/NPU 设备检查):

python inference.py
# 或显式指定权重路径
python inference.py --ckpt /tmp/models/facebook/PE-Core-S16-384/PE-Core-S16-384.pt

其他模式:

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

输入合约:

  • 图像:RGB, 384x384, Resize((384,384))+ToTensor+Normalize(mean=0.5,std=0.5), batch=1, 固定使用 apps/pe/docs/assets/cat.png
  • 文本:5 候选 ["a diagram", "a dog", "a cat", "a photo of a kitchen", "a beach"], SimpleTokenizer(context_length=32), 含正确标签 "a cat"

输出合约:

  • image_embeds: [1, 512]
  • text_embeds: [5, 512]
  • similarity/logits: [1,5] (logit_scale * image_embeds @ text_embeds.T)
  • ranking: 完整 5 候选排序与 softmax 概率

5. 真实推理结果

以下为单次 python inference.py 在 npu:0 上的真实输出(已同步):

[INFO] Model: facebook/PE-Core-S16-384
[INFO] Device: npu:0
[INFO] Model first param device: npu:0
[INFO] Model image_size: 384, context_length: 32
[INFO] Candidate texts (5): ['a diagram', 'a dog', 'a cat', 'a photo of a kitchen', 'a beach']
[INFO] Input image tensor: torch.Size([1, 3, 384, 384]), device=npu:0
[INFO] Input text tensor: torch.Size([5, 32]), device=npu:0
[INFO] Inference latency: 256.05 ms (first run includes compilation)
[RESULT] image_embeds shape: torch.Size([1, 512])
[RESULT] text_embeds shape: torch.Size([5, 512])
[RESULT] similarity logits shape: torch.Size([1, 5])
[RESULT] logit_scale: 100.0000
[RESULT] probs shape: torch.Size([1, 5])
[RESULT] Ranking (candidate -> prob):
  1. a cat                          -> 0.999839
  2. a dog                          -> 0.000152
  3. a photo of a kitchen           -> 0.000008
  4. a beach                        -> 0.000002
  5. a diagram                      -> 0.000000
[RESULT] Top-1: a cat (index 2, prob 0.999839)
[RESULT] image_embeds[0,:5]: [-0.002490175422281027, -0.11016861349344254, 0.017008453607559204, 0.05427229404449463, -0.023992488160729408]
[RESULT] text_embeds[0,:5]: [0.019027935341000557, 0.03382189944386482, -0.017982596531510353, 0.020765826106071472, 0.001331470557488501]
[RESULT] PASS - NPU inference successful, top-1 correct

说明:首次运行 256ms 含编译;稳定延迟见第 7 节。

6. CPU-NPU 一致性验证

相同权重、相同输入、相同预处理,分别在 CPU 与 NPU 执行 python inference.py --mode validate:

[VALIDATE] image_embeds cosine: 0.999998 (threshold >=0.999)
[VALIDATE] text_embeds cosine: 1.000000 (threshold >=0.999)
[VALIDATE] logits max abs error: 0.008016
[VALIDATE] logits mean abs error: 0.004536
[VALIDATE] probs max abs error: 0.000000
[VALIDATE] Top-1 CPU: a cat, NPU: a cat, consistent: True
[VALIDATE] Top-3 overlap: 3/3
[VALIDATE] Validation PASS
  • dtype: float32, batch=1, 图像 384x384, 文本 5 句
  • FP32 余弦阈值 0.999,Top-1 必须一致;本次全部通过,排序完全一致。
  • 验证类型:固定样例 smoke consistency(非全量数据集评测),但覆盖 embedding 与相似度。

7. 性能测试

执行 python inference.py --mode benchmark(torch.npu.synchronize 前后计时,warmup 3 次,measure 10 次):

[BENCHMARK] End-to-end latency (ms): avg=18.70, min=18.28, max=19.85, p50=18.52, p90=19.85, p95=19.85
[BENCHMARK] Image encode latency avg: 11.95 ms (batch=1, 384x384)
[BENCHMARK] Text encode latency avg: 6.97 ms (num_texts=5)
[BENCHMARK] Similarity compute latency: avg=~0.06 ms
[BENCHMARK] Dtype: torch.float32, batch_size: 1, image_size: 384
[BENCHMARK] Peak memory: N/A (Ascend910 64GB, model ~333MB, single-card sufficient)

额外说明:

  • 首次编译延迟 ~256ms,稳定端到端 ~18-19ms。
  • 其他候选数量下文本编码近似线性;图像编码不随文本数变化。

8. 自验证截图

三张证据图均从本次真实日志通过 scripts/render_xterm_evidence.mjs --style raw 渲染:

  • assets/agent_workflow.png — 下载、侦察、加载、NPU 推理、验证与性能完整 workflow
  • assets/npu_device_call.png — npu-smi 与 torch.npu 设备检查
  • assets/model_result.png — 默认推理真实输出与 PASS/FAIL

agent workflow npu device call model result

9. 已知限制

  • 仅验证 npu:0 单卡 FP32;未测试 FP16/BF16、双卡或大 batch。
  • 权重未提交,需自行通过 ModelScope snapshot_download("facebook/PE-Core-S16-384") 或 HF 获取 PE-Core-S16-384.pt 至 /tmp/models/...。
  • 首次运行含图算编译 (~250ms),后续稳定 ~19ms;不同 CANN/驱动版本可能略有波动。
  • 依赖 timm DropPath 与 perception_models 源码;未支持 pipeline("zero-shot-image-classification") 封装。
  • 输入固定为 cat.png 与 5 文本;其他图像/文本已在模型能力范围内,但未经全量评测。

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, Vision, CLIP, Perception-Encoder, Zero-Shot