Repository navigation
Generalized Tensor Parallelism (GTP) #3005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
43a6cea
Generalized Tensor Parallelism (GTP) init commit
fanshiqing a7d0925
Merge remote-tracking branch 'origin/main' into gtp_release
fanshiqing a532120
GTP + gmm fusion
fanshiqing 2b26b69
Merge remote-tracking branch 'origin' into gtp_release
fanshiqing 8bb26f0
[fix] Respect per-op activation-offload markers in fused grouped MLP
fanshiqing b7c1595
Merge commit '4130d73e' into gtp_gmm_fusion
fanshiqing 8aaa5a1
Code clean: rename GTP weight-sharding axis to gtp_remat
fanshiqing 48d5413
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5f1cd62
Revert "[fix] Respect per-op activation-offload markers in fused grou…
fanshiqing 96ef071
Make TE GTP-agnostic at construction
fanshiqing 5252b0e
GTP+nvfp4: fix GTP backward GEMM scaling-mode mismatch for bf16-gathe…
fanshiqing e59a054
Make TE runtime GTP-agnostic via a DistributedWeight protocol
fanshiqing de9dc81
Code clean
fanshiqing 0f7226e
Simplify the NVFP4 gather post-process; Materialize the EGTP FC1 weig…
fanshiqing c256929
Code clean
fanshiqing d09d3b5
Merge remote-tracking branch 'origin' into gtp_release
fanshiqing ae56984
fix comments
fanshiqing aad67cc
Support DistributedWeight in the fusible grouped-linear ops path
fanshiqing 4d29de3
Unify distributed-weight wgrad finalize to return a graph-safe dummy
fanshiqing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # See LICENSE for license information. | ||
|
|
||
| """Unit tests for the GTP-agnostic DistributedWeight protocol and dispatchers. | ||
|
|
||
| These tests exercise TE's weight-parallelism extension point in isolation, with | ||
| a tiny in-repo ``FakeDistributedWeight`` stub standing in for a real implementer | ||
| (e.g. Megatron's GTPShardedParam). No GPU, process group, or Megatron import is | ||
| required: the whole contract lives in TE and is verified against the fake. | ||
| """ | ||
|
|
||
| import torch | ||
|
|
||
| from transformer_engine.pytorch.distributed_weight import ( | ||
| DistributedWeight, | ||
| is_distributed_weight, | ||
| materialize_weight_for_forward, | ||
| materialize_weight_for_backward, | ||
| finalize_weight_grads, | ||
| ) | ||
|
|
||
|
|
||
| class FakeDistributedWeight(torch.Tensor): | ||
| """Minimal DistributedWeight implementer for dispatcher tests. | ||
|
|
||
| Records how it was called and returns marker tensors so the dispatcher's | ||
| behavior (delegation, list normalization, no-op fallback) is observable. | ||
| """ | ||
|
|
||
| is_distributed_weight = True | ||
|
|
||
| def __new__(cls, group_size=1): | ||
| t = torch.zeros(1).as_subclass(cls) | ||
| t.group_size = group_size | ||
| t.calls = [] | ||
| return t | ||
|
|
||
| def materialize_group_for_forward(self): | ||
| self.calls.append("fwd") | ||
| out = [torch.full((2, 2), float(i)) for i in range(self.group_size)] | ||
| # Match the real GTP contract: single weight returns a bare tensor. | ||
| return out if self.group_size > 1 else out[0] | ||
|
|
||
| def materialize_group_for_backward(self, **kwargs): | ||
| self.calls.append(("bwd", kwargs)) | ||
| out = [torch.full((2, 2), float(10 + i)) for i in range(self.group_size)] | ||
| return out if self.group_size > 1 else out[0] | ||
|
|
||
| def finalize_group_grads(self, wgrads, **kwargs): | ||
| self.calls.append(("finalize", wgrads)) | ||
| wl = wgrads if isinstance(wgrads, (list, tuple)) else [wgrads] | ||
| out = [w + 100 for w in wl] | ||
| return out if self.group_size > 1 else out[0] | ||
|
|
||
| def grad_buffer(self): | ||
| return torch.full((2, 2), -1.0) | ||
|
|
||
|
|
||
| def test_protocol_runtime_checkable(): | ||
| """A conforming object passes isinstance; a plain tensor does not.""" | ||
| assert isinstance(FakeDistributedWeight(), DistributedWeight) | ||
| assert not isinstance(torch.zeros(2), DistributedWeight) | ||
|
|
||
|
|
||
| def test_is_distributed_weight(): | ||
| assert is_distributed_weight(FakeDistributedWeight()) | ||
| assert not is_distributed_weight(torch.zeros(2)) | ||
| assert not is_distributed_weight(torch.nn.Parameter(torch.zeros(2))) | ||
|
|
||
|
|
||
| def test_forward_noop_on_plain_tensor(): | ||
| """Plain weights pass through unchanged — the critical non-regression.""" | ||
| w = torch.nn.Parameter(torch.randn(4, 4)) | ||
| out = materialize_weight_for_forward(w) | ||
| assert out == [w] | ||
| assert out[0] is w | ||
|
|
||
|
|
||
| def test_forward_dispatches_single_weight(): | ||
| """Linear (N=1): dispatcher delegates and returns a length-1 list.""" | ||
| w = FakeDistributedWeight(group_size=1) | ||
| out = materialize_weight_for_forward(w) | ||
| assert isinstance(out, list) and len(out) == 1 | ||
| assert torch.equal(out[0], torch.zeros(2, 2)) | ||
| # The forward dispatcher owns the forward semantic; the leader is delegated to once. | ||
| assert w.calls == ["fwd"] | ||
|
|
||
|
|
||
| def test_forward_dispatches_grouped_weights(): | ||
| """GroupedLinear (N=k): one coalesced call, full list returned.""" | ||
| w = FakeDistributedWeight(group_size=3) | ||
| out = materialize_weight_for_forward(w) | ||
| assert isinstance(out, list) and len(out) == 3 | ||
| # Leader was called exactly once (coalesced), not once per weight. | ||
| assert len(w.calls) == 1 | ||
|
|
||
|
|
||
| def test_backward_dispatch_and_noop(): | ||
| w = FakeDistributedWeight(group_size=2) | ||
| out = materialize_weight_for_backward(w) | ||
| assert len(out) == 2 and torch.equal(out[0], torch.full((2, 2), 10.0)) | ||
|
|
||
| plain = torch.zeros(2) | ||
| assert materialize_weight_for_backward(plain) == [plain] | ||
|
|
||
|
|
||
| def test_finalize_grads_dispatch_and_noop(): | ||
| w = FakeDistributedWeight(group_size=1) | ||
| wgrads = [torch.zeros(2, 2)] | ||
| out = finalize_weight_grads(w, wgrads) | ||
| assert len(out) == 1 and torch.equal(out[0], torch.full((2, 2), 100.0)) | ||
|
|
||
| # No-op path leaves the grads untouched. | ||
| plain_w = torch.nn.Parameter(torch.zeros(2)) | ||
| g = [torch.ones(2)] | ||
| assert finalize_weight_grads(plain_w, g) == g | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| for name, fn in sorted(globals().items()): | ||
| if name.startswith("test_") and callable(fn): | ||
| fn() | ||
| print(f"{name}: PASS") |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.