Skip to content

Strip the :N suffix on SAM3's single-prompt path (#15811) - #15840

Open
ntdat812 wants to merge 1 commit into
Comfy-Org:masterfrom
ntdat812:fix/sam3-single-prompt-suffix-15811
Open

Strip the :N suffix on SAM3's single-prompt path (#15811)#15840
ntdat812 wants to merge 1 commit into
Comfy-Org:masterfrom
ntdat812:fix/sam3-single-prompt-suffix-15811

Conversation

@ntdat812

Copy link
Copy Markdown

Fixes #15811.

The bug

:N sets max_detections for a category and defaults to 1, so person:1 is by definition the same detection prompt as person. _parse_prompts() strips the suffix correctly — the fast path did not:

parsed = _parse_prompts(text)
if len(parsed) <= 1 and (not parsed or parsed[0][1] == 1):
    return super().tokenize_with_weights(text, ...)   # raw text, still ":1"

That branch is taken for exactly two shapes: foo (nothing to strip) and foo:1 (everything to strip). So the only case it can affect is the broken one, and the encoder grounded on the literal "person:1".

The change

Forward parsed[0][0] instead of text, falling back to text when nothing parses so an empty prompt still reaches the tokenizer unchanged rather than becoming None.

One knock-on effect worth naming: _parse_prompts() also strips parentheses (SAM3 sets disable_weights = True), so (person) now tokenizes as person on this path too. That is what the multi-prompt path has always done — the two paths now agree instead of disagreeing on the same input.

Tests

tests-unit/comfy_test/sam3_prompt_parse_test.py — 10 tests asserting which string the inner tokenizer is actually handed.

They load sam3_clip.py with comfy.sd1_clip stubbed by a recording tokenizer, so the forwarding contract is checked without torch, transformers or model weights. The stub is scoped to the load and restored afterwards — it never enters sys.modules under the real module name, so nothing else in the session sees it.

Against master with only comfy/text_encoders/sam3_clip.py reverted:

FAILED test_max_detections_suffix_does_not_reach_the_encoder
FAILED test_explicit_one_matches_the_bare_prompt
FAILED test_single_prompt_shapes[person:1-expected1]
FAILED test_single_prompt_shapes[  person  :  1  -expected2]
FAILED test_single_prompt_shapes[a person on a bike:1-expected3]
FAILED test_single_prompt_shapes[(person)-expected4]
6 failed, 4 passed

The four that pass on both are the ones pinning what must not change: the bare person prompt, the empty-prompt fallback, the multi-prompt path (person:2, car → still two batches with [2, 1]), and _parse_prompts() itself.

With the fix: 10 passed. ruff check clean on both files.

@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 5dc7b7ba-f326-4706-9742-27aee4fec828

📥 Commits

Reviewing files that changed from the base of the PR and between b78cec8 and d8ff27d.

📒 Files selected for processing (2)
  • comfy/text_encoders/sam3_clip.py
  • tests-unit/comfy_test/sam3_prompt_parse_test.py

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (6)
**/*

📄 CodeRabbit inference engine (AGENTS.md)

**/*: Keep changes small, direct, and limited to the narrowest necessary code path and smallest number of files.
Prefer practical fixes, minimal dependencies, and existing repository patterns; remove obsolete, dead, unreachable, or unused code.
Preserve existing APIs, node names, model-loading behavior, file layout, and workflow compatibility unless replacement is explicitly intended.
Core ComfyUI must not add outbound internet requests, telemetry, tracking, reporting, remote configuration, or background network activity. User-authorized model downloads are limited to the requested artifact and must exclude telemetry and unrelated metadata.

Files:

  • comfy/text_encoders/sam3_clip.py
  • tests-unit/comfy_test/sam3_prompt_parse_test.py
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Keep state and capability flags on the object that owns the behavior. Prefer explicit parent-owned attributes over probing child objects with getattr; use child checks only when the child owns the delegated behavior.
Preserve shared method signatures, argument order, return shapes, side effects, and error behavior unless every affected caller and interface is intentionally updated.
Do not add unused compatibility parameters, flags, attributes, constructor options, or model-specific options to shared helpers; keep one-off behavior at the integration boundary.
Normalize third-party return conventions at integration boundaries so core code receives the expected type and shape; avoid undocumented caller-side unwrapping.
Do not add torch.no_grad, torch.inference_mode, or inference-mode wrappers. Do not add model freeze/unfreeze toggles; only disable globally enabled inference mode when a training path requires gradients.
Remove inference-only training behavior such as dropout while preserving checkpoint and state-dict compatibility; use nn.Identity when deleting a module would alter keys or ordering.
Keep imports at module scope except established optional-backend probes or imports required to avoid cycles; avoid unnecessary try/except blocks and use specific exceptions with useful fallbacks.
Do not add workarounds for unsupported library versions, especially PyTorch exception-and-float-cast retries, unless a comment names the exact versions still requiring them.
Let unsupported model formats, invalid quantization metadata, and bad states fail with clear errors instead of silently degrading output.
Match local style, keep comments sparse and useful, and remove comments that merely restate obvious code.
Treat dtype, device placement, VRAM use, and offloading as correctness concerns across CPU, CUDA, ROCm, MPS, DirectML, XPU, NPU, and low-VRAM environments.
Prefer existing ComfyUI and Comfy Kitchen operations, quantization helpers, cast/offload helpe...

Files:

  • comfy/text_encoders/sam3_clip.py
  • tests-unit/comfy_test/sam3_prompt_parse_test.py
**/*.{py,json}

📄 CodeRabbit inference engine (AGENTS.md)

Treat legacy combo, io.Combo, and io.DynamicCombo values affecting filesystem access as untrusted; revalidate them at load/save boundaries with folder_paths, containment checks, or fixed allowlists.

Files:

  • comfy/text_encoders/sam3_clip.py
  • tests-unit/comfy_test/sam3_prompt_parse_test.py
**/*.{py,md,txt,json}

📄 CodeRabbit inference engine (AGENTS.md)

Keep warning and info messages short and actionable, remove noisy or misleading logging, and make documentation edits concise, factual, and tied to changed behavior.

Files:

  • comfy/text_encoders/sam3_clip.py
  • tests-unit/comfy_test/sam3_prompt_parse_test.py
**

⚙️ CodeRabbit configuration file

**: IMPORTANT: Only comment on issues directly introduced by this PR's code changes.
Treat AGENTS.md as mandatory repository policy, not optional style guidance.
Flag PR changes that violate AGENTS.md even when the code is otherwise functional.
In particular, enforce architecture boundaries, dtype/device/memory rules,
interface contracts, import style, no unnecessary try/except blocks, no inline
imports, no outbound internet paths in core ComfyUI, and narrow scoped fixes.
Prefer direct findings over suggestions when a rule is violated. Only ignore
AGENTS.md when it clearly conflicts with a newer explicit maintainer instruction
in the PR.
Do NOT flag pre-existing issues in code that was merely moved, re-indented,
de-indented, or reformatted without logic changes. If code appears in the diff
only due to whitespace or structural reformatting (e.g., removing a with: block),
treat it as unchanged. Contributors should not feel obligated to address
pre-existing issues outside the scope of their contribution.

Files:

  • comfy/text_encoders/sam3_clip.py
  • tests-unit/comfy_test/sam3_prompt_parse_test.py
comfy/**

⚙️ CodeRabbit configuration file

comfy/**: Core ML/diffusion engine. Focus on:

  • Backward compatibility (breaking changes affect all custom nodes)
  • Memory management and GPU resource handling
  • Performance implications in hot paths
  • Thread safety for concurrent execution

Files:

  • comfy/text_encoders/sam3_clip.py
🔇 Additional comments (2)
comfy/text_encoders/sam3_clip.py (1)

54-59: LGTM!

tests-unit/comfy_test/sam3_prompt_parse_test.py (1)

1-138: LGTM!


📝 Walkthrough

Walkthrough

SAM3TokenizerWrapper.tokenize_with_weights() now passes the parsed phrase to the encoder for single prompts, including prompts with a :1 suffix. Empty prompts still use the original text. Multi-prompt tokenization remains unchanged. New unit tests stub dependencies, record encoder input, and verify suffix removal, whitespace and parenthesized forms, empty prompts, multi-prompts, detection counts, and _parse_prompts() results.

Merge Risk: ⚪ Minimal · up to d8ff2

The change makes single prompts such as "person:1" and "(person)" reach the encoder in their intended normalized form without changing multi-prompt behavior; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: removing the :N suffix on SAM3's single-prompt path.
Description check ✅ Passed The description directly explains the bug, the fix, affected behavior, and test coverage.
Linked Issues check ✅ Passed The changes satisfy issue #15811 by forwarding the parsed phrase while preserving bare, empty, multi-prompt, and parser behavior.
Out of Scope Changes check ✅ Passed The code and tests are limited to the SAM3 prompt-forwarding bug and its required regression coverage.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

`:N` sets max_detections per category and defaults to 1, so `person:1` is by
definition the same detection prompt as `person`. `_parse_prompts()` strips
the suffix, but the fast path in `SAM3TokenizerWrapper.tokenize_with_weights()`
forwarded the raw text — and that branch is taken for exactly the `foo:1`
shape, so the encoder grounded on the literal "person:1".

Forward the parsed phrase instead, falling back to the raw text when nothing
parses (empty prompt). This also makes the fast path agree with the
multi-prompt path on parentheses, which `_parse_prompts()` already strips.
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.

SAM3: "person:1" is not equivalent to "person" — the ":N" suffix leaks into the encoded prompt

1 participant