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

17 18
#include <algorithm>
#include <condition_variable>  // NOLINT
T
tangwei12 已提交
19
#include <memory>
20 21
#include <mutex>  // NOLINT
#include <set>
T
tangwei12 已提交
22 23 24
#include <string>
#include <unordered_map>
#include <vector>
25

26
#include "paddle/fluid/distributed/common/utils.h"
27
#include "paddle/fluid/distributed/ps/table/table.h"
T
tangwei12 已提交
28 29 30 31
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/platform/device_context.h"

32 33 34 35 36 37 38 39
namespace paddle {
namespace framework {
class Executor;
class Scope;
struct ExecutorPrepareContext;
}  // namespace framework
}  // namespace paddle

T
tangwei12 已提交
40 41 42
namespace paddle {
namespace distributed {

43 44 45
#define LEARNING_RATE_DECAY_COUNTER "@LR_DECAY_COUNTER@"
#define STEP_COUNTER "@PS_STEP_COUNTER@"

T
tangwei12 已提交
46 47
class TensorTable : public Table {
 public:
48
  TensorTable() {}
T
tangwei12 已提交
49 50
  virtual ~TensorTable() {}

51
  int32_t pull_dense(float *values, size_t num) override { return 0; }
T
tangwei12 已提交
52

53
  int32_t push_dense(const float *values, size_t num) override { return 0; }
T
tangwei12 已提交
54

55 56
  int32_t pull_sparse(float *values,
                      const PullSparseValue &pull_value) override {
T
tangwei12 已提交
57
    return 0;
58 59 60 61 62
  }
  int32_t push_sparse(const uint64_t *keys, const float *values,
                      size_t num) override {
    return 0;
  }
63
  int32_t shrink(const std::string &param) override { return 0; }
64 65

  virtual void *get_shard(size_t shard_idx) { return 0; }
T
tangwei12 已提交
66

67
  virtual int32_t initialize_shard() { return 0; };
T
tangwei12 已提交
68

69 70 71
  virtual int32_t flush() { return 0; };

  virtual int32_t load(const std::string &path, const std::string &param) {
T
tangwei12 已提交
72
    return 0;
73 74 75 76 77 78
  }
  virtual int32_t save(const std::string &path, const std::string &param) {
    return 0;
  }

  virtual void clear(){};
T
tangwei12 已提交
79

80 81 82 83
  virtual int32_t initialize() override { return 0; };

  virtual int32_t push_dense(const int64_t *values,
                             const int32_t trainer_id) override {
T
tangwei12 已提交
84 85 86
    return 0;
  };

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  virtual int32_t set_program_env(
      framework::Scope *scope, platform::Place place,
      const std::vector<framework::ProgramDesc> *sub_program) override;

 protected:
  framework::Executor *executor_;
  framework::Scope *scope_;
  platform::Place place_ = platform::CPUPlace();
  const std::vector<framework::ProgramDesc> *sub_program_;
  paddle::distributed::TensorAccessorParameter program_config_;
  std::shared_ptr<framework::ExecutorPrepareContext> exec_context_ = nullptr;
};

class DenseTensorTable : public TensorTable {
 public:
  DenseTensorTable() {}
  virtual ~DenseTensorTable() {}

105 106
  int32_t pull_sparse(float *values,
                      const PullSparseValue &pull_value) override {
T
tangwei12 已提交
107 108
    return 0;
  }
109 110 111 112
  int32_t push_sparse(const uint64_t *keys, const float *values,
                      size_t num) override {
    return 0;
  }
113
  int32_t shrink(const std::string &param) override { return 0; }
T
tangwei12 已提交
114

115
  virtual void *get_shard(size_t shard_idx) { return 0; }
T
tangwei12 已提交
116

117
  virtual int32_t initialize_shard() { return 0; }
T
tangwei12 已提交
118 119 120

  virtual int32_t flush() { return 0; }

121 122 123 124
  virtual void clear() {}

  // Todo: Support program Load & Save
  virtual int32_t load(const std::string &path, const std::string &param) {
T
tangwei12 已提交
125 126
    return 0;
  }
127
  virtual int32_t save(const std::string &path, const std::string &param) {
T
tangwei12 已提交
128 129 130
    return 0;
  }

131 132 133 134 135 136 137 138
  // Todo: Support pull dense
  int32_t pull_dense(float *values, size_t num) override { return 0; }

  /*----------------------------------------------------------------------*/

  virtual int32_t initialize() override { return 0; }

  int32_t push_dense(const float *values, size_t num) override { return 0; }
T
tangwei12 已提交
139

140
  int32_t push_dense(const int64_t *values, const int32_t trainer_id) {
T
tangwei12 已提交
141 142 143
    return 0;
  }

144 145 146 147 148
 protected:
  virtual int32_t _run_program(const float *values, size_t num,
                               const uint32_t trainer_id) {
    return 0;
  }
T
tangwei12 已提交
149

150 151 152 153
  int startup_program_id_ = -1;
  int main_program_id_ = -1;
  std::string feed_var_name_ = "";
  std::string fetch_var_name_ = "";
T
tangwei12 已提交
154 155
};

156
class GlobalStepTable : public DenseTensorTable {
T
tangwei12 已提交
157
 public:
158 159
  GlobalStepTable() {}
  virtual ~GlobalStepTable() {}
T
tangwei12 已提交
160

161 162
  int32_t pull_sparse(float *values,
                      const PullSparseValue &pull_value) override {
T
tangwei12 已提交
163 164
    return 0;
  }
165 166
  int32_t push_sparse(const uint64_t *keys, const float *values,
                      size_t num) override {
T
tangwei12 已提交
167 168
    return 0;
  }
169
  int32_t shrink(const std::string &param) override { return 0; }
T
tangwei12 已提交
170

171 172 173
  virtual void *get_shard(size_t shard_idx) { return 0; }

  virtual int32_t initialize_shard() { return 0; }
T
tangwei12 已提交
174 175 176

  virtual int32_t flush() { return 0; }

177 178 179
  virtual void clear() {}

  virtual int32_t load(const std::string &path, const std::string &param) {
T
tangwei12 已提交
180 181
    return 0;
  }
182
  virtual int32_t save(const std::string &path, const std::string &param) {
T
tangwei12 已提交
183 184 185
    return 0;
  }

186
  int32_t pull_dense(float *values, size_t num) override { return 0; }
T
tangwei12 已提交
187

188
  /*----------------------------------------------------------------------*/
T
tangwei12 已提交
189

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
  int32_t initialize() override;

  int32_t push_dense(const float *values, size_t num) override { return 0; }

  int32_t push_dense(const int64_t *values, const int32_t trainer_id);

  int32_t set_table_map(
      std::unordered_map<uint32_t, std::shared_ptr<Table>> *table_map) override;

 private:
  virtual int32_t _run_program(const int64_t *values,
                               const uint32_t trainer_id);

 private:
  std::unordered_map<int, int64_t> decay_counters_;
  int32_t trainers_;
T
tangwei12 已提交
206
};
207

T
tangwei12 已提交
208 209
}  // namespace distributed
}  // namespace paddle