panhg/MambaVision-B-1K
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

MambaVision-B-1K (Ascend NPU Adaptation)

MambaVision-B is a hybrid vision model combining Mamba (State Space Models) with self-attention for image classification, developed by NVIDIA. This repository provides an Ascend NPU-adapted inference pipeline that enables deployment on Huawei Ascend 910 hardware without requiring CUDA-dependent mamba_ssm kernels.

Model Overview

PropertyValue
ModelMambaVision-B-1K
TaskImage Classification (ImageNet-1K)
Parameters97.69M
Input224x224 RGB image
Output1000-class probability distribution
Original FrameworkPyTorch + mamba_ssm (CUDA)
NPU AdaptationPure PyTorch selective scan (Ascend-compatible)
CANN Version8.5.1
Ascend HardwareAscend 910 (Atlas 800 A2)

Architecture

MambaVision combines three architectural paradigms across 4 stages:

StageDepthTypeResolution
13ConvBlocks56x56
23ConvBlocks28x28
310Mamba SSM + Self-Attention14x14
45Mamba SSM + Self-Attention7x7

The key innovation is using Mamba selective scan (S6 SSM) in early blocks and transitioning to self-attention in later blocks within each transformer stage.

NPU Adaptation Notes

The original model relies on mamba_ssm.ops.selective_scan_interface.selective_scan_fn, a CUDA-optimized kernel incompatible with Ascend NPU. This adaptation replaces it with a pure PyTorch implementation of the selective scan algorithm that works on CPU, CUDA, and Ascend NPU.

Key Modifications

  1. Selective Scan: Replaced mamba_ssm CUDA kernel with selective_scan_pytorch() -- a pure PyTorch sequential scan implementing the S6 recurrence
  2. Weight Key Mapping: Mapped checkpoint gamma_1/gamma_2 to model g_1/g_2 (layer scale parameters)
  3. Device Handling: Model loads directly to NPU via torch_npu

Quick Start

Requirements

# Ascend NPU environment
torch >= 2.9.0
torch_npu >= 2.9.0
CANN >= 8.5.1

# Dependencies
pip install torchvision timm einops pillow

Download Model

pip install modelscope
modelscope download --model nv-community/MambaVision-B-1K

Inference

# NPU inference
python inference.py --device npu

# CPU inference
python inference.py --device cpu

# With custom image
python inference.py --device npu --image /path/to/image.jpg

Accuracy Verification

# Compare NPU vs CPU precision
python accuracy_test.py

# With custom tolerance
python accuracy_test.py --tolerance 0.01

Performance Benchmark

# NPU benchmark
python benchmark.py --device npu:0

# Custom runs
python benchmark.py --warmup 20 --runs 200

Evaluation Results (Ascend 910 NPU)

Accuracy (NPU vs CPU)

MetricValueThresholdStatus
Mean Absolute Error4.2x10^{-7}< 1x10^{-2}PASS
Max Absolute Error1.61x10^{-4}----
Max Relative Error2.15x10^{-3}----
SNR71.40 dB----
Cosine Similarity1.00000000> 0.9999PASS
Top-1 MatchTrue--PASS
Top-5 Overlap5/5--PASS

Performance (Ascend 910 x1)

MetricValue
Mean Latency192.54 ms
Min Latency185.99 ms
Max Latency195.33 ms
Throughput5.19 FPS
NPU Memory0.40 GB / 65.79 GB

Sample Inference

Input: COCO val2017/000000039769.jpg (two cats on a couch)

Rank  Class                        Confidence
1     tabby, tabby cat             0.2825
2     Egyptian cat                  0.1323
3     tiger cat                     0.1202
4     remote control, remote        0.0532
5     cellular telephone            0.0026

File Structure

mambavision-npu/
|-- inference.py          # Main inference script (NPU/CPU)
|-- accuracy_test.py      # Accuracy verification (NPU vs CPU)
|-- benchmark.py          # Performance benchmark
|-- README.md             # This document
|-- results/
    |-- accuracy.json     # Accuracy test results
    |-- benchmark.json    # Performance benchmark results
    |-- run_log.txt       # Full execution log
    |-- accuracy_verification.json  # Verification details

Citation

@misc{hatamizadeh2025mambavision,
      title={MambaVision: A Hybrid Mamba-Transformer Vision Backbone},
      author={Ali Hatamizadeh and Jan Kautz},
      year={2025},
      eprint={2407.08083},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

License

Apache 2.0 -- see LICENSE in the original NVIDIA repository.


Adapted for Huawei Ascend NPU by implementing pure PyTorch selective scan. Tested on Ascend 910 with CANN 8.5.1 and torch_npu 2.9.0.