[Common] Fix TMA synchronization in quantization kernels - #3417
Open
Oleg-Goncharov wants to merge 1 commit into
Open
[Common] Fix TMA synchronization in quantization kernels#3417Oleg-Goncharov wants to merge 1 commit into
Oleg-Goncharov wants to merge 1 commit into
Conversation
Signed-off-by: Oleg Goncharov <ogoncharov@nvidia.com>
Collaborator
Author
|
/te-ci |
Contributor
Greptile SummaryThe PR repairs synchronization around asynchronous TMA shared-to-global stores in FP8, MXFP8, and NVFP4 quantization kernels.
Confidence Score: 5/5The PR appears safe to merge, with no concrete correctness, build, or security regression identified in the changed synchronization paths. The changed kernels consistently make the TMA issuer observe completion before a CTA rendezvous permits shared-buffer reuse, and the examined ring sizes, wait depths, control flow, tail drains, and shared-memory calculations remain internally consistent. Important Files Changed
Sequence DiagramsequenceDiagram
participant CTA as Cooperative CTA threads
participant Leader as TMA-issuing leader
participant TMA as TMA engine
CTA->>CTA: Populate shared output tile
CTA->>Leader: CTA synchronization
Leader->>TMA: Issue shared-to-global transfer
Leader->>Leader: Commit bulk async group
Leader->>Leader: Wait before ring-buffer reuse
TMA-->>Leader: Shared-memory read completed
Leader->>CTA: CTA-wide completion handoff
CTA->>CTA: Safely overwrite reused buffer
Leader->>TMA: Full tail drain
TMA-->>Leader: All transfers completed
Leader->>CTA: Final CTA rendezvous
Reviews (1): Last reviewed commit: "Fix TMA shared-memory reuse synchronizat..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes shared-memory reuse races and incomplete completion handling in kernels using TMA shared-to-global transfers.
The issue was not isolated to a single kernel. The same TMA pipeline and synchronization pattern had been copied and adapted across multiple NVFP4, FP8, and MXFP8 kernels. As a result, the same underlying synchronization bug appeared in several related implementations.
Motivation and root cause
The affected kernels use shared-memory ring buffers as the source of asynchronous TMA shared-to-global transfers. A typical pipeline iteration performs the following operations:
Bulk async-groups are maintained per issuing thread. Only the leader thread issues the TMA operation and calls
cp_async_bulk_commit_group(). Therefore, only that thread has the corresponding outstanding async-groups.Some kernels called
cp_async_bulk_wait_group_read<N>()without a subsequent CTA-wide synchronization point before all threads started overwriting the reused shared-memory buffer. Calling the wait from non-issuing threads does not solve the problem because those threads do not own the leader's async-groups.Consequently, the following race was possible:
This could result in rare, timing-dependent output corruption, particularly under global-memory backpressure.
The existing
fence.proxy.async.shared::ctainstructions were correct and remain necessary. They make cooperative generic-proxy shared-memory writes visible to the asynchronous TMA proxy before a transfer is issued. However, a proxy fence does not communicate the leader's later async-group completion to the rest of the CTA and therefore cannot by itself protect shared-memory buffer reuse.In several kernels, the output-buffer wait was also coupled to the conditional prefetch of the next input stage. This meant that the wait could be skipped on the final pipeline stage even though output-buffer lifetime and input-prefetch state are independent concerns.
A related latent issue existed in the tuned NVFP4 kernel: the transposed output ring was hard-coded to two buffers while the allowed number of outstanding TMA groups was controlled by
PREFETCH_STAGES. Increasing the prefetch depth could therefore make the ring smaller than required by the TMA wait depth.Type of change
Changes
This PR introduces the following changes:
Added an explicit issuer-to-CTA completion handoff before shared-memory output buffers are reused:
cp_async_bulk_wait_group_read<N>().__syncthreads()ensures that no cooperative writer can overwrite the buffer before the issuer has observed completion of the relevant TMA read.Moved output-ring synchronization out of input-prefetch conditionals.
Corrected wait depths to derive from the actual number of shared-memory output buffers rather than unrelated input-prefetch constants.
Added full issuer-only
cp_async_bulk_wait_group()drains at kernel tails.Fixed the tuned NVFP4 transposed-output ring size.
BUFFS_NUM_OUT_TRnow followsPREFETCH_STAGES + 1.Added compile-time pipeline invariants for MXFP8 kernels.
Added compiler
"memory"clobbers to the inline PTX wrappers for:cp.async.bulk.commit_groupcp.async.bulk.wait_groupcp.async.bulk.wait_group.readThese clobbers prevent the compiler from moving memory operations across synchronization instructions whose memory effects are otherwise not visible to the C++ compiler.
Replaced final source-read-only waits with full completion waits in one-shot TMA store paths, including fused grouped requantization and square-blockwise quantize-transpose.
Audited all current TMA shared-to-global call sites.
wait_group_read<0>()calls are intentional intermediate waits protecting reuse of dynamic shared memory fordbiasreduction; they are not incomplete kernel-tail drains.Checklist: