Skip to content
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] Adapt distributed API broadcast #64190

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion python/paddle/distributed/communication/stream/broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle import framework
from paddle import _C_ops, framework
from paddle.base import data_feeder
from paddle.distributed.communication.group import (
_get_global_group,
_get_or_throw_group_rank,
_warn_cur_rank_not_in_group,
)
from paddle.distributed.communication.reduce import _to_inplace_op
from paddle.framework import in_pir_mode


def _broadcast_in_dygraph(
Expand Down Expand Up @@ -59,6 +61,11 @@ def _broadcast_in_static_mode(
helper = framework.LayerHelper(op_type, **locals())
ring_id = 0 if group is None else group.id

if in_pir_mode():
op_type = _to_inplace_op(op_type)
getattr(_C_ops, op_type)(tensor, src_rank_in_group, sync_op, ring_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的参数顺序有点问题,请参考paddle/fluid/pir/dialect/operator/ir/ops.yaml中c_broadcast的args顺序~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改~

return

helper.append_op(
type=op_type,
inputs={'X': [tensor]},
Expand Down
60 changes: 60 additions & 0 deletions test/collective/process_group_nccl_pir.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,66 @@ def test_allreduce_prod_with_0d_input(self):
np.multiply(x_np, y_np), y_out
)

def test_broadcast(self):
pg = self.pg
# rank 0
x_np = np.random.random(self.shape).astype(self.dtype)
# rank 1
y_np = np.random.random(self.shape).astype(self.dtype)
with paddle.pir_utils.IrGuard():
main_program = paddle.static.Program()
startup_program = paddle.static.Program()
with paddle.static.program_guard(main_program, startup_program):
x = paddle.static.data(
name="x", shape=self.shape, dtype=self.dtype
)
y = paddle.static.data(
name="y", shape=self.shape, dtype=self.dtype
)
broadcast_result = paddle.assign(x)
exe = paddle.static.Executor()
if pg.rank() == 0:
dist.broadcast(x, 0, sync_op=False)
else:
dist.broadcast(y, 0)
(x_out, y_out) = exe.run(
main_program,
feed={"x": x_np, "y": y_np},
fetch_list=[x, y],
)
if pg.rank() == 0:
np.testing.assert_array_equal(broadcast_result, x_out)
else:
np.testing.assert_array_equal(broadcast_result, y_out)

def test_broadcast_with_0d_input(self):
pg = self.pg
# rank 0
x_np = np.random.random([]).astype(self.dtype)
# rank 1
y_np = np.random.random([]).astype(self.dtype)
with paddle.pir_utils.IrGuard():
main_program = paddle.static.Program()
startup_program = paddle.static.Program()
with paddle.static.program_guard(main_program, startup_program):
x = paddle.static.data(name="x", shape=[], dtype=self.dtype)
y = paddle.static.data(name="y", shape=[], dtype=self.dtype)
broadcast_result = paddle.assign(x)
exe = paddle.static.Executor()
if pg.rank() == 0:
dist.broadcast(x, 0, sync_op=False)
else:
dist.broadcast(y, 0)
(x_out, y_out) = exe.run(
main_program,
feed={"x": x_np, "y": y_np},
fetch_list=[x, y],
)
if pg.rank() == 0:
np.testing.assert_array_equal(broadcast_result, x_out)
else:
np.testing.assert_array_equal(broadcast_result, y_out)


class TestProcessGroupFp16(TestProcessGroupFp32):
def setUp(self):
Expand Down