SAM (Segment Anything Model) ViT-Huge adapted for Huawei Ascend NPU inference. This model card documents the NPU adaptation, accuracy verification, and performance benchmarks on Ascend 910 hardware.
| Component | Version |
|---|---|
| CANN | 8.5.1 |
| PyTorch | 2.9.0 |
| torch_npu | 25.5.2 |
| transformers | 4.57.6 |
| NPU | Ascend 910 (2 chips) |
| Precision | float16 (NPU) |
pip install torch torch_npu transformers modelscope pillow requests numpy# Via ModelScope
pip install modelscope
python -c "from modelscope import snapshot_download; snapshot_download('KyanChen/sam-vit-huge')"Model will be cached at: ~/.cache/modelscope/hub/models/KyanChen/sam-vit-huge/
import torch
import torch_npu
from transformers import SamModel, SamProcessor
from PIL import Image
# Load model in float16 for NPU
model_path = "KyanChen/sam-vit-huge" # or local path
model = SamModel.from_pretrained(model_path, torch_dtype=torch.float16).to("npu:0")
model.eval()
processor = SamProcessor.from_pretrained(model_path)
# Load image
image = Image.open("your_image.jpg").convert("RGB")
# Point prompt inference
inputs = processor(image, input_points=[[[320, 240]]], input_labels=[[1]],
return_tensors="pt")
inputs = {k: (v.to("npu:0").to(torch.float16) if v.dtype == torch.float32 else v.to("npu:0"))
for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
masks = processor.image_processor.post_process_masks(
outputs.pred_masks.cpu(),
inputs["original_sizes"].cpu(),
inputs["reshaped_input_sizes"].cpu()
)Or use the provided inference script:
python inference.pyNPU (float16) vs CPU (float32) accuracy comparison on COCO val2017 test image.
| Metric | Value | Threshold | Status |
|---|---|---|---|
| Logit Relative Error | 0.19% | <1% | PASS |
| Cosine Similarity | 1.000005 | >0.999 | PASS |
| Mask IoU | 0.9859 | >0.98 | PASS |
| Pixel Accuracy | 99.85% | >99% | PASS |
| Normalized Mean Diff | 4.0e-04 | <0.01 | PASS |
Result: NPU float16 inference matches CPU float32 reference within tolerance. All accuracy metrics pass.
Single point-prompt inference on 640x480 image, Ascend 910 NPU, float16.
| Metric | Value |
|---|---|
| Mean Latency | 170.82 ms |
| Median Latency | 170.68 ms |
| P95 Latency | 172.11 ms |
| Std Deviation | 0.93 ms |
| Min Latency | 169.65 ms |
| Max Latency | 172.54 ms |
Performance is stable with <1% variance across 20 iterations after warmup.
sam-npu-adapt/
├── inference.py # NPU inference & evaluation script
├── README.md # This file
└── output/
├── benchmark_npu.json # Performance benchmark results
├── accuracy_results.json # Accuracy comparison results
└── accuracy_logit_results.json # Detailed logit-level comparisonApache License 2.0 (same as original SAM model)
@article{kirillov2023segany,
title={Segment Anything},
author={Kirillov, Alexander and Mintun, Eric and Ravi, Nikhila and Mao, Hanzi and Rolland, Chloe and Gustafson, Laura and Xiao, Tete and Whitehead, Spencer and Berg, Alexander C. and Lo, Wan-Yen and Dollar, Piotr and Girshick, Ross},
journal={arXiv:2304.02643},
year={2023}
}