Abstract
SAM-Lightening is a highly optimized version of Meta’s Segment Anything Model (SAM) designed for real-time applications. By re-engineering the core attention mechanism with Dilated Flash Attention and introducing a novel Dynamic Layer-Wise Distillation (DLD) strategy, the model achieves a 30x speedup and a 96% reduction in memory usage compared to the original SAM-ViT-H. With an inference speed of 7ms per image and a memory footprint of 224MB, SAM-Lightening enables high-quality zero-shot segmentation on mobile and edge devices while maintaining competitive accuracy.
Introduction
The original Segment Anything Model (SAM) established a benchmark for zero-shot image segmentation. However, its massive image encoder, comprising 632 million parameters, accounts for over 98% of processing time, hindering real-time use in fields like Augmented Reality (AR) or mobile editing. While previous lightweight models like MobileSAM or FastSAM attempted to address this, they often sacrificed accuracy or failed to optimize the fundamental inefficiency of the attention mechanism.
Methods
SAM-Lightening addresses these bottlenecks through architectural innovation and a refined training curriculum.
Dilated Flash Attention
Standard self-attention is computationally expensive due to its quadratic complexity. SAM-Lightening introduces Dilated Flash Attention, which segments the input and applies sparsification by selecting rows at fixed intervals. This reduces data volume while remaining compatible with FlashAttention hardware-level optimizations, significantly accelerating memory access on modern GPUs.
Dynamic Layer-Wise Distillation (DLD)
To transfer knowledge from the massive SAM ViT-H "Teacher" to the "Student" model, the authors developed Dynamic Layer-Wise Distillation (DLD). This approach uses a progressive curriculum: early training focuses on aligning the student's initial layers with the teacher's, shifting dynamically to deeper layers as training evolves. This ensures the student builds a robust feature foundation before tackling complex high-level representations.
Results
The model was trained on a 1% subset of the SA-1B dataset and evaluated on COCO and LVIS benchmarks. SAM-Lightening demonstrates significant improvements across all key metrics:
- Speed: Inference is completed in 7ms, representing a 30.1x acceleration over the original SAM and a 2.1x improvement over MobileSAM.
- Memory: Peak memory usage is reduced by 96.5% to 224MB.
- Accuracy: On the COCO dataset with box prompts, it achieved a mean Intersection over Union (mIoU) of 78.8, closely trailing the original SAM (80.1) and outperforming MobileSAM (77.9).
Throughput tests also indicate that SAM-Lightening handles large batch sizes efficiently without the "Out of Memory" (OOM) errors common in the original model.
Conclusion
While SAM-Lightening offers massive gains, its performance is most pronounced on modern GPUs supporting FlashAttention. Additionally, the benefits of the attention mechanism vary with input resolution. Future work may involve combining SAM-Lightening with pruning or quantization to further enhance its efficiency for edge deployment.
Summary updated