Skip to content

Add build_sam3_multiplex_tracking and Sam3VideoPredictor multiplex routing - #600

Open
johnwbrisbin wants to merge 1 commit into
facebookresearch:sam3.1from
johnwbrisbin:feat/multiplex-image-inference
Open

Add build_sam3_multiplex_tracking and Sam3VideoPredictor multiplex routing#600
johnwbrisbin wants to merge 1 commit into
facebookresearch:sam3.1from
johnwbrisbin:feat/multiplex-image-inference

Conversation

@johnwbrisbin

Copy link
Copy Markdown

Summary

Adds three capabilities needed for single-image inference with the SAM 3.1 multiplex checkpoint:

  1. build_sam3_multiplex_tracking() — new public builder that assembles Sam3MultiplexTrackingWithInteractivity without the video-predictor wrapper. Exposes .detector (Sam3MultiplexDetector) for use with Sam3Processor-based image inference. Includes FA3 auto-detection (imports flash_attn_interface, falls back to use_fa3=False if absent).

  2. Sam3VideoPredictor multiplex routing — when checkpoint_path contains "multiplex", __init__ now calls build_sam3_multiplex_tracking instead of build_sam3_video_model, which was incompatible with the multiplex checkpoint architecture (different tensor shapes in maskmem_backbone). Adds enable_inst_interactivity parameter (instance interactivity is always enabled in the multiplex architecture).

  3. decoder.py forward_ffn dtype fix — casts tgt to the weight dtype before the autocast-disabled region, preventing dtype mismatches when torch.compile emits bf16 kernels (observed on CUDA 13 / Blackwell).

  4. sam3/__init__.py — exports Sam3VideoPredictor and Sam3Processor from the top-level package.

Motivation

Sam3VideoPredictor loaded with sam3.1_multiplex_fp16.safetensors crashed with size mismatches because the multiplex checkpoint uses a different maskmem_backbone architecture than the non-multiplex model built by build_sam3_video_model. build_sam3_multiplex_tracking builds the correct architecture and loads the full checkpoint (tracker + detector) in one pass.

Test plan

  • Load sam3.1_multiplex_fp16.safetensors via Sam3VideoPredictor — previously crashed with size mismatch, now loads the multiplex architecture correctly
  • Confirm Sam3VideoPredictor with a SAM 3 (non-multiplex) checkpoint still routes to build_sam3_video_model unchanged
  • Confirm build_sam3_multiplex_tracking() returns a model with .detector accessible to Sam3Processor
  • Confirm bf16 inference on CUDA 13 no longer raises dtype errors in forward_ffn

Adds three related capabilities needed for single-image inference with
the SAM 3.1 multiplex checkpoint:

1. build_sam3_multiplex_tracking(): new public builder that assembles
   Sam3MultiplexTrackingWithInteractivity without the video-predictor
   wrapper. Exposes .detector (Sam3MultiplexDetector) for use with
   Sam3Processor-based image inference. Includes FA3 auto-detection
   (imports flash_attn_interface, falls back to use_fa3=False if absent).

2. Sam3VideoPredictor routing: when checkpoint_path contains "multiplex",
   __init__ now calls build_sam3_multiplex_tracking instead of
   build_sam3_video_model, which was incompatible with the multiplex
   checkpoint architecture (different tensor shapes in maskmem_backbone).
   Adds enable_inst_interactivity parameter (no-op; instance interactivity
   is always enabled in the multiplex architecture).

3. decoder.py forward_ffn: cast tgt to the weight dtype before the
   autocast-disabled region. Prevents dtype mismatches when torch.compile
   emits bf16 kernels (observed on CUDA 13 / Blackwell) while the
   activation tensor is still fp32 from an outer autocast context.

4. sam3/__init__.py: export Sam3VideoPredictor and Sam3Processor so
   callers can import them directly from the top-level package.
@meta-cla

meta-cla Bot commented Jul 1, 2026

Copy link
Copy Markdown

Hi @johnwbrisbin!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 1, 2026
@meta-cla

meta-cla Bot commented Jul 1, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant