Skip to content

[PyTorch] Isolate GroupedTensor GEMM workspaces by invocation - #4004

Open
Vtmpas wants to merge 3 commits into
NVIDIA:mainfrom
Vtmpas:fix/grouped-tensor-workspace
Open

Vtmpas wants to merge 3 commits into
NVIDIA:mainfrom
Vtmpas:fix/grouped-tensor-workspace

Conversation

@Vtmpas

@Vtmpas Vtmpas commented Dec 3, 2026

Copy link
Copy Markdown

Two overlapping PyTorch GroupedTensor GEMMs can write the same setup arrays and use the same cuBLASLt scratch. The caches are keyed by device and group count or layout, so they also alias across independent CUDA graph captures. Fixes #3963.

Allocate both mutable buffers for each invocation on the caller's CUDA stream. PyTorch orders eager reuse on that stream and keeps captured storage in the graph pool. For cuBLASLt versions before 13.8, clear the 32 MiB scratch before every launch to guard against the stale descriptors seen on mixed-layout replay in #3171.

  • Compare concurrent TN, NN, NT and NN→NT calls to serial execution. Capture two independent graphs on the same stream and replay them on separate streams. Check both workspace addresses and the numerical outputs.
  • Replay the existing FP8 block-scaling training graph twice. Drop leaf gradient references between replays so the test cannot overwrite graph-owned gradients.

Pre-commit passed. On H200 with cuBLASLt 13.5.1, the allocation changes passed all four workspace cases and two FP8 block-scaling replays against an existing TE native build. This used the exact allocation code and test definitions from this branch in that runtime. Full upstream CI and performance measurements are pending. The legacy scratch clear adds a device operation per call.

Allocate the mutable pointer arrays and cuBLAS scratch on the invoking stream. Clear scratch on older cuBLASLt before dispatch to avoid carrying stale descriptors across layouts. Cover independent graph generations and repeated FP8 block-scaling replay.

Signed-off-by: Matvey Saprykin <[email protected]>
@Vtmpas

Vtmpas commented Dec 3, 2026

Copy link
Copy Markdown
Author

@rohitna thanks again for reporting this. The GroupedTensor fix for #3963 is in this draft at f59f5cde. On H200 with cuBLASLt 13.5.1, the workspace changes passed the two-stream and independent graph tests, including mixed NN→NT replay, and two FP8 block-scaling training replays. If you have the delayed-wgrad reproducer from your integration, please link it here so I can check that case too.

FYI @ksivaman @timmoon10 @denera, this is the GroupedTensor follow-up to #3625 and the legacy replay case discussed in #3171. Pre-commit and DCO passed. The H200 run used the existing TE native build, and the full CI and scratch-clear timing are still pending. I'll keep it in draft while I review the patch.

@Vtmpas
Vtmpas marked this pull request as ready for review December 3, 2026 10:38
@Vtmpas
Vtmpas requested a review from ksivaman as a code owner December 3, 2026 10:38
@Vtmpas

Vtmpas commented Dec 3, 2026

Copy link
Copy Markdown
Author

@ksivaman @timmoon10 @zhongbozhu #4004 is now ready for review at f59f5cde. Could one of you run PyTorch L0/L1 on this commit? The targeted H200 tests passed, including concurrent GroupedTensor calls and a second FP8 block-scaling graph replay. CI coverage on Blackwell would be helpful. Thanks!

@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Gives each GroupedTensor GEMM its own setup and cuBLASLt scratch buffers.

  • Each GroupedTensor GEMM call gets its own workspaces.

No actionable issues found. Review was source-based; GPU tests were not run.

Reviews (1) · Last reviewed commit: "Remove obsolete gradient-reset call from..." · Reviewed by Greptile

@rohitna

rohitna commented Dec 5, 2026

Copy link
Copy Markdown

Thanks for turning this around so quickly, and for the reproducer in #3963.

Blackwell run. You asked for coverage on Blackwell, so I ran this PR's test on one GB300 node. I used an existing native build (TE 2.19.0 at 5e52befd; tex.get_cublasLt_version() reports 130101 there, so the scratch-clear path for older cuBLASLt is the one exercised), applied this PR's gemm.py change to it (it applies cleanly) and appended test_grouped_gemm_grouped_tensor_workspace_isolation unchanged to that revision's test_grouped_linear.py. So this checks the Python change and the test, not the head of this branch as a whole.

gemm.py test_grouped_gemm_grouped_tensor_workspace_isolation (TN, NN, NT, NN→NT) other grouped-tensor GEMM tests + test_grouped_linear_fused_path_cuda_graph_safe
upstream helpers (cached per device) 4 of 4 fail, in 3 of 3 runs 316 passed
this PR 4 of 4 pass, in 3 of 3 runs 316 passed
a per-stream cache key (what we carry downstream) 4 of 4 fail not run

All failures are at the address check after the two graphs are captured (isdisjoint), which is the aliasing you describe. The eager two-stream comparison earlier in the test passed in every run, so I did not see a numerical mismatch from eager overlap here, only the shared addresses. The third row supports your point about a stream key: it separates two eager streams but not two graphs captured on one stream.

The delayed-wgrad case. I cannot link it, the code is in an internal tree, and I do not have a stand-alone reproducer. What it does:

  • One MoE layer's tokens are split into two chunks. Expert forward, recompute and dgrad of both chunks run on the compute stream.
  • Expert weight gradients are deferred (delay_wgrad_compute) and launched with backward_dw on a side stream.
  • Originally the wgrad of a chunk waited only for that chunk's dgrad. It could then run beside the other chunk's expert recompute (TN) or dgrad (NN), with the same number of experts, so both used the same setup arrays. The scratch differs by layout in that case; the setup buffer is the shared one.
  • Symptom, intermittent: an illegal address inside the grouped GEMM, or a grouped GEMM that never returned. GB300, fused grouped MLP on the GroupedTensor path, TE 2.19.0 development build.

One correction to what I wrote on #3625: I said the problem went away once both buffers were keyed by stream. We do carry that key, but what I can stand behind is narrower. The change we validated was scheduling: the wgrad now waits for every chunk's dgrad, so the two grouped GEMMs no longer overlap. With this PR that wait should not be needed for correctness; I have not tested removing it yet.

If it is useful, I can rerun this test on GB300 once the branch has a head you consider final.

This branch has not been deployed

No deployments
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.

GroupedTensor GEMMs on independent CUDA streams share mutable setup and cuBLAS workspaces

2 participants