Repository navigation
[https://nvbugs/6963140][fix] Default Gemma3 VLM to FlashInfer attention and reject other backends - #23238
[https://nvbugs/6963140][fix] Default Gemma3 VLM to FlashInfer attention and reject other backends#23238aswinvisva wants to merge 1 commit into
Conversation
…ion and reject other backends Signed-off-by: Aswin Visva <31215515+aswinvisva@users.noreply.github.com>
trtllm-agent-review
left a comment
There was a problem hiding this comment.
Thanks for the PR. Defaults Gemma 3 VLM (Gemma3ForConditionalGeneration) to the FlashInfer attention backend its text decoder is built with, and rejects any other attn_backend at model construction, to fix the warmup crash with default settings.
Reviewed at commit 060910cc2b (3 files, +26/−2).
The image + KV-reuse failure also exists on main with an explicit attn_backend='FLASHINFER'; it is raised here because this PR makes that path the default.
Request changes: 1 Blocker needs fixing before merge.
Blockers (1)
- (B2: Crash, hang, race or leak) The new default keeps KV block reuse on, so in the default configuration a Gemma 3 image request that hits the KV cache raises in forward, fails every active request and can shut the executor down. (modeling_gemma3vl.py:195)
Possible blockers (1)
- (B5: User-visible switch behavior) The #13293 reproduction (quickstart_multimodal.py on gemma-3-4b-it) still fails: quickstart_advanced.py passes attn_backend='TRTLLM' explicitly, so the new default never applies and the new ValueError fires. (modeling_gemma3vl.py:219)
Nice to have (4): 4 inline, 0 below
- (N1: Architecture and design) FLASHINFER now has to agree in three places, although the shared Gemma 3 decoder accepts TRTLLM custom masks since #16214; passing model_config.attn_backend to the text sub-config keeps one source of truth. (modeling_gemma3vl.py:219)
- (N2: Redundant tests) The new TRTLLM row adds 8 cases that repeat what the gemma4 explicit-backend rows already pin, and reads as if TRTLLM were a usable Gemma 3 VLM setting. (test_llm_args.py:1488)
- (N3: Missing or ineffective tests) No CI test runs Gemma 3 VLM with the new default, so the fixed warmup crash and the engine/decoder backend agreement have no regression test. (test_modeling_gemma3.py:763)
- (R6: Required documentation) The FlashInfer-only restriction is undocumented: the Gemma3ForConditionalGeneration row of supported-models.md has no backend note since the Gemma3 VL README section was removed in #16610. (modeling_gemma3vl.py:3)
What this review checked
- The new cpu_only test fails on the old code: without the guard,
Gemma3VLM.__init__reachestorch.tensor(..., device="cuda")(modeling_gemma3vl.py:231) and raises something other than the matched ValueError; the file now carriespytest.mark.cpu_only, so l0_cpu'sunittest/_torch/modelingentry collects it, and its module imports match other cpu_only files collected there. - The default reaches the engine:
ModelLoader.load_config_with_defaultsapplies it beforeModelConfigtakesllm_args.attn_backend(model_loader.py:1916) and beforeresolve_attention_backendpicks the metadata (engine/model_setup.py:513);Gemma3ForConditionalGenerationis the only architecture that maps toGemma3VLM, and an explicit user value still wins. - The guard upper-cases the value as
get_attention_backenddoes (attention/backends/utils.py:44) and runs before any allocation; no existing test or tool buildsGemma3VLMwith another backend (the LoRA tests use__new__). - Not covered: No GPU run: the tests and the image + KV-reuse failure are traced in code, not executed.
- Not covered: TRTLLM-backend image accuracy for Gemma 3 VLM (the design question) was not measured.
- Not covered: No CI run of this PR yet.
Automated review by trtllm-agent-review. If a point is wrong, reply in its thread. Once you have pushed code changes and replied in every Blocker thread, the bot re-checks the Blockers within a few hours. A Blocker you think should not be fixed needs no code change: reply in its thread with the reason and the evidence.
| @classmethod | ||
| def get_model_defaults(cls, llm_args: "TorchLlmArgs") -> dict: | ||
| """Default to the attention backend the text decoder requires.""" | ||
| return {"attn_backend": "FLASHINFER"} |
There was a problem hiding this comment.
[Blocker] (B2: Crash, hang, race or leak) With this default the server starts, but KV block reuse stays on (KvCacheConfig.enable_block_reuse defaults to True, tensorrt_llm/llmapi/llm_args.py:6651), and the Gemma 3 image path cannot run a prefill whose prefix came from the cache:
Gemma3VLM.forwardpassesimage_token_maskwhenever the batch carries pixel values (lines 331-353), andGemma3ForCausalLM._get_image_context_lengthsassertsnot any(cached_token_lens)for every context request in that batch (modeling_gemma3.py:525-533). A hit that reaches into the image fails one step earlier with "Multimodal token count mismatch" (modeling_multimodal_utils.py:755-759).- So the next request that shares a cached prefix with an earlier one (the same image again, a multi-turn chat that resends the image, or a shared system prompt of one 32-token block), or any cache hit batched with an image request, raises in forward.
_forward_stepthen calls_handle_errors(error_msg)without a request list (py_executor.py:13137), which fails every active request, other users' text requests included (py_executor.py:13478), and charges the error budget (0.1 of 1.0 per error,error_classification.py:118-148); about ten of these within a few seconds shut the executor down (py_executor.py:13439).
The description notes that image prompts still need enable_block_reuse=False until #22886. Since this PR is what makes the default configuration usable, the default should carry that requirement too; the multimodal README removed in #16610 listed FlashInfer, chunked prefill off and KV cache reuse off as required for Gemma3 VL. Suggested fix, as Qwen3NextForCausalLM and NemotronHForCausalLM do, keeping reuse where a deployment needs it: a KV connector (py_executor.py:2149-2181) or a block_reuse_config option (llm_args.py:7153-7200):
@classmethod
def get_model_defaults(cls, llm_args: "TorchLlmArgs") -> dict:
"""FlashInfer for the text decoder, and no block reuse: an image prefill cannot start from cached KV."""
defaults = {"attn_backend": "FLASHINFER"}
# A KV connector and block_reuse_config options need block reuse.
if (llm_args.kv_connector_config is None
and "block_reuse_config" not in llm_args.kv_cache_config.model_fields_set):
defaults["kv_cache_config"] = {"enable_block_reuse": False}
return defaultsAn explicit enable_block_reuse=True still wins (user fields are merged last, llm_utils.py:537-558). In test_gemma_scratch_defaults, assert enable_block_reuse is False for the gemma3_vlm rows with kv_setting="omitted" (fails without the change), and add a case with a KvCacheConnectorConfig and one with a block_reuse_config where it stays True. Deployments that keep reuse on (those two, and entry points that pass enable_block_reuse themselves: quickstart_advanced.py:285, trtllm-eval at commands/eval.py:192) still need image prompts without cache hits until #22886 lands; drop the default then.
| "Gemma3VLM does not support disaggregated inference yet. Please unset " | ||
| f"the {_MULTIMODAL_ENV_NAME} environment variable, or set it to '0'." | ||
| ) | ||
| if model_config.attn_backend.upper() != "FLASHINFER": |
There was a problem hiding this comment.
[Nice to have] (N1: Architecture and design) Is FlashInfer still required for the VLM, or only pinned? The mismatch this fixes comes from the literal in get_sub_model_config (preferred_backend = "FLASHINFER" if name == "text_config" else "TRTLLM", line 270, from #5973), while the engine builds its metadata from llm_args.attn_backend (engine/model_setup.py:513). Since #16214 the shared decoder accepts TrtllmAttentionMetadata with custom masks (modeling_gemma3.py:294-299), and the TRTLLM backend has a Triton custom-mask context FMHA (attention/backends/fmha/triton_custom_mask.py). Passing model_config.attn_backend to the text sub-config (vision stays TRTLLM) would make the decoder and the engine agree by construction, keep TRTLLM selectable with text-only Gemma 3's defaults, and spare FLASHINFER from having to match in three places (this guard, get_model_defaults and get_sub_model_config), where relaxing one later brings the warmup crash back. If FlashInfer has to stay (for example because that FMHA declines SWA scratch reuse, triton_custom_mask.py:122-123, or TRTLLM image accuracy is not validated), please say why in the docstring rather than "the attention backend the text decoder requires", and derive the guard and the sub-config from one constant.
| ("gemma4", 100, "TRTLLM", True, "TRTLLM", False), | ||
| ("gemma3_vlm", 100, None, False, "TRTLLM", False), | ||
| ("gemma3_vlm", 100, None, False, "FLASHINFER", False), | ||
| ("gemma3_vlm", 100, "TRTLLM", False, "TRTLLM", False), |
There was a problem hiding this comment.
[Nice to have] (N2: Redundant tests) This row's 8 cases (4 kv_setting x 2 enable_block_reuse) check only that an explicit attn_backend survives the model defaults, which rows such as ("gemma4", 90, "TRTLLM", False, "TRTLLM", True) (line 1480) already pin; Gemma3VLM.get_model_defaults returns a constant, and the rejection is tested by test_gemma3_vlm_rejects_non_flashinfer_attn_backend. It also reads as if TRTLLM were a usable explicit Gemma 3 VLM setting. Consider dropping it, or add a comment that Gemma3VLM.__init__ rejects this value.
|
|
||
|
|
||
| @pytest.mark.cpu_only | ||
| def test_gemma3_vlm_rejects_non_flashinfer_attn_backend() -> None: |
There was a problem hiding this comment.
[Nice to have] (N3: Missing or ineffective tests) Nothing in CI runs Gemma3ForConditionalGeneration with the new default: no test-db list or accuracy suite has a Gemma 3 VLM checkpoint, and the GPU tests in this file build Gemma3ForCausalLM only. So the warmup crash this PR fixes has no regression test; these tests pin the default's value and the rejection, not that the engine and the decoder now agree. A cheap cpu_only check of that invariant: apply Gemma3VLM.get_model_defaults to TorchLlmArgs(model=...), build ModelConfig(pretrained_config=config, attn_backend=args.attn_backend) and assert Gemma3VLM.get_sub_model_config(model_config, "text_config").attn_backend == args.attn_backend. If a Gemma 3 VLM checkpoint is in $LLM_MODELS_ROOT, a short GPU test with one text and one repeated image prompt at default settings would cover the original crash and the block-reuse case on get_model_defaults.
There was a problem hiding this comment.
[Possible blocker] (B5: User-visible switch behavior) tensorrt_llm/_torch/models/modeling_gemma3vl.py:219: examples/llm-api/quickstart_advanced.py:78-81 defaults --attention_backend to 'TRTLLM', and setup_llm always passes it (attn_backend=args.attention_backend, examples/llm-api/quickstart_advanced.py:357); examples/llm-api/quickstart_multimodal.py:204 builds its LLM through setup_llm. An explicitly set field wins over model defaults (tensorrt_llm/llmapi/llm_utils.py:537-558), so Gemma3VLM.get_model_defaults never applies there, and the reproduction from #13293 (python quickstart_multimodal.py --model_dir google/gemma-3-4b-it --modality image) now stops in Gemma3VLM.__init__ with the new error (tensorrt_llm/_torch/models/modeling_gemma3vl.py:219-223) unless --attention_backend FLASHINFER is passed. On main it crashed in warmup, so this entry point still does not run by default, now with a clearer error. Suggested fix: default --attention_backend to None and pass it only when given, for example **({"attn_backend": args.attention_backend} if args.attention_backend else {}) in the LLM(...) call, so per-model defaults apply (Gemma 3 VLM here, and the attn_backend defaults of Gemma4 and PplxDecider too). No CI test runs the quickstarts on those models.
There was a problem hiding this comment.
[Nice to have] (R6: Required documentation) tensorrt_llm/_torch/models/modeling_gemma3vl.py:3: Gemma3ForConditionalGeneration now rejects every attn_backend but FLASHINFER at load, and nothing in the docs says so: its row in the multimodal feature matrix (docs/source/models/supported-models.md:166) has no backend note, unlike the TRTLLM requirement stated for encoder-decoder models (docs/source/models/supported-models.md:154). The multimodal example README listed the Gemma3 VL requirements (FlashInfer attention, chunked prefill off, KV cache reuse off) until #16610 removed it. Please add a footnote to that row: FLASHINFER is the model's default and the only backend it accepts, and, unless the block-reuse default suggested on get_model_defaults is adopted, image prompts need enable_block_reuse=False and no chunked prefill until #22886.
|
What this PR changes, by code owner For each code owner (a team or a person) of files this PR changes: what it changes there. The review above has the findings.
Files
Files
|
|
Re-review status. @trtllm-agent-review re-reviews this PR once all three of these hold since its last review:
Not met yet:
A Blocker you think should not be fixed needs no new code: reply in its thread with the reason (its premise does not hold, this PR does not introduce it, the PR rejects that configuration on purpose, the PR or the docs declare the limit, the PR or the docs say who must be trusted, or it is resolved elsewhere) and the evidence. To fix one later, say so in its thread, ideally with a link to the NVBug, JIRA ticket or PR that tracks the fix; a crash, hang, wrong result or CI break needs that link. Once every open Blocker thread has a reply and there are no merge conflicts, the bot checks those replies, and approves when they hold. The bot checks again in its next round. |
@coderabbitai summary
Description
Gemma3VLMalways builds its text decoder with FlashInfer attention, but the engine kept the default TRTLLM backend, soLLM("gemma-3-*-it")andtrtllm-servecrashed during warmup with anAttributeError(https://nvbugs/6963140, same cause as #13293).Gemma3VLMnow setsattn_backendtoFLASHINFERby default throughget_model_defaults, like Gemma4 and PplxDecider, and raises a clearValueErrorbefore loading weights if a different backend is set explicitly. Image prompts still needkv_cache_config.enable_block_reuse=False, the same as withattn_backend="FLASHINFER"on main; #22886 addresses that separately.Test Coverage
The Gemma3 VLM rows of
TestModelDefaults::test_gemma_scratch_defaultsnow expectFLASHINFERby default, and the newtest_gemma3_vlm_rejects_non_flashinfer_attn_backendchecks the error for an explicitTRTLLM; both are CPU tests that run in l0_cpu and fail without this change. On an H200 with gemma-3-12b-it, the default settings now start and return the same text and image results asattn_backend="FLASHINFER"on main, where before they crashed in warmup after 179 s.attn_backend="TRTLLM"now stops after 28 s with the new error message instead of crashing in warmup.PR Checklist
Please review the following before submitting your PR:
PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.
PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.
Test cases are provided for new code paths (see test instructions)
If PR introduces API changes, an appropriate PR label is added - either
api-compatibleorapi-breaking. Forapi-breaking, includeBREAKINGin the PR title.Any new dependencies have been scanned for license and vulnerabilities
CODEOWNERS updated if ownership changes
Documentation updated as needed
Update tava architecture diagram if there is a significant design change in PR.
The reviewers assigned automatically/manually are appropriate for the PR.
Please check this after reviewing the above items as appropriate for this PR.
GitHub Bot Help
To see a list of available CI bot commands, please comment
/bot help.