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 端对端推理。
pip install -r requirements.txt
# requirements.txt 已包含 torch, torch_npu, timm, ftfy, regex, einops, Pillow, torchvision
# 需配合 CANN 8.5.1 与 Ascend 驱动默认命令(真实 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)输入合约:
输出合约:
以下为单次 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 节。
相同权重、相同输入、相同预处理,分别在 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执行 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)额外说明:
三张证据图均从本次真实日志通过 scripts/render_xterm_evidence.mjs --style raw 渲染:
assets/agent_workflow.png — 下载、侦察、加载、NPU 推理、验证与性能完整 workflowassets/npu_device_call.png — npu-smi 与 torch.npu 设备检查assets/model_result.png — 默认推理真实输出与 PASS/FAIL

snapshot_download("facebook/PE-Core-S16-384") 或 HF 获取 PE-Core-S16-384.pt 至 /tmp/models/...。Hardware: NPU, NPU, Ascend, Ascend910, Vision, CLIP, Perception-Encoder, Zero-Shot