distributed_py.cc 55.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

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

#ifdef _XOPEN_SOURCE
#undef _XOPEN_SOURCE
#endif

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

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

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

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

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

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

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

57 58 59 60 61
namespace py = pybind11;

namespace paddle {
namespace pybind {

62
using Tensor = paddle::Tensor;
63

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

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

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

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

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

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

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

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

zhenhailiu's avatar
zhenhailiu 已提交
116 117 118 119
  py::class_<distributed::GatherOptions>(*m, "GatherOptions")
      .def(py::init<>())
      .def_readwrite("root_rank", &distributed::GatherOptions::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;
W
Wen Sun 已提交
174
                return self.Send(out_dense, dst, sync_op);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
              },
              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);
190
                auto p_dense =
191
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
192
                auto out_dense = *p_dense;
193

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                distributed::ScatterOptions opts{src};
520
                return self.Scatter(out_dense, in_dense, opts, sync_op);
521 522
              },
              py::arg("out"),
523
              py::arg("in"),
524 525 526
              py::arg("src"),
              py::arg("sync_op"),
              py::call_guard<py::gil_scoped_release>())
zhenhailiu's avatar
zhenhailiu 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540
          .def(
              "gather",
              [](distributed::ProcessGroup &self,
                 py::handle py_in_tensor,
                 py::handle py_gather_tensor_list,
                 int dst,
                 bool sync_op,
                 bool use_calc_stream) {
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_gather_tensor_list.ptr(), 0);
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
                    stack_out_tensor.impl());
                auto *out_dense = p_out_tensor.get();
541

zhenhailiu's avatar
zhenhailiu 已提交
542 543 544 545 546 547 548
                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;

                auto *dev_ctx =
                    self.GetDeviceContext(in_tensor.place(), use_calc_stream);
549
                distributed::GatherOptions gather_opts{dst};
zhenhailiu's avatar
zhenhailiu 已提交
550
                auto task = self.Gather(
551
                    out_dense, in_dense, gather_opts, sync_op, use_calc_stream);
zhenhailiu's avatar
zhenhailiu 已提交
552
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
553 554
                if (!use_calc_stream &&
                    dev_ctx->GetPlace() != platform::CPUPlace()) {
zhenhailiu's avatar
zhenhailiu 已提交
555 556 557 558 559 560 561 562 563
                  // calculate stream will wait comm stream
                  task->UpdateWaitChain(*dev_ctx);
                }
                return task;
              },
              py::arg("in"),
              py::arg("out"),
              py::arg("dst"),
              py::arg("sync_op"),
564
              py::arg("use_calc_stream") = false,
zhenhailiu's avatar
zhenhailiu 已提交
565
              py::call_guard<py::gil_scoped_release>())
L
LiYuRio 已提交
566 567
          .def(
              "barrier",
568
              [](distributed::ProcessGroup &self, int8_t device_id) {
L
LiYuRio 已提交
569
                distributed::BarrierOptions opts;
570
                opts.device_id = device_id;
L
LiYuRio 已提交
571 572
                return self.Barrier(opts);
              },
573
              py::arg("device_id") = -1,
L
LiYuRio 已提交
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 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
              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,
664
                 py::handle py_in_tensor,
L
LiYuRio 已提交
665 666 667
                 int nranks,
                 int rank_id) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
668
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
L
LiYuRio 已提交
669
                    out_tensor.impl());
670 671 672 673 674 675 676 677
                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 已提交
678 679
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
680 681
                return self.AllGather(
                    out_dense, in_dense, offset, send_numel, /*sync_op*/ true);
L
LiYuRio 已提交
682 683
              },
              py::arg("out"),
684
              py::arg("in"),
L
LiYuRio 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
              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,
713 714
                 const std::vector<int64_t> in_sizes,
                 const std::vector<int64_t> out_sizes) {
L
LiYuRio 已提交
715
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
716
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
L
LiYuRio 已提交
717
                    out_tensor.impl());
718 719 720 721 722 723 724 725 726
                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 已提交
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
              },
              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"),
775
              py::call_guard<py::gil_scoped_release>())
776

777
          .def(
L
LiYuRio 已提交
778
              "all_gather_on_calc_stream",
779
              [](distributed::ProcessGroup &self,
780 781
                 py::handle py_out_tensor_list,
                 py::handle py_in_tensor) {
782 783
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
784
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
785
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
786
                    stack_out_tensor.impl());
787 788 789 790 791 792
                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;
793

794
                auto *dev_ctx = self.GetDeviceContext(in_tensor.place(), true);
795 796
                auto task = self.AllGather(out_dense,
                                           in_dense,
797 798
                                           /*sync_op*/ true,
                                           /*use_calc_stream*/ true);
799
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
800 801 802
                return task;
              },
              py::arg("out"),
803
              py::arg("in"),
804 805 806
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
807
              "all_gather_into_tensor_on_calc_stream",
808
              [](distributed::ProcessGroup &self,
809 810
                 py::handle py_out_tensor,
                 py::handle py_in_tensor) {
811
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
812
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
813
                    out_tensor.impl());
814
                auto *out_dense = p_out_tensor.get();
815

816 817 818 819 820 821 822
                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,
823 824 825 826
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
              },
              py::arg("out"),
827
              py::arg("in"),
828 829
              py::call_guard<py::gil_scoped_release>())

830 831
          .def(
              "all_gather_partial_on_calc_stream",
832
              [](distributed::ProcessGroup &self,
833
                 py::handle py_out_tensor,
834
                 py::handle py_in_tensor,
835 836 837
                 int nranks,
                 int rank_id) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
838
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
839
                    out_tensor.impl());
840 841 842 843 844 845 846 847
                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();
848 849
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
850 851 852 853 854 855 856

                return self.AllGather(out_dense,
                                      in_dense,
                                      offset,
                                      send_numel,
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
857 858
              },
              py::arg("out"),
859
              py::arg("in"),
860 861 862 863
              py::arg("num"),
              py::arg("id"),
              py::call_guard<py::gil_scoped_release>())

864
          .def(
L
LiYuRio 已提交
865
              "all_reduce_on_calc_stream",
866
              [](distributed::ProcessGroup &self,
867 868 869
                 py::handle py_tensor,
                 distributed::ReduceOp op) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
870
                auto p_dense =
871
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
872 873 874 875 876
                auto in_dense = *p_dense;
                auto *out_dense = p_dense.get();
                distributed::AllreduceOptions opts{op};
                return self.AllReduce(out_dense,
                                      in_dense,
877 878 879 880 881
                                      opts,
                                      /*sync_op*/ true,
                                      /*use_calc_stream*/ true);
              },
              py::arg("tensor"),
L
LiYuRio 已提交
882
              py::arg("op") = distributed::ReduceOp::SUM,
883 884
              py::call_guard<py::gil_scoped_release>())

885
          .def(
L
LiYuRio 已提交
886
              "all_to_all_on_calc_stream",
887
              [](distributed::ProcessGroup &self,
888 889
                 py::handle py_out_tensor_list,
                 py::handle py_in_tensor_list) {
890 891
                auto out_tensor_list =
                    CastPyArg2VectorOfTensor(py_out_tensor_list.ptr(), 0);
892
                Tensor stack_out_tensor = paddle::stack(out_tensor_list, 0);
893
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
894
                    stack_out_tensor.impl());
895 896 897 898
                auto *out_dense = p_out_tensor.get();

                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
899
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
900
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
901
                    stack_in_tensor.impl());
902
                auto in_dense = *p_in_tensor;
903

904
                // in_tensor_list should not be empty
905
                auto *dev_ctx = self.GetDeviceContext(
906
                    in_tensor_list.back().place(), /*use_calc_stream*/ true);
907 908 909 910 911 912 913 914
                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);
915
                SplitTensor(*dev_ctx, *out_dense, &out_tensor_list);
916 917 918
                return task;
              },
              py::arg("out"),
919
              py::arg("in"),
920 921 922
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
923
              "all_to_all_tensor_on_calc_stream",
924
              [](distributed::ProcessGroup &self,
925 926
                 py::handle py_out_tensor,
                 py::handle py_in_tensor) {
927
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
928
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
929
                    out_tensor.impl());
930
                auto *out_dense = p_out_tensor.get();
931

932 933 934 935 936 937 938 939 940 941 942 943 944
                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);
945 946
              },
              py::arg("out"),
947
              py::arg("in"),
948 949 950
              py::call_guard<py::gil_scoped_release>())

          .def(
L
LiYuRio 已提交
951
              "all_to_all_single_on_calc_stream",
952
              [](distributed::ProcessGroup &self,
953
                 py::handle py_out_tensor,
954 955 956
                 py::handle py_in_tensor,
                 const std::vector<int64_t> &out_sizes,
                 const std::vector<int64_t> &in_sizes) {
957
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
958
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
959
                    out_tensor.impl());
960
                auto *out_dense = p_out_tensor.get();
961

962 963 964 965 966 967 968 969 970 971 972
                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);
973 974
              },
              py::arg("out"),
975
              py::arg("in"),
976
              py::arg("out_sizes"),
977
              py::arg("in_sizes"),
978 979 980 981
              py::call_guard<py::gil_scoped_release>())

          .def(
              "broadcast_on_calc_stream",
982
              [](distributed::ProcessGroup &self,
983 984 985
                 py::handle py_tensor,
                 int src) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
986
                auto p_dense =
987
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
988 989 990 991 992
                auto *out_dense = p_dense.get();
                auto in_dense = *p_dense;
                distributed::BroadcastOptions opts{src};
                return self.Broadcast(out_dense,
                                      in_dense,
993 994 995 996 997 998 999 1000 1001 1002
                                      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",
1003
              [](distributed::ProcessGroup &self,
1004
                 py::handle py_tensor,
1005 1006
                 int dst,
                 distributed::ReduceOp op) {
1007 1008 1009 1010 1011
                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;
1012
                distributed::ReduceOptions opts{op, dst};
1013 1014
                return self.Reduce(out_dense,
                                   in_dense,
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
                                   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",
1026
              [](distributed::ProcessGroup &self,
1027
                 py::handle py_out_tensor,
1028
                 py::handle py_in_tensor_list,
1029
                 distributed::ReduceOp op) {
1030 1031 1032 1033 1034
                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();

1035 1036
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
1037
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
1038
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1039
                    stack_in_tensor.impl());
1040
                auto in_dense = *p_in_tensor;
1041 1042

                distributed::ReduceScatterOptions opts{op};
1043 1044
                return self.ReduceScatter(out_dense,
                                          in_dense,
1045 1046 1047 1048 1049
                                          opts,
                                          /*sync_op*/ true,
                                          /*use_calc_stream*/ true);
              },
              py::arg("out"),
1050
              py::arg("in"),
1051 1052 1053 1054 1055
              py::arg("op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "reduce_scatter_tensor_on_calc_stream",
1056
              [](distributed::ProcessGroup &self,
1057
                 py::handle py_out_tensor,
1058
                 py::handle py_in_tensor,
1059 1060
                 distributed::ReduceOp op) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
1061
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1062
                    out_tensor.impl());
1063 1064 1065 1066 1067 1068
                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;
1069 1070

                distributed::ReduceScatterOptions opts{op};
1071 1072
                return self.ReduceScatter(out_dense,
                                          in_dense,
1073 1074 1075
                                          opts,
                                          /*sync_op*/ true,
                                          /*use_calc_stream*/ true);
1076 1077
              },
              py::arg("out"),
1078
              py::arg("in"),
1079 1080 1081 1082 1083
              py::arg("op"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter_on_calc_stream",
1084
              [](distributed::ProcessGroup &self,
1085
                 py::handle py_out_tensor,
1086
                 py::handle py_in_tensor_list,
1087
                 int src) {
1088 1089 1090 1091 1092
                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();

1093 1094
                auto in_tensor_list =
                    CastPyArg2VectorOfTensor(py_in_tensor_list.ptr(), 0);
1095
                Tensor stack_in_tensor = paddle::stack(in_tensor_list, 0);
1096
                auto p_in_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1097
                    stack_in_tensor.impl());
1098
                auto in_dense = *p_in_tensor;
1099 1100

                distributed::ScatterOptions opts{src};
1101 1102
                return self.Scatter(out_dense,
                                    in_dense,
1103 1104 1105 1106 1107
                                    opts,
                                    /*sync_op*/ true,
                                    /*use_calc_stream*/ true);
              },
              py::arg("out"),
1108
              py::arg("in"),
1109 1110 1111 1112 1113
              py::arg("src"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "scatter_tensor_on_calc_stream",
1114
              [](distributed::ProcessGroup &self,
1115
                 py::handle py_out_tensor,
1116
                 py::handle py_in_tensor,
1117 1118
                 int src) {
                auto out_tensor = CastPyArg2Tensor(py_out_tensor.ptr(), 0);
1119
                auto p_out_tensor = std::dynamic_pointer_cast<phi::DenseTensor>(
1120
                    out_tensor.impl());
1121 1122 1123 1124 1125 1126
                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;
1127 1128

                distributed::ScatterOptions opts{src};
1129 1130
                return self.Scatter(out_dense,
                                    in_dense,
1131 1132 1133 1134 1135
                                    opts,
                                    /*sync_op*/ true,
                                    /*use_calc_stream*/ true);
              },
              py::arg("out"),
1136
              py::arg("in"),
1137
              py::arg("src"),
1138 1139
              py::call_guard<py::gil_scoped_release>())

1140 1141
          .def(
              "send_on_calc_stream",
1142
              [](distributed::ProcessGroup &self,
1143 1144 1145
                 py::handle py_tensor,
                 int dst) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1146
                auto p_dense =
1147
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1148
                auto out_dense = *p_dense;
1149
                return self.Send(out_dense,
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
                                 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",
1160
              [](distributed::ProcessGroup &self,
1161 1162 1163 1164 1165
                 py::handle py_tensor,
                 int dst_rank,
                 int nranks,
                 int rank_id) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1166
                auto p_dense =
1167
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1168
                auto out_dense = *p_dense;
1169

1170
                int64_t numel = p_dense->numel();
1171 1172
                int64_t send_numel = numel / nranks;
                int64_t offset = send_numel * rank_id;
1173 1174 1175 1176 1177 1178 1179

                return self.Send(out_dense,
                                 dst_rank,
                                 offset,
                                 send_numel,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
1180 1181 1182 1183 1184 1185 1186 1187 1188
              },
              py::arg("tensor"),
              py::arg("dst"),
              py::arg("num"),
              py::arg("id"),
              py::call_guard<py::gil_scoped_release>())

          .def(
              "recv_on_calc_stream",
1189
              [](distributed::ProcessGroup &self,
1190 1191 1192
                 py::handle py_tensor,
                 int src) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1193
                auto p_dense =
1194
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1195 1196
                auto *in_dense = p_dense.get();
                return self.Recv(in_dense,
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
                                 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",
1207
              [](distributed::ProcessGroup &self,
1208 1209 1210 1211 1212
                 py::handle py_tensor,
                 int src_rank,
                 int nranks,
                 int rank_id) {
                auto tensor = CastPyArg2Tensor(py_tensor.ptr(), 0);
1213
                auto p_dense =
1214
                    std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl());
1215 1216
                auto *out_dense = p_dense.get();

1217
                int64_t numel = p_dense->numel();
1218 1219
                int64_t recv_numel = numel / nranks;
                int64_t offset = recv_numel * rank_id;
1220 1221 1222 1223 1224 1225 1226

                return self.Recv(out_dense,
                                 src_rank,
                                 offset,
                                 recv_numel,
                                 /*sync_op*/ true,
                                 /*use_calc_stream*/ true);
1227 1228 1229 1230 1231
              },
              py::arg("tensor"),
              py::arg("src"),
              py::arg("num"),
              py::arg("id"),
1232 1233
              py::call_guard<py::gil_scoped_release>());

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

1248
#endif
1249

W
wuhuachaocoding 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
#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

1269 1270 1271 1272
#if defined(PADDLE_WITH_CUSTOM_DEVICE)
  py::class_<distributed::ProcessGroupCustom,
             std::shared_ptr<distributed::ProcessGroupCustom>>(
      *m, "ProcessGroupCustom", ProcessGroup)
L
LiYuRio 已提交
1273 1274 1275 1276 1277 1278 1279 1280
      .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>());
1281

1282 1283
#endif

J
james 已提交
1284 1285 1286 1287
#if defined(PADDLE_WITH_XPU_BKCL)
  auto processGroupBKCL =
      py::class_<distributed::ProcessGroupBKCL,
                 std::shared_ptr<distributed::ProcessGroupBKCL>>(
1288
          *m, "ProcessGroupBKCL", ProcessGroup)
L
LiYuRio 已提交
1289 1290 1291 1292 1293 1294
          .def_static("create",
                      distributed::ProcessGroupBKCL::CreateProcessGroupBKCL,
                      py::arg("store"),
                      py::arg("rank"),
                      py::arg("world_size"),
                      py::arg("group_id") = 0,
1295 1296 1297
                      py::call_guard<py::gil_scoped_release>())
          .def_static("group_start", distributed::ProcessGroupBKCL::GroupStart)
          .def_static("group_end", distributed::ProcessGroupBKCL::GroupEnd);
J
james 已提交
1298 1299
#endif

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

1312 1313 1314
#if defined(PADDLE_WITH_GLOO)
  py::class_<ProcessGroupGloo, std::shared_ptr<ProcessGroupGloo>>(
      *m, "ProcessGroupGloo", ProcessGroup)
L
LiYuRio 已提交
1315 1316 1317 1318 1319 1320 1321
      .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>())
1322 1323 1324 1325
      .def_static("create_default_device",
                  &ProcessGroupGloo::createDefaultDevice);
#endif

1326 1327
  m->def(
      "eager_assign_group_by_size",
1328 1329
      [](py::handle py_tensors,
         std::vector<bool> is_sparse_gradient,
1330 1331 1332 1333 1334 1335
         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);
      },
1336 1337
      py::arg("tensors"),
      py::arg("is_sparse_gradient"),
1338 1339 1340
      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>());
1341 1342

  py::class_<distributed::EagerReducer,
1343 1344
             std::shared_ptr<distributed::EagerReducer>>(
      *m, "EagerReducer", R"DOC()DOC")
1345
      .def(py::init(&CreateEagerReducer))
1346 1347
      .def(
          "prepare_for_backward",
1348
          [](distributed::EagerReducer &self, py::handle py_tensors) {
1349
            auto params = CastPyArg2VectorOfTensor(py_tensors.ptr(), 0);
1350
            self.PrepareForBackward(params);
1351
          },
1352 1353
          py::arg("tensors"),
          py::call_guard<py::gil_scoped_release>());
1354 1355 1356 1357 1358 1359 1360

  py::class_<distributed::ProcessGroupIdMap,
             std::shared_ptr<distributed::ProcessGroupIdMap>>(
      *m, "ProcessGroupIdMap")
      .def_static("destroy",
                  distributed::ProcessGroupIdMap::DestroyProcessGroup,
                  py::call_guard<py::gil_scoped_release>());
1361 1362 1363 1364
}

}  // end namespace pybind
}  // namespace paddle