Skip to content

fix(inference): preserve valid rows, cached KV, and greedy semantics - #7061

Draft
wdykas wants to merge 3 commits into
NVIDIA:mainfrom
wdykas:fix/dynamic-inference-correctness-edge-cases
Draft

fix(inference): preserve valid rows, cached KV, and greedy semantics#7061
wdykas wants to merge 3 commits into
NVIDIA:mainfrom
wdykas:fix/dynamic-inference-correctness-edge-cases

Conversation

@wdykas

@wdykas wdykas commented Sep 3, 2026

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

Fixes three independently reproduced dynamic-inference correctness edge cases that remain on current main:

  1. NVLS with EP=1 treats CUDA-graph padding rows as valid tokens.
    DynamicInferenceContext._nvls_dispatcher is false at EP=1, although the model still constructs NVLSAllGatherVDispatcher. Consequently, its GPU real-token-count tensor was never bound, and token_dispatch() reported the graph-padded local tensor height as valid. This change binds the scalar whenever NVLS is selected, masks routing padding, and converts the global count to an SP-local valid-row count.
  2. A continuation can rewrite an inherited shared KV block when it finds no new complete-block matches.
    The write-redirection guard checked only num_matched_blocks from the current chunk. A short, unaligned continuation within a partial block inherited from an earlier prefix match has zero newly matched blocks, so its recomputed KV was written into the shared cached block. This change also checks req.num_matched_prefix_blocks before redirecting those writes to the dummy block.
  3. FlashInfer top_k=1 is not reliably greedy when maximum logits tie.
    FlashInfer's threshold-based top-k kernel retains equal logits at the cutoff and can sample among them, whereas SamplingParams and the Torch backend define top_k=1 as first-argmax. This change uses argmax for all-greedy batches, repairs greedy rows in mixed batches, and returns the corresponding one-hot processed log-probability distribution. The common unfiltered path still returns before the greedy checks.

Each issue has a focused regression test that failed on untouched main at 06a1d8e6f and passes with this branch.

Upstream audit

Related candidates were excluded because current main already contains their fixes:

Validation

  • Untouched 06a1d8e6f: 6 failed and 1 passed across the new focused reproducers.
  • This branch: all 7 focused regression cases passed.
  • Broader affected test groups: 28 passed.
  • Direct FlashInfer tie reproducer on GPU: 4,096 tied rows returned IDs {1: 2087, 3: 2009}; Torch first-argmax returned ID 1.
  • ruff check on all changed files: passed.
  • git diff --check: passed.

This remains a draft because the complete repository autoformatter is not available in the supplied runtime image.

Issue tracking

Linked issue: N/A

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge this PR.

Bind the context real-token scalar whenever the NVLS dispatcher is selected, including EP=1, and use it to gate fused-MoE work and mask CUDA-graph padding rows. Preserve the global-to-local count conversion for sequence-parallel shards.

Signed-off-by: William Dykas <wdykas@nvidia.com>
Continue redirecting writes into an inherited hash-matched partial block even when a continuation chunk finds no additional complete blocks. Otherwise recomputation can mutate KV shared with another request.

Signed-off-by: William Dykas <wdykas@nvidia.com>
Honor the torch backend contract that top_k=1 is greedy. Use first-argmax tie breaking for all-greedy batches, repair greedy rows in mixed batches, and report a one-hot processed distribution for greedy log probabilities.

Signed-off-by: William Dykas <wdykas@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Sep 3, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@wdykas

wdykas commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

/claude strict-review

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.

1 participant