table.h 5.3 KB
Newer Older
T
tangwei12 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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 <assert.h>
18

T
tangwei12 已提交
19 20 21 22 23
#include <atomic>
#include <future>  // NOLINT
#include <memory>
#include <string>
#include <utility>
24

Z
zhaocaibei123 已提交
25
#include "paddle/fluid/distributed/common/afs_warpper.h"
26 27 28
#include "paddle/fluid/distributed/ps/table/accessor.h"
#include "paddle/fluid/distributed/ps/table/depends/sparse_utils.h"
#include "paddle/fluid/distributed/ps/table/graph/graph_node.h"
Z
zhaocaibei123 已提交
29
#include "paddle/fluid/framework/channel.h"
30 31 32 33
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/place.h"
T
tangwei12 已提交
34 35 36 37
#include "paddle/fluid/string/string_helper.h"

namespace paddle {
namespace distributed {
Y
yaoxuefeng 已提交
38 39 40

enum ValueType { Sparse = 0, Dense = 1 };

41 42
struct TablePullContext {
  const uint64_t *keys = nullptr;
43
  PullSparseValue pull_value;
44 45 46 47
  float *values = nullptr;
  char **ptr_values = nullptr;
  std::vector<uint64_t> *geo_pull_keys = nullptr;  // for GEO
  std::vector<float> *geo_pull_values = nullptr;   // for GEO
Y
yaoxuefeng 已提交
48 49 50
};

struct TablePushContext {
51 52 53 54 55
  const uint64_t *keys = nullptr;
  const float *values = nullptr;
  const float **ptr_values = nullptr;
  const int64_t *push_steps = nullptr;  // for global step
  bool is_param = false;  // true: push param, false: push gradient
Y
yaoxuefeng 已提交
56 57 58 59
};

struct TableContext {
  ValueType value_type;
60
  TablePullContext pull_context;
Y
yaoxuefeng 已提交
61 62
  TablePushContext push_context;
  size_t num;
63
  bool use_ptr = false;
64
  uint32_t trainer_id;  // for GEO and global step
Y
yaoxuefeng 已提交
65 66
};

T
tangwei12 已提交
67 68 69 70
class Table {
 public:
  Table() {}
  virtual ~Table() {}
Z
zhaocaibei123 已提交
71
  virtual int32_t Initialize(const TableParameter &config,
72
                             const FsClientParameter &fs_config);
T
tangwei12 已提交
73

Z
zhaocaibei123 已提交
74 75
  virtual int32_t Pull(TableContext &context) = 0;  // NOLINT
  virtual int32_t Push(TableContext &context) = 0;  // NOLINT
T
tangwei12 已提交
76 77

  // only for barrier
Z
zhaocaibei123 已提交
78
  virtual int32_t Barrier(const uint32_t trainer_id,
T
tangwei12 已提交
79 80 81 82 83
                          const std::string barrier_type) {
    return 0;
  }

  // only for barrier table
Z
zhaocaibei123 已提交
84
  virtual int32_t SetTableMap(
T
tangwei12 已提交
85 86 87 88
      std::unordered_map<uint32_t, std::shared_ptr<Table>> *table_map) {
    return 0;
  }

89
  // only for tensor table
Z
zhaocaibei123 已提交
90
  virtual int32_t SetProgramEnv(
91 92
      framework::Scope *scope,
      platform::Place place,
93 94 95 96
      const std::vector<framework::ProgramDesc> *sub_program) {
    return 0;
  }

Z
zhaocaibei123 已提交
97
  virtual int32_t SetGlobalLR(float *lr) {
98 99 100 101
    _global_lr = lr;
    return 0;
  }

Z
zhaocaibei123 已提交
102
  virtual int32_t Pour() { return 0; }
T
tangwei12 已提交
103

Z
zhaocaibei123 已提交
104 105 106
  virtual void Clear() = 0;
  virtual int32_t Flush() = 0;
  virtual int32_t Shrink(const std::string &param) = 0;
T
tangwei12 已提交
107

Z
zhaocaibei123 已提交
108
  // 指定加载路径
Z
zhaocaibei123 已提交
109
  virtual int32_t Load(const std::string &path,
T
tangwei12 已提交
110
                       const std::string &converter) = 0;
Z
zhaocaibei123 已提交
111
  // 指定保存路径
Z
zhaocaibei123 已提交
112
  virtual int32_t Save(const std::string &path,
T
tangwei12 已提交
113
                       const std::string &converter) = 0;
Z
zhaocaibei123 已提交
114 115
  // for cache
  virtual int32_t SaveCache(
116 117
      const std::string &path,
      const std::string &param,
Z
zhaocaibei123 已提交
118 119 120 121 122 123
      paddle::framework::Channel<std::pair<uint64_t, std::string>>
          &shuffled_channel) {
    return 0;
  }

  virtual int64_t CacheShuffle(
124 125 126 127
      const std::string &path,
      const std::string &param,
      double cache_threshold,
      std::function<std::future<int32_t>(
Z
zhaocaibei123 已提交
128 129
          int msg_type, int to_pserver_id, std::string &msg)>  // NOLINT
          send_msg_func,
Z
zhaocaibei123 已提交
130 131 132 133 134 135 136
      paddle::framework::Channel<std::pair<uint64_t, std::string>>
          &shuffled_channel,
      const std::vector<Table *> &table_ptrs) {
    return 0;
  }

  virtual double GetCacheThreshold() { return 0.0; }
T
tangwei12 已提交
137

Z
zhaocaibei123 已提交
138
  virtual int32_t SetShard(size_t shard_idx, size_t shard_num) {
T
tangwei12 已提交
139 140
    _shard_idx = shard_idx;
    _shard_num = shard_num;
Z
zhaocaibei123 已提交
141
    return InitializeShard();
T
tangwei12 已提交
142 143
  }

Z
zhaocaibei123 已提交
144
  inline std::shared_ptr<ValueAccessor> ValueAccesor() {
T
tangwei12 已提交
145 146 147
    return _value_accesor;
  }

Z
zhaocaibei123 已提交
148 149
  virtual void *GetShard(size_t shard_idx) = 0;
  virtual std::pair<int64_t, int64_t> PrintTableStat() { return {0, 0}; }
T
tangwei12 已提交
150

Z
zhaocaibei123 已提交
151 152 153 154
  // for patch model
  virtual void Revert() {}
  virtual void CheckSavePrePatchDone() {}

T
tangwei12 已提交
155
 protected:
Z
zhaocaibei123 已提交
156 157 158 159
  virtual int32_t Initialize() = 0;
  virtual int32_t InitializeAccessor();
  virtual int32_t InitializeShard() = 0;
  virtual std::string TableDir(const std::string &model_dir) {
160 161
    return paddle::string::format_string(
        "%s/%03d/", model_dir.c_str(), _config.table_id());
T
tangwei12 已提交
162 163 164 165 166
  }

  size_t _shard_idx;  // table 分片编号
  size_t _shard_num;  // table 分片总数
  TableParameter _config;
167
  float *_global_lr = nullptr;
T
tangwei12 已提交
168
  std::shared_ptr<ValueAccessor> _value_accesor;
Z
zhaocaibei123 已提交
169
  AfsClient _afs_client;
T
tangwei12 已提交
170
};
T
tangwei12 已提交
171
REGISTER_PSCORE_REGISTERER(Table);
T
tangwei12 已提交
172 173 174

class TableManager {
 public:
Z
zhaocaibei123 已提交
175
  static TableManager &Instance() {
T
tangwei12 已提交
176 177 178
    static TableManager manager;
    return manager;
  }
Z
zhaocaibei123 已提交
179
  int32_t Initialize();
T
tangwei12 已提交
180 181 182 183 184

 private:
  TableManager() {}
  ~TableManager() {}
};
S
seemingwang 已提交
185

T
tangwei12 已提交
186 187
}  // namespace distributed
}  // namespace paddle