Skip to content

IMA fix by making the copy of book keeping buffer to GPU blocking - #5715

Merged
shanmugamr1992 merged 5 commits into
NVIDIA:mainfrom
shanmugamr1992:ima_fix
Jul 16, 2026
Merged

IMA fix by making the copy of book keeping buffer to GPU blocking#5715
shanmugamr1992 merged 5 commits into
NVIDIA:mainfrom
shanmugamr1992:ima_fix

Conversation

@shanmugamr1992

@shanmugamr1992 shanmugamr1992 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor
  1. The original issue. Each inference step, Megatron copies a pinned CPU
    bookkeeping buffer (_cpu_bookkeeping_buf) to the GPU with an async
    (non_blocking=True) copy, then immediately reuses/overwrites that same CPU
    buffer on the next step. The host doesn't wait for the copy to finish, so it
    clobbers bytes that the in-flight GPU copy is still reading. The GPU ends up
    with corrupted token/KV-block indices and dereferences out-of-bounds memory
    illegal memory access. CUDA-graph warmup runs steps back-to-back with
    almost no host work in between, so the race fires every time there.

    Fix:
    to make the copy blocking would make sure this issue doesn't happen

Window blocking (fix #1) shadow (fix #2) Δ shadow vs blocking
all 6 steps 115.1 114.7 +0.3% slower
steady (steps 2–6) 117.9 114.2 +3.2% slower
steady (steps 3–6) 121.4 119.8 +1.3% slower
No measurable throughput benefit from the shadow buffer.The two are within run-to-run noise, and the small deltas actually favor which shadow physically can't lose to on real overlap, so it's just noise.

timer aggregates ~370 decode iterations/seq; the per-iteration copy is a few µs and any blocking host-stall (~10–30 µs) is a fraction of a percent of the ~32 ms/iter forward pass — diluted below noise.

With larger models this is even less a concern,

@shanmugamr1992
shanmugamr1992 requested review from a team as code owners July 8, 2026 19:46
@copy-pr-bot

copy-pr-bot Bot commented Jul 8, 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.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft July 8, 2026 19:46
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

Signed-off-by: Shanmugam Ramasamy <111910568+shanmugamr1992@users.noreply.github.com>
@shanmugamr1992
shanmugamr1992 marked this pull request as ready for review July 8, 2026 19:57
@tdene
tdene requested review from lmcafee-nvidia and santhnm2 July 8, 2026 20:47
@kvareddy
kvareddy requested a review from tdene July 9, 2026 05:59
@kvareddy

kvareddy commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

@tdene can you signoff of these changes?

@kvareddy

Copy link
Copy Markdown
Contributor

@lmcafee-nvidia can you please take a look at these changes?

The pinned `_cpu_bookkeeping_buf` is re-staged in place on the next step,
so a non_blocking H2D copy races with host writes and can corrupt token/block
indices on the GPU. Use a blocking copy instead; the per-step sync cost is
negligible relative to the forward pass.

Signed-off-by: Shanmugam Ramasamy <111910568+shanmugamr1992@users.noreply.github.com>

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

Can you update the PR title and description?

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Jul 13, 2026
@shanmugamr1992 shanmugamr1992 changed the title Ima fix IMA fix by making the copy of book keeping buffer to GPU blocking Jul 13, 2026
Comment thread megatron/core/inference/contexts/dynamic_context.py
Comment thread megatron/core/inference/contexts/dynamic_context.py
Update transfer_bookkeeping_to_gpu docs to match non_blocking=False and
explain the host restage race that requires a blocking copy.

Signed-off-by: Shanmugam Ramasamy <111910568+shanmugamr1992@users.noreply.github.com>
@shanmugamr1992
shanmugamr1992 enabled auto-merge July 14, 2026 18:51
@shanmugamr1992

Copy link
Copy Markdown
Contributor Author

/ok to test 75dd58f

@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 14, 2026
sidsingh-nvidia pushed a commit to sidsingh-nvidia/Megatron-LM that referenced this pull request Jul 14, 2026
The pinned `_cpu_bookkeeping_buf` is re-staged in place on the next step, so a
non_blocking H2D copy races with host writes and can corrupt token/block indices
on the GPU. Use a blocking copy instead; the per-step sync cost is negligible
relative to the forward pass.

Cherry-picked from NVIDIA/Megatron-LM PR NVIDIA#5715.
Signed-off-by: Shanmugam Ramasamy <111910568+shanmugamr1992@users.noreply.github.com>
@shanmugamr1992

Copy link
Copy Markdown
Contributor Author

/ok to test 8cee542

@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/29458845201

Merged via the queue into NVIDIA:main with commit 53a2dd5 Jul 16, 2026
89 checks passed
@shanmugamr1992
shanmugamr1992 deleted the ima_fix branch July 16, 2026 02:23
chochowski pushed a commit to chochowski/Megatron-LM that referenced this pull request Jul 20, 2026
…IDIA#5715)

Signed-off-by: Shanmugam Ramasamy <111910568+shanmugamr1992@users.noreply.github.com>
Signed-off-by: mchochowski <mchochowski@nvidia.com>
terminator123 pushed a commit to 021ai/Megatron-LM that referenced this pull request Aug 3, 2026
…IDIA#5715)

Signed-off-by: Shanmugam Ramasamy <111910568+shanmugamr1992@users.noreply.github.com>
svcnvidia-nemo-ci pushed a commit to dimapihtar/Megatron-LM that referenced this pull request Aug 4, 2026
…IDIA#5715)

Signed-off-by: Shanmugam Ramasamy <111910568+shanmugamr1992@users.noreply.github.com>
Signed-off-by: Dmytro Pykhtar <dpykhtar@nvidia.com>
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.

7 participants