Using DTensor to handle local num_heads change while TP is applied#3465
Merged
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/3465
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Contributor
Author
|
cc @tianyu-l |
tianyu-l
approved these changes
Jul 16, 2025
| If there are any more tensor operations (such as view operations) between the column-wise linear and the row-wise linear, we would need to adjust the relevant shape related ops to sharded shape. | ||
|
|
||
| For the Llama model, in the attention layer there are couple of view operations that are shape related. In particular, column-wise parallel for ``wq``/ ``wk``/ ``wv`` linear layers, the activation tensor is sharded on the ``num_heads`` dimension, so we would need to adjust the ``num_heads`` to local ``num_heads``. | ||
| For the Llama model, in the attention layer, there are several view operations related to shape. Specifically, for column-wise parallelism in the ``wq``/``wk``/``wv`` linear layers, the activation tensor is sharded on the ``num_heads`` dimension. To manage the difference between global and local ``num_heads``, we should set ``use_local_output=False`` to ensure the output is a DTensor. Unlike a regular tensor, a DTensor is aware of the parallelism plans and will automatically handle changes in the ``num_heads`` dimension. |
Contributor
There was a problem hiding this comment.
I think we should be able to use DTensor i.e. set use_local_output=False everywhere.
Maybe it's OK to keep a mixed usage of use_local_output so people are aware of this flexibility, but we should mention it here.
svekars
approved these changes
Jul 16, 2025
mikaylagawarecki
pushed a commit
that referenced
this pull request
Jul 23, 2025
…3465) * fsdp1 -> fsdp2 * change num_heads in tutorial --------- Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
mikaylagawarecki
pushed a commit
that referenced
this pull request
Jul 23, 2025
…3465) * fsdp1 -> fsdp2 * change num_heads in tutorial --------- Co-authored-by: Svetlana Karslioglu <svekars@meta.com>
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.
Fixes #ISSUE_NUMBER. This PR is to make the TP tutorial up-to-date with DTensor changes.
Description
After DTensor enhancement, we are not able to use DTensor to handle the change of
num_headsinstead of manually handle the tensor shape while TP is applied.Corresponding changes in
pytorch/examples: pytorch/examples#1373Checklist