executor_thread_worker.h 7.8 KB
Newer Older
W
Wang Guibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Copyright (c) 2018 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 <map>
#include <memory>
#include <mutex>  // NOLINT
#include <set>
#include <string>
#include <thread>  // NOLINT
#include <vector>
#include "paddle/fluid/framework/data_feed.h"
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/framework/program_desc.h"
#include "paddle/fluid/framework/scope.h"
H
heqiaozhi 已提交
28
#ifdef PADDLE_WITH_PSLIB
H
heqiaozhi 已提交
29
#include <pslib.h>
H
heqiaozhi 已提交
30
#endif
W
Wang Guibao 已提交
31 32 33

namespace paddle {
namespace framework {
34

W
Wang Guibao 已提交
35
void CreateTensor(Variable* var, proto::VarType::Type var_type);
H
heqiaozhi 已提交
36
#ifdef PADDLE_WITH_PSLIB
H
heqiaozhi 已提交
37
static const uint32_t MAX_FEASIGN_NUM = 1000 * 100 * 100;
W
Wang Guibao 已提交
38

39
struct AsyncWorkerParamConfig {
40 41 42 43
  int slot_dim;
  int fea_dim;
  int32_t tmp_push_dense_wait_times;
  int32_t tmp_push_sparse_wait_times;
H
heqiaozhi 已提交
44

45
  std::vector<std::string> skip_op;
H
heqiaozhi 已提交
46

47 48
  std::map<uint64_t, std::vector<std::string>> dense_variable_name;
  std::map<uint64_t, std::vector<std::string>> dense_gradient_variable_name;
H
heqiaozhi 已提交
49
  std::vector<int> dense_table_id;
50
  // fea_dim for each dense table
H
heqiaozhi 已提交
51 52
  std::vector<uint32_t> dense_table_size;
  std::vector<int> sparse_table_id;
53 54 55
  std::map<uint64_t, std::vector<std::string>> slot_input_vec;
  std::map<uint64_t, std::vector<std::string>> gradient_var;
  std::map<std::string, uint64_t> slot_alias_to_table;
56 57 58
};

struct DensePullThreadParam {
H
heqiaozhi 已提交
59 60 61 62 63 64
  std::shared_ptr<paddle::ps::PSClient> ps_client;
  int threshold;
  int training_thread_num;
  Scope* root_scope;
  std::map<uint64_t, std::vector<std::string>>* dense_params;
  int sleep_time_ms = 2;
65 66 67
};

class DensePullThread {
68
 public:
H
heqiaozhi 已提交
69 70
  explicit DensePullThread(const DensePullThreadParam& param)
      : _running(false) {
D
dongdaxiang 已提交
71 72 73 74 75
    _ps_client = param.ps_client;
    _threshold = param.threshold;
    _thread_num = param.training_thread_num;
    _root_scope = param.root_scope;
    _sleep_time_ms = param.sleep_time_ms;
H
heqiaozhi 已提交
76

D
dongdaxiang 已提交
77
    for (auto& t : *param.dense_params) {
H
heqiaozhi 已提交
78 79
      _dense_variable_name[t.first].insert(_dense_variable_name[t.first].end(),
                                           t.second.begin(), t.second.end());
D
dongdaxiang 已提交
80 81 82
      _training_versions[t.first].resize(_thread_num, 0);
      _last_versions[t.first] = 0;
      _current_version[t.first] = 0;
83
    }
D
dongdaxiang 已提交
84
  }
H
heqiaozhi 已提交
85

D
dongdaxiang 已提交
86
  int start();
H
heqiaozhi 已提交
87

D
dongdaxiang 已提交
88 89 90 91
  void stop() {
    if (_running) {
      _running = false;
      _t.join();
92
    }
D
dongdaxiang 已提交
93
  }
H
heqiaozhi 已提交
94

D
dongdaxiang 已提交
95 96 97 98 99
  void increase_thread_version(int thread_id, uint64_t table_id);
  void reset_thread_version(uint64_t table_id);
  std::future<int32_t> pull_dense(uint64_t table_id);
  void pull_dense2(uint64_t table_id);
  void wait_all();
H
heqiaozhi 已提交
100

101
 private:
D
dongdaxiang 已提交
102 103
  void run();
  bool check_update_param(uint64_t table_id);
H
heqiaozhi 已提交
104

105
 private:
D
dongdaxiang 已提交
106 107 108 109 110 111 112 113 114
  std::shared_ptr<paddle::ps::PSClient> _ps_client;
  int _thread_num;
  int _threshold;
  int _sleep_time_ms;
  Scope* _root_scope;
  bool _running;

  std::map<uint64_t, uint64_t> _last_versions;
  std::map<uint64_t, uint64_t> _current_version;
H
heqiaozhi 已提交
115
  std::mutex _mutex_for_version;
D
dongdaxiang 已提交
116 117
  std::map<uint64_t, std::vector<uint64_t>> _training_versions;
  std::map<uint64_t, std::vector<std::string>> _dense_variable_name;
H
heqiaozhi 已提交
118

D
dongdaxiang 已提交
119
  std::thread _t;
H
heqiaozhi 已提交
120

D
dongdaxiang 已提交
121
  std::vector<::std::future<int32_t>> _pull_dense_status;
H
heqiaozhi 已提交
122

D
dongdaxiang 已提交
123
  std::map<uint64_t, std::vector<paddle::ps::Region>> _regions;
H
heqiaozhi 已提交
124 125 126 127 128
  uint32_t _pull_dense_fail_times = 0;

  std::vector<float> _base_norm_param;
  std::vector<float> _mean;
  std::vector<float> _scale;
D
dongdaxiang 已提交
129 130
  float _squared_sum_epsilon = 1e-4;
  std::mutex _mutex_for_mean_scale;
H
heqiaozhi 已提交
131

D
dongdaxiang 已提交
132
  float _total_batch_num = 0;
133
};
H
heqiaozhi 已提交
134 135
#endif

W
Wang Guibao 已提交
136 137
class ExecutorThreadWorker {
 public:
H
heqiaozhi 已提交
138 139
  ExecutorThreadWorker()
      : thread_id_(-1), root_scope_(NULL), thread_scope_(NULL), debug_(false) {}
140
  virtual ~ExecutorThreadWorker() {}
H
heqiaozhi 已提交
141

W
Wang Guibao 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  void CreateThreadResource(const framework::ProgramDesc& program,
                            const paddle::platform::Place& place);
  void SetThreadId(int tid);
  void SetDebug(const bool debug) { debug_ = debug; }
  void SetRootScope(Scope* g_scope);
  // set cpu device in this function
  // cpu binding is used by default
  void SetDevice();
  // since we read data into memory that can not be accessed by program
  // we need to bind memory of data with corresponding variables in program
  // this function should be called after data feed is set
  void BindingDataFeedMemory();
  // set data feed declared in executor
  void SetDataFeed(const std::shared_ptr<DataFeed>& datafeed);
  // A multi-thread training function
157
  virtual void TrainFiles();
W
Wang Guibao 已提交
158 159
  // set fetch variable names from python interface assigned by users
  void SetFetchVarNames(const std::vector<std::string>& fetch_var_names);
H
heqiaozhi 已提交
160
#ifdef PADDLE_WITH_PSLIB
161
  virtual void SetPSlibPtr(
D
dongdaxiang 已提交
162
      std::shared_ptr<paddle::distributed::PSlib> pslib_ptr) {}
H
heqiaozhi 已提交
163 164
  virtual void SetPullDenseThread(std::shared_ptr<DensePullThread> dpt) {}
  virtual void SetParamConfig(AsyncWorkerParamConfig* param_config) {}
H
heqiaozhi 已提交
165
#endif
166

W
Wang Guibao 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
 private:
  void CreateThreadScope(const framework::ProgramDesc& program);
  void CreateThreadOperators(const framework::ProgramDesc& program);
  void SetMainProgram(const ProgramDesc& main_program_desc);
  void SetPlace(const paddle::platform::Place& place);

 protected:
  // thread index
  std::shared_ptr<DataFeed> thread_reader_;  // shared queue, thread buffer
  int thread_id_;
  // operator name
  std::vector<std::string> op_names_;
  // thread level, local operators for forward and backward
  std::vector<OperatorBase*> ops_;
  // main program for training
  std::unique_ptr<framework::ProgramDesc> main_program_;
  // execution place
  platform::Place place_;
  // root scope for model parameters
  Scope* root_scope_;
  // a thread scope, father scope is global score which is shared
  Scope* thread_scope_;
  std::vector<std::string> fetch_var_names_;
  std::vector<std::vector<float>> fetch_values_;
  bool debug_;
};

H
heqiaozhi 已提交
194
#ifdef PADDLE_WITH_PSLIB
H
heqiaozhi 已提交
195
class AsyncExecutorThreadWorker : public ExecutorThreadWorker {
196 197 198 199 200 201 202 203 204 205 206 207 208 209
 public:
  AsyncExecutorThreadWorker() {}
  virtual ~AsyncExecutorThreadWorker() {}
  void SetPSlibPtr(std::shared_ptr<paddle::distributed::PSlib> pslib_ptr);
  void SetPullDenseThread(std::shared_ptr<DensePullThread> dpt);
  void SetParamConfig(AsyncWorkerParamConfig* param_config);
  void TrainFiles();
  void TrainOneNetwork();
  void PrepareParams();
  void UpdateParams();
  void PullSparse(int table_id);
  void FillSparse(int table_id);
  void PushSparse(int table_id);
  void PushDense(int table_id);
H
heqiaozhi 已提交
210

211
  void check_pull_push_memory(const std::vector<uint64_t>& features,
H
heqiaozhi 已提交
212 213 214
                              std::vector<float*>* push_g, int dim);
  void check_pull_push_memory(const std::vector<uint64_t>& features,
                              std::vector<std::vector<float>>* push_g, int dim);
D
dongdaxiang 已提交
215
  void collect_feasign_info(int table_id);
H
heqiaozhi 已提交
216

217
 private:
D
dongdaxiang 已提交
218 219 220 221 222
  struct FeasignInfo {
    uint32_t slot;
    uint32_t ins;
    int64_t label;
  };
H
heqiaozhi 已提交
223 224 225

  std::map<uint64_t, std::vector<uint64_t>> _features;
  std::map<uint64_t, std::vector<FeasignInfo>> _fea_info;
D
dongdaxiang 已提交
226 227
  std::map<uint64_t, std::vector<std::vector<float>>> _feature_value;
  std::map<uint64_t, std::vector<std::vector<float>>> _feature_push_value;
H
heqiaozhi 已提交
228 229 230 231 232 233 234 235 236 237 238

  std::shared_ptr<paddle::distributed::PSlib> _pslib_ptr;

  std::shared_ptr<DensePullThread> _pull_dense_thread;

  std::vector<::std::future<int32_t>> _pull_sparse_status;
  std::vector<::std::future<int32_t>> _pull_dense_status;
  std::vector<::std::future<int32_t>> _push_sparse_status;
  std::vector<::std::future<int32_t>> _push_dense_status;

  AsyncWorkerParamConfig* _param_config;
239
};
H
heqiaozhi 已提交
240
#endif
241

W
Wang Guibao 已提交
242 243
}  // namespace framework
}  // namespace paddle