heter_client.h 8.5 KB
Newer Older
T
tangwei12 已提交
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) 2020 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. */

#pragma once
#include <atomic>
#include <ctime>
#include <map>
#include <memory>
#include <random>
#include <string>
#include <unordered_map>
#include <vector>
24

T
tangwei12 已提交
25 26 27
#include "brpc/channel.h"
#include "brpc/controller.h"
#include "brpc/server.h"
28 29 30
#include "paddle/fluid/distributed/ps/service/brpc_ps_client.h"
#include "paddle/fluid/distributed/ps/service/brpc_utils.h"
#include "paddle/fluid/distributed/ps/service/sendrecv.pb.h"
T
tangwei12 已提交
31 32 33 34
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/framework/variable_helper.h"
#include "paddle/fluid/platform/macros.h"  // for DISABLE_COPY_AND_ASSIGN
35
#include "paddle/fluid/string/split.h"
T
tangwei12 已提交
36

37 38 39 40 41
namespace paddle {
namespace framework {
class Scope;
}  // namespace framework
}  // namespace paddle
42

T
tangwei12 已提交
43 44
namespace paddle {
namespace distributed {
45
DECLARE_int32(pserver_timeout_ms);
T
tangwei12 已提交
46 47
using MultiVarMsg = ::paddle::distributed::MultiVariableMessage;
using VarMsg = ::paddle::distributed::VariableMessage;
T
tangwei12 已提交
48 49 50 51 52

typedef std::function<void(void*)> HeterRpcCallbackFunc;

class OnHeterRpcDone : public google::protobuf::Closure {
 public:
T
tangwei12 已提交
53
  explicit OnHeterRpcDone(HeterRpcCallbackFunc func) : handler_(func) {}
T
tangwei12 已提交
54
  virtual ~OnHeterRpcDone() {}
55 56 57 58
  void Run() { handler_(this); }

  void add_promise(std::shared_ptr<std::promise<int32_t>>& promise) {  // NOLINT
    _promises.push_back(promise);
T
tangwei12 已提交
59 60
  }

61 62 63 64 65 66 67
  void set_promise_value(int value) {
    for (auto& promise : _promises) {
      promise->set_value(value);
    }
  }
  int CheckResponse() { return 0; }
  std::vector<std::shared_ptr<std::promise<int32_t>>> _promises;
T
tangwei12 已提交
68
  HeterRpcCallbackFunc handler_;
69 70

  MultiVariableMessage request;
T
tangwei12 已提交
71
  MultiVariableMessage response;
72 73 74

  PsResponseMessage ps_response;

T
tangwei12 已提交
75
  brpc::Controller cntl;
76 77 78 79 80
  // PsRequestMessage *request(size_t i) { return &_requests[i]; }
  // PsResponseMessage *response(size_t i) { return &_responses[i]; }
  // std::vector<PsRequestMessage> _requests;
  // std::vector<PsResponseMessage> _responses;
  // std::vector<std::shared_ptr<brpc::Controller>> _cntls;
T
tangwei12 已提交
81 82 83 84 85 86
};

class HeterClient {
 public:
  virtual ~HeterClient() {}

87 88 89 90 91 92 93 94 95
  void InitClientChannels(bool need_encrypt,
                          const std::vector<std::string>& node_list,
                          int32_t peer_role) {
    brpc::ChannelOptions options;
    options.protocol = "baidu_std";
    options.connection_type = "single";
    options.timeout_ms = FLAGS_pserver_timeout_ms;
    std::vector<std::shared_ptr<brpc::Channel>>* client_channels = nullptr;
    if (peer_role == PEER_ROLE_IS_SWITCH) {
96 97 98 99 100 101 102
#ifdef PADDLE_WITH_ARM_BRPC
      if (need_encrypt) {
        options.mutable_ssl_options();
      }
      options.connection_type = "";
      VLOG(4) << "ssl enabled in arm";
#else
103 104 105
      if (need_encrypt) {
        options.mutable_ssl_options();
      }
106
#endif
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      client_channels = &peer_switch_channels_;
    } else if (peer_role == PEER_ROLE_IS_WORKER) {
      client_channels = &peer_worker_channels_;
    } else {
      LOG(ERROR) << "init switch client failed, peer_role not valid";
    }
    (*client_channels).resize(node_list.size());
    for (size_t i = 0; i < node_list.size(); ++i) {
      (*client_channels)[i].reset(new brpc::Channel());
      if ((*client_channels)[i]->Init(node_list[i].c_str(), "", &options) !=
          0) {
        VLOG(0) << "client channel init failed! try again";
        auto ip_port = paddle::string::Split(node_list[i], ':');
        std::string ip = ip_port[0];
        int port = std::stoi(ip_port[1]);
        std::string int_ip_port = GetIntTypeEndpoint(ip, port);
        if ((*client_channels)[i]->Init(int_ip_port.c_str(), "", &options) !=
            0) {
          LOG(ERROR) << "client channel init failed! peer ip_port = "
                     << int_ip_port;
        }
      }
    }
    VLOG(4) << "InitClientChannels success";
T
tangwei12 已提交
131 132 133 134
  }

  void CreateClient2XpuConnection();

135
  void SendAndRecvAsync(const platform::DeviceContext& ctx,
T
tangwei12 已提交
136 137 138
                        const framework::Scope& scope,
                        const std::string& message_name,
                        const std::vector<std::string>& send_var_name,
139 140
                        const std::vector<std::string>& recv_var_name,
                        const std::string& mode = "forward");
T
tangwei12 已提交
141

142 143 144 145
  int Send(int group_id,
           const std::vector<std::string>& var_names,
           const std::vector<int64_t>& vars_len,
           void* data_ptr,
146
           int64_t data_size);
147

148 149
  int Send(const platform::DeviceContext& ctx,
           const framework::Scope& scope,
150 151 152
           const std::string& message_name,
           const std::vector<std::string>& send_var_names);

153 154 155 156
  int Recv(int group_id,
           const std::vector<std::string>& var_names,
           void* data_ptr,
           int64_t data_size);
157 158 159 160 161 162

  int Recv(const platform::DeviceContext& ctx,
           framework::Scope& recv_scope,  // NOLINT
           const std::string& message_name,
           const std::vector<std::string>& recv_var_names);

T
tangwei12 已提交
163 164
  // HeterClient singleton
  static std::shared_ptr<HeterClient> GetInstance(
165 166
      const std::vector<std::string>& endpoints,
      const std::vector<std::string>& previous_endpoints,
167
      const int& trainer_id) {
T
tangwei12 已提交
168
    if (NULL == s_instance_) {
169
      s_instance_.reset(new HeterClient());
170 171
      s_instance_->SetXpuList(endpoints);
      s_instance_->SetPreviousXpuList(previous_endpoints);
T
tangwei12 已提交
172 173 174 175 176 177
      s_instance_->SetTrainerID(trainer_id);
      s_instance_->CreateClient2XpuConnection();
    }
    return s_instance_;
  }

178
  // switch client singleton
Z
ziyoujiyi 已提交
179
  static std::shared_ptr<HeterClient> GetSwitchInstance(
180
      const std::vector<std::string>& peer_endpoints, int32_t peer_role) {
181 182 183 184 185 186
    std::unique_lock<std::mutex> lock(mtx_);
    if (peer_endpoints.empty()) {
      VLOG(4) << "init switch client failed, null peer_endpoints";
    }
    VLOG(4) << "peer role is: " << peer_role
            << ", addr is: " << peer_endpoints[0];
Z
ziyoujiyi 已提交
187
    if (switch_s_instance_ == nullptr) {
188 189 190
      switch_s_instance_.reset(new HeterClient());
      switch_s_instance_->SetPeerSwitchList(peer_endpoints);
      switch_s_instance_->InitClientChannels(false, peer_endpoints, peer_role);
191 192 193
    }
    return switch_s_instance_;
  }
T
tangwei12 已提交
194

195 196 197
  void SetPeerSwitchList(const std::vector<std::string>& peer_endpoints) {
    peer_switch_list_ = peer_endpoints;
  }
T
tangwei12 已提交
198

199 200 201
  void SetPeerWorkerList(const std::vector<std::string>& worker_endpoints) {
    peer_worker_list_ = worker_endpoints;
  }
T
tangwei12 已提交
202

203
  void Stop();
T
tangwei12 已提交
204

205 206
  std::future<int32_t> SendCmd(uint32_t table_id,
                               int cmd_id,
T
tangwei12 已提交
207 208 209
                               const std::vector<std::string>& params);

  std::future<int32_t> StartProfiler();
T
tangwei12 已提交
210

T
tangwei12 已提交
211 212 213 214 215 216 217
  std::future<int32_t> StopProfiler();
  std::future<int32_t> StopHeterWorker();

  std::vector<std::string>& GetXpuList() { return xpu_list_; }

  void SetXpuList(const std::vector<std::string>& xpu_list) {
    xpu_list_ = xpu_list;
T
tangwei12 已提交
218
  }
T
tangwei12 已提交
219

220 221 222 223
  void SetPreviousXpuList(const std::vector<std::string>& xpu_list) {
    previous_xpu_list_ = xpu_list;
  }

T
tangwei12 已提交
224 225
  void SetTrainerID(const int& trainer_id) { trainer_id_ = trainer_id; }

226 227 228 229 230 231 232 233 234 235 236 237
 public:
  std::vector<std::string> send_switch_list_;
  std::vector<std::string> recv_switch_list_;

  std::vector<std::string> peer_switch_list_;
  std::vector<std::string> peer_worker_list_;
  std::vector<std::shared_ptr<brpc::Channel>> send_switch_channels_;
  std::vector<std::shared_ptr<brpc::Channel>> recv_switch_channels_;

  std::vector<std::shared_ptr<brpc::Channel>> peer_switch_channels_;
  std::vector<std::shared_ptr<brpc::Channel>> peer_worker_channels_;

T
tangwei12 已提交
238
 private:
239 240 241 242
  HeterClient() {}
  HeterClient& operator=(const HeterClient&);
  HeterClient(const HeterClient&);

T
tangwei12 已提交
243
  static std::shared_ptr<HeterClient> s_instance_;
Z
ziyoujiyi 已提交
244 245
  static std::mutex mtx_;
  static std::shared_ptr<HeterClient> switch_s_instance_;
T
tangwei12 已提交
246
  std::vector<std::shared_ptr<brpc::Channel>> xpu_channels_;
247
  std::vector<std::shared_ptr<brpc::Channel>> previous_xpu_channels_;
T
tangwei12 已提交
248

249
  // DISABLE_COPY_AND_ASSIGN(HeterClient);
T
tangwei12 已提交
250
  std::vector<std::string> xpu_list_;
251
  std::vector<std::string> previous_xpu_list_;
T
tangwei12 已提交
252 253 254 255 256 257

  int trainer_id_;
};

}  // end namespace distributed
}  // end namespace paddle