enable tensor parallelism for MXLinear#2434
Merged
Merged
Conversation
Contributor
Author
|
Stack from ghstack (oldest at bottom): |
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/2434
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 11 PendingAs of commit 1001602 with merge base 7d6bb6a ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced Jun 24, 2025
vkuzo
added a commit
that referenced
this pull request
Jun 24, 2025
Summary: Enables TP for MXLinear. Specifically: 1. change the reshape logic from `x.reshape(-1, block_size)` to `x.reshape(*orig_shape[:-1], orig_shape[-1] // block_size, block_size)` 2. modify the rest of the code to adhere to (1) 3. cast input tensor and max_abs to float32 before calculating the MX scale, in order to get around another bug in DTensor + view + int16 target type (1) is necessary because the old reshape logic would flatten dims, which did not work if one of those flattened dims was sharded. Test Plan: ``` pytest test/prototype/mx_formats ./test/prototype/mx_formats/test_dtensor.sh ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 0bcd13e ghstack-comment-id: 3000664086 Pull Request resolved: #2434
vkuzo
added a commit
that referenced
this pull request
Jun 24, 2025
Summary: Enables TP for MXLinear. Specifically: 1. change the reshape logic from `x.reshape(-1, block_size)` to `x.reshape(*orig_shape[:-1], orig_shape[-1] // block_size, block_size)` 2. modify the rest of the code to adhere to (1) 3. cast input tensor and max_abs to float32 before calculating the MX scale, in order to get around another bug in DTensor + view + int16 target type (1) is necessary because the old reshape logic would flatten dims, which did not work if one of those flattened dims was sharded. Test Plan: ``` pytest test/prototype/mx_formats ./test/prototype/mx_formats/test_dtensor.sh ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: b742646 ghstack-comment-id: 3000664086 Pull Request resolved: #2434
vkuzo
added a commit
that referenced
this pull request
Jun 24, 2025
Summary: Enables TP for MXLinear. Specifically: 1. change the reshape logic from `x.reshape(-1, block_size)` to `x.reshape(*orig_shape[:-1], orig_shape[-1] // block_size, block_size)` 2. modify the rest of the code to adhere to (1) 3. cast input tensor and max_abs to float32 before calculating the MX scale, in order to get around another bug in DTensor + view + int16 target type (1) is necessary because the old reshape logic would flatten dims, which did not work if one of those flattened dims was sharded. Test Plan: ``` pytest test/prototype/mx_formats ./test/prototype/mx_formats/test_dtensor.sh ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 0cf8a1f ghstack-comment-id: 3000664086 Pull Request resolved: #2434
vkuzo
added a commit
that referenced
this pull request
Jun 24, 2025
Summary: Enables TP for MXLinear. Specifically: 1. change the reshape logic from `x.reshape(-1, block_size)` to `x.reshape(*orig_shape[:-1], orig_shape[-1] // block_size, block_size)` 2. modify the rest of the code to adhere to (1) 3. cast input tensor and max_abs to float32 before calculating the MX scale, in order to get around another bug in DTensor + view + int16 target type (1) is necessary because the old reshape logic would flatten dims, which did not work if one of those flattened dims was sharded. Test Plan: ``` pytest test/prototype/mx_formats ./test/prototype/mx_formats/test_dtensor.sh ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 0cf8a1f ghstack-comment-id: 3000664086 Pull Request resolved: #2434
vkuzo
added a commit
that referenced
this pull request
Jun 24, 2025
Summary: Enables TP for MXLinear. Specifically: 1. change the reshape logic from `x.reshape(-1, block_size)` to `x.reshape(*orig_shape[:-1], orig_shape[-1] // block_size, block_size)` 2. modify the rest of the code to adhere to (1) 3. cast input tensor and max_abs to float32 before calculating the MX scale, in order to get around another bug in DTensor + view + int16 target type (1) is necessary because the old reshape logic would flatten dims, which did not work if one of those flattened dims was sharded. Test Plan: ``` pytest test/prototype/mx_formats ./test/prototype/mx_formats/test_dtensor.sh ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 0cf8a1f ghstack-comment-id: 3000664086 Pull Request resolved: #2434
vkuzo
commented
Jun 24, 2025
| def test_activation_checkpointing(): | ||
| input_shape = (2, 4) | ||
| grad_shape = (2, 8) | ||
| input_shape = (16, 4) |
Contributor
Author
There was a problem hiding this comment.
this was broken before, caught by enforcing that inner dim is divisible by block size
vkuzo
commented
Jun 24, 2025
| # torchtitan but not in a unit test, so not enough info to file a good | ||
| # issue in pytorch/pytorch. For now, work around. In the future we should | ||
| # debug and fix this properly. | ||
| data_hp = data_hp.to(torch.float32) |
Contributor
Author
There was a problem hiding this comment.
performance testing showed that with compile on, having this in float32 does not regress performance
vkuzo
commented
Jun 24, 2025
|
|
||
| tp_out = tp_model(x_fp32_tp_input) | ||
| tp_out.sum().backward() | ||
| tp_out.backward(go_fp32_tp) |
Contributor
Author
There was a problem hiding this comment.
to make sure grad flowing into the last linear is contiguous
vkuzo
added a commit
that referenced
this pull request
Jun 24, 2025
Summary: Enables TP for MXLinear. Specifically: 1. change the reshape logic from `x.reshape(-1, block_size)` to `x.reshape(*orig_shape[:-1], orig_shape[-1] // block_size, block_size)` 2. modify the rest of the code to adhere to (1) 3. cast input tensor and max_abs to float32 before calculating the MX scale, in order to get around another bug in DTensor + view + int16 target type (1) is necessary because the old reshape logic would flatten dims, which did not work if one of those flattened dims was sharded. Test Plan: ``` pytest test/prototype/mx_formats ./test/prototype/mx_formats/test_dtensor.sh ``` Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: 860833d ghstack-comment-id: 3000664086 Pull Request resolved: #2434
drisspg
approved these changes
Jun 24, 2025
liangel-02
pushed a commit
that referenced
this pull request
Aug 25, 2025
* Update [ghstack-poisoned] * Update [ghstack-poisoned] * Update [ghstack-poisoned] * Update [ghstack-poisoned] * Update [ghstack-poisoned] * Update [ghstack-poisoned] * Update [ghstack-poisoned] * Update [ghstack-poisoned] * Update [ghstack-poisoned] * Update [ghstack-poisoned]
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.
Summary:
Enables TP for MXLinear. Specifically:
x.reshape(-1, block_size)tox.reshape(*orig_shape[:-1], orig_shape[-1] // block_size, block_size)scale, in order to get around another bug in DTensor + view + int16
target type
(1) is necessary because the old reshape logic would flatten dims, which
did not work if one of those flattened dims was sharded.
Note that TP does not yet work with the custom dim1 triton kernel, we'll need a separate PR to fix that by adding a sharding strategy to the kernel.
I verified that performance for FSDP + mxfp8 + compile is not affected by this stack, with torchtitan llama 3 8B on 8 B200 GPUs:
Test Plan:
Reviewers:
Subscribers:
Tasks:
Tags: