Skip to content

SAM 3: text-only prompts drop the geometry CLS token that Meta's implementation always adds #49592

Open

Description

@ju-ius

System Info

  • transformers version: 5.19.0 (same code on main @5602e09)
  • Platform: macOS-26.6.2-arm64-arm-64bit
  • Python version: 3.12.13
  • Huggingface_hub version: 2.2.0
  • Safetensors version: 0.8.0
  • PyTorch version (accelerator?): 2.14.1 (NA)
  • Using distributed or parallel set-up in script?: No

Who can help?

@yonigozlan (SAM 3 port), @molbap @guarin

Description

For text-only prompts, Sam3Model skips the geometry encoder, so the DETR encoder and decoder only see the text tokens. Meta's reference implementation always runs the geometry encoder, also for text-only prompts, and appends its learned CLS token to the text tokens. From the public code, the checkpoint was trained with that token always present.

Reproduction

import requests
import torch
from PIL import Image
from transformers import Sam3Model, Sam3Processor

model = Sam3Model.from_pretrained("facebook/sam3").eval()
processor = Sam3Processor.from_pretrained("facebook/sam3")
url = "http://images.cocodataset.org/val2017/000000077595.jpg"
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")

calls, prompt_len = [], []
model.geometry_encoder.register_forward_hook(lambda module, args, output: calls.append(1))
model.detr_encoder.register_forward_pre_hook(
    lambda module, args, kwargs: prompt_len.append(kwargs["text_features"].shape[1]), with_kwargs=True
)

def run(**prompt):
    calls.clear()
    prompt_len.clear()
    with torch.no_grad():
        model(**processor(images=image, return_tensors="pt", **prompt))
    return len(calls), prompt_len[0]

print("text only : geometry encoder calls = %d, prompt tokens = %d" % run(text="ear"))
print("text + box: geometry encoder calls = %d, prompt tokens = %d" % run(
    text="ear", input_boxes=[[[100.0, 100.0, 300.0, 300.0]]], input_boxes_labels=[[1]]))

Output:

text only : geometry encoder calls = 0, prompt tokens = 32
text + box: geometry encoder calls = 1, prompt tokens = 34

Expected behavior

As in Meta's implementation, the geometry encoder also runs for text-only prompts, so the DETR encoder receives the text tokens plus the CLS token (33 instead of 32 above).

References

Meta (facebookresearch/sam3 @0570b3a)

transformers (main @5602e09)

Related. #46128 (SAM 3.1, open) removes this gate. Its code comment says skipping the call "produces a different prompt structure than the pretrained checkpoint was trained with". That PR has been inactive since June, and the issue affects SAM 3 on main independently of SAM 3.1. #45059 (closed) shows a related symptom: adding a dummy box, which runs the geometry encoder, changes text-only results.

Suggestion

Always run the geometry encoder, as #46128 does, or make the behaviour explicit with a flag and document that text-only outputs otherwise differ from Meta's reference.

Activity

  1. devtyagi3909 commented on Oct 14, 2026

    @devtyagi3909

    opened a fix in #49614 - removes the outer has_geometry_prompts gate so the geometry encoder is invoked for text-only prompts and passes the learned cls token through to detr, with unit test coverage added.

  2. BurakBebek1 commented on Oct 14, 2026

    @BurakBebek1

    Hi, thanks for the fix!

    I independently reproduced #49592 on macOS using the facebook/sam3 checkpoint and also confirmed the behavior with a failing regression test.

    I was planning to ask the maintainers whether this is indeed a bug or an intentional implementation decision.

    Could a maintainer confirm whether the proposed fix aligns with the intended behavior and Meta's reference implementation?

    Thanks!

  3. devtyagi3909 commented on Oct 15, 2026

    @devtyagi3909

    yeah meta's reference PromptEncoder always runs on empty coords and appends the learned cls token to prompt tokens. in transformers the forward pass actually already prepared the zero placeholder in the else branch, but the outer if was gating it. pr #46128 also called out that dropping it produces a different prompt structure than pretraining.

  4. markhayden commented on Nov 8, 2026

    @markhayden

    I can confirm this on transformers 5.22.1 and add numbers on how much it moves the scores.

    I ran Meta's code (facebookresearch/sam3 @ f1647e8, sam3.pt, bf16 autocast as in its examples) and Sam3Model (fp32) on three COCO val2017 images, 12 text prompts, all on the CPU, and compared instance scores:

    max |score − Meta| (top instances) median
    Sam3Model, text only 0.110 0.031
    Sam3Model with the geometry encoder run (workaround below) 0.016 0.003

    Text-only scores come out consistently high. Some examples:

    image prompt Meta text only with workaround
    000000000139 potted plant 0.594 0.439 0.406 0.295 0.692 0.548 0.509 0.381 0.599 0.445 0.409 0.298
    000000000139 vase (top 3) 0.895 0.742 0.719 0.932 0.811 0.811 0.893 0.736 0.714
    000000000139 clock 0.863 0.899 0.863
    000000039769 cat 0.961 0.957 0.974 0.968 0.961 0.957

    The remaining 0.016 is consistent with bf16 against fp32.

    Because the scores shift, an instance near the cut-off can appear or disappear. On a set of 20 images of my own, one object at 0.35 with text only is below 0.2 in Meta's code and in Meta's playground, so a 0.2 cut-off finds it in Transformers only. With the workaround, all 20 images find the same instances as Meta's code at both 0.2 and 0.5.

    Workaround until #49614 lands: pass one padding box (label -10). That runs the geometry encoder, which appends its CLS token, without adding a box:

    inputs = processor(images=image, text="ear", return_tensors="pt")
    outputs = model(
        **inputs,
        input_boxes=torch.zeros(1, 1, 4),
        input_boxes_labels=torch.full((1, 1), -10, dtype=torch.long),
    )
  5. github-actions commented on Dec 3, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

    Please note that issues that do not follow the contributing guidelines are likely to be ignored.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions