Skip to content

feat: add pyproject.toml (uv) and Apple Silicon MPS support#47

Open
Micuks wants to merge 3 commits intoBIT-DataLab:mainfrom
Micuks:feat/pyproject-toml-and-mps-support
Open

feat: add pyproject.toml (uv) and Apple Silicon MPS support#47
Micuks wants to merge 3 commits intoBIT-DataLab:mainfrom
Micuks:feat/pyproject-toml-and-mps-support

Conversation

@Micuks
Copy link
Copy Markdown

@Micuks Micuks commented Mar 28, 2026

Background

I was trying to set up Edit Banana on my MacBook (Apple Silicon) and ran into a few issues:

  1. No pyproject.toml — had to manually pip install -r requirements.txt, and optional deps like torch/sam3 weren't structured, making it hard to manage with modern tools like uv.
  2. SAM3 device detection only checks for CUDA, so it falls back to CPU on macOS even though MPS (Metal Performance Shaders) is available and much faster.

Changes

1. Add pyproject.toml

  • Migrate deps from requirements.txt into a standard pyproject.toml
  • Add optional dependency groups so users can install what they need:
    • pip install -e ".[torch]" — PyTorch + torchvision
    • pip install -e ".[sam3]" — SAM3 + torch + missing upstream deps (einops, pycocotools, psutil)
    • pip install -e ".[paddleocr]" / .[formula] / .[rmbg] — same as before
  • Configure [tool.uv.sources] for local sam3_src/ editable install
  • Update .gitignore to exclude uv.lock and *.egg-info/

2. MPS device support

  • SAM3Model.__init__: auto-detect MPS when no device is specified (cuda > mps > cpu)
  • config.yaml.example: document the "mps" device option and PYTORCH_ENABLE_MPS_FALLBACK=1 env var

Note: Running on MPS also requires patches to the upstream SAM3 source (bfloat16 → float32 cast, fused op fallback, pin_memory guard, etc.). These are tracked in facebookresearch/sam3#173 and need to be applied to the local sam3_src/ clone. I can add a patch script or document the steps if that would be helpful.

Test

Tested on MacBook Pro (M-series) with Python 3.10, PyTorch 2.11, uv 0.6:

$ PYTORCH_ENABLE_MPS_FALLBACK=1 uv run python main.py -i input/test.png
...
[SAM3Model] 加载模型中... (设备: mps)
[SAM3Model] 模型加载完成!
...
Done: 1/1 succeeded

Compatibility

  • requirements.txt is kept as-is for users who don't use pyproject.toml
  • CUDA users are not affected — CUDA is still the first choice when available
  • The existing scripts/setup_sam3.sh workflow is unchanged

Micuks added 3 commits March 28, 2026 23:31
Migrate from requirements.txt to pyproject.toml for modern Python packaging.
Add optional dependency groups: paddleocr, formula, rmbg, torch, sam3.
Configure uv sources for local sam3_src editable install.
Also update .gitignore to exclude uv.lock and *.egg-info/.
Auto-detect MPS backend in SAM3Model when no device is specified
(cuda > mps > cpu). Update config.yaml.example to document the
"mps" option and the PYTORCH_ENABLE_MPS_FALLBACK=1 env var.

Tested on M-series MacBook with PyTorch 2.11 + MPS backend.
Upstream SAM3 MPS patches tracked in facebookresearch/sam3#173.
MPS backend lacks a few ops (e.g. aten::_assert_async). Setting
PYTORCH_ENABLE_MPS_FALLBACK=1 in main.py before any torch import
lets PyTorch silently fall back to CPU for unsupported ops, so
users don't need to remember the env var.
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