ps_client.h 13.6 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
// 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 <future>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
23

24
#include "paddle/fluid/distributed/common/cost_timer.h"
25 26 27 28
#include "paddle/fluid/distributed/ps/service/env.h"
#include "paddle/fluid/distributed/ps/service/sendrecv.pb.h"
#include "paddle/fluid/distributed/ps/table/accessor.h"
#include "paddle/fluid/distributed/ps/table/graph/graph_node.h"
29
#include "paddle/fluid/distributed/the_one_ps.pb.h"
30
#include "paddle/fluid/platform/timer.h"
T
tangwei12 已提交
31 32 33 34

namespace paddle {
namespace distributed {

T
tangwei12 已提交
35 36 37
using paddle::distributed::PsRequestMessage;
using paddle::distributed::PsResponseMessage;

T
tangwei12 已提交
38 39 40
typedef std::function<void(void *)> PSClientCallBack;
class PSClientClosure : public google::protobuf::Closure {
 public:
41
  explicit PSClientClosure(PSClientCallBack callback) : _callback(callback) {}
T
tangwei12 已提交
42 43 44 45 46 47 48
  virtual ~PSClientClosure() {}
  virtual void set_promise_value(int value) {
    for (auto &promise : _promises) {
      promise->set_value(value);
    }
  }

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

53 54 55 56
  void add_timer(std::shared_ptr<CostTimer> &timer) {  // NOLINT
    _timers.push_back(timer);
  }

T
tangwei12 已提交
57 58
 protected:
  PSClientCallBack _callback;
59
  std::vector<std::shared_ptr<CostTimer>> _timers;
T
tangwei12 已提交
60 61 62 63 64 65 66 67 68 69
  std::vector<std::shared_ptr<std::promise<int32_t>>> _promises;
};

class PSClient {
 public:
  PSClient() {}
  virtual ~PSClient() {}
  PSClient(PSClient &&) = delete;
  PSClient(const PSClient &) = delete;

Z
zhaocaibei123 已提交
70
  virtual int32_t Configure(
T
tangwei12 已提交
71 72 73
      const PSParameter &config,
      const std::map<uint64_t, std::vector<paddle::distributed::Region>>
          &regions,
Z
zhaocaibei123 已提交
74 75
      PSEnvironment &_env,  // NOLINT
      size_t client_id) final;
T
tangwei12 已提交
76

Z
zhaocaibei123 已提交
77 78 79
  virtual int32_t CreateClient2ClientConnection(int pserver_timeout_ms,
                                                int pserver_connect_timeout_ms,
                                                int max_retry) = 0;
T
tangwei12 已提交
80 81

  // 触发table数据退场
Z
zhaocaibei123 已提交
82
  virtual std::future<int32_t> Shrink(uint32_t table_id,
83
                                      const std::string threshold) = 0;
T
tangwei12 已提交
84 85

  // 全量table进行数据load
Z
zhaocaibei123 已提交
86
  virtual std::future<int32_t> Load(const std::string &epoch,
T
tangwei12 已提交
87 88
                                    const std::string &mode) = 0;
  // 指定table数据load
89 90
  virtual std::future<int32_t> Load(uint32_t table_id,
                                    const std::string &epoch,
T
tangwei12 已提交
91
                                    const std::string &mode) = 0;
Y
yaoxuefeng 已提交
92

T
tangwei12 已提交
93
  // 全量table数据save  value_accessor根据mode,可能有不同的save条件
Z
zhaocaibei123 已提交
94
  virtual std::future<int32_t> Save(const std::string &epoch,
T
tangwei12 已提交
95 96
                                    const std::string &mode) = 0;
  // 指定table数据save  value_accessor根据mode,可能有不同的save条件
97 98
  virtual std::future<int32_t> Save(uint32_t table_id,
                                    const std::string &epoch,
T
tangwei12 已提交
99 100
                                    const std::string &mode) = 0;

101
  // 清空table数据
Z
zhaocaibei123 已提交
102 103
  virtual std::future<int32_t> Clear() = 0;
  virtual std::future<int32_t> Clear(uint32_t table_id) = 0;
T
tangwei12 已提交
104 105 106 107 108 109 110 111

  // pull dense的参数部分,并分块填充到本地网络参数中
  // start和num用于拉取部分参数
  // future结束前keys和values缓冲区不能再次使用
  // client将values按照区块拆包后送交多个sender
  // sender聚集同一区块的请求,累计多个填充buffer
  // server将参数区块中配置的某一维提取返回
  // 返回数据解包后填充到累计的多个buffer中
112 113
  virtual std::future<int32_t> PullDense(Region *regions,
                                         size_t region_num,
Z
zhaocaibei123 已提交
114
                                         size_t table_id) = 0;  // 保留
Y
yaoxuefeng 已提交
115

T
tangwei12 已提交
116
  // firstly push dense param for parameter server
117
  // this is necessary because dense weight initialized in trainer on cold
T
tangwei12 已提交
118
  // start
Z
zhaocaibei123 已提交
119 120 121
  virtual std::future<int32_t> PushDenseParam(const Region *regions,
                                              size_t region_num,
                                              size_t table_id) = 0;
Y
yaoxuefeng 已提交
122

Z
zhaocaibei123 已提交
123 124 125
  virtual std::future<int32_t> PushDense(const Region *regions,
                                         size_t region_num,
                                         size_t table_id) = 0;
Y
yaoxuefeng 已提交
126

T
tangwei12 已提交
127 128 129 130 131
  // 使用keys进行pull请求,结果填充values
  // keys和values的个数均为num个,每个value占用select_size空间
  // future结束前keys和values缓冲区不能再次使用
  // 整合多个线程请求的keys,聚集并分散发送到server
  // 返回结果后,遍历buffer并对values赋值
132
  // is_training 用于区分请求是训练/预测,server端对于特征和准入会有不同的处理.
Z
zhaocaibei123 已提交
133
  virtual std::future<int32_t> PullSparse(float **select_values,
134 135 136 137
                                          size_t table_id,
                                          const uint64_t *keys,
                                          size_t num,
                                          bool is_training) = 0;
Z
zhaocaibei123 已提交
138 139 140

  virtual std::future<int32_t> PullSparseParam(float **select_values,
                                               size_t table_id,
141 142
                                               const uint64_t *keys,
                                               size_t num,
Z
zhaocaibei123 已提交
143
                                               bool is_training) {
Z
zhaocaibei123 已提交
144 145 146 147 148 149 150
    VLOG(0) << "Did not implement";
    std::promise<int32_t> promise;
    std::future<int> fut = promise.get_future();
    promise.set_value(-1);
    return fut;
  }

Z
zhaocaibei123 已提交
151 152 153 154
  virtual ::std::future<int32_t> PullSparsePtr(char **select_values,
                                               size_t table_id,
                                               const uint64_t *keys,
                                               size_t num) {
T
Thunderbrook 已提交
155 156 157 158 159 160 161
    VLOG(0) << "Did not implement";
    std::promise<int32_t> promise;
    std::future<int> fut = promise.get_future();
    promise.set_value(-1);
    return fut;
  }

Z
zhaocaibei123 已提交
162
  virtual std::future<int32_t> PrintTableStat(uint32_t table_id) = 0;
T
tangwei12 已提交
163 164

  // 确保所有积攒中的请求都发起发送
Z
zhaocaibei123 已提交
165
  virtual std::future<int32_t> Flush() = 0;
T
tangwei12 已提交
166
  // server优雅退出
Z
zhaocaibei123 已提交
167
  virtual std::future<int32_t> StopServer() = 0;
T
tangwei12 已提交
168 169

  // server profilera
Z
zhaocaibei123 已提交
170 171
  virtual std::future<int32_t> StartProfiler() = 0;
  virtual std::future<int32_t> StopProfiler() = 0;
T
tangwei12 已提交
172

Z
zhaocaibei123 已提交
173
  virtual std::future<int32_t> Barrier(size_t table_id,
T
tangwei12 已提交
174 175
                                       uint32_t barrier_type) = 0;

Z
zhaocaibei123 已提交
176 177 178 179
  virtual std::future<int32_t> PullGeoParam(size_t table_id,
                                            std::vector<float> *values,
                                            std::vector<uint64_t> *keys,
                                            int pserver_idx) = 0;
T
tangwei12 已提交
180

Z
zhaocaibei123 已提交
181 182 183
  virtual std::future<int32_t> PushGlobalStep(int table_id,
                                              int64_t *total_send_data,
                                              void *done) = 0;
184 185

  // recv table from server and save it in LodTensor
Z
zhaocaibei123 已提交
186 187
  virtual int32_t RecvAndSaveTable(const uint64_t table_id,
                                   const std::string &path) = 0;
188

Z
zhaocaibei123 已提交
189
  virtual void FinalizeWorker() = 0;
T
tangwei12 已提交
190
  // client to client, 消息发送
Z
zhaocaibei123 已提交
191 192 193
  virtual std::future<int32_t> SendClient2ClientMsg(int msg_type,
                                                    int to_client_id,
                                                    const std::string &msg) {
T
Thunderbrook 已提交
194
    VLOG(0) << "Did not implement";
T
tangwei12 已提交
195 196 197 198 199
    std::promise<int32_t> promise;
    std::future<int> fut = promise.get_future();
    promise.set_value(-1);
    return fut;
  }
S
seemingwang 已提交
200

T
tangwei12 已提交
201 202 203
  // client2client消息处理,std::function<int32_t (int, int, const std::string&)
  // -> ret (msg_type, from_client_id, msg)
  typedef std::function<int32_t(int, int, const std::string &)> MsgHandlerFunc;
Z
zhaocaibei123 已提交
204 205
  virtual int RegisteClient2ClientMsgHandler(int msg_type,
                                             MsgHandlerFunc handler) {
T
tangwei12 已提交
206 207 208
    _msg_handler_map[msg_type] = handler;
    return 0;
  }
Z
zhaocaibei123 已提交
209

210 211
  virtual int HandleClient2ClientMsg(int msg_type,
                                     int from_client_id,
Z
zhaocaibei123 已提交
212
                                     const std::string &msg) {
T
tangwei12 已提交
213 214 215 216 217 218 219 220
    auto itr = _msg_handler_map.find(msg_type);
    if (itr == _msg_handler_map.end()) {
      LOG(WARNING) << "unknown client2client_msg type:" << msg_type;
      return -1;
    }
    return itr->second(msg_type, from_client_id, msg);
  }

Z
zhaocaibei123 已提交
221
  virtual ValueAccessor *GetTableAccessor(size_t table_id) {
T
tangwei12 已提交
222 223 224 225 226 227 228
    auto itr = _table_accessors.find(table_id);
    if (itr == _table_accessors.end()) {
      return NULL;
    }
    return itr->second.get();
  }

Z
zhaocaibei123 已提交
229
  virtual size_t GetServerNums() = 0;
T
tangwei12 已提交
230

Z
zhaocaibei123 已提交
231 232 233 234
  virtual std::future<int32_t> PushDenseRawGradient(int table_id,
                                                    float *total_send_data,
                                                    size_t total_send_data_size,
                                                    void *done) = 0;
T
tangwei12 已提交
235

Z
zhaocaibei123 已提交
236
  virtual std::future<int32_t> PushSparseRawGradient(
237 238 239 240 241
      size_t table_id,
      const uint64_t *keys,
      const float **update_values,
      size_t num,
      void *done) = 0;
T
tangwei12 已提交
242

Z
zhaocaibei123 已提交
243
  virtual std::future<int32_t> PushSparseRawGradientPartial(
244 245 246 247 248 249
      size_t table_id,
      const uint64_t *keys,
      const float **update_values,
      uint32_t num,
      void *done,
      int pserver_idx) = 0;
T
tangwei12 已提交
250

Z
zhaocaibei123 已提交
251 252 253
  virtual std::future<int32_t> PushSparseParam(size_t table_id,
                                               const uint64_t *keys,
                                               const float **update_values,
254 255 256 257
                                               size_t num,
                                               void *done) = 0;
  virtual std::future<int32_t> PushSparse(size_t table_id,
                                          const uint64_t *keys,
Z
zhaocaibei123 已提交
258 259
                                          const float **update_values,
                                          size_t num) = 0;
T
tangwei12 已提交
260

Z
zhaocaibei123 已提交
261 262
  // for save cache
  virtual std::future<int32_t> CacheShuffle(
263 264 265
      uint32_t table_id,
      const std::string &path,
      const std::string &mode,
Z
zhaocaibei123 已提交
266 267 268 269 270 271 272 273 274
      const std::string &cache_threshold) {
    VLOG(0) << "Did not implement";
    std::promise<int32_t> promise;
    std::future<int> fut = promise.get_future();
    promise.set_value(-1);
    return fut;
  }

  virtual std::future<int32_t> CacheShuffleMultiTable(
275 276 277
      std::vector<int> tables,
      const std::string &path,
      const std::string &mode,
Z
zhaocaibei123 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
      const std::string &cache_threshold) {
    VLOG(0) << "Did not implement";
    std::promise<int32_t> promise;
    std::future<int> fut = promise.get_future();
    promise.set_value(-1);
    return fut;
  }

  virtual std::future<int32_t> SaveCache(uint32_t table_id,
                                         const std::string &path,
                                         const std::string &mode) {
    VLOG(0) << "Did not implement";
    std::promise<int32_t> promise;
    std::future<int> fut = promise.get_future();
    promise.set_value(-1);
    return fut;
  }

Z
zhaocaibei123 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  virtual std::future<int32_t> GetCacheThreshold(
      uint32_t table_id,
      double &cache_threshold) {  // NOLINT
    VLOG(0) << "Did not implement";
    std::promise<int32_t> promise;
    std::future<int> fut = promise.get_future();
    promise.set_value(-1);
    return fut;
  }

  virtual std::future<int32_t> Revert() {
    VLOG(0) << "Did not implement";
    std::promise<int32_t> promise;
    std::future<int> fut = promise.get_future();
    promise.set_value(-1);
    return fut;
  }

  virtual std::future<int32_t> CheckSavePrePatchDone() {
Z
zhaocaibei123 已提交
315 316 317 318 319 320 321
    VLOG(0) << "Did not implement";
    std::promise<int32_t> promise;
    std::future<int> fut = promise.get_future();
    promise.set_value(-1);
    return fut;
  }

T
tangwei12 已提交
322
 protected:
Z
zhaocaibei123 已提交
323
  virtual int32_t Initialize() = 0;
T
tangwei12 已提交
324 325 326 327 328
  PSParameter _config;
  std::map<uint64_t, std::vector<paddle::distributed::Region>>
      _dense_pull_regions;
  std::unordered_map<uint32_t, std::shared_ptr<ValueAccessor>> _table_accessors;
  std::unordered_map<int32_t, MsgHandlerFunc>
329
      _msg_handler_map;  // 处理client2client消息
330 331 332 333

 public:
  size_t _client_id;
  PSEnvironment *_env;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
};

template <class T>
class AsyncRequestTask {
 public:
  AsyncRequestTask() : _promise(std::make_shared<std::promise<int32_t>>()) {}
  AsyncRequestTask(T &data, size_t table_id, std::shared_ptr<CostTimer> &timer)
      : _table_id(table_id),
        _timer(timer),
        _promise(std::make_shared<std::promise<int32_t>>()) {
    _data = std::move(data);
  }

  AsyncRequestTask(AsyncRequestTask &data)  // NOLINT
      : _table_id(data.table_id()),
        _timer(data.timer()),
        _promise(data.promise()) {
    _data = std::move(data.data());
  }

  ~AsyncRequestTask() {}

  inline T &data() { return _data; }
  inline size_t table_id() { return _table_id; }
  inline std::shared_ptr<CostTimer> &timer() { return _timer; }
  inline std::future<int32_t> get_future() { return _promise->get_future(); }
  inline std::shared_ptr<std::promise<int32_t>> &promise() { return _promise; }

 private:
  T _data;
  size_t _table_id;
  std::shared_ptr<CostTimer> _timer;
  std::shared_ptr<std::promise<int32_t>> _promise;
T
tangwei12 已提交
367
};
368

T
tangwei12 已提交
369
REGISTER_PSCORE_REGISTERER(PSClient);
T
tangwei12 已提交
370 371 372

class PSClientFactory {
 public:
Z
zhaocaibei123 已提交
373
  static PSClient *Create(const PSParameter &config);
T
tangwei12 已提交
374 375 376
};
}  // namespace distributed
}  // namespace paddle