Skip to content

Correct prefix-caching ref-count accounting - #6047

Merged
tdene merged 1 commit into
NVIDIA:mainfrom
tdene:tde/fix_prefix_release_refcount
Jul 28, 2026
Merged

Correct prefix-caching ref-count accounting#6047
tdene merged 1 commit into
NVIDIA:mainfrom
tdene:tde/fix_prefix_release_refcount

Conversation

@tdene

@tdene tdene commented Jul 26, 2026

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

What does this PR do?

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

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 your PR.

@copy-pr-bot

copy-pr-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@tdene
tdene marked this pull request as ready for review July 26, 2026 06:55
@tdene
tdene requested review from a team as code owners July 26, 2026 06:55
Signed-off-by: Teodor-Dumitru Ene <teodord.ene@gmail.com>
@tdene
tdene force-pushed the tde/fix_prefix_release_refcount branch from 071d064 to 6f0dd71 Compare July 26, 2026 06:58

@kvareddy kvareddy 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.

good find.

@kvareddy

Copy link
Copy Markdown
Contributor

@lmcafee-nvidia, we missed this in our testing.

@sidsingh-nvidia sidsingh-nvidia 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.

Great find, thanks!

@sidsingh-nvidia

Copy link
Copy Markdown
Contributor

/claude strict-review

@claude claude 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.

Strict review passed — no significant issues found. LGTM

Verified the fix addresses a real ref-count leak: self.block_ref_counts[blocks] -= 1 only decrements once per unique index under advanced indexing, so a prefix block shared by multiple requests finishing in the same batched release was under-decremented and leaked. scatter_add_ correctly accumulates per occurrence (the deterministic-accumulate op).

Also confirmed:

  • The added torch.unique on both the REF_ZERO deregister path and the LRU pool-return path fixes a companion double-free (a duplicated block ID reaching ref 0 would otherwise be returned to the pool / deregistered twice).
  • dtype/device are consistent: int32 self + int32 source + int64 index on CPU, matching scatter_add_ requirements; no new device constraint over the prior indexing.
  • -1 sentinels are filtered by the caller before reaching the scatter, so no out-of-range index.

The new unit test is thorough — staged multi-owner release, in-batch duplication with a private block mixed in, and both eviction policies, including a set-uniqueness assertion that guards against double-returned IDs.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added Approved All necessary approvals have been made and removed Final Review PR is in the "final review" stage labels Jul 28, 2026
@tdene
tdene enabled auto-merge July 28, 2026 17:25
@tdene
tdene added this pull request to the merge queue Jul 28, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30383126962

Merged via the queue into NVIDIA:main with commit 2576c15 Jul 28, 2026
94 of 96 checks passed
@tdene
tdene deleted the tde/fix_prefix_release_refcount branch July 28, 2026 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants