模型名称: facebook/mask2former-swin-large-ade-semantic
模型权重URL: https://huggingface.co/facebook/mask2former-swin-large-ade-semantic (通过 HF 镜像 https://hf-mirror.com 下载, 本地缓存 /opt/atomgit/adapt-npu-agent/model_cache/facebook_mask2former-swin-large-ade-semantic)
任务: 图像语义分割 Semantic Segmentation (ADE20K 150 类, Mask2FormerForUniversalSegmentation + SwinForImageClassification Large backbone, depths [2,2,18,2] embed_dim 192 hidden 1536)
架构: Mask2Former paper Masked-attention Mask Transformer for Universal Image Segmentation – multi-scale deformable attention pixel decoder + masked-attention Transformer decoder, 100 object queries, 151 logits (150 + no-object), masks [B, 100, H/4, W/4]
输入: RGB 图像任意尺寸, 预处理经 Mask2FormerImageProcessor: resize 384x384 (size_divisor 32, size {384,384}) → rescale 1/255 → normalize mean [0.485,0.456,0.406] std [0.229,0.224,0.225] → pixel_values [B,3,H,W] FP32 + pixel_mask [B,H,W]
输出: class_queries_logits [B,100,151] + masks_queries_logits [B,100,96,96] (96=384/4), 后处理 post_process_semantic_segmentation 插值回原图尺寸 → semantic_map [H,W] 取值 0..149 (0=background/no-object 对应 ADE)
权重: model.safetensors 826 MB (FP32, 单卡可加载), 总参数量 215.49M (215,488,779), config.json 81KB, preprocessor_config.json 538B, do_resize true, num_labels 150, ignore_index 255
特殊依赖: 无 trust_remote_code, 仅需 transformers, safetensors, Pillow, numpy; NPU 运行时有 allow_internal_format=False 提示 (自动转 base format) 与 ArgSort on AICPU 提示 (int64 sort 回落 CPU), 无 CPU fallback, 全程 npu:0
官方最小示例: AutoImageProcessor + Mask2FormerForUniversalSegmentation.from_pretrained(... ) → processor(images) → model(pixel_values, pixel_mask) → processor.post_process_semantic_segmentation. 本适配在 Ascend 910B 单卡 npu:0 上完成真实权重加载与端到端推理, 保持权重与预处理不变。
| 项目 | 值 |
|---|---|
| NPU | Ascend910_9362 (2 devices, tested npu:0) HBM 64GB free ~62GB, Power 169W Temp 46C |
| npu-smi | 25.5.5 健康 OK AICore 0% Mem 3105/65536 MB (推断时 4689) |
| torch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 CANN 8.5.1 |
| transformers | 4.57.6 |
| Python | 3.11.14 |
| 验证镜像 | Synthetic 384x384 (白底红椭圆绿矩蓝对角线黄圆棕多边形, seed 0) + 二次测试 640x480 seed1 |
| 输入尺寸 | 384x384 batch=1 dtype=float32, 同时验证 640x480 动态 shape |
执行 python scripts/check_npu.py 返回 torch_npu_available true, npu_available true, device_count 2, device_name Ascend910_9362.
pip install -r requirements.txt
# requirements.txt 已包含:
# torch==2.9.0, torch-npu==2.9.0.post1, transformers==4.57.6, Pillow, numpy, safetensors, huggingface_hub模型权重下载 (首次需联网, 走环境变量 HF_ENDPOINT=https://hf-mirror.com):
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download facebook/mask2former-swin-large-ade-semantic --local-dir /opt/atomgit/adapt-npu-agent/model_cache/facebook_mask2former-swin-large-ade-semantic --local-dir-use-symlinks False
# 或 python 方式:
HF_ENDPOINT=https://hf-mirror.com python3 -c "from huggingface_hub import snapshot_download; snapshot_download('facebook/mask2former-swin-large-ade-semantic', local_dir='/opt/atomgit/adapt-npu-agent/model_cache/facebook_mask2former-swin-large-ade-semantic', local_dir_use_symlinks=False, max_workers=1)"本地权重路径: /opt/atomgit/adapt-npu-agent/model_cache/facebook_mask2former-swin-large-ade-semantic (含 model.safetensors 826M, config.json, preprocessor_config.json, README.md)
默认命令 (真实 NPU 推理, 自动选择 npu:0 若可用, 否则 CPU):
python inference.py其他模式:
python inference.py --mode validate # 复验 CPU-NPU 一致性 (argmax + pixel agreement)
python inference.py --mode benchmark # 3 次 warmup + 10 次计时 (含 torch.npu.synchronize)
python inference.py --model-dir /path/to/mask2former-swin-large-ade-semantic关键实现:
AutoImageProcessor.from_pretrained(..., local_files_only=True) + Mask2FormerForUniversalSegmentation.from_pretrained(..., local_files_only=True) 从本地加载model.to(npu:0) 显式搬运, 输入 pixel_values 与 pixel_mask 同步到 npu:0, 首个参数 device npu:0 dtype FP32torch.npu.synchronize() 包裹计时区间, 输出 class_queries_logits 与 masks_queries_logits 统计 min/max/meanprocessor.post_process_semantic_segmentation(outputs, target_sizes=[h,w])[0] 得到 H×W 语义图, 统计 unique labels, Top-K 像素占比Mask2FormerImageProcessor 完全一致: resize 384 → /255 → normalize执行: python inference.py (npu:0, FP32, 合成图 384x384, torch.npu.synchronize 计时)
======================================================================
Model: facebook/mask2former-swin-large-ade-semantic
Task: semantic-segmentation (ADE20K 150 classes, Mask2Former + Swin-L)
Model dir: /opt/atomgit/adapt-npu-agent/model_cache/facebook_mask2former-swin-large-ade-semantic
Device: npu:0 torch=2.9.0+cpu torch_npu=available
torch_npu version: 2.9.0.post1+gitee7ba04
NPU device name: Ascend910_9362
NPU device count: 2
[INFO] Loading processor from /opt/atomgit/adapt-npu-agent/model_cache/facebook_mask2former-swin-large-ade-semantic
[INFO] Processor: Mask2FormerImageProcessor do_resize=True size={'height': 384, 'width': 384} num_labels=150
[INFO] Loading model from /opt/atomgit/adapt-npu-agent/model_cache/facebook_mask2former-swin-large-ade-semantic
[INFO] Model: facebook/mask2former-swin-large-ade-semantic Architecture: Mask2FormerForUniversalSegmentation (Swin-L backbone) Task: semantic-segmentation ADE20K 150 classes
[INFO] Original weight dtype: torch.float32
[INFO] Total params: 215.49M (215488779)
[INFO] Model loaded on npu:0 dtype=torch.float32 orig_dtype=torch.float32
Input: synthetic image 384x384 RGB (white bg, red ellipse, green rect, blue diagonal, yellow circle, brown polygon)
Preprocess: processor rescale 1/255, normalize mean=[0.485, 0.456, 0.406] std=[0.229, 0.224, 0.225], resize {'height': 384, 'width': 384} divisor 32
Inputs pixel_values shape: torch.Size([1, 3, 384, 384]) dtype=torch.float32 device_before=cpu
Inputs pixel_mask shape: torch.Size([1, 384, 384]) dtype=torch.int64
Inputs moved to npu:0: pixel_values npu:0, pixel_mask npu:0
Model first param device: npu:0 dtype: torch.float32
Inference latency (single, sync): 447.59 ms
Output class_queries_logits shape: torch.Size([1, 100, 151]) dtype=torch.float32 device=npu:0 min=-10.907886 max=11.393948 mean=-3.186775
Output masks_queries_logits shape: torch.Size([1, 100, 96, 96]) dtype=torch.float32 device=npu:0 min=-83.111526 max=12.396488 mean=-7.739247
Post-process semantic map shape: torch.Size([384, 384]) dtype=torch.int64 device=npu:0
unique labels (first 20): [2, 4, 6]
semantic min 2 max 6 mean 2.6669
Top-3 predicted classes by pixel count:
class 2 count 98620 ratio 0.6688
class 4 count 48502 ratio 0.3289
class 6 count 334 ratio 0.0023
Secondary test: 640x480 image -> class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96]) latency 63.75 ms sem shape torch.Size([480, 640]) unique [0, 2, 4]
----------------------------------------------------------------------
[INFO] Running CPU-NPU consistency check (same synthetic 384x384, same weight, same preprocess, FP32)...
[INFO] Loading model from /opt/atomgit/adapt-npu-agent/model_cache/facebook_mask2former-swin-large-ade-semantic
[INFO] Model: facebook/mask2former-swin-large-ade-semantic Architecture: Mask2FormerForUniversalSegmentation (Swin-L backbone) Task: semantic-segmentation ADE20K 150 classes
[INFO] Original weight dtype: torch.float32
[INFO] Total params: 215.49M (215488779)
[INFO] Model loaded on cpu dtype=torch.float32 orig_dtype=torch.float32
CPU vs NPU class_queries diff: max=0.279711 mean=0.014383
CPU vs NPU masks_queries diff: max=2.098727 mean=0.085675
Class argmax agreement: 1.000000 (expected 1.0, both predict same class per query)
Semantic map pixel agreement: 0.999546 (147389/147456 pixels)
[PASS] CPU-NPU consistency within tolerance (argmax 1.0, pixel_agree 0.9995) masks max 2.0987 (threshold 0.6)
Saved cpu/npu npy to /tmp for compare_outputs.py validation
======================================================================
[RESULT] PASS - facebook/mask2former-swin-large-ade-semantic NPU inference successful
======================================================================解读:
npu:0 上以 FP32 成功前向, 首个参数 device npu:0, 输入 pixel_values npu:0, 输出 class [1,100,151] 与 masks [1,100,96,96] 均在 npu:0preprocessor_config.json: 384 固定尺寸, divisor 32, 归一化后输入384x384 仅 3 类 (ADE label 2 wall, 4 building, 6 window-like), Top 类 2 占比 0.668, 符合合成图白底大面积 wall 预测size_divisor 动态填充正确, 延迟 63.75ms (不同 shape 未走缓存编译, 仍全 NPU)allow_internal_format=False → base format 与 ArgSort on AICPU 为预期 NPU 友好提示, 非 fallback方法: 固定合成图 (seed 0, 无随机性, 确定性 Pillow 绘图), 同一 FP32 权重分别在 cpu 与 npu:0 上 eval 前向, 对 class_queries_logits 与 masks_queries_logits 计算逐元素绝对差, 并检查类别 argmax 与最终语义图像素一致率。
CPU vs NPU class_queries diff: max=0.279711 mean=0.014383
CPU vs NPU masks_queries diff: max=2.098727 mean=0.085675
Class argmax agreement: 1.000000 (expected 1.0, both predict same class per query)
Semantic map pixel agreement: 0.999546 (147389/147456 pixels)class argmax 1.0 且 pixel agreement >=0.99 即通过; 实测 1.0 与 0.999546 通过argmax 完全一致, 最终语义图 99.95% 像素一致 (仅 67/147456 像素在类别边界抖动)atol=1e-4 一刀切; 与 scripts/compare_outputs.py --task classification 的 argmax 逻辑一致, 回归/预测类任务亦分别检查数值fallback to CPU, 日志输入/参数/输出 device 均为 npu:0, torch.npu.is_available true官方未提供 ADE20K 全量评测脚本, 本验证为固定样例一致性, 禁止描述为完整精度评测。
环境: 合成图 384x384 RGB, batch=1, FP32, torch.npu.synchronize() 前后计时, warmup 3 次, 正式 10 次
命令: python inference.py --mode benchmark
Warming up 3 runs...
warmup 1 done
warmup 2 done
warmup 3 done
Benchmarking 10 runs...
run 1: 58.01 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
run 2: 58.62 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
run 3: 58.30 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
run 4: 57.89 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
run 5: 58.45 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
run 6: 58.20 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
run 7: 58.12 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
run 8: 58.15 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
run 9: 58.11 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
run 10: 57.91 ms output class torch.Size([1, 100, 151]) masks torch.Size([1, 100, 96, 96])
avg 58.18 min 57.89 max 58.62 p50 58.13 p90 58.46 p95 58.54 throughput 17.19 img/s batch=1 shape=384x384 dtype=FP32processor 的 resize/normalize 与 post_process_semantic_segmentation 的双线性插值, 均为 CPU 部分, 若仅计时 model(pixel_values, pixel_mask) 则略低 5msassets/agent_workflow.png — 完整阶段日志 (下载、侦察、加载、NPU 推理、一致性、性能)assets/npu_device_call.png — npu-smi info + torch.npu.is_available() + 设备名 + 参数 device/dtype + 输入/输出 deviceassets/model_result.png — python inference.py 真实输出与 PASS生成方式: node scripts/render_xterm_evidence.mjs --manifest evidence.json --output-dir assets --style raw 从真实日志渲染, PNG 含源日志 SHA-256。图片由 xterm.js 根据本次真实日志生成, 非原始桌面截图。

size_divisor 32 会填充 pixel_mask, 边缘块可能预测为 ignore 255, 建议保持官方 384 或 640 等 32 倍数ArgSort on AICPU (int64/int32), 对极大 batch (>8) 或超大图 (>1024) 可能在 CPU 侧增加延迟Hardware: NPU, NPU, Ascend, Ascend910, Ascend910B, image-segmentation, semantic-segmentation, Mask2Former, Swin-Transformer, ADE20K, facebook
#NPU