distributed_py.cc 56.3 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 24
/* 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

#include "paddle/fluid/distributed/collective/ProcessGroup.h"
25
#include "paddle/fluid/distributed/collective/ProcessGroupStream.h"
26
#include "paddle/fluid/distributed/collective/Types.h"
27
#include "paddle/fluid/distributed/collective/reducer.h"
28 29 30 31 32
#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"
33
#include "paddle/fluid/pybind/process_group_utils.h"
34 35
#include "paddle/phi/api/all.h"

36
#if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL)
37 38 39
#include "paddle/fluid/distributed/collective/ProcessGroupNCCL.h"
#endif

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

44 45 46 47
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
#include "paddle/fluid/distributed/collective/ProcessGroupCustom.h"
#endif

48 49 50 51 52
#if defined(PADDLE_WITH_GLOO)
#include "paddle/fluid/distributed/collective/ProcessGroupGloo.h"
#include "paddle/fluid/distributed/store/tcp_store.h"
#endif

J
james 已提交
53 54 55 56
#if defined(PADDLE_WITH_XPU_BKCL)
#include "paddle/fluid/distributed/collective/ProcessGroupBKCL.h"
#endif

57 58
#include "paddle/phi/kernels/sync_batch_norm_kernel.h"

59 60 61 62 63 64 65
namespace py = pybind11;

namespace paddle {
namespace pybind {

using Tensor = paddle::experimental::Tensor;

66 67 68 69 70
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,
71 72
    const std::vector<size_t> &group_size_limits,
    bool find_unused_parameters) {
73
  auto params = CastPyArg2VectorOfTensor(py_tensors.ptr(), 0);
74 75 76 77 78 79
  return std::make_shared<distributed::EagerReducer>(params,
                                                     group_indices,
                                                     is_sparse_gradient,
                                                     process_group,
                                                     group_size_limits,
                                                     find_unused_parameters);
80 81
}

82 83 84 85 86 87 88 89
#if defined(PADDLE_WITH_GLOO)
using ProcessGroupGloo = paddle::distributed::ProcessGroupGloo;
using GlooStore = paddle::distributed::ProcessGroupGloo::GlooStore;
using GlooOptions = paddle::distributed::ProcessGroupGloo::GlooOptions;
#endif

static std::string GLOO_SOCKET_IFNAME_ENV = "GLOO_SOCKET_IFNAME";  // NOLINT

90 91 92
static UNUSED void *use_ccl_comm_func =
    phi::detail::GetCCLComm(phi::CPUPlace());

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
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 已提交
111 112
  py::class_<distributed::BarrierOptions>(*m, "BarrierOptions")
      .def(py::init<>())
113
      .def_readwrite("device_id", &distributed::BarrierOptions::device_id);
B
Baibaifan 已提交
114

115 116 117 118 119
  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);

120 121 122 123 124 125
  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)
126
          .def(
L
LiYuRio 已提交
127
              "all_reduce",
128 129 130 131 132
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 distributed::ReduceOp op,
                 bool sync_op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
133
                auto p_dense =
134
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
135 136 137 138
                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);
139 140 141 142 143 144
              },
              py::arg("tensor"),
              py::arg("op"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

145 146 147 148 149 150 151
          .def(
              "broadcast",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int src,
                 bool sync_op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
152
                auto p_dense =
153
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
154 155 156 157
                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);
158 159 160 161 162 163
              },
              py::arg("tensor"),
              py::arg("src"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

164 165 166 167 168 169 170
          .def(
              "send",
              [](distributed::ProcessGroup &self,
                 py::handle py_tensor,
                 int dst,
                 bool sync_op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
171
                auto p_dense =
172
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
173
                auto *out_dense = p_dense.get();
174 175 176
                // numel == -1 indicates sending the whole tensor
                return self.Send(
                    out_dense, dst, /*offset*/ 0, /*numel*/ -1, sync_op);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
              },
              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);
192
                auto p_dense =
193
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
194 195
                auto *out_dense = p_dense.get();

196
                int64_t numel = p_dense->numel();
197 198
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
199 200

                return self.Send(
201
                    out_dense, dst_rank, offset, send_numel, sync_op);
202 203 204 205 206
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("num"),
              py::arg("id"),
207
              py::arg("sync_op") = true,
208 209 210 211 212 213 214 215 216
              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);
217
                auto p_dense =
218
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
219
                auto *in_dense = p_dense.get();
220 221 222
                // numel == -1 indicates receiving the whole tensor
                return self.Recv(
                    in_dense, src, /*offset*/ 0, /*numel*/ -1, sync_op);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
              },
              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);
238
                auto p_dense =
239
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
240 241
                auto *out_dense = p_dense.get();

242
                int64_t numel = p_dense->numel();
243 244
                int64_t recv_numel = numel / nranks;
                int64_t offset = recv_numel * rank_id;
245 246

                return self.Recv(
247
                    out_dense, src_rank, offset, recv_numel, sync_op);
248 249 250 251 252
              },
              py::arg("tensor"),
              py::arg("src"),
              py::arg("num"),
              py::arg("id"),
253
              py::arg("sync_op") = true,
254 255
              py::call_guard<py::gil_scoped_release>())

256 257
          .def(
              "all_gather",
258 259
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor_list,
260
                 py::handle py_in_tensor,
261 262 263 264
                 bool sync_op) {
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
                Tensor concat_out_tensor = paddle::concat(out_tensor_list, 0);
265
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
266
                    concat_out_tensor.impl());
267 268 269 270 271 272
                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;
273

274
                const auto &dev_ctx = self.GetDeviceContext(in_tensor.place());
275 276 277 278 279
                auto task = self.AllGather(out_dense,
                                           in_dense,
                                           /*offset*/ 0,
                                           /*numel*/ -1,
                                           sync_op);
280
                SplitTensor(dev_ctx, *out_dense, &out_tensor_list);
281
                task->UpdateWaitChain(dev_ctx);
282 283 284
                return task;
              },
              py::arg("out"),
285
              py::arg("in"),
286 287 288 289
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
290
              "all_gather_into_tensor",
291 292
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
293
                 py::handle py_in_tensor,
294 295
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
296
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
297
                    out_tensor.impl());
298 299 300 301 302 303
                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;
304

305 306 307 308 309
                return self.AllGather(out_dense,
                                      in_dense,
                                      /*offset*/ 0,
                                      /*numel*/ -1,
                                      sync_op);
310 311
              },
              py::arg("out"),
312
              py::arg("in"),
313 314 315
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

316
          .def(
L
LiYuRio 已提交
317
              "all_to_all",
318 319
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor_list,
320
                 py::handle py_in_tensor_list,
321 322 323 324
                 bool sync_op) {
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
                Tensor concat_out_tensor = paddle::concat(out_tensor_list, 0);
325
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
326
                    concat_out_tensor.impl());
327 328 329 330 331 332 333 334
                auto *out_dense = p_out_tensor.get();

                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
                Tensor concat_in_tensor = paddle::concat(in_tensor_list, 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    concat_in_tensor.impl());
                auto in_dense = *p_in_tensor;
335 336

                // in_tensor_list should not be empty
337
                const auto &dev_ctx =
338
                    self.GetDeviceContext(in_tensor_list.back().place());
339 340 341 342 343 344 345 346
                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);
                SplitTensor(dev_ctx, *out_dense, &out_tensor_list);
347
                task->UpdateWaitChain(dev_ctx);
348 349 350
                return task;
              },
              py::arg("out"),
351
              py::arg("in"),
352 353 354 355
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
356
              "all_to_all_tensor",
357 358
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
359
                 py::handle py_in_tensor,
360 361
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
362
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
363
                    out_tensor.impl());
364 365 366 367 368 369
                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;
370

371 372 373 374 375 376 377
                int world_size = self.GetSize();
                return self.AllToAll(
                    out_dense,
                    in_dense,
                    GetDefaultSplitSizes(*out_dense, world_size),
                    GetDefaultSplitSizes(in_dense, world_size),
                    sync_op);
378 379
              },
              py::arg("out"),
380
              py::arg("in"),
381 382 383
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

384
          .def(
L
LiYuRio 已提交
385
              "all_to_all_single",
386 387
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
388 389 390
                 py::handle py_in_tensor,
                 const std::vector<int64_t> &out_sizes,
                 const std::vector<int64_t> &in_sizes,
391 392
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
393
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
394
                    out_tensor.impl());
395 396 397 398 399 400
                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;
401

402 403
                return self.AllToAll(
                    out_dense, in_dense, out_sizes, in_sizes, sync_op);
404 405
              },
              py::arg("out"),
406
              py::arg("in"),
407
              py::arg("out_sizes"),
408
              py::arg("in_sizes"),
409 410 411 412 413 414
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "reduce",
              [](distributed::ProcessGroup &self,
415
                 py::handle py_tensor,
416 417 418
                 int dst,
                 distributed::ReduceOp op,
                 bool sync_op) {
419 420 421 422 423
                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;
424
                distributed::ReduceOptions opts{op, dst};
425
                return self.Reduce(out_dense, in_dense, opts, sync_op);
426 427 428 429 430 431 432 433 434 435 436
              },
              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,
437
                 py::handle py_in_tensor_list,
438 439
                 distributed::ReduceOp op,
                 bool sync_op) {
440 441 442 443 444
                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();

445 446 447
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
                Tensor concat_in_tensor = paddle::concat(in_tensor_list, 0);
448
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
449
                    concat_in_tensor.impl());
450
                auto in_dense = *p_in_tensor;
451 452

                distributed::ReduceScatterOptions opts{op};
453
                return self.ReduceScatter(out_dense, in_dense, opts, sync_op);
454 455
              },
              py::arg("out"),
456
              py::arg("in"),
457 458 459 460 461 462 463 464
              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,
465
                 py::handle py_in_tensor,
466 467 468
                 distributed::ReduceOp op,
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
469
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
470
                    out_tensor.impl());
471 472 473 474 475 476
                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;
477 478

                distributed::ReduceScatterOptions opts{op};
479
                return self.ReduceScatter(out_dense, in_dense, opts, sync_op);
480 481
              },
              py::arg("out"),
482
              py::arg("in"),
483 484 485 486 487 488 489 490
              py::arg("op"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter",
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
491
                 py::handle py_in_tensor_list,
492 493
                 int src,
                 bool sync_op) {
494 495 496 497 498
                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();

499 500 501
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
                Tensor concat_in_tensor = paddle::concat(in_tensor_list, 0);
502
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
503
                    concat_in_tensor.impl());
504
                auto in_dense = *p_in_tensor;
505 506

                distributed::ScatterOptions opts{src};
507
                return self.Scatter(out_dense, in_dense, opts, sync_op);
508 509
              },
              py::arg("out"),
510
              py::arg("in"),
511 512 513 514 515 516 517 518
              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,
519
                 py::handle py_in_tensor,
520 521 522
                 int src,
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
523
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
524
                    out_tensor.impl());
525 526 527 528 529 530
                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;
531 532

                distributed::ScatterOptions opts{src};
533
                return self.Scatter(out_dense, in_dense, opts, sync_op);
534 535
              },
              py::arg("out"),
536
              py::arg("in"),
537 538 539 540
              py::arg("src"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

L
LiYuRio 已提交
541 542
          .def(
              "barrier",
543
              [](distributed::ProcessGroup &self, int8_t device_id) {
L
LiYuRio 已提交
544
                distributed::BarrierOptions opts;
545
                opts.device_id = device_id;
L
LiYuRio 已提交
546 547
                return self.Barrier(opts);
              },
548
              py::arg("device_id") = -1,
L
LiYuRio 已提交
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 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
              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,
639
                 py::handle py_in_tensor,
L
LiYuRio 已提交
640 641 642
                 int nranks,
                 int rank_id) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
643
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
L
LiYuRio 已提交
644
                    out_tensor.impl());
645 646 647 648 649 650 651 652
                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 已提交
653 654
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
655 656
                return self.AllGather(
                    out_dense, in_dense, offset, send_numel, /*sync_op*/ true);
L
LiYuRio 已提交
657 658
              },
              py::arg("out"),
659
              py::arg("in"),
L
LiYuRio 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
              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,
688 689
                 const std::vector<int64_t> in_sizes,
                 const std::vector<int64_t> out_sizes) {
L
LiYuRio 已提交
690
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
691
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
L
LiYuRio 已提交
692
                    out_tensor.impl());
693 694 695 696 697 698 699 700 701
                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 已提交
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 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
              },
              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"),
750
              py::call_guard<py::gil_scoped_release>());
751

752 753 754 755
  auto ProcessGroupStream =
      py::class_<distributed::ProcessGroupStream,
                 std::shared_ptr<distributed::ProcessGroupStream>>(
          *m, "ProcessGroupStream", ProcessGroup)
756
          .def(
L
LiYuRio 已提交
757
              "all_gather_on_calc_stream",
758
              [](distributed::ProcessGroupStream &self,
759 760
                 py::handle py_out_tensor_list,
                 py::handle py_in_tensor) {
761 762 763
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
                Tensor concat_out_tensor = paddle::concat(out_tensor_list, 0);
764
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
765
                    concat_out_tensor.impl());
766 767 768 769 770 771
                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;
772

773
                const auto &dev_ctx =
774
                    self.GetDeviceContext(in_tensor.place(), true);
775 776
                auto task = self.AllGather(out_dense,
                                           in_dense,
777 778
                                           /*offset*/ 0,
                                           /*numel*/ -1,
779 780
                                           /*sync_op*/ true,
                                           /*use_calc_stream*/ true);
781
                SplitTensor(dev_ctx, *out_dense, &out_tensor_list);
782 783 784
                return task;
              },
              py::arg("out"),
785
              py::arg("in"),
786 787 788
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
789
              "all_gather_into_tensor_on_calc_stream",
790
              [](distributed::ProcessGroupStream &self,
791 792
                 py::handle py_out_tensor,
                 py::handle py_in_tensor) {
793
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
794
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
795
                    out_tensor.impl());
796
                auto *out_dense = p_out_tensor.get();
797

798 799 800 801 802 803 804
                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,
805 806
                                      /*offset*/ 0,
                                      /*numel*/ -1,
807 808 809 810
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
              },
              py::arg("out"),
811
              py::arg("in"),
812 813
              py::call_guard<py::gil_scoped_release>())

814 815 816 817
          .def(
              "all_gather_partial_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
818
                 py::handle py_in_tensor,
819 820 821
                 int nranks,
                 int rank_id) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
822
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
823
                    out_tensor.impl());
824 825 826 827 828 829 830 831
                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();
832 833
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
834 835 836 837 838 839 840

                return self.AllGather(out_dense,
                                      in_dense,
                                      offset,
                                      send_numel,
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
841 842
              },
              py::arg("out"),
843
              py::arg("in"),
844 845 846 847
              py::arg("num"),
              py::arg("id"),
              py::call_guard<py::gil_scoped_release>())

848
          .def(
L
LiYuRio 已提交
849
              "all_reduce_on_calc_stream",
850 851 852 853
              [](distributed::ProcessGroupStream &self,
                 py::handle py_tensor,
                 distributed::ReduceOp op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
854
                auto p_dense =
855
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
856 857 858 859 860
                auto in_dense = *p_dense;
                auto *out_dense = p_dense.get();
                distributed::AllreduceOptions opts{op};
                return self.AllReduce(out_dense,
                                      in_dense,
861 862 863 864 865
                                      opts,
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
              },
              py::arg("tensor"),
L
LiYuRio 已提交
866
              py::arg("op") = distributed::ReduceOp::SUM,
867 868
              py::call_guard<py::gil_scoped_release>())

869
          .def(
L
LiYuRio 已提交
870
              "all_to_all_on_calc_stream",
871
              [](distributed::ProcessGroupStream &self,
872 873
                 py::handle py_out_tensor_list,
                 py::handle py_in_tensor_list) {
874 875 876
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
                Tensor concat_out_tensor = paddle::concat(out_tensor_list, 0);
877
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
878
                    concat_out_tensor.impl());
879 880 881 882 883 884 885 886
                auto *out_dense = p_out_tensor.get();

                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
                Tensor concat_in_tensor = paddle::concat(in_tensor_list, 0);
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    concat_in_tensor.impl());
                auto in_dense = *p_in_tensor;
887

888
                // in_tensor_list should not be empty
889
                const auto &dev_ctx = self.GetDeviceContext(
890
                    in_tensor_list.back().place(), /*use_calc_stream*/ true);
891 892 893 894 895 896 897 898 899
                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);
                SplitTensor(dev_ctx, *out_dense, &out_tensor_list);
900 901 902
                return task;
              },
              py::arg("out"),
903
              py::arg("in"),
904 905 906
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
907
              "all_to_all_tensor_on_calc_stream",
908
              [](distributed::ProcessGroupStream &self,
909 910
                 py::handle py_out_tensor,
                 py::handle py_in_tensor) {
911
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
912
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
913
                    out_tensor.impl());
914
                auto *out_dense = p_out_tensor.get();
915

916 917 918 919 920 921 922 923 924 925 926 927 928
                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);
929 930
              },
              py::arg("out"),
931
              py::arg("in"),
932 933 934
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
935
              "all_to_all_single_on_calc_stream",
936 937
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
938 939 940
                 py::handle py_in_tensor,
                 const std::vector<int64_t> &out_sizes,
                 const std::vector<int64_t> &in_sizes) {
941
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
942
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
943
                    out_tensor.impl());
944
                auto *out_dense = p_out_tensor.get();
945

946 947 948 949 950 951 952 953 954 955 956
                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);
957 958
              },
              py::arg("out"),
959
              py::arg("in"),
960
              py::arg("out_sizes"),
961
              py::arg("in_sizes"),
962 963 964 965 966 967 968 969
              py::call_guard<py::gil_scoped_release>())

          .def(
              "broadcast_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_tensor,
                 int src) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
970
                auto p_dense =
971
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
972 973 974 975 976
                auto *out_dense = p_dense.get();
                auto in_dense = *p_dense;
                distributed::BroadcastOptions opts{src};
                return self.Broadcast(out_dense,
                                      in_dense,
977 978 979 980 981 982 983 984 985 986 987
                                      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",
              [](distributed::ProcessGroupStream &self,
988
                 py::handle py_tensor,
989 990
                 int dst,
                 distributed::ReduceOp op) {
991 992 993 994 995
                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;
996
                distributed::ReduceOptions opts{op, dst};
997 998
                return self.Reduce(out_dense,
                                   in_dense,
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
                                   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",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
1012
                 py::handle py_in_tensor_list,
1013
                 distributed::ReduceOp op) {
1014 1015 1016 1017 1018
                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();

1019 1020 1021
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
                Tensor concat_in_tensor = paddle::concat(in_tensor_list, 0);
1022
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1023
                    concat_in_tensor.impl());
1024
                auto in_dense = *p_in_tensor;
1025 1026

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

          .def(
              "reduce_scatter_tensor_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
1042
                 py::handle py_in_tensor,
1043 1044
                 distributed::ReduceOp op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
1045
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1046
                    out_tensor.impl());
1047 1048 1049 1050 1051 1052
                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;
1053 1054

                distributed::ReduceScatterOptions opts{op};
1055 1056
                return self.ReduceScatter(out_dense,
                                          in_dense,
1057 1058 1059
                                          opts,
                                          /*sync_op*/ true,
                                          /*use_calc_stream*/ true);
1060 1061
              },
              py::arg("out"),
1062
              py::arg("in"),
1063 1064 1065 1066 1067 1068 1069
              py::arg("op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
1070
                 py::handle py_in_tensor_list,
1071
                 int src) {
1072 1073 1074 1075 1076
                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();

1077 1078 1079
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
                Tensor concat_in_tensor = paddle::concat(in_tensor_list, 0);
1080
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1081
                    concat_in_tensor.impl());
1082
                auto in_dense = *p_in_tensor;
1083 1084

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

          .def(
              "scatter_tensor_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
1100
                 py::handle py_in_tensor,
1101 1102
                 int src) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
1103
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1104
                    out_tensor.impl());
1105 1106 1107 1108 1109 1110
                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;
1111 1112

                distributed::ScatterOptions opts{src};
1113 1114
                return self.Scatter(out_dense,
                                    in_dense,
1115 1116 1117 1118 1119
                                    opts,
                                    /*sync_op*/ true,
                                    /*use_calc_stream*/ true);
              },
              py::arg("out"),
1120
              py::arg("in"),
1121
              py::arg("src"),
1122 1123
              py::call_guard<py::gil_scoped_release>())

1124 1125 1126 1127 1128 1129
          .def(
              "send_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_tensor,
                 int dst) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1130
                auto p_dense =
1131
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1132
                auto *out_dense = p_dense.get();
1133
                // numel == -1 indicates sending the whole tensor
1134
                return self.Send(out_dense,
1135
                                 dst,
1136 1137
                                 /*offset*/ 0,
                                 /*numel*/ -1,
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
                                 /*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",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_tensor,
                 int dst_rank,
                 int nranks,
                 int rank_id) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1153
                auto p_dense =
1154
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1155 1156
                auto *out_dense = p_dense.get();

1157
                int64_t numel = p_dense->numel();
1158 1159
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
1160 1161 1162 1163 1164 1165 1166

                return self.Send(out_dense,
                                 dst_rank,
                                 offset,
                                 send_numel,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("num"),
              py::arg("id"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "recv_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_tensor,
                 int src) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1180
                auto p_dense =
1181
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1182
                auto *in_dense = p_dense.get();
1183
                // numel == -1 indicates receiving the whole tensor
1184
                return self.Recv(in_dense,
1185
                                 src,
1186 1187
                                 /*offset*/ 0,
                                 /*numel*/ -1,
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
                                 /*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",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_tensor,
                 int src_rank,
                 int nranks,
                 int rank_id) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1203
                auto p_dense =
1204
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1205 1206
                auto *out_dense = p_dense.get();

1207
                int64_t numel = p_dense->numel();
1208 1209
                int64_t recv_numel = numel / nranks;
                int64_t offset = recv_numel * rank_id;
1210 1211 1212 1213 1214 1215 1216

                return self.Recv(out_dense,
                                 src_rank,
                                 offset,
                                 recv_numel,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
1217 1218 1219 1220 1221
              },
              py::arg("tensor"),
              py::arg("src"),
              py::arg("num"),
              py::arg("id"),
1222 1223
              py::call_guard<py::gil_scoped_release>());

1224
#if defined(PADDLE_WITH_RCCL) || defined(PADDLE_WITH_NCCL)
1225 1226 1227
  auto processGroupNCCL =
      py::class_<distributed::ProcessGroupNCCL,
                 std::shared_ptr<distributed::ProcessGroupNCCL>>(
1228
          *m, "ProcessGroupNCCL", ProcessGroupStream)
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
          .def(py::init<const std::shared_ptr<distributed::Store> &,
                        int,
                        int,
                        int>(),
               py::arg("store"),
               py::arg("rank"),
               py::arg("world_size"),
               py::arg("group_id") = 0,
               py::call_guard<py::gil_scoped_release>());

  processGroupNCCL.def_static(
      "group_start", []() { distributed::ProcessGroupNCCL::GroupStart(); });
  processGroupNCCL.def_static(
      "group_end", []() { distributed::ProcessGroupNCCL::GroupEnd(); });

1244
#endif
1245

W
wuhuachaocoding 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
#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

1265 1266 1267 1268 1269
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
  py::class_<distributed::ProcessGroupCustom,
             std::shared_ptr<distributed::ProcessGroupCustom>>(
      *m, "ProcessGroupCustom", ProcessGroup)
      .def(py::init<const std::shared_ptr<distributed::Store> &,
1270
                    const std::string &,
1271 1272 1273 1274
                    int,
                    int,
                    int>(),
           py::arg("store"),
1275
           py::arg("device_type"),
1276 1277 1278 1279 1280
           py::arg("rank"),
           py::arg("world_size"),
           py::arg("group_id") = 0,
           py::call_guard<py::gil_scoped_release>());

1281 1282
#endif

J
james 已提交
1283 1284 1285 1286
#if defined(PADDLE_WITH_XPU_BKCL)
  auto processGroupBKCL =
      py::class_<distributed::ProcessGroupBKCL,
                 std::shared_ptr<distributed::ProcessGroupBKCL>>(
1287
          *m, "ProcessGroupBKCL", ProcessGroupStream)
J
james 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
          .def(py::init<const std::shared_ptr<distributed::Store> &,
                        int,
                        int,
                        int>(),
               py::arg("store"),
               py::arg("rank"),
               py::arg("world_size"),
               py::arg("group_id") = 0,
               py::call_guard<py::gil_scoped_release>());
#endif

1299 1300 1301
  py::class_<distributed::ProcessGroup::Task,
             std::shared_ptr<distributed::ProcessGroup::Task>>(*m, "task")
      .def("is_completed", &distributed::ProcessGroup::Task::IsCompleted)
1302
      .def("is_sync", &distributed::ProcessGroup::Task::IsSync)
1303 1304
      .def("wait",
           &distributed::ProcessGroup::Task::Wait,
1305 1306
           py::arg("timeout") = kWaitTimeout,
           py::call_guard<py::gil_scoped_release>())
1307 1308
      .def("synchronize",
           &distributed::ProcessGroup::Task::Synchronize,
1309 1310
           py::call_guard<py::gil_scoped_release>());

1311 1312 1313
#if defined(PADDLE_WITH_GLOO)
  py::class_<ProcessGroupGloo, std::shared_ptr<ProcessGroupGloo>>(
      *m, "ProcessGroupGloo", ProcessGroup)
1314 1315 1316 1317
      .def(py::init<const std::shared_ptr<paddle::distributed::Store> &,
                    int,
                    int,
                    int,
1318
                    std::shared_ptr<GlooOptions> &>(),
1319
           py::call_guard<py::gil_scoped_release>())
1320
      .def(py::init([](const std::shared_ptr<paddle::distributed::Store> &store,
1321 1322 1323
                       int rank,
                       int world_size,
                       int gid) {
1324 1325 1326 1327 1328 1329 1330 1331
             auto opts = GlooOptions::create();
             char *ifname = getenv(GLOO_SOCKET_IFNAME_ENV.c_str());
             if (ifname && strlen(ifname) > 1) {
               opts->device = ProcessGroupGloo::createDeviceForInterface(
                   std::string(ifname));
             } else {
               opts->device = ProcessGroupGloo::createDefaultDevice();
             }
1332
             return std::make_shared<ProcessGroupGloo>(
1333
                 store, rank, world_size, gid, opts);
1334
           }),
1335 1336 1337 1338
           py::arg("store"),
           py::arg("rank"),
           py::arg("world_size"),
           py::arg("group_id") = 0,
1339
           py::call_guard<py::gil_scoped_release>())
1340 1341 1342 1343
      .def_static("create_default_device",
                  &ProcessGroupGloo::createDefaultDevice);
#endif

1344 1345
  m->def(
      "eager_assign_group_by_size",
1346 1347
      [](py::handle py_tensors,
         std::vector<bool> is_sparse_gradient,
1348 1349 1350 1351 1352 1353
         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);
      },
1354 1355
      py::arg("tensors"),
      py::arg("is_sparse_gradient"),
1356 1357 1358
      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>());
1359 1360

  py::class_<distributed::EagerReducer,
1361 1362
             std::shared_ptr<distributed::EagerReducer>>(
      *m, "EagerReducer", R"DOC()DOC")
1363
      .def(py::init(&CreateEagerReducer))
1364 1365
      .def(
          "prepare_for_backward",
1366
          [](distributed::EagerReducer &self, py::handle py_tensors) {
1367
            auto params = CastPyArg2VectorOfTensor(py_tensors.ptr(), 0);
1368
            self.PrepareForBackward(params);
1369
          },
1370 1371
          py::arg("tensors"),
          py::call_guard<py::gil_scoped_release>());
1372 1373 1374 1375
}

}  // end namespace pybind
}  // namespace paddle