-
Notifications
You must be signed in to change notification settings - Fork 74
AllToAll implementation #5705
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
AllToAll implementation #5705
Changes from 11 commits
caefdd1
f6c1684
12cc822
0370b89
06ea908
f0763e5
2bb6353
a3c6a47
f0626d8
07b51ad
62109c2
90a2174
347061b
3166c61
31da19a
27fa2d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -293,6 +293,40 @@ void lowerToReduceScatter( | |
| backend)); | ||
| } | ||
|
|
||
| void lowerToAllToAll( | ||
| TensorView* input_tv, | ||
| TensorView* output_tv, | ||
| const CommunicatorBackend backend, | ||
| std::vector<Expr*>& comms) { | ||
| const DeviceMesh& sender_mesh = input_tv->getDeviceMesh(); | ||
| const DeviceMesh& receiver_mesh = output_tv->getDeviceMesh(); | ||
| NVF_ERROR_EQ( | ||
| sender_mesh.rank(), | ||
| 1, | ||
| "AllToAll sender mesh must be a 1D mesh. Given ", | ||
| sender_mesh); | ||
| NVF_ERROR_EQ( | ||
| receiver_mesh.rank(), | ||
| 1, | ||
| "AllToAll receiver mesh must be a 1D mesh. Given ", | ||
| receiver_mesh); | ||
| NVF_ERROR_EQ( | ||
| sender_mesh, | ||
| receiver_mesh, | ||
| "AllToAll sender and receiver meshes must be the same. Given ", | ||
| sender_mesh, | ||
| " and ", | ||
| receiver_mesh); | ||
| comms.push_back(IrBuilder::create<Communication>( | ||
| CommunicationType::AllToAll, | ||
| output_tv, | ||
| input_tv, | ||
| sender_mesh.vector(), | ||
| /*root=*/-1, | ||
| c10d::ReduceOp::RedOpType::UNUSED, | ||
| backend)); | ||
| } | ||
|
|
||
| IterDomain* getLogicalFromLoopId(TensorView* tv, IterDomain* loop_id) { | ||
| std::unordered_set<IterDomain*> logical_ids = | ||
| getInputsInTargetDomain({loop_id}, tv->getLogicalDomain()); | ||
|
|
@@ -379,8 +413,14 @@ CommunicationInfo getCommunicationInfo(Expr* e) { | |
| IterDomain* p_logical_id = getLogicalFromLoopId(producer, p_loop_did); | ||
| IterDomain* c_logical_id = getLogicalFromLoopId(consumer, c_loop_did); | ||
| // TODO(#4604): This is problematic for 2D sharding. | ||
| fill_communication_info( | ||
| CommunicationType::SendRecv, p_logical_id, c_logical_id); | ||
|
|
||
| if (c_logical_id == p2c_map.at(p_logical_id)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As long as tests pass, I'm fine with this condition. But can you tell me the math behind it? If the two DIDs are mapped, isn't that a non-communication?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation of send recv assumes that the input and output are sharded on the same dimension, but have different meshes.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it -- we'll have to think about how to represent CollectivePermute for ring-based communication. |
||
| fill_communication_info( | ||
| CommunicationType::SendRecv, p_logical_id, c_logical_id); | ||
| } else { | ||
| fill_communication_info( | ||
| CommunicationType::AllToAll, nullptr, nullptr); | ||
Priya2698 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
Priya2698 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
417
to
423
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting both For example, The current workaround is that AllToAll is explicitly handled in
|
||
| } | ||
| } else { | ||
| NVF_ERROR(e->isA<ReductionOp>() || e->isA<SqueezeOp>()); | ||
|
|
@@ -441,10 +481,12 @@ Layout getCommunicationLayout( | |
| // For the following communication types, the sharded_id does not have to be | ||
| // outermost in allocation domain. Nonetheless, `tv` still needs to be | ||
| // contiguous and therefore .contiguous() at the beginning of this function. | ||
| // TODO(prmishra): Fix the layout for AllToAll. | ||
|
||
| if (type == CommunicationType::Reduce || | ||
| type == CommunicationType::Allreduce || | ||
| type == CommunicationType::Broadcast || | ||
| type == CommunicationType::SendRecv) { | ||
| type == CommunicationType::SendRecv || | ||
| type == CommunicationType::AllToAll) { | ||
Priya2698 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return layout; | ||
| } | ||
|
|
||
|
|
@@ -568,6 +610,9 @@ std::vector<Expr*> convertSingleOpToCommunication( | |
| case CommunicationType::Reduce: | ||
| lowerToReduce(input_tv, output_tv, op_type(e), backend, comms); | ||
| break; | ||
| case CommunicationType::AllToAll: | ||
| lowerToAllToAll(input_tv, output_tv, backend, comms); | ||
| break; | ||
| } | ||
|
|
||
| return comms; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -54,6 +54,9 @@ std::ostream& operator<<(std::ostream& os, const CommunicationType& type) { | |||||
| case CommunicationType::SendRecv: | ||||||
| os << "SendRecv"; | ||||||
| break; | ||||||
| case CommunicationType::AllToAll: | ||||||
| os << "AllToAll"; | ||||||
| break; | ||||||
| default: | ||||||
| NVF_THROW("unrecognized CommunicationType: ", type); | ||||||
|
||||||
| default: | |
| NVF_THROW("unrecognized CommunicationType: ", type); |
trust -Wswitch
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| default: | |
| NVF_THROW("unrecognized CommunicationType: ", type); |
You may need an std::unreachable() after the switch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: missing team_size == 1 edge case - when only one device, no communication needed, should return early after local copy (like postBroadcast at line 344)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment describing the input/output tensor shapes appears to be inconsistent with the actual implementation below.
The comment states:
input_tv = [DIDx(d), n/d, m, ...]m: scattered dimension (at position 2)
But the code at lines 644-651 operates on dimension 1 (input_sizes.at(1)), treating it as the scattered dimension. Based on the test case in test_alltoall, the actual runtime input tensor shape is [n/d, m, k, ...] (after permutation), where:
- Position 0:
n/d(gathered dimension) - Position 1:
m(scattered dimension) - Position 2+: other dimensions
The comment should be updated to reflect the actual runtime tensor layout rather than the logical sharding representation, or clarify that it's describing logical sharding rather than physical memory layout.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message says "Scattered dimension must be divisible by the team size" but this check only validates one dimension.
For a complete AllToAll operation, both the scattered dimension (at position 1 in the runtime tensor) and the gathered dimension (at position 0) should be validated:
- The scattered dimension
input_sizes.at(1)must be divisible byd(currently checked ✓) - The gathered dimension should result in an output where the gathered size equals
d * input_sizes.at(0)
Consider adding validation for the output tensor dimensions as well to catch shape mismatches early, rather than relying on assertBuffersHaveSameSize at line 668 which only provides a generic error.
Uh oh!
There was an error while loading. Please reload this page.