Add stream-ordered CP gradient return primitive#3235
Conversation
Signed-off-by: ningyunxiao.nyx <ningyunxiao.nyx@antgroup.com>
for more information, see https://pre-commit.ci
Greptile SummaryThis PR adds an experimental stream-ordered CP=4 gradient-return primitive for a Megatron NVSHMEM context-parallel attention backend. The implementation is narrow (three files) and deliberately does not add a hard NVSHMEM dependency — it uses caller-supplied peer tensor views and dispatches
Confidence Score: 5/5Safe to merge; the new primitive is isolated to three files, does not touch existing fused-attention paths, and all previously raised concerns (lazy driver loading, self-alias assertion, current-device validation) have been addressed. The change is narrow and well-guarded: every tensor argument is validated for CUDA device membership, the self-peer alias invariant is enforced with explicit data_ptr() comparisons, and driver symbol resolution goes through the lazy loader so there is no link-time libcuda dependency. The two remaining observations are minor API-consistency gaps in internal helper functions that have no effect on correctness at the current call site. transformer_engine/pytorch/csrc/extensions/nvshmem_comm.cpp — specifically the cp_stream_wait_epochs helper and the static epoch map lifetime. Important Files Changed
Sequence DiagramsequenceDiagram
participant PE0 as PE 0 (rank=0)
participant PE1 as PE 1 (rank=1)
participant PEN as PE N (rank=N)
participant GPU as GPU Stream
Note over PE0,PEN: All PEs call nvshmem_cp_global_grad_return_execute concurrently
PE0->>GPU: "copy_() scatter dk_global to peer_grad_key_returns[owner][rank=0]"
PE1->>GPU: "copy_() scatter dk_global to peer_grad_key_returns[owner][rank=1]"
PEN->>GPU: "copy_() scatter dk_global to peer_grad_key_returns[owner][rank=N]"
Note over PE0,PEN: Each PE writes its epoch to all owners' epoch buffers
PE0->>GPU: "cuStreamWriteValue32 peer_grad_committed_epochs[owner][rank=0] = epoch"
PE1->>GPU: "cuStreamWriteValue32 peer_grad_committed_epochs[owner][rank=1] = epoch"
PEN->>GPU: "cuStreamWriteValue32 peer_grad_committed_epochs[owner][rank=N] = epoch"
Note over PE0,PEN: Each PE waits until its local epoch buffer has all cp_size slots >= epoch
PE0->>GPU: "cuStreamWaitValue32 grad_committed_epoch[0..cp_size-1] >= epoch"
PE1->>GPU: "cuStreamWaitValue32 grad_committed_epoch[0..cp_size-1] >= epoch"
PEN->>GPU: "cuStreamWaitValue32 grad_committed_epoch[0..cp_size-1] >= epoch"
Note over PE0,PEN: Barrier satisfied - all scatter writes visible
PE0->>GPU: accumulate grad_key_return[0..cp_size-1] to dk
PE1->>GPU: accumulate grad_key_return[0..cp_size-1] to dk
PEN->>GPU: accumulate grad_key_return[0..cp_size-1] to dk
Reviews (4): Last reviewed commit: "Validate CP gradient return tensor devic..." | Re-trigger Greptile |
Signed-off-by: ningyunxiao.nyx <ningyunxiao.nyx@antgroup.com>
Signed-off-by: ningyunxiao.nyx <ningyunxiao.nyx@antgroup.com>
Signed-off-by: ningyunxiao.nyx <ningyunxiao.nyx@antgroup.com>
Description
Add one experimental CP=4 gradient-return primitive used by an opt-in Megatron
NVSHMEM context-parallel attention backend.
The primitive:
two-chunk CP layout;
cuStreamWriteValue32;cuStreamWaitValue32;NVSHMEM dependency to Transformer Engine.
This is intentionally narrow: three files and 122 additions. It does not add a
Transformer Engine attention backend or modify existing fused-attention
behavior.
Type of change
Changes
existing lazy driver loader, avoiding a hard
libcudalink dependency.Validation
main.verified that
nvshmem_cp_global_grad_return_executeis present in theresulting extension.
f676f681;readelfreports nolibcuda.sodependency. Wheel SHA256:
7e6df6d2042fa14b6e9e72cad5e669aafa72e31809ad253a898f363d321f569f.Megatron CP=4 training at S=262144, H=2560, 20 heads, head dimension 128,
four layers, and vocabulary 157184.
max-absolute difference
1.9073486328125e-6(atol=2e-6,rtol=0).For that single clean integrated comparison, median iteration time over updates
4-7 was 1707.45 ms for TE p2p and 1413.65 ms for the NVSHMEM candidate, 17.21%
lower. This is an integrated stack result, not a standalone speed claim for the
primitive in this PR.
Checklist
The end-to-end correctness test currently lives in the dependent Megatron PR,
which owns symmetric allocation and peer tensor construction. The current-main
validation is a compile/export check; end-to-end runtime validation used the
reviewed Transformer Engine 2.13 integration branch.
Stack relationship
provides two-section causal backward over shared K/V.
owns the opt-in backend, symmetric allocation, and peer tensor views passed
to this primitive.