Skip to content

feat: Add Flash-Attention Support#1429

Open
bobqianic wants to merge 4 commits intoMouseLand:mainfrom
bobqianic:main
Open

feat: Add Flash-Attention Support#1429
bobqianic wants to merge 4 commits intoMouseLand:mainfrom
bobqianic:main

Conversation

@bobqianic
Copy link
Copy Markdown

@bobqianic bobqianic commented Apr 6, 2026

I noticed that Segment Anything doesn’t use Flash Attention, so I patched it using PyTorch’s built-in scaled_dot_product_attention.

https://github.com/bobqianic/cellpose/blob/f9957bb799e8bdf7ddfe59fe5a747c65623ffe1d/cellpose/sam/modeling/image_encoder.py#L232-L278

I benchmarked it on my dataset and observed a quite significant speedup. My setup uses an RTX 3060:

  • Eval (Vanilla): 1.108s
  • Eval (Flash-Attn): 0.585s

Speedup: ~1.89×

Performance may vary across different setups, but I believe this optimization can have a positive impact.

Changes

  • Added all files from Segment Anything
  • Replaced the image encoder attention with Flash Attention
  • Removed segment-anything from setup.py

Feedback

Feedback is very welcome, especially on performance across different hardware

Install

pip install git+https://github.com/bobqianic/cellpose.git

…remove setup.py dependency

- add all files from segment-anything
- replace image encoder attention with FlashAttention
- remove segment-anything from setup.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant