#NPU
buildborderless/CommunityForensics-DeepfakeDet-ViT (ViT-Small for Deepfake Detection)ac6ee457bea904a373065754107451793b56db00 (config.json 524 B, model.safetensors 83.23 MB / 87,270,764 bytes, 21.81M 参数 float32)ViTForImageClassification — ViT Small (vit) 12 layers, hidden 384, intermediate 1536, 6 attention heads (64 dim/head), patch 16, image 384, hidden_act gelu, qkv_bias true, num_labels 1 (sigmoid single logit 二分类 fake/real), torch_dtype float32, transformers_version 5.4.0logits [1,1] → sigmoid fake_prob ∈ [0,1] → fake if >0.5 else real; 训练于 CVPR 2025 Community Forensics (2.7M 样本, 4,803 generators)timm/vit_small_patch16_384.augreg_in21k_ft_in1kCLIPImageProcessor — shortest_edge 440 (BICUBIC resample 3) 保持纵横比 → center_crop 384 → CLIP 归一化 mean [0.48145466,0.4578275,0.40821073] std [0.26862954,0.26130258,0.27577711] (do_normalize true, do_resize true, do_center_crop true, crop_size 384)transformers ViTForImageClassification + ViTImageProcessor (≥4.50, 推荐 ≥5.4 以支持 shortest_edge; 本仓兼容 4.57.6 通过手动 PIL 回退)model.to("npu:0") + pixel_values.to("npu:0"), 全部前向在 NPU, 已验证无 CPU fallback| 项 | 值 |
|---|---|
| 输入类型 | 单张 PIL RGB 图像, 任意尺寸 (H,W), 3 通道, 值域 0-255 uint8 |
| 预处理 | shortest_edge=440 等比缩放 (BICUBIC) → center_crop 384 → /255 → (x-mean)/std (CLIP) → [1,3,384,384] float32 NCHW |
| 轴顺序 | HWC (numpy) → CHW (tensor) → NCHW batch |
| 数据 | 合成确定性 (seed 42): 500×300 梯度图 data[y,x,0]=(x*255/W+y*10)%255 data[y,x,1]=(y*255/H+x*7)%255 data[y,x,2]=((x+y)*255/(W+H))%255 + 均匀噪声 N(-10,10) clip 0-255, SHA256 8f070b04be19 前 12 hex, 原图 mean 126.41 std 67.78, 另验证 batch 2 第二张 800×600 纯色 (128,64,192) |
| 像素张量 | pixel_values [1,3,384,384] float32 mean 0.1679 std 0.9212 (NPU 前), device npu:0 after .to(npu:0) |
| 输出 | logits [1,1] float32 → sigmoid → fake_prob 标量; 示例 logit 1.41185 prob 0.804058 verdict fake |
| dtype | float32 (config torch_dtype float32, 全部 float32 前向) |
| 预处理选择 | 优先 ViTImageProcessor (transformers ≥5.4 自动处理 shortest_edge+center_crop); 旧版本 (4.57.6) 检测到 ValueError: size must contain height/width 时自动回退到手动 PIL (BICUBIC + center_crop + CLIP normalize, 逐行等价) |
| 批处理 | 支持 batch N (示例 batch 2: logits [1.41185, -5.63834] 对应 fake/real) |
Ascend910_9362 ×2, 目标 npu:0 (3号板 Phy 6 Bus 0000:0B:00.0, HBM 64GB)torch.npu.is_available()==True, device_count==2snapshot_download 或优先 LOCAL_SNAPSHOT (/opt/atomgit/.cache/.../ac6ee...) → ViTForImageClassification.from_pretrained (cpu) → model.to("npu:0"), 输入 manual_process 或 ViTImageProcessor 后 pixel_values.to("npu:0") via NPUpip install -r requirements.txt
# 包含: torch>=2.0 torch_npu transformers>=4.50 huggingface_hub pillow numpy safetensorspython inference.py加载 LOCAL_SNAPSHOT 或自动从 HF 镜像下载至 ./.cache/CommunityForensics-DeepfakeDet-ViT → npu:0 → warmup 3 → torch.npu.synchronize() 计时 → 打印真实预测。
关键日志(npu:0):
[INFO] model_name: buildborderless/CommunityForensics-DeepfakeDet-ViT
[INFO] device: npu:0
[INFO] npu_device_name: Ascend910_9362
[INFO] npu_count: 2
[INFO] first param device after to(npu:0): npu:0
[INFO] input image size (500, 300) -> PIL (500, 300) SHA256 8f070b04be19
[INFO] pixel_values shape torch.Size([1, 3, 384, 384]) mean 0.1679 std 0.9212 device cpu
[INFO] input tensor device npu:0 dtype torch.float32
[RESULT] NPU logits shape (1, 1) device npu:0 dtype torch.float32 value [[1.411852240562439]]
[RESULT] fake_prob 0.804058 verdict fake (sigmoid)
[PERF] warmup=3 formal=10 | avg 6.00 ms min 5.95 max 6.06 p50 6.00 p90 6.04 p95 6.05 throughput 166.54 images/s
[SUCCESS] NPU inference completed on npu:0 with buildborderless/CommunityForensics-DeepfakeDet-ViT (ViT 384)input PIL (500,300) SHA256 8f070b04be19 raw mean 126.41 std 67.78, pixel_values [1,3,384,384] mean 0.1679 std 0.9212, device npu:0 after movelogits [1,1] device npu:0 float32 [[1.411852240562439]], sigmoid fake_prob 0.804058 verdict fake (>0.5), finite True shape_ok True fallback Falsebatch 2 验证 [500×300 合成, 800×600 纯色] → logits [1.41185, -5.63834] → probs [0.804, 0.00356] (合成 fake, 纯色 real)预处理 手动 PIL BICUBIC (与 ViTImageProcessor CLIP 完全等价, 回退路径已验证), processor 在 4.57.6 下触发 ValueError height/width 后自动 fallback权重 model.safetensors 83.23 MB (87270764 bytes), 21.81M params, config 524 B, preprocessor 306 Bpython .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_output.npy --npu working/npu_output.npy --task regression --atol 1e-4 --rtol 1e-3| 输出 | CPU | NPU | max_abs | mean_abs | passed |
|---|---|---|---|---|---|
logits [1,1] float32 | 1.41222453 | 1.41185224 | 0.00037229 | 0.00037229 | ✅ (atol 1e-4 rtol 1e-3) |
| prob (sigmoid) | 0.804117 | 0.804058 | 0.000059 | 0.000059 | ✅ |
阈值说明: FP32 默认 1e-4/1e-3 对 12 层 ViT (384 hidden, 6 heads) 足够严格 (实测 3.72e-04 在 atol 1e-4 边界但 rtol 1e-3 通过, 误差源 Attention softmax 与 NPU/CPU 浮点结合顺序); 同权重同输入同归一同手动预处理,
torch.npu.synchronize()后落盘 npy 直接比较; 无 CPU fallback (全部 12 层 + patch_embed + classifier 在 npu:0).
torch.npu.synchronize() 前后计时, 纯模型 forward, 含预处理后 NPU 推理, batch 1, shape [1,3,384,384], float32)PIL 500×300 → resize 440 + crop 384 → [1,3,384,384] → ViT 12 layers → logits [1,1]sync elapsed 6.09 ms (warmup 后首测)model(pixel_values) NPU 前向 + synchronize 屏障, 不含 PIL resize/crop (预处理在 CPU, 约 3 ms, 可忽略); 若含预处理则 avg ~9 ms

由
scripts/render_xterm_evidence.mjs --style raw根据本次真实日志生成,提示符atomgit@pod-a94f8701860f4700b161b00e290de466:~$仅为展示标签,不证明服务器身份。
LOCAL_SNAPSHOT (83 MB)。Cannot create tensor with interal format 为 torch_npu 提示无害; transformers 4.57.6 下自动 fallback 到手动 PIL (与 5.4 处理等价, 已验证 pixel mean/std 一致)。submission/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
├── agent_workflow.png
├── npu_device_call.png
└── model_result.png