Skip to content

[None][fix] Select FlashInfer hybrid default KV layer - #17382

Open
mihai-chiorean wants to merge 5 commits into
NVIDIA:mainfrom
mihai-chiorean:fix/flashinfer-hybrid-primary-kv-layer
Open

[None][fix] Select FlashInfer hybrid default KV layer#17382
mihai-chiorean wants to merge 5 commits into
NVIDIA:mainfrom
mihai-chiorean:fix/flashinfer-hybrid-primary-kv-layer

Conversation

@mihai-chiorean

@mihai-chiorean mihai-chiorean commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Use the first attention-backed KV-cache layer as FlashInfer metadata's default page-index source. Hybrid recurrent layers can expose placeholder page IDs, which are invalid for paged attention. The change also excludes recurrent-only layers from page-table sizing and multi-pool mapping.

Page-index buffer sizing now derives from attention-layer buffers for both hybrid and non-hybrid managers. Managers that cannot expose those buffers retain the existing blocks_in_primary_pool sizing fallback; legitimate recurrent-only ranks keep a zero-length page-index buffer, log that paged KV metadata is disabled, and clear page-table state.

Testing

  • pre-commit run --files tensorrt_llm/_torch/attention_backend/flashinfer.py tensorrt_llm/_torch/pyexecutor/resource_manager.py tests/unittest/_torch/executor/test_mamba_cache_manager.py tests/integration/defs/accuracy/test_llm_api_pytorch.py tests/integration/test_lists/test-db/l0_h100.yml tests/integration/test_lists/qa/llm_function_core.txt tests/integration/test_lists/qa/llm_function_rtx6k.txt
  • Spark SM121 focused GPU suite: 5 passed, covering V1/V2 hybrid layer selection, fallback sizing, and V1/V2 recurrent-only ranks.
  • Qwen3.6-35B-A3B NVFP4 production smoke on Spark SM121 with FlashInfer, V2 KV manager, FP8 KV, overlap scheduling, and CUDA graphs: health/chat HTTP 200 and 64 coherent generated tokens.
  • Qwen3.5-4B BF16 + FlashInfer GSM8K accuracy coverage is registered in H100 CI and core/RTX 6000 QA. The full accuracy run is retained because end-to-end backend coverage was requested during review.

No user-facing API change.

Dev Engineer Review

  • FlashInfer metadata now uses the first attention-backed KV-cache layer on the current pipeline rank as the default page-index source.
  • Recurrent-only layers are excluded from page-table sizing and multi-pool mapping.
  • Managers without attention-backed layers receive cleared cache metadata and skip page processing.
  • Draft metadata, VSWA buffers, cache preparation, and active-index restoration use attention-layer-specific indices.
  • The obsolete blocks_in_primary_pool FIXME was removed.
  • No public API declarations changed.
  • Test-list entries use valid paths and register the new accuracy test for H100 CI and RTX 6000 QA.
  • No duplicates or unintended configuration scope changes were identified.
  • Pre-commit checks passed.
  • No correctness, performance, error-handling, or API-consistency issues were identified.

QA Engineer Review

Test code changed in:

  • tests/unittest/_torch/executor/test_mamba_cache_manager.py
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py

Added test coverage verifies:

  • Mixed Mamba and attention managers use the correct attention layer for FlashInfer page tables.
  • Draft page-table preparation uses attention-layer-specific indices.
  • Recurrent-only ranks omit paged KV metadata.
  • Managers fall back to default-pool sizing when required buffers are unavailable.
  • FlashInfer-specific tests skip when FlashInfer is unavailable.
  • Qwen3.5-4B BF16 GSM8K accuracy runs with the FlashInfer backend.

The Qwen3.5-4B accuracy test is registered in:

  • tests/integration/test_lists/test-db/l0_h100.yml
  • tests/integration/test_lists/qa/llm_function_core.txt
  • tests/integration/test_lists/qa/llm_function_rtx6k.txt

The changed unit tests are not registered in the integration test lists. The integration accuracy test is registered for CI and QA.

Verdict: sufficient.

Validation included focused hybrid unit tests, a passing Spark SM121 / FlashInfer 0.6.15 V1 hybrid regression, and Qwen3.6-35B-A3B NVFP4 production-path smoke testing. The V2 regression was not runnable in the live H059 container because its older runtime rejected BlockReuseConfig(policy=...) before metadata construction. V2 remains covered by pre-merge CI and current-build validation.

Signed-off-by: Mihai Chiorean <mihai.v.chiorean@gmail.com>
@mihai-chiorean
mihai-chiorean marked this pull request as ready for review August 6, 2026 22:42
@mihai-chiorean
mihai-chiorean requested review from a team as code owners August 6, 2026 22:42
@mihai-chiorean

Copy link
Copy Markdown
Contributor Author

/bot run

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 5325e26e-8bbd-425f-bc04-a4f73a4396f2

📥 Commits

Reviewing files that changed from the base of the PR and between 3d3d7c9 and 27f208c.

📒 Files selected for processing (7)
  • tensorrt_llm/_torch/attention_backend/flashinfer.py
  • tensorrt_llm/_torch/pyexecutor/resource_manager.py
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py
  • tests/integration/test_lists/qa/llm_function_core.txt
  • tests/integration/test_lists/qa/llm_function_rtx6k.txt
  • tests/integration/test_lists/test-db/l0_h100.yml
  • tests/unittest/_torch/executor/test_mamba_cache_manager.py
💤 Files with no reviewable changes (1)
  • tensorrt_llm/_torch/pyexecutor/resource_manager.py
🚧 Files skipped from review as they are similar to previous changes (6)
  • tests/integration/test_lists/qa/llm_function_core.txt
  • tests/integration/test_lists/qa/llm_function_rtx6k.txt
  • tests/integration/test_lists/test-db/l0_h100.yml
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py
  • tensorrt_llm/_torch/attention_backend/flashinfer.py
  • tests/unittest/_torch/executor/test_mamba_cache_manager.py

Walkthrough

FlashInfer metadata now selects attention-backed KV layers explicitly. Page-table preparation and VSWA pool handling use the selected layer. Tests cover hybrid and recurrent-only Mamba cache managers and Qwen3.5 FlashInfer accuracy.

Changes

FlashInfer layer-aware KV handling

Layer / File(s) Summary
Attention-layer metadata and buffer initialization
tensorrt_llm/_torch/attention_backend/flashinfer.py
Metadata identifies attention-backed layers, selects the default KV layer, and sizes shared page-index buffers across those layers.
Layer-aware page-table preparation
tensorrt_llm/_torch/attention_backend/flashinfer.py
Preparation, draft synchronization, VSWA setup, and cleanup use the selected KV layer. Managers without attention-backed layers clear cache state and skip paged KV processing.
Hybrid Mamba and FlashInfer regression coverage
tests/unittest/_torch/executor/test_mamba_cache_manager.py, tests/integration/defs/accuracy/test_llm_api_pytorch.py, tests/integration/test_lists/qa/*, tests/integration/test_lists/test-db/l0_h100.yml, tensorrt_llm/_torch/pyexecutor/resource_manager.py
Tests verify layer-specific page-table lookup, fallback metadata sizing, recurrent-only rank behavior, and Qwen3.5 BF16 FlashInfer accuracy. An obsolete FIXME comment was removed.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CacheManager
  participant FlashInferMetadata
  participant VSWA
  CacheManager->>FlashInferMetadata: select attention-backed default KV layer
  CacheManager->>FlashInferMetadata: prepare layer-specific page indices
  FlashInferMetadata->>CacheManager: return page-table metadata
  CacheManager->>VSWA: activate the default layer pool
  VSWA->>CacheManager: restore active page-index state
Loading

Suggested reviewers: schetlur-nv, qijune, yingguo-trt

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the FlashInfer hybrid default KV-layer fix and follows the repository's required format.
Description check ✅ Passed The description explains the problem, solution, testing, validation results, and API impact; the checklist section is omitted but non-critical.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@eopXD eopXD left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With your change, we should be good to resolve FIXME under resource_manager.py given it should be safe to remove usage of blocks_in_primary_pool here.

Your merge request should enable hybrid models to run with the FlashInfer backend. Please add e2e test coverage for this.

Comment thread tensorrt_llm/_torch/attention_backend/flashinfer.py Outdated
Comment thread tensorrt_llm/_torch/attention_backend/flashinfer.py Outdated
Comment thread tensorrt_llm/_torch/attention_backend/flashinfer.py Outdated
Signed-off-by: Mihai Chiorean <mihai.v.chiorean@gmail.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/integration/defs/accuracy/test_llm_api_pytorch.py`:
- Line 6222: Update test_bf16_flashinfer to include an explicit None return
annotation, then register this test in both the l0_h100.yml and
llm_function_core.txt test lists so it runs in CI and manual QA.
- Around line 6221-6233: Register TestQwen3_5_4B::test_bf16_flashinfer from
accuracy/test_llm_api_pytorch.py in all applicable test lists: l0_h100.yml,
llm_function_core.txt, and llm_function_rtx6k.txt.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 965c688d-5a40-4c02-9dad-b13d1658650a

📥 Commits

Reviewing files that changed from the base of the PR and between f32e197 and 9077559.

📒 Files selected for processing (4)
  • tensorrt_llm/_torch/attention_backend/flashinfer.py
  • tensorrt_llm/_torch/pyexecutor/resource_manager.py
  • tests/integration/defs/accuracy/test_llm_api_pytorch.py
  • tests/unittest/_torch/executor/test_mamba_cache_manager.py
💤 Files with no reviewable changes (1)
  • tensorrt_llm/_torch/pyexecutor/resource_manager.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/unittest/_torch/executor/test_mamba_cache_manager.py
  • tensorrt_llm/_torch/attention_backend/flashinfer.py

Comment thread tests/integration/defs/accuracy/test_llm_api_pytorch.py
Comment thread tests/integration/defs/accuracy/test_llm_api_pytorch.py Outdated
Signed-off-by: Mihai Chiorean <mihai.v.chiorean@gmail.com>
@mihai-chiorean
mihai-chiorean requested a review from a team as a code owner August 12, 2026 07:14
@mihai-chiorean mihai-chiorean changed the title [None][fix] Select FlashInfer hybrid primary KV layer [None][fix] Select FlashInfer hybrid default KV layer Aug 12, 2026
@mihai-chiorean

Copy link
Copy Markdown
Contributor Author

Addressed the review in 907755952b and b30fdc12d1:

  • renamed the selected layer/pool terminology from primary to default;
  • clarified that the helper returns global layer IDs assigned to the current PP rank;
  • removed the blocks_in_primary_pool seed and the obsolete resource-manager FIXME;
  • added a Qwen3.5-4B BF16 + FlashInfer GSM8K accuracy test and registered it in H100 CI plus core/RTX 6000 QA.

Validation:

  • focused hybrid FlashInfer unit tests on SM121: 3 passed (legacy manager, V2 manager, recurrent-only rank);
  • Qwen3.6-35B-A3B NVFP4 production-path smoke on SM121 with FlashInfer, V2 KV manager, FP8 KV, overlap scheduling, and CUDA graphs: health 200, chat completion 200, 64 generated tokens with coherent output;
  • new integration test collects successfully and maps to the H100 PyTorch stages;
  • pre-commit passed for every touched file.

@brnguyen2 brnguyen2 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving — the comments below are optional touch-ups, not blockers.

The core selection logic is right — keying the default page-index source off num_kv_heads_per_layer[offset] > 0 matches how the hybrid managers mark recurrent layers (CppMambaHybridCacheManager.__init__ zeroes those entries), and restricting the multi-pool layer_space scan to attention layers removes a spurious pool that could have flipped the VSWA path on for plain hybrid models. Two robustness points on the sizing path and the draft-view lookup are inline.

One behavior worth calling out beyond what the description covers: _paged_kv_indices is no longer floored by blocks_in_primary_pool. For every model it is now sized purely from get_buffers(layer).shape[0] over attention layers, which changes the allocation for non-hybrid models too — smaller in the common case, but zero if get_buffers ever returns None/is absent for the selected layer. The description reads as if the change only affects hybrid models.

For pre-merge cost: test_bf16_flashinfer adds a full GSM8K run to l0_h100. If the intent is a regression guard for the page-index selection rather than accuracy coverage of the backend, a cheaper single-config run would give the same signal.

The description is otherwise accurate against the diff, and the change is internal, so no docs/release-note gap.

Comment thread tensorrt_llm/_torch/attention_backend/flashinfer.py
Comment thread tensorrt_llm/_torch/attention_backend/flashinfer.py
Comment thread tensorrt_llm/_torch/attention_backend/flashinfer.py
Comment thread tests/unittest/_torch/executor/test_mamba_cache_manager.py
Signed-off-by: Mihai Chiorean <mihai.v.chiorean@gmail.com>
Signed-off-by: Mihai Chiorean <mihai.v.chiorean@gmail.com>
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@mihai-chiorean

Copy link
Copy Markdown
Contributor Author

@brnguyen2 Thanks for the detailed review. The optional robustness points are addressed in 5ddb185f14: guarded/fallback buffer sizing, target-map draft lookup, recurrent-only logging/state clearing, and V1/V2 coverage. Current main is merged, all threads are resolved, focused SM121 tests pass (5/5), and current-head pre-commit is green. Could you please renew the approval when convenient?


if self.kv_cache_manager is not None:
blocks_in_primary_pool = self.kv_cache_manager.blocks_in_primary_pool
pp_attention_layer_ids = self._get_pp_attention_layer_ids()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we directly use kv_cache_manager.pp_layers instead of _get_pp_attention_layer_ids?

for layer_idx in pp_attention_layer_ids:
layer_buffer = get_buffers(layer_idx)
if layer_buffer is not None:
max_num_blocks = max(max_num_blocks,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yizhang-nv whether it is the simplest and correct way to calculate max_num_blocks?

@eopXD eopXD left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for addressing the comments.

@mihai-chiorean

Copy link
Copy Markdown
Contributor Author

LGTM, thank you for addressing the comments.

Of course! No it just needs CI.

@yufeiwu-nv
yufeiwu-nv removed their request for review August 14, 2026 02:41
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.

6 participants