distributed_py.cc 54.0 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"
W
Wen Sun 已提交
25
#include "paddle/fluid/distributed/collective/process_group_stream.h"
26
#include "paddle/fluid/distributed/collective/reducer.h"
W
Wen Sun 已提交
27
#include "paddle/fluid/distributed/collective/types.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)
W
Wen Sun 已提交
37
#include "paddle/fluid/distributed/collective/process_group_nccl.h"
38 39
#endif

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

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

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

J
james 已提交
53
#if defined(PADDLE_WITH_XPU_BKCL)
W
Wen Sun 已提交
54
#include "paddle/fluid/distributed/collective/process_group_bkcl.h"
J
james 已提交
55 56
#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
#if defined(PADDLE_WITH_GLOO)
using ProcessGroupGloo = paddle::distributed::ProcessGroupGloo;
using GlooStore = paddle::distributed::ProcessGroupGloo::GlooStore;
using GlooOptions = paddle::distributed::ProcessGroupGloo::GlooOptions;
#endif

88 89 90
static UNUSED void *use_ccl_comm_func =
    phi::detail::GetCCLComm(phi::CPUPlace());

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

113 114 115 116 117
  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);

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

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

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

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

                return self.Send(
197
                    out_dense, dst_rank, offset, send_numel, sync_op);
198 199 200 201 202
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("num"),
              py::arg("id"),
203
              py::arg("sync_op") = true,
204 205 206 207 208 209 210 211 212
              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);
213
                auto p_dense =
214
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
215
                auto *in_dense = p_dense.get();
W
Wen Sun 已提交
216
                return self.Recv(in_dense, src, sync_op);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
              },
              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);
232
                auto p_dense =
233
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
234 235
                auto *out_dense = p_dense.get();

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

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

250 251
          .def(
              "all_gather",
252 253
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor_list,
254
                 py::handle py_in_tensor,
255 256 257
                 bool sync_op) {
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
258
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
259
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
260
                    stack_out_tensor.impl());
261 262 263 264 265 266
                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;
267

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

          .def(
L
LiYuRio 已提交
280
              "all_gather_into_tensor",
281 282
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
283
                 py::handle py_in_tensor,
284 285
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
286
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
287
                    out_tensor.impl());
288 289 290 291 292 293
                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;
294

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

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

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

                // in_tensor_list should not be empty
323
                auto *dev_ctx =
324
                    self.GetDeviceContext(in_tensor_list.back().place());
325 326 327 328 329 330 331
                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);
332 333
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
                task->UpdateWaitChain(*dev_ctx);
334 335 336
                return task;
              },
              py::arg("out"),
337
              py::arg("in"),
338 339 340 341
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
342
              "all_to_all_tensor",
343 344
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
345
                 py::handle py_in_tensor,
346 347
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
348
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
349
                    out_tensor.impl());
350 351 352 353 354 355
                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;
356

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

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

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

          .def(
              "reduce",
              [](distributed::ProcessGroup &self,
401
                 py::handle py_tensor,
402 403 404
                 int dst,
                 distributed::ReduceOp op,
                 bool sync_op) {
405 406 407 408 409
                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;
410
                distributed::ReduceOptions opts{op, dst};
411
                return self.Reduce(out_dense, in_dense, opts, sync_op);
412 413 414 415 416 417 418 419 420 421 422
              },
              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,
423
                 py::handle py_in_tensor_list,
424 425
                 distributed::ReduceOp op,
                 bool sync_op) {
426 427 428 429 430
                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();

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

                distributed::ReduceScatterOptions opts{op};
439
                return self.ReduceScatter(out_dense, in_dense, opts, sync_op);
440 441
              },
              py::arg("out"),
442
              py::arg("in"),
443 444 445 446 447 448 449 450
              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,
451
                 py::handle py_in_tensor,
452 453 454
                 distributed::ReduceOp op,
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
455
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
456
                    out_tensor.impl());
457 458 459 460 461 462
                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;
463 464

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

          .def(
              "scatter",
              [](distributed::ProcessGroup &self,
                 py::handle py_out_tensor,
477
                 py::handle py_in_tensor_list,
478 479
                 int src,
                 bool sync_op) {
480 481 482 483 484
                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();

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

                distributed::ScatterOptions opts{src};
493
                return self.Scatter(out_dense, in_dense, opts, sync_op);
494 495
              },
              py::arg("out"),
496
              py::arg("in"),
497 498 499 500 501 502 503 504
              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,
505
                 py::handle py_in_tensor,
506 507 508
                 int src,
                 bool sync_op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
509
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
510
                    out_tensor.impl());
511 512 513 514 515 516
                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;
517 518

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

L
LiYuRio 已提交
527 528
          .def(
              "barrier",
529
              [](distributed::ProcessGroup &self, int8_t device_id) {
L
LiYuRio 已提交
530
                distributed::BarrierOptions opts;
531
                opts.device_id = device_id;
L
LiYuRio 已提交
532 533
                return self.Barrier(opts);
              },
534
              py::arg("device_id") = -1,
L
LiYuRio 已提交
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 623 624
              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,
625
                 py::handle py_in_tensor,
L
LiYuRio 已提交
626 627 628
                 int nranks,
                 int rank_id) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
629
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
L
LiYuRio 已提交
630
                    out_tensor.impl());
631 632 633 634 635 636 637 638
                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 已提交
639 640
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
641 642
                return self.AllGather(
                    out_dense, in_dense, offset, send_numel, /*sync_op*/ true);
L
LiYuRio 已提交
643 644
              },
              py::arg("out"),
645
              py::arg("in"),
L
LiYuRio 已提交
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 672 673
              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,
674 675
                 const std::vector<int64_t> in_sizes,
                 const std::vector<int64_t> out_sizes) {
L
LiYuRio 已提交
676
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
677
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
L
LiYuRio 已提交
678
                    out_tensor.impl());
679 680 681 682 683 684 685 686 687
                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 已提交
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 734 735
              },
              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"),
736
              py::call_guard<py::gil_scoped_release>());
737

738 739 740 741
  auto ProcessGroupStream =
      py::class_<distributed::ProcessGroupStream,
                 std::shared_ptr<distributed::ProcessGroupStream>>(
          *m, "ProcessGroupStream", ProcessGroup)
742
          .def(
L
LiYuRio 已提交
743
              "all_gather_on_calc_stream",
744
              [](distributed::ProcessGroupStream &self,
745 746
                 py::handle py_out_tensor_list,
                 py::handle py_in_tensor) {
747 748
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
749
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
750
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
751
                    stack_out_tensor.impl());
752 753 754 755 756 757
                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;
758

759
                auto *dev_ctx = self.GetDeviceContext(in_tensor.place(), true);
760 761
                auto task = self.AllGather(out_dense,
                                           in_dense,
762 763
                                           /*sync_op*/ true,
                                           /*use_calc_stream*/ true);
764
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
765 766 767
                return task;
              },
              py::arg("out"),
768
              py::arg("in"),
769 770 771
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
772
              "all_gather_into_tensor_on_calc_stream",
773
              [](distributed::ProcessGroupStream &self,
774 775
                 py::handle py_out_tensor,
                 py::handle py_in_tensor) {
776
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
777
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
778
                    out_tensor.impl());
779
                auto *out_dense = p_out_tensor.get();
780

781 782 783 784 785 786 787
                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,
788 789 790 791
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
              },
              py::arg("out"),
792
              py::arg("in"),
793 794
              py::call_guard<py::gil_scoped_release>())

795 796 797 798
          .def(
              "all_gather_partial_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
799
                 py::handle py_in_tensor,
800 801 802
                 int nranks,
                 int rank_id) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
803
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
804
                    out_tensor.impl());
805 806 807 808 809 810 811 812
                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();
813 814
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
815 816 817 818 819 820 821

                return self.AllGather(out_dense,
                                      in_dense,
                                      offset,
                                      send_numel,
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
822 823
              },
              py::arg("out"),
824
              py::arg("in"),
825 826 827 828
              py::arg("num"),
              py::arg("id"),
              py::call_guard<py::gil_scoped_release>())

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

850
          .def(
L
LiYuRio 已提交
851
              "all_to_all_on_calc_stream",
852
              [](distributed::ProcessGroupStream &self,
853 854
                 py::handle py_out_tensor_list,
                 py::handle py_in_tensor_list) {
855 856
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
857
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
858
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
859
                    stack_out_tensor.impl());
860 861 862 863
                auto *out_dense = p_out_tensor.get();

                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
864
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
865
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
866
                    stack_in_tensor.impl());
867
                auto in_dense = *p_in_tensor;
868

869
                // in_tensor_list should not be empty
870
                auto *dev_ctx = self.GetDeviceContext(
871
                    in_tensor_list.back().place(), /*use_calc_stream*/ true);
872 873 874 875 876 877 878 879
                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);
880
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
881 882 883
                return task;
              },
              py::arg("out"),
884
              py::arg("in"),
885 886 887
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
888
              "all_to_all_tensor_on_calc_stream",
889
              [](distributed::ProcessGroupStream &self,
890 891
                 py::handle py_out_tensor,
                 py::handle py_in_tensor) {
892
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
893
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
894
                    out_tensor.impl());
895
                auto *out_dense = p_out_tensor.get();
896

897 898 899 900 901 902 903 904 905 906 907 908 909
                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);
910 911
              },
              py::arg("out"),
912
              py::arg("in"),
913 914 915
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
916
              "all_to_all_single_on_calc_stream",
917 918
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
919 920 921
                 py::handle py_in_tensor,
                 const std::vector<int64_t> &out_sizes,
                 const std::vector<int64_t> &in_sizes) {
922
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
923
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
924
                    out_tensor.impl());
925
                auto *out_dense = p_out_tensor.get();
926

927 928 929 930 931 932 933 934 935 936 937
                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);
938 939
              },
              py::arg("out"),
940
              py::arg("in"),
941
              py::arg("out_sizes"),
942
              py::arg("in_sizes"),
943 944 945 946 947 948 949 950
              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);
951
                auto p_dense =
952
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
953 954 955 956 957
                auto *out_dense = p_dense.get();
                auto in_dense = *p_dense;
                distributed::BroadcastOptions opts{src};
                return self.Broadcast(out_dense,
                                      in_dense,
958 959 960 961 962 963 964 965 966 967 968
                                      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,
969
                 py::handle py_tensor,
970 971
                 int dst,
                 distributed::ReduceOp op) {
972 973 974 975 976
                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;
977
                distributed::ReduceOptions opts{op, dst};
978 979
                return self.Reduce(out_dense,
                                   in_dense,
980 981 982 983 984 985 986 987 988 989 990 991 992
                                   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,
993
                 py::handle py_in_tensor_list,
994
                 distributed::ReduceOp op) {
995 996 997 998 999
                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();

1000 1001
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
1002
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
1003
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1004
                    stack_in_tensor.impl());
1005
                auto in_dense = *p_in_tensor;
1006 1007

                distributed::ReduceScatterOptions opts{op};
1008 1009
                return self.ReduceScatter(out_dense,
                                          in_dense,
1010 1011 1012 1013 1014
                                          opts,
                                          /*sync_op*/ true,
                                          /*use_calc_stream*/ true);
              },
              py::arg("out"),
1015
              py::arg("in"),
1016 1017 1018 1019 1020 1021 1022
              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,
1023
                 py::handle py_in_tensor,
1024 1025
                 distributed::ReduceOp op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
1026
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1027
                    out_tensor.impl());
1028 1029 1030 1031 1032 1033
                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;
1034 1035

                distributed::ReduceScatterOptions opts{op};
1036 1037
                return self.ReduceScatter(out_dense,
                                          in_dense,
1038 1039 1040
                                          opts,
                                          /*sync_op*/ true,
                                          /*use_calc_stream*/ true);
1041 1042
              },
              py::arg("out"),
1043
              py::arg("in"),
1044 1045 1046 1047 1048 1049 1050
              py::arg("op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
1051
                 py::handle py_in_tensor_list,
1052
                 int src) {
1053 1054 1055 1056 1057
                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();

1058 1059
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
1060
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
1061
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1062
                    stack_in_tensor.impl());
1063
                auto in_dense = *p_in_tensor;
1064 1065

                distributed::ScatterOptions opts{src};
1066 1067
                return self.Scatter(out_dense,
                                    in_dense,
1068 1069 1070 1071 1072
                                    opts,
                                    /*sync_op*/ true,
                                    /*use_calc_stream*/ true);
              },
              py::arg("out"),
1073
              py::arg("in"),
1074 1075 1076 1077 1078 1079 1080
              py::arg("src"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter_tensor_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_out_tensor,
1081
                 py::handle py_in_tensor,
1082 1083
                 int src) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
1084
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1085
                    out_tensor.impl());
1086 1087 1088 1089 1090 1091
                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;
1092 1093

                distributed::ScatterOptions opts{src};
1094 1095
                return self.Scatter(out_dense,
                                    in_dense,
1096 1097 1098 1099 1100
                                    opts,
                                    /*sync_op*/ true,
                                    /*use_calc_stream*/ true);
              },
              py::arg("out"),
1101
              py::arg("in"),
1102
              py::arg("src"),
1103 1104
              py::call_guard<py::gil_scoped_release>())

1105 1106 1107 1108 1109 1110
          .def(
              "send_on_calc_stream",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_tensor,
                 int dst) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1111
                auto p_dense =
1112
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1113
                auto out_dense = *p_dense;
1114
                return self.Send(out_dense,
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
                                 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",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_tensor,
                 int dst_rank,
                 int nranks,
                 int rank_id) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1131
                auto p_dense =
1132
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1133
                auto out_dense = *p_dense;
1134

1135
                int64_t numel = p_dense->numel();
1136 1137
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
1138 1139 1140 1141 1142 1143 1144

                return self.Send(out_dense,
                                 dst_rank,
                                 offset,
                                 send_numel,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
              },
              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);
1158
                auto p_dense =
1159
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1160 1161
                auto *in_dense = p_dense.get();
                return self.Recv(in_dense,
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
                                 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",
              [](distributed::ProcessGroupStream &self,
                 py::handle py_tensor,
                 int src_rank,
                 int nranks,
                 int rank_id) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1178
                auto p_dense =
1179
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1180 1181
                auto *out_dense = p_dense.get();

1182
                int64_t numel = p_dense->numel();
1183 1184
                int64_t recv_numel = numel / nranks;
                int64_t offset = recv_numel * rank_id;
1185 1186 1187 1188 1189 1190 1191

                return self.Recv(out_dense,
                                 src_rank,
                                 offset,
                                 recv_numel,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
1192 1193 1194 1195 1196
              },
              py::arg("tensor"),
              py::arg("src"),
              py::arg("num"),
              py::arg("id"),
1197 1198
              py::call_guard<py::gil_scoped_release>());

1199
#if defined(PADDLE_WITH_RCCL) || defined(PADDLE_WITH_NCCL)
L
LiYuRio 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
  py::class_<distributed::ProcessGroupNCCL,
             std::shared_ptr<distributed::ProcessGroupNCCL>>(
      *m, "ProcessGroupNCCL", ProcessGroupStream)
      .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);
1212

1213
#endif
1214

W
wuhuachaocoding 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
#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

1234 1235 1236 1237
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
  py::class_<distributed::ProcessGroupCustom,
             std::shared_ptr<distributed::ProcessGroupCustom>>(
      *m, "ProcessGroupCustom", ProcessGroup)
L
LiYuRio 已提交
1238 1239 1240 1241 1242 1243 1244 1245
      .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>());
1246

1247 1248
#endif

J
james 已提交
1249 1250 1251 1252
#if defined(PADDLE_WITH_XPU_BKCL)
  auto processGroupBKCL =
      py::class_<distributed::ProcessGroupBKCL,
                 std::shared_ptr<distributed::ProcessGroupBKCL>>(
1253
          *m, "ProcessGroupBKCL", ProcessGroupStream)
L
LiYuRio 已提交
1254 1255 1256 1257 1258 1259 1260
          .def_static("create",
                      distributed::ProcessGroupBKCL::CreateProcessGroupBKCL,
                      py::arg("store"),
                      py::arg("rank"),
                      py::arg("world_size"),
                      py::arg("group_id") = 0,
                      py::call_guard<py::gil_scoped_release>());
J
james 已提交
1261 1262
#endif

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

1275 1276 1277
#if defined(PADDLE_WITH_GLOO)
  py::class_<ProcessGroupGloo, std::shared_ptr<ProcessGroupGloo>>(
      *m, "ProcessGroupGloo", ProcessGroup)
L
LiYuRio 已提交
1278 1279 1280 1281 1282 1283 1284
      .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>())
1285 1286 1287 1288
      .def_static("create_default_device",
                  &ProcessGroupGloo::createDefaultDevice);
#endif

1289 1290
  m->def(
      "eager_assign_group_by_size",
1291 1292
      [](py::handle py_tensors,
         std::vector<bool> is_sparse_gradient,
1293 1294 1295 1296 1297 1298
         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);
      },
1299 1300
      py::arg("tensors"),
      py::arg("is_sparse_gradient"),
1301 1302 1303
      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>());
1304 1305

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

}  // end namespace pybind
}  // namespace paddle