-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
[pir+auto parallel] add reshard op for input when needed #63072
Changes from all commits
ccab43e
aa9955c
ab1f241
eb3bf88
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import paddle | ||
|
||
|
||
def apply_partition_pass(program): | ||
new_program = program.clone() | ||
with paddle.static.program_guard(new_program): | ||
for op in new_program.global_block().ops: | ||
# assert len(op.operands()) == len(op.dist_attr().operand_dist_attrs()), f'The number of operand and operand_dist_attrs are not equal in op: {op}' | ||
for var, operand_dist_attr in zip( | ||
op.operands(), op.dist_attr().operand_dist_attrs() | ||
): | ||
if ( | ||
var.source().is_dist_dense_tensor_type() | ||
and var.source().dist_attr() != operand_dist_attr | ||
): | ||
paddle.pir.set_insertion_point(op) | ||
# insert reshard | ||
reshard_var = paddle._pir_ops.reshard_v2( | ||
var.source(), operand_dist_attr | ||
) | ||
var.set_source(reshard_var) | ||
return new_program | ||
|
||
|
||
def apply_reshard_pass(program): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ def __init__(self, mesh): | |
) | ||
|
||
def forward(self, x): | ||
x.stop_gradient = False | ||
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. not need to make x require for gradient, the relu_grad in backward will trigger the partial-->replicated allreduce 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. it is needed, otherwise, relu_grad is not executed. |
||
out = self.relu_0(x) # triggle backward partial allreduce | ||
out = self.linear_0(out) | ||
out = self.relu_1(out) | ||
|
@@ -138,6 +139,8 @@ def test_to_static_program(self): | |
backward_op_list = [ | ||
"pd_op.sgd_", | ||
"pd_op.sgd_", | ||
"pd_op.relu_grad", | ||
"dist_op.reshard", | ||
"pd_op.matmul_grad", | ||
"pd_op.relu_grad", | ||
"pd_op.matmul_grad", | ||
|
@@ -225,10 +228,10 @@ def test_to_static_program(self): | |
tensor._local_shape, [BATCH_SIZE, CLASS_NUM] | ||
) | ||
elif matmul_grad_idx == 1: | ||
self.assertEqual(tensor.dist_attr().dims_mapping, [-1, 0]) | ||
self.assertEqual(tensor.dist_attr().partial_dims, set()) | ||
self.assertEqual(tensor.dist_attr().dims_mapping, [-1, -1]) | ||
self.assertEqual(tensor.dist_attr().partial_dims, {0}) | ||
self.assertEqual( | ||
tensor._local_shape, [BATCH_SIZE, IMAGE_SIZE // 2] | ||
tensor._local_shape, [BATCH_SIZE, IMAGE_SIZE] | ||
) | ||
matmul_grad_idx += 1 | ||
if op.name() == 'pd_op.sgd_': | ||
|
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.
In scenario where src_dist_attr and dst_dist_attr have different mesh (e.g. Pipeline Parallelism), it would be better to insert two reshard ops.
one reshard op's mesh = src_dist_attr's mesh
the other's mesh = dst_dist_attr's mesh
therefore in the following (pipeline stage) pruning pass, different stage will keep the reshard op by the mesh it need and remove the other one.
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.
It could be refined in the next PR