communicator_py.cc 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2019 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 "paddle/fluid/pybind/communicator_py.h"

#include <Python.h>
18
#include <map>
19
#include <memory>
20 21
#include <string>
#include <vector>
22 23 24 25
#include "paddle/fluid/framework/program_desc.h"
#include "pybind11/pybind11.h"

#include "paddle/fluid/operators/distributed/communicator.h"
26
#include "paddle/fluid/operators/distributed/large_scale_kv.h"
27
#include "paddle/fluid/operators/distributed/ps/service/communicator/communicator_common.h"
28 29 30 31

namespace py = pybind11;

using paddle::framework::ProgramDesc;
32
using paddle::framework::Scope;
T
tangwei12 已提交
33
using paddle::operators::distributed::AsyncCommunicator;
34
using paddle::operators::distributed::Communicator;
35
using paddle::operators::distributed::GeoCommunicator;
36
using paddle::operators::distributed::HalfAsyncCommunicator;
T
tangwei12 已提交
37
using paddle::operators::distributed::SyncCommunicator;
38

39 40 41 42 43
using paddle::operators::distributed::CommContext;
using paddle::operators::distributed::RpcCtxMap;

using paddle::operators::distributed::LargeScaleKV;

44 45 46
namespace paddle {
namespace pybind {

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
void BindCommunicatorContext(py::module* m) {
  py::class_<CommContext>(*m, "CommContext")
      .def(
          py::init<const std::string&, const std::vector<std::string>&,
                   const std::vector<std::string>&, const std::vector<int64_t>&,
                   const std::vector<std::string>&, int, bool, bool, bool>())
      .def("var_name", [](const CommContext& self) { return self.var_name; })
      .def("trainer_id",
           [](const CommContext& self) { return self.trainer_id; })
      .def("split_varnames",
           [](const CommContext& self) { return self.splited_varnames; })
      .def("split_endpoints",
           [](const CommContext& self) { return self.epmap; })
      .def("sections",
           [](const CommContext& self) { return self.height_sections; })
      .def("aggregate", [](const CommContext& self) { return self.merge_add; })
      .def("is_sparse", [](const CommContext& self) { return self.is_sparse; })
      .def("is_distributed",
           [](const CommContext& self) { return self.is_distributed; })
      .def("origin_varnames",
           [](const CommContext& self) { return self.origin_varnames; })
      .def("__str__", [](const CommContext& self) { return self.print(); });
}

71 72 73 74
void BindCommunicator(py::module* m) {
  // Communicator is already used by nccl, change to DistCommunicator
  py::class_<Communicator, std::shared_ptr<Communicator>>(*m,
                                                          "DistCommunicator")
75 76
      .def(py::init([](const std::string& mode, const RpcCtxMap& send_ctx,
                       const RpcCtxMap& recv_ctx, Scope* param_scope,
77 78
                       std::map<std::string, std::string>& envs) {
        if (mode == "HALF_ASYNC") {
79
          Communicator::InitInstance<HalfAsyncCommunicator>(send_ctx, recv_ctx,
80 81
                                                            param_scope, envs);
        } else if (mode == "ASYNC") {
82 83
          Communicator::InitInstance<AsyncCommunicator>(send_ctx, recv_ctx,
                                                        param_scope, envs);
T
tangwei12 已提交
84
        } else if (mode == "SYNC") {
85 86 87 88 89
          Communicator::InitInstance<SyncCommunicator>(send_ctx, recv_ctx,
                                                       param_scope, envs);
        } else if (mode == "GEO") {
          Communicator::InitInstance<GeoCommunicator>(send_ctx, recv_ctx,
                                                      param_scope, envs);
90 91 92 93
        } else {
          PADDLE_THROW(platform::errors::InvalidArgument(
              "unsuported communicator MODE"));
        }
94

95 96
        return Communicator::GetInstantcePtr();
      }))
97
      .def("stop", &Communicator::Stop)
98
      .def("start", &Communicator::Start)
99 100 101 102 103 104 105 106 107 108 109 110 111
      .def("is_running", &Communicator::IsRunning)
      .def("recv", &Communicator::RecvNoBarrier);
}

void BindLargeScaleKV(py::module* m) {
  py::class_<LargeScaleKV, std::shared_ptr<LargeScaleKV>>(*m, "LargeScaleKV")
      .def(py::init([]() { return LargeScaleKV::GetInstantcePtr(); }))
      .def("load",
           [](LargeScaleKV& self, const std::string& table_name,
              const std::string& dir) {
             auto* sparse_variable = self.Get(table_name);
             sparse_variable->Load(dir);
           })
112 113 114 115 116 117 118
      .def("save",
           [](LargeScaleKV& self, const std::string& table_name,
              const std::string& dir) {
             auto* sparse_variable = self.Get(table_name);
             sparse_variable->Save(dir);
           })
      .def("size", [](LargeScaleKV& self, const std::string& table_name) {
119
        auto* sparse_variable = self.Get(table_name);
120
        return sparse_variable->Size();
121
      });
122 123 124
}
}  // namespace pybind
}  // namespace paddle