tensor_table.h 5.7 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
#include "paddle/fluid/distributed/common/utils.h"
T
tangwei12 已提交
26 27 28 29 30 31 32 33
#include "paddle/fluid/distributed/table/table.h"
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/platform/device_context.h"

namespace paddle {
namespace distributed {

34 35 36
#define LEARNING_RATE_DECAY_COUNTER "@LR_DECAY_COUNTER@"
#define STEP_COUNTER "@PS_STEP_COUNTER@"

T
tangwei12 已提交
37 38
class TensorTable : public Table {
 public:
39
  TensorTable() {}
T
tangwei12 已提交
40 41
  virtual ~TensorTable() {}

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

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

46 47
  int32_t pull_sparse(float *values, const uint64_t *keys,
                      size_t num) override {
T
tangwei12 已提交
48
    return 0;
49 50 51 52 53 54 55 56
  }
  int32_t push_sparse(const uint64_t *keys, const float *values,
                      size_t num) override {
    return 0;
  }
  int32_t shrink() override { return 0; }

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

58
  virtual int32_t initialize_shard() { return 0; };
T
tangwei12 已提交
59

60 61 62
  virtual int32_t flush() { return 0; };

  virtual int32_t load(const std::string &path, const std::string &param) {
T
tangwei12 已提交
63
    return 0;
64 65 66 67 68 69
  }
  virtual int32_t save(const std::string &path, const std::string &param) {
    return 0;
  }

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

71 72 73 74
  virtual int32_t initialize() override { return 0; };

  virtual int32_t push_dense(const int64_t *values,
                             const int32_t trainer_id) override {
T
tangwei12 已提交
75 76 77
    return 0;
  };

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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() {}

  int32_t pull_sparse(float *values, const uint64_t *keys,
                      size_t num) override {
T
tangwei12 已提交
98 99
    return 0;
  }
100 101 102 103 104
  int32_t push_sparse(const uint64_t *keys, const float *values,
                      size_t num) override {
    return 0;
  }
  int32_t shrink() override { return 0; }
T
tangwei12 已提交
105

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

108
  virtual int32_t initialize_shard() { return 0; }
T
tangwei12 已提交
109 110 111

  virtual int32_t flush() { return 0; }

112 113 114 115
  virtual void clear() {}

  // Todo: Support program Load & Save
  virtual int32_t load(const std::string &path, const std::string &param) {
T
tangwei12 已提交
116 117
    return 0;
  }
118
  virtual int32_t save(const std::string &path, const std::string &param) {
T
tangwei12 已提交
119 120 121
    return 0;
  }

122 123 124 125 126 127 128 129
  // 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 已提交
130

131
  int32_t push_dense(const int64_t *values, const int32_t trainer_id) {
T
tangwei12 已提交
132 133 134
    return 0;
  }

135 136 137 138 139
 protected:
  virtual int32_t _run_program(const float *values, size_t num,
                               const uint32_t trainer_id) {
    return 0;
  }
T
tangwei12 已提交
140

141 142 143 144
  int startup_program_id_ = -1;
  int main_program_id_ = -1;
  std::string feed_var_name_ = "";
  std::string fetch_var_name_ = "";
T
tangwei12 已提交
145 146
};

147
class GlobalStepTable : public DenseTensorTable {
T
tangwei12 已提交
148
 public:
149 150
  GlobalStepTable() {}
  virtual ~GlobalStepTable() {}
T
tangwei12 已提交
151

152 153
  int32_t pull_sparse(float *values, const uint64_t *keys,
                      size_t num) override {
T
tangwei12 已提交
154 155
    return 0;
  }
156 157
  int32_t push_sparse(const uint64_t *keys, const float *values,
                      size_t num) override {
T
tangwei12 已提交
158 159
    return 0;
  }
160
  int32_t shrink() override { return 0; }
T
tangwei12 已提交
161

162 163 164
  virtual void *get_shard(size_t shard_idx) { return 0; }

  virtual int32_t initialize_shard() { return 0; }
T
tangwei12 已提交
165 166 167

  virtual int32_t flush() { return 0; }

168 169 170
  virtual void clear() {}

  virtual int32_t load(const std::string &path, const std::string &param) {
T
tangwei12 已提交
171 172
    return 0;
  }
173
  virtual int32_t save(const std::string &path, const std::string &param) {
T
tangwei12 已提交
174 175 176
    return 0;
  }

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

179
  /*----------------------------------------------------------------------*/
T
tangwei12 已提交
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  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 已提交
197
};
198

T
tangwei12 已提交
199 200
}  // namespace distributed
}  // namespace paddle