distributed_py.cc 54.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (c) 2022 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. */

#include <fcntl.h>
#ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif

#ifdef _XOPEN_SOURCE
#undef _XOPEN_SOURCE
#endif

W
Wen Sun 已提交
24
#include "paddle/fluid/distributed/collective/process_group.h"
25
#include "paddle/fluid/distributed/collective/reducer.h"
W
Wen Sun 已提交
26
#include "paddle/fluid/distributed/collective/types.h"
27 28 29 30 31
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/imperative/layer.h"
#include "paddle/fluid/pybind/distributed_py.h"
#include "paddle/fluid/pybind/eager_utils.h"
32
#include "paddle/fluid/pybind/process_group_utils.h"
33 34
#include "paddle/phi/api/all.h"

35
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
W
Wen Sun 已提交
36
#include "paddle/fluid/distributed/collective/process_group_nccl.h"
37 38
#endif

W
wuhuachaocoding 已提交
39
#if defined(PADDLE_WITH_MPI)
W
Wen Sun 已提交
40
#include "paddle/fluid/distributed/collective/process_group_mpi.h"
W
wuhuachaocoding 已提交
41 42
#endif

43
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
W
Wen Sun 已提交
44
#include "paddle/fluid/distributed/collective/process_group_custom.h"
45 46
#endif

47
#if defined(PADDLE_WITH_GLOO)
W
Wen Sun 已提交
48
#include "paddle/fluid/distributed/collective/process_group_gloo.h"
49 50
#endif

J
james 已提交
51
#if defined(PADDLE_WITH_XPU_BKCL)
W
Wen Sun 已提交
52
#include "paddle/fluid/distributed/collective/process_group_bkcl.h"
J
james 已提交
53 54
#endif

55 56
#include "paddle/phi/kernels/sync_batch_norm_kernel.h"

57 58 59 60 61 62 63
namespace py = pybind11;

namespace paddle {
namespace pybind {

using Tensor = paddle::experimental::Tensor;

64 65 66 67 68
std::shared_ptr<distributed::EagerReducer> CreateEagerReducer(
    py::handle py_tensors,
    const std::vector<std::vector<size_t>> &group_indices,
    const std::vector<bool> &is_sparse_gradient,
    std::shared_ptr<distributed::ProcessGroup> process_group,
69 70
    const std::vector<size_t> &group_size_limits,
    bool find_unused_parameters) {
71
  auto params = CastPyArg2VectorOfTensor(py_tensors.ptr(), 0);
72 73 74 75 76 77
  return std::make_shared<distributed::EagerReducer>(params,
                                                     group_indices,
                                                     is_sparse_gradient,
                                                     process_group,
                                                     group_size_limits,
                                                     find_unused_parameters);
78 79
}

80 81 82 83 84 85
#if defined(PADDLE_WITH_GLOO)
using ProcessGroupGloo = paddle::distributed::ProcessGroupGloo;
using GlooStore = paddle::distributed::ProcessGroupGloo::GlooStore;
using GlooOptions = paddle::distributed::ProcessGroupGloo::GlooOptions;
#endif

86 87 88
static UNUSED void *use_ccl_comm_func =
    phi::detail::GetCCLComm(phi::CPUPlace());

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
void BindDistributed(py::module *m) {
  py::enum_<distributed::ReduceOp>(*m, "ReduceOp")
      .value("SUM", distributed::ReduceOp::SUM)
      .value("AVG", distributed::ReduceOp::AVG)
      .value("MAX", distributed::ReduceOp::MAX)
      .value("MIN", distributed::ReduceOp::MIN)
      .value("PRODUCT", distributed::ReduceOp::PRODUCT);

  py::class_<distributed::AllreduceOptions>(*m, "AllreduceOptions")
      .def(py::init<>())
      .def_readwrite("reduce_op", &distributed::AllreduceOptions::reduce_op);

  py::class_<distributed::BroadcastOptions>(*m, "BroadcastOptions")
      .def(py::init<>())
      .def_readwrite("source_rank", &distributed::BroadcastOptions::source_rank)
      .def_readwrite("source_root",
                     &distributed::BroadcastOptions::source_root);

B
Baibaifan 已提交
107 108
  py::class_<distributed::BarrierOptions>(*m, "BarrierOptions")
      .def(py::init<>())
109
      .def_readwrite("device_id", &distributed::BarrierOptions::device_id);
B
Baibaifan 已提交
110

111 112 113 114 115
  py::class_<distributed::ReduceOptions>(*m, "ReduceOptions")
      .def(py::init<>())
      .def_readwrite("reduce_op", &distributed::ReduceOptions::reduce_op)
      .def_readwrite("source_root", &distributed::ReduceOptions::root_rank);

116 117 118 119 120 121
  auto ProcessGroup =
      py::class_<distributed::ProcessGroup,
                 std::shared_ptr<distributed::ProcessGroup>>(*m, "ProcessGroup")
          .def("rank", &distributed::ProcessGroup::GetRank)
          .def("size", &distributed::ProcessGroup::GetSize)
          .def("name", &distributed::ProcessGroup::GetBackendName)
122
          .def(
L
LiYuRio 已提交
123
              "all_reduce",
124 125 126 127 128
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 distributed::ReduceOp op,
                 bool sync_op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
129
                auto p_dense =
130
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
131 132 133 134
                auto *out_dense = p_dense.get();
                auto in_dense = *p_dense;
                distributed::AllreduceOptions opts{op};
                return self.AllReduce(out_dense, in_dense, opts, sync_op);
135 136 137 138 139 140
              },
              py::arg("tensor"),
              py::arg("op"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

141 142 143 144 145 146 147
          .def(
              "broadcast",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int src,
                 bool sync_op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
148
                auto p_dense =
149
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
150 151 152 153
                auto *out_dense = p_dense.get();
                auto in_dense = *p_dense;
                distributed::BroadcastOptions opts{src};
                return self.Broadcast(out_dense, in_dense, opts, sync_op);
154 155 156 157 158 159
              },
              py::arg("tensor"),
              py::arg("src"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

160 161 162 163 164 165 166
          .def(
              "send",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int dst,
                 bool sync_op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
167
                auto p_dense =
168
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
169
                auto out_dense = *p_dense;
W
Wen Sun 已提交
170
                return self.Send(out_dense, dst, sync_op);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "send_partial",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int dst_rank,
                 int nranks,
                 int rank_id,
                 bool sync_op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
186
                auto p_dense =
187
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
188
                auto out_dense = *p_dense;
189

190
                int64_t numel = p_dense->numel();
191 192
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
193 194

                return self.Send(
195
                    out_dense, dst_rank, offset, send_numel, sync_op);
196 197 198 199 200
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("num"),
              py::arg("id"),
201
              py::arg("sync_op") = true,
202 203 204 205 206 207 208 209 210
              py::call_guard<py::gil_scoped_release>())

          .def(
              "recv",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int src,
                 bool sync_op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
211
                auto p_dense =
212
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
213
                auto *in_dense = p_dense.get();
W
Wen Sun 已提交
214
                return self.Recv(in_dense, src, sync_op);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
              },
              py::arg("tensor"),
              py::arg("src"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "recv_partial",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int src_rank,
                 int nranks,
                 int rank_id,
                 bool sync_op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
230
                auto p_dense =
231
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
232 233
                auto *out_dense = p_dense.get();

234
                int64_t numel = p_dense->numel();
235 236
                int64_t recv_numel = numel / nranks;
                int64_t offset = recv_numel * rank_id;
237 238

                return self.Recv(
239
                    out_dense, src_rank, offset, recv_numel, sync_op);
240 241 242 243 244
              },
              py::arg("tensor"),
              py::arg("src"),
              py::arg("num"),
              py::arg("id"),
245
              py::arg("sync_op") = true,
246 247
              py::call_guard<py::gil_scoped_release>())

248 249
          .def(
              "all_gather",
250 251
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor_list,
252
                 py::handle py_in_tensor,
253 254 255
                 bool sync_op) {
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
256
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
257
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
258
                    stack_out_tensor.impl());
259 260 261 262 263 264
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;
265

266
                auto *dev_ctx = self.GetDeviceContext(in_tensor.place());
W
Wen Sun 已提交
267
                auto task = self.AllGather(out_dense, in_dense, sync_op);
268 269
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
                task->UpdateWaitChain(*dev_ctx);
270 271 272
                return task;
              },
              py::arg("out"),
273
              py::arg("in"),
274 275 276 277
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
278
              "all_gather_into_tensor",
279 280
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
281
                 py::handle py_in_tensor,
282 283
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
284
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
285
                    out_tensor.impl());
286 287 288 289 290 291
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;
292

W
Wen Sun 已提交
293
                return self.AllGather(out_dense, in_dense, sync_op);
294 295
              },
              py::arg("out"),
296
              py::arg("in"),
297 298 299
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

300
          .def(
L
LiYuRio 已提交
301
              "all_to_all",
302 303
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor_list,
304
                 py::handle py_in_tensor_list,
305 306 307
                 bool sync_op) {
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
308
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
309
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
310
                    stack_out_tensor.impl());
311 312 313 314
                auto *out_dense = p_out_tensor.get();

                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
315
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
316
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
317
                    stack_in_tensor.impl());
318
                auto in_dense = *p_in_tensor;
319 320

                // in_tensor_list should not be empty
321
                auto *dev_ctx =
322
                    self.GetDeviceContext(in_tensor_list.back().place());
323 324 325 326 327 328 329
                int world_size = self.GetSize();
                auto task =
                    self.AllToAll(out_dense,
                                  in_dense,
                                  GetDefaultSplitSizes(*out_dense, world_size),
                                  GetDefaultSplitSizes(in_dense, world_size),
                                  sync_op);
330 331
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
                task->UpdateWaitChain(*dev_ctx);
332 333 334
                return task;
              },
              py::arg("out"),
335
              py::arg("in"),
336 337 338 339
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
340
              "all_to_all_tensor",
341 342
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
343
                 py::handle py_in_tensor,
344 345
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
346
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
347
                    out_tensor.impl());
348 349 350 351 352 353
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;
354

355 356 357 358 359 360 361
                int world_size = self.GetSize();
                return self.AllToAll(
                    out_dense,
                    in_dense,
                    GetDefaultSplitSizes(*out_dense, world_size),
                    GetDefaultSplitSizes(in_dense, world_size),
                    sync_op);
362 363
              },
              py::arg("out"),
364
              py::arg("in"),
365 366 367
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

368
          .def(
L
LiYuRio 已提交
369
              "all_to_all_single",
370 371
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
372 373 374
                 py::handle py_in_tensor,
                 const std::vector<int64_t> &out_sizes,
                 const std::vector<int64_t> &in_sizes,
375 376
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
377
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
378
                    out_tensor.impl());
379 380 381 382 383 384
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;
385

386 387
                return self.AllToAll(
                    out_dense, in_dense, out_sizes, in_sizes, sync_op);
388 389
              },
              py::arg("out"),
390
              py::arg("in"),
391
              py::arg("out_sizes"),
392
              py::arg("in_sizes"),
393 394 395 396 397 398
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "reduce",
              [](distributed::ProcessGroup &self,
399
                 py::handle py_tensor,
400 401 402
                 int dst,
                 distributed::ReduceOp op,
                 bool sync_op) {
403 404 405 406 407
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
                auto p_dense =
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
                auto *out_dense = p_dense.get();
                auto in_dense = *p_dense;
408
                distributed::ReduceOptions opts{op, dst};
409
                return self.Reduce(out_dense, in_dense, opts, sync_op);
410 411 412 413 414 415 416 417 418 419 420
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("op"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "reduce_scatter",
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
421
                 py::handle py_in_tensor_list,
422 423
                 distributed::ReduceOp op,
                 bool sync_op) {
424 425 426 427 428
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    out_tensor.impl());
                auto out_dense = p_out_tensor.get();

429 430
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
431
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
432
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
433
                    stack_in_tensor.impl());
434
                auto in_dense = *p_in_tensor;
435 436

                distributed::ReduceScatterOptions opts{op};
437
                return self.ReduceScatter(out_dense, in_dense, opts, sync_op);
438 439
              },
              py::arg("out"),
440
              py::arg("in"),
441 442 443 444 445 446 447 448
              py::arg("op"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "reduce_scatter_tensor",
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
449
                 py::handle py_in_tensor,
450 451 452
                 distributed::ReduceOp op,
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
453
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
454
                    out_tensor.impl());
455 456 457 458 459 460
                auto out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;
461 462

                distributed::ReduceScatterOptions opts{op};
463
                return self.ReduceScatter(out_dense, in_dense, opts, sync_op);
464 465
              },
              py::arg("out"),
466
              py::arg("in"),
467 468 469 470 471 472 473 474
              py::arg("op"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter",
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
475
                 py::handle py_in_tensor_list,
476 477
                 int src,
                 bool sync_op) {
478 479 480 481 482
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    out_tensor.impl());
                auto *out_dense = p_out_tensor.get();

483 484
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
485
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
486
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
487
                    stack_in_tensor.impl());
488
                auto in_dense = *p_in_tensor;
489 490

                distributed::ScatterOptions opts{src};
491
                return self.Scatter(out_dense, in_dense, opts, sync_op);
492 493
              },
              py::arg("out"),
494
              py::arg("in"),
495 496 497 498 499 500 501 502
              py::arg("src"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter_tensor",
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
503
                 py::handle py_in_tensor,
504 505 506
                 int src,
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
507
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
508
                    out_tensor.impl());
509 510 511 512 513 514
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;
515 516

                distributed::ScatterOptions opts{src};
517
                return self.Scatter(out_dense, in_dense, opts, sync_op);
518 519
              },
              py::arg("out"),
520
              py::arg("in"),
521 522 523 524
              py::arg("src"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

L
LiYuRio 已提交
525 526
          .def(
              "barrier",
527
              [](distributed::ProcessGroup &self, int8_t device_id) {
L
LiYuRio 已提交
528
                distributed::BarrierOptions opts;
529
                opts.device_id = device_id;
L
LiYuRio 已提交
530 531
                return self.Barrier(opts);
              },
532
              py::arg("device_id") = -1,
L
LiYuRio 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
              py::call_guard<py::gil_scoped_release>())

          // TODO(liyurui): Interface below will be removed in the future.
          .def(
              "allreduce",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 distributed::ReduceOp op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
                distributed::AllreduceOptions opts;
                opts.reduce_op = op;
                auto dense =
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
                std::vector<phi::DenseTensor> tensors = {*dense};
                return self.AllReduce(tensors, tensors, opts);
              },
              py::arg("tensor"),
              py::arg("op") = distributed::ReduceOp::SUM,
              py::call_guard<py::gil_scoped_release>())

          .def(
              "broadcast",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int source_rank) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
                distributed::BroadcastOptions opts;
                opts.source_rank = source_rank;
                auto dense =
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
                std::vector<phi::DenseTensor> tensors = {*dense};
                return self.Broadcast(tensors, tensors, opts);
              },
              py::arg("tensor"),
              py::arg("source_rank"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "send",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int dst) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
                auto dense =
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
                std::vector<phi::DenseTensor> tensors = {*dense};
                return self.Send(tensors, dst);
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "recv",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int src) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
                auto dense =
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
                std::vector<phi::DenseTensor> tensors = {*dense};
                return self.Recv(tensors, src);
              },
              py::arg("tensor"),
              py::arg("src"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "all_gather",
              [](distributed::ProcessGroup &self,
                 py::handle py_in_tensor,
                 py::handle py_out_tensor) {
                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
                auto in_dense = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto out_dense = std::dynamic_pointer_cast<phi::DenseTensor>(
                    out_tensor.impl());
                std::vector<phi::DenseTensor> in_tensors = {*in_dense};
                std::vector<phi::DenseTensor> out_tensors = {*out_dense};
                return self.AllGather(in_tensors, out_tensors);
              },
              py::arg("in"),
              py::arg("out"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "all_gather_partial",
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
623
                 py::handle py_in_tensor,
L
LiYuRio 已提交
624 625 626
                 int nranks,
                 int rank_id) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
627
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
L
LiYuRio 已提交
628
                    out_tensor.impl());
629 630 631 632 633 634 635 636
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;

                int64_t numel = in_dense.numel();
L
LiYuRio 已提交
637 638
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
639 640
                return self.AllGather(
                    out_dense, in_dense, offset, send_numel, /*sync_op*/ true);
L
LiYuRio 已提交
641 642
              },
              py::arg("out"),
643
              py::arg("in"),
L
LiYuRio 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
              py::arg("num"),
              py::arg("id"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "alltoall",
              [](distributed::ProcessGroup &self,
                 py::handle py_in_tensor,
                 py::handle py_out_tensor) {
                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
                auto in_dense = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto out_dense = std::dynamic_pointer_cast<phi::DenseTensor>(
                    out_tensor.impl());
                std::vector<phi::DenseTensor> in_tensors = {*in_dense};
                std::vector<phi::DenseTensor> out_tensors = {*out_dense};
                return self.AllToAll(in_tensors, out_tensors);
              },
              py::arg("in"),
              py::arg("out"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "alltoall_single",
              [](distributed::ProcessGroup &self,
                 py::handle py_in_tensor,
                 py::handle py_out_tensor,
672 673
                 const std::vector<int64_t> in_sizes,
                 const std::vector<int64_t> out_sizes) {
L
LiYuRio 已提交
674
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
675
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
L
LiYuRio 已提交
676
                    out_tensor.impl());
677 678 679 680 681 682 683 684 685
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;

                return self.AllToAll(
                    out_dense, in_dense, out_sizes, in_sizes, /*sync_op*/ true);
L
LiYuRio 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
              },
              py::arg("in"),
              py::arg("out"),
              py::arg("in_sizes"),
              py::arg("out_sizes"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "reduce",
              [](distributed::ProcessGroup &self,
                 py::handle py_in_tensor,
                 int dst,
                 distributed::ReduceOp op) {
                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                distributed::ReduceOptions opts;
                opts.reduce_op = op;
                opts.root_rank = dst;
                auto dense = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                std::vector<phi::DenseTensor> tensors = {*dense};
                return self.Reduce(tensors, tensors, opts);
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("op") = distributed::ReduceOp::SUM,
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter",
              [](distributed::ProcessGroup &self,
                 py::handle py_in_tensor,
                 py::handle py_out_tensor,
                 int src) {
                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
                distributed::ScatterOptions opts;
                opts.root_rank = src;
                auto in_dense = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto out_dense = std::dynamic_pointer_cast<phi::DenseTensor>(
                    out_tensor.impl());
                std::vector<phi::DenseTensor> in_tensors = {*in_dense};
                std::vector<phi::DenseTensor> out_tensors = {*out_dense};
                return self.Scatter(in_tensors, out_tensors, opts);
              },
              py::arg("in"),
              py::arg("out"),
              py::arg("src"),
734
              py::call_guard<py::gil_scoped_release>())
735

736
          .def(
L
LiYuRio 已提交
737
              "all_gather_on_calc_stream",
738
              [](distributed::ProcessGroup &self,
739 740
                 py::handle py_out_tensor_list,
                 py::handle py_in_tensor) {
741 742
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
743
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
744
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
745
                    stack_out_tensor.impl());
746 747 748 749 750 751
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;
752

753
                auto *dev_ctx = self.GetDeviceContext(in_tensor.place(), true);
754 755
                auto task = self.AllGather(out_dense,
                                           in_dense,
756 757
                                           /*sync_op*/ true,
                                           /*use_calc_stream*/ true);
758
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
759 760 761
                return task;
              },
              py::arg("out"),
762
              py::arg("in"),
763 764 765
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
766
              "all_gather_into_tensor_on_calc_stream",
767
              [](distributed::ProcessGroup &self,
768 769
                 py::handle py_out_tensor,
                 py::handle py_in_tensor) {
770
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
771
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
772
                    out_tensor.impl());
773
                auto *out_dense = p_out_tensor.get();
774

775 776 777 778 779 780 781
                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;

                return self.AllGather(out_dense,
                                      in_dense,
782 783 784 785
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
              },
              py::arg("out"),
786
              py::arg("in"),
787 788
              py::call_guard<py::gil_scoped_release>())

789 790
          .def(
              "all_gather_partial_on_calc_stream",
791
              [](distributed::ProcessGroup &self,
792
                 py::handle py_out_tensor,
793
                 py::handle py_in_tensor,
794 795 796
                 int nranks,
                 int rank_id) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
797
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
798
                    out_tensor.impl());
799 800 801 802 803 804 805 806
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;

                int64_t numel = in_dense.numel();
807 808
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
809 810 811 812 813 814 815

                return self.AllGather(out_dense,
                                      in_dense,
                                      offset,
                                      send_numel,
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
816 817
              },
              py::arg("out"),
818
              py::arg("in"),
819 820 821 822
              py::arg("num"),
              py::arg("id"),
              py::call_guard<py::gil_scoped_release>())

823
          .def(
L
LiYuRio 已提交
824
              "all_reduce_on_calc_stream",
825
              [](distributed::ProcessGroup &self,
826 827 828
                 py::handle py_tensor,
                 distributed::ReduceOp op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
829
                auto p_dense =
830
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
831 832 833 834 835
                auto in_dense = *p_dense;
                auto *out_dense = p_dense.get();
                distributed::AllreduceOptions opts{op};
                return self.AllReduce(out_dense,
                                      in_dense,
836 837 838 839 840
                                      opts,
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
              },
              py::arg("tensor"),
L
LiYuRio 已提交
841
              py::arg("op") = distributed::ReduceOp::SUM,
842 843
              py::call_guard<py::gil_scoped_release>())

844
          .def(
L
LiYuRio 已提交
845
              "all_to_all_on_calc_stream",
846
              [](distributed::ProcessGroup &self,
847 848
                 py::handle py_out_tensor_list,
                 py::handle py_in_tensor_list) {
849 850
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
851
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
852
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
853
                    stack_out_tensor.impl());
854 855 856 857
                auto *out_dense = p_out_tensor.get();

                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
858
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
859
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
860
                    stack_in_tensor.impl());
861
                auto in_dense = *p_in_tensor;
862

863
                // in_tensor_list should not be empty
864
                auto *dev_ctx = self.GetDeviceContext(
865
                    in_tensor_list.back().place(), /*use_calc_stream*/ true);
866 867 868 869 870 871 872 873
                int world_size = self.GetSize();
                auto task =
                    self.AllToAll(out_dense,
                                  in_dense,
                                  GetDefaultSplitSizes(*out_dense, world_size),
                                  GetDefaultSplitSizes(in_dense, world_size),
                                  /*sync_op*/ true,
                                  /*use_calc_stream*/ true);
874
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
875 876 877
                return task;
              },
              py::arg("out"),
878
              py::arg("in"),
879 880 881
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
882
              "all_to_all_tensor_on_calc_stream",
883
              [](distributed::ProcessGroup &self,
884 885
                 py::handle py_out_tensor,
                 py::handle py_in_tensor) {
886
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
887
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
888
                    out_tensor.impl());
889
                auto *out_dense = p_out_tensor.get();
890

891 892 893 894 895 896 897 898 899 900 901 902 903
                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;

                int world_size = self.GetSize();
                return self.AllToAll(
                    out_dense,
                    in_dense,
                    GetDefaultSplitSizes(*out_dense, world_size),
                    GetDefaultSplitSizes(in_dense, world_size),
                    /*sync_op*/ true,
                    /*use_calc_stream*/ true);
904 905
              },
              py::arg("out"),
906
              py::arg("in"),
907 908 909
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
910
              "all_to_all_single_on_calc_stream",
911
              [](distributed::ProcessGroup &self,
912
                 py::handle py_out_tensor,
913 914 915
                 py::handle py_in_tensor,
                 const std::vector<int64_t> &out_sizes,
                 const std::vector<int64_t> &in_sizes) {
916
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
917
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
918
                    out_tensor.impl());
919
                auto *out_dense = p_out_tensor.get();
920

921 922 923 924 925 926 927 928 929 930 931
                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;

                return self.AllToAll(out_dense,
                                     in_dense,
                                     out_sizes,
                                     in_sizes,
                                     /*sync_op*/ true,
                                     /*use_calc_stream*/ true);
932 933
              },
              py::arg("out"),
934
              py::arg("in"),
935
              py::arg("out_sizes"),
936
              py::arg("in_sizes"),
937 938 939 940
              py::call_guard<py::gil_scoped_release>())

          .def(
              "broadcast_on_calc_stream",
941
              [](distributed::ProcessGroup &self,
942 943 944
                 py::handle py_tensor,
                 int src) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
945
                auto p_dense =
946
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
947 948 949 950 951
                auto *out_dense = p_dense.get();
                auto in_dense = *p_dense;
                distributed::BroadcastOptions opts{src};
                return self.Broadcast(out_dense,
                                      in_dense,
952 953 954 955 956 957 958 959 960 961
                                      opts,
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
              },
              py::arg("tensor"),
              py::arg("src"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "reduce_on_calc_stream",
962
              [](distributed::ProcessGroup &self,
963
                 py::handle py_tensor,
964 965
                 int dst,
                 distributed::ReduceOp op) {
966 967 968 969 970
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
                auto p_dense =
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
                auto *out_dense = p_dense.get();
                auto in_dense = *p_dense;
971
                distributed::ReduceOptions opts{op, dst};
972 973
                return self.Reduce(out_dense,
                                   in_dense,
974 975 976 977 978 979 980 981 982 983 984
                                   opts,
                                   /*sync_op*/ true,
                                   /*use_calc_stream*/ true);
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "reduce_scatter_on_calc_stream",
985
              [](distributed::ProcessGroup &self,
986
                 py::handle py_out_tensor,
987
                 py::handle py_in_tensor_list,
988
                 distributed::ReduceOp op) {
989 990 991 992 993
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    out_tensor.impl());
                auto out_dense = p_out_tensor.get();

994 995
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
996
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
997
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
998
                    stack_in_tensor.impl());
999
                auto in_dense = *p_in_tensor;
1000 1001

                distributed::ReduceScatterOptions opts{op};
1002 1003
                return self.ReduceScatter(out_dense,
                                          in_dense,
1004 1005 1006 1007 1008
                                          opts,
                                          /*sync_op*/ true,
                                          /*use_calc_stream*/ true);
              },
              py::arg("out"),
1009
              py::arg("in"),
1010 1011 1012 1013 1014
              py::arg("op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "reduce_scatter_tensor_on_calc_stream",
1015
              [](distributed::ProcessGroup &self,
1016
                 py::handle py_out_tensor,
1017
                 py::handle py_in_tensor,
1018 1019
                 distributed::ReduceOp op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
1020
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1021
                    out_tensor.impl());
1022 1023 1024 1025 1026 1027
                auto out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;
1028 1029

                distributed::ReduceScatterOptions opts{op};
1030 1031
                return self.ReduceScatter(out_dense,
                                          in_dense,
1032 1033 1034
                                          opts,
                                          /*sync_op*/ true,
                                          /*use_calc_stream*/ true);
1035 1036
              },
              py::arg("out"),
1037
              py::arg("in"),
1038 1039 1040 1041 1042
              py::arg("op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter_on_calc_stream",
1043
              [](distributed::ProcessGroup &self,
1044
                 py::handle py_out_tensor,
1045
                 py::handle py_in_tensor_list,
1046
                 int src) {
1047 1048 1049 1050 1051
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    out_tensor.impl());
                auto *out_dense = p_out_tensor.get();

1052 1053
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
1054
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
1055
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1056
                    stack_in_tensor.impl());
1057
                auto in_dense = *p_in_tensor;
1058 1059

                distributed::ScatterOptions opts{src};
1060 1061
                return self.Scatter(out_dense,
                                    in_dense,
1062 1063 1064 1065 1066
                                    opts,
                                    /*sync_op*/ true,
                                    /*use_calc_stream*/ true);
              },
              py::arg("out"),
1067
              py::arg("in"),
1068 1069 1070 1071 1072
              py::arg("src"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter_tensor_on_calc_stream",
1073
              [](distributed::ProcessGroup &self,
1074
                 py::handle py_out_tensor,
1075
                 py::handle py_in_tensor,
1076 1077
                 int src) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
1078
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1079
                    out_tensor.impl());
1080 1081 1082 1083 1084 1085
                auto *out_dense = p_out_tensor.get();

                auto in_tensor = CastPyArg2Tensor(py_in_tensor.ptr(), 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    in_tensor.impl());
                auto in_dense = *p_in_tensor;
1086 1087

                distributed::ScatterOptions opts{src};
1088 1089
                return self.Scatter(out_dense,
                                    in_dense,
1090 1091 1092 1093 1094
                                    opts,
                                    /*sync_op*/ true,
                                    /*use_calc_stream*/ true);
              },
              py::arg("out"),
1095
              py::arg("in"),
1096
              py::arg("src"),
1097 1098
              py::call_guard<py::gil_scoped_release>())

1099 1100
          .def(
              "send_on_calc_stream",
1101
              [](distributed::ProcessGroup &self,
1102 1103 1104
                 py::handle py_tensor,
                 int dst) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1105
                auto p_dense =
1106
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1107
                auto out_dense = *p_dense;
1108
                return self.Send(out_dense,
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
                                 dst,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "send_partial_on_calc_stream",
1119
              [](distributed::ProcessGroup &self,
1120 1121 1122 1123 1124
                 py::handle py_tensor,
                 int dst_rank,
                 int nranks,
                 int rank_id) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1125
                auto p_dense =
1126
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1127
                auto out_dense = *p_dense;
1128

1129
                int64_t numel = p_dense->numel();
1130 1131
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
1132 1133 1134 1135 1136 1137 1138

                return self.Send(out_dense,
                                 dst_rank,
                                 offset,
                                 send_numel,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
1139 1140 1141 1142 1143 1144 1145 1146 1147
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("num"),
              py::arg("id"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "recv_on_calc_stream",
1148
              [](distributed::ProcessGroup &self,
1149 1150 1151
                 py::handle py_tensor,
                 int src) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1152
                auto p_dense =
1153
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1154 1155
                auto *in_dense = p_dense.get();
                return self.Recv(in_dense,
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
                                 src,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
              },
              py::arg("tensor"),
              py::arg("src"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "recv_partial_on_calc_stream",
1166
              [](distributed::ProcessGroup &self,
1167 1168 1169 1170 1171
                 py::handle py_tensor,
                 int src_rank,
                 int nranks,
                 int rank_id) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1172
                auto p_dense =
1173
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1174 1175
                auto *out_dense = p_dense.get();

1176
                int64_t numel = p_dense->numel();
1177 1178
                int64_t recv_numel = numel / nranks;
                int64_t offset = recv_numel * rank_id;
1179 1180 1181 1182 1183 1184 1185

                return self.Recv(out_dense,
                                 src_rank,
                                 offset,
                                 recv_numel,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
1186 1187 1188 1189 1190
              },
              py::arg("tensor"),
              py::arg("src"),
              py::arg("num"),
              py::arg("id"),
1191 1192
              py::call_guard<py::gil_scoped_release>());

1193
#if defined(PADDLE_WITH_RCCL) || defined(PADDLE_WITH_NCCL)
L
LiYuRio 已提交
1194 1195
  py::class_<distributed::ProcessGroupNCCL,
             std::shared_ptr<distributed::ProcessGroupNCCL>>(
1196
      *m, "ProcessGroupNCCL", ProcessGroup)
L
LiYuRio 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205
      .def_static("create",
                  distributed::ProcessGroupNCCL::CreateProcessGroupNCCL,
                  py::arg("store"),
                  py::arg("rank"),
                  py::arg("world_size"),
                  py::arg("group_id") = 0,
                  py::call_guard<py::gil_scoped_release>())
      .def_static("group_start", distributed::ProcessGroupNCCL::GroupStart)
      .def_static("group_end", distributed::ProcessGroupNCCL::GroupEnd);
1206

1207
#endif
1208

W
wuhuachaocoding 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
#if defined(PADDLE_WITH_MPI)
  py::class_<distributed::ProcessGroupMPI,
             std::shared_ptr<distributed::ProcessGroupMPI>>(
      *m, "ProcessGroupMPI", ProcessGroup)
      .def_static(
          "create",
          [](const std::vector<int> &ranks,
             int gid) -> std::shared_ptr<distributed::ProcessGroupMPI> {
            return paddle::distributed::ProcessGroupMPI::CreateProcessGroupMPI(
                ranks, gid);
          })
      .def("get_rank",
           &distributed::ProcessGroup::GetRank,
           py::call_guard<py::gil_scoped_release>())
      .def("get_world_size",
           &distributed::ProcessGroup::GetSize,
           py::call_guard<py::gil_scoped_release>());
#endif

1228 1229 1230 1231
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
  py::class_<distributed::ProcessGroupCustom,
             std::shared_ptr<distributed::ProcessGroupCustom>>(
      *m, "ProcessGroupCustom", ProcessGroup)
L
LiYuRio 已提交
1232 1233 1234 1235 1236 1237 1238 1239
      .def_static("create",
                  distributed::ProcessGroupCustom::CreateProcessGroupCustom,
                  py::arg("store"),
                  py::arg("device_type"),
                  py::arg("rank"),
                  py::arg("world_size"),
                  py::arg("group_id") = 0,
                  py::call_guard<py::gil_scoped_release>());
1240

1241 1242
#endif

J
james 已提交
1243 1244 1245 1246
#if defined(PADDLE_WITH_XPU_BKCL)
  auto processGroupBKCL =
      py::class_<distributed::ProcessGroupBKCL,
                 std::shared_ptr<distributed::ProcessGroupBKCL>>(
1247
          *m, "ProcessGroupBKCL", ProcessGroup)
L
LiYuRio 已提交
1248 1249 1250 1251 1252 1253
          .def_static("create",
                      distributed::ProcessGroupBKCL::CreateProcessGroupBKCL,
                      py::arg("store"),
                      py::arg("rank"),
                      py::arg("world_size"),
                      py::arg("group_id") = 0,
1254 1255 1256
                      py::call_guard<py::gil_scoped_release>())
          .def_static("group_start", distributed::ProcessGroupBKCL::GroupStart)
          .def_static("group_end", distributed::ProcessGroupBKCL::GroupEnd);
J
james 已提交
1257 1258
#endif

1259 1260 1261
  py::class_<distributed::ProcessGroup::Task,
             std::shared_ptr<distributed::ProcessGroup::Task>>(*m, "task")
      .def("is_completed", &distributed::ProcessGroup::Task::IsCompleted)
1262
      .def("is_sync", &distributed::ProcessGroup::Task::IsSync)
1263 1264
      .def("wait",
           &distributed::ProcessGroup::Task::Wait,
1265 1266
           py::arg("timeout") = kWaitTimeout,
           py::call_guard<py::gil_scoped_release>())
1267 1268
      .def("synchronize",
           &distributed::ProcessGroup::Task::Synchronize,
1269 1270
           py::call_guard<py::gil_scoped_release>());

1271 1272 1273
#if defined(PADDLE_WITH_GLOO)
  py::class_<ProcessGroupGloo, std::shared_ptr<ProcessGroupGloo>>(
      *m, "ProcessGroupGloo", ProcessGroup)
L
LiYuRio 已提交
1274 1275 1276 1277 1278 1279 1280
      .def_static("create",
                  distributed::ProcessGroupGloo::CreateProcessGroupGloo,
                  py::arg("store"),
                  py::arg("rank"),
                  py::arg("world_size"),
                  py::arg("group_id") = 0,
                  py::call_guard<py::gil_scoped_release>())
1281 1282 1283 1284
      .def_static("create_default_device",
                  &ProcessGroupGloo::createDefaultDevice);
#endif

1285 1286
  m->def(
      "eager_assign_group_by_size",
1287 1288
      [](py::handle py_tensors,
         std::vector<bool> is_sparse_gradient,
1289 1290 1291 1292 1293 1294
         std::vector<size_t> group_size_limits,
         std::vector<int64_t> tensor_indices) {
        auto tensors = CastPyArg2VectorOfTensor(py_tensors.ptr(), 0);
        return distributed::Eager_AssignGroupBySize(
            tensors, is_sparse_gradient, group_size_limits, tensor_indices);
      },
1295 1296
      py::arg("tensors"),
      py::arg("is_sparse_gradient"),
1297 1298 1299
      py::arg("group_size_limits") = std::vector<size_t>{25 * 1024 * 1024},
      py::arg("tensor_indices") = std::vector<int64_t>{},
      py::call_guard<py::gil_scoped_release>());
1300 1301

  py::class_<distributed::EagerReducer,
1302 1303
             std::shared_ptr<distributed::EagerReducer>>(
      *m, "EagerReducer", R"DOC()DOC")
1304
      .def(py::init(&CreateEagerReducer))
1305 1306
      .def(
          "prepare_for_backward",
1307
          [](distributed::EagerReducer &self, py::handle py_tensors) {
1308
            auto params = CastPyArg2VectorOfTensor(py_tensors.ptr(), 0);
1309
            self.PrepareForBackward(params);
1310
          },
1311 1312
          py::arg("tensors"),
          py::call_guard<py::gil_scoped_release>());
1313 1314 1315 1316 1317 1318 1319 1320

  py::class_<distributed::ProcessGroupIdMap,
             std::shared_ptr<distributed::ProcessGroupIdMap>>(
      *m, "ProcessGroupIdMap")
      .def_static("destroy",
                  distributed::ProcessGroupIdMap::DestroyProcessGroup,
                  py::arg("group_id") = 0,
                  py::call_guard<py::gil_scoped_release>());
1321 1322 1323 1324
}

}  // end namespace pybind
}  // namespace paddle