Skip to content

[SAM3] Enable single-scale input support in Mask Decoder#43044

Open
CodersAcademy006 wants to merge 3 commits into
huggingface:mainfrom
CodersAcademy006:feat/sam3-single-scale-support
Open

[SAM3] Enable single-scale input support in Mask Decoder#43044
CodersAcademy006 wants to merge 3 commits into
huggingface:mainfrom
CodersAcademy006:feat/sam3-single-scale-support

Conversation

@CodersAcademy006

Copy link
Copy Markdown

Enables single-scale inference for Sam3MaskDecoder, bringing parity with the original implementation where full FPN levels aren't strictly required. I modified the forward method to handle single-tensor inputs by bypassing the multi-scale fusion block and routing the feature directly through the primary projection layer.

This change is fully backward compatible: standard multi-scale inputs continue to use the existing FPN path. I also added a contract check that logs a warning when the input scale count deviates from the config, ensuring users are explicitly aware when the model falls back to single-scale mode. Tests added to verify both the new fallback path and regression safety.

Fixes #43043

Srijan Upadhyay added 2 commits May 2, 2026 21:50
Allow Sam3MaskDecoder.forward to receive a single tensor for
backbone_features in addition to the standard multi-scale list.
When a bare tensor is passed it is wrapped in a list internally
and a UserWarning is emitted so callers are aware they are using
the single-scale fallback path.

The existing _embed_pixels method and multi-scale FPN path are
unchanged — a single-element list already works correctly through
_embed_pixels since it operates on the last element of the list.

Adds test_mask_decoder_single_scale_input to verify:
- single tensor input produces valid output and emits UserWarning
- multi-scale list input continues to work unchanged

Fixes huggingface#43043
Sam3LiteTextMaskDecoder inherits from Sam3MaskDecoder via the modular
conversion system. Apply the same single-tensor normalization introduced
in the parent class so the generated file stays consistent with what
check_modular_conversion expects.

Fixes check_repository_consistency CI failure caused by stale generated
modeling_sam3_lite_text.py after the parent class was updated.
@github-actions

github-actions Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: sam3, sam3_lite_text

…tput

Align docstring wording with what check_modular_conversion generates from
the parent Sam3MaskDecoder class, resolving the remaining consistency check
failure.
@Qodo-Free-For-OSS

Copy link
Copy Markdown

Hi, Sam3MaskDecoder/Sam3LiteTextMaskDecoder only warn when backbone_features is a Tensor, but do not validate the length when a list is provided, so incorrect multi-scale inputs can still crash with an IndexError inside the pixel decoder.

Severity: action required | Category: correctness

How to fix: Validate feature list length

Agent prompt to fix - you can give this to your LLM of choice:

Issue description

Sam3MaskDecoder.forward (and lite text variant) only warns for Tensor inputs, but list inputs can still be an unexpected length and crash later in Sam3PixelDecoder.forward with an IndexError.

Issue Context

Sam3PixelDecoder has num_upsampling_stages conv/norm layers, so it can only fuse up to num_upsampling_stages + 1 feature maps.

Fix Focus Areas

  • src/transformers/models/sam3/modeling_sam3.py[2047-2083]
  • src/transformers/models/sam3/modeling_sam3.py[1940-1987]
  • src/transformers/models/sam3_lite_text/modeling_sam3_lite_text.py[1816-1852]

What to change

  • Add an explicit check after normalization to a list:
    • if len(backbone_features) == 0: raise a clear ValueError
    • if len(backbone_features) > self.config.num_upsampling_stages + 1: raise ValueError (or warn and truncate, but be explicit)
    • optionally warn when len(backbone_features) != self.config.num_upsampling_stages + 1 to satisfy the PR contract claim.
  • Ensure the same logic exists in both decoders.

We noticed a couple of other issues in this PR as well - happy to share if helpful.


Found by Qodo code review. FYI, Qodo is free for open-source.

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.

Single-scale image input support for SAM3 Mask Decoder

3 participants