Repository navigation
Description
System Info
transformersversion: 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)
- Text-only prompts get an empty dummy geometric prompt: sam3_image_processor.py#L124
_encode_promptalways calls the geometry encoder, and the training forward uses the same path: sam3_image.py#L190, forward- The encoder appends the CLS whenever
cls_embedexists: geometry_encoders.py#L821-L829. The released model is built withadd_cls=True, the text-only training config setsadd_cls: true, and the encoder asserts "It's currently highly recommended to add a CLS when using a transformer" (L576-L578).
transformers (main @5602e09)
- Gate: modeling_sam3.py#L2371-L2376
- Text-only branch: L2427
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
devtyagi3909 commentedon Oct 14, 2026
opened a fix in #49614 - removes the outer
has_geometry_promptsgate so the geometry encoder is invoked for text-only prompts and passes the learned cls token through to detr, with unit test coverage added.BurakBebek1 commentedon Oct 14, 2026
Hi, thanks for the fix!
I independently reproduced #49592 on macOS using the
facebook/sam3checkpoint 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!
devtyagi3909 commentedon Oct 15, 2026
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.
markhayden commentedon Nov 8, 2026
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) andSam3Model(fp32) on three COCO val2017 images, 12 text prompts, all on the CPU, and compared instance scores:Sam3Model, text onlySam3Modelwith the geometry encoder run (workaround below)Text-only scores come out consistently high. Some examples:
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:fix(sam3): invoke geometry encoder for text-only prompts to include l…
github-actions commentedon Dec 3, 2026
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.