tensor_table.h 6.0 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() {}

Y
yaoxuefeng 已提交
51 52
  virtual int32_t Pull(TableContext &context) { return 0; }
  virtual int32_t Push(TableContext &context) { return 0; }
53
  int32_t pull_dense(float *values, size_t num) override { return 0; }
T
tangwei12 已提交
54

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

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

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

69
  virtual int32_t initialize_shard() { return 0; };
T
tangwei12 已提交
70

71 72 73
  virtual int32_t flush() { return 0; };

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

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

82 83 84 85
  virtual int32_t initialize() override { return 0; };

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

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  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() {}

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

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

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

  virtual int32_t flush() { return 0; }

123 124 125 126
  virtual void clear() {}

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

133 134 135 136 137 138 139 140
  // 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 已提交
141

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

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

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

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

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

173 174 175
  virtual void *get_shard(size_t shard_idx) { return 0; }

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

  virtual int32_t flush() { return 0; }

179 180 181
  virtual void clear() {}

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

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

190
  /*----------------------------------------------------------------------*/
T
tangwei12 已提交
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  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 已提交
208
};
209

T
tangwei12 已提交
210 211
}  // namespace distributed
}  // namespace paddle