pull_dense_worker.cc 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */
#include <time.h>
#include "paddle/fluid/framework/device_worker.h"

17
namespace phi {
18
class DenseTensor;
19
}  // namespace phi
20

21 22 23
namespace paddle {
namespace framework {

24 25 26
class Scope;
class Variable;

27
std::shared_ptr<PullDenseWorker> PullDenseWorker::s_instance_ = NULL;
D
dongdaxiang 已提交
28 29 30 31 32 33
std::mutex PullDenseWorker::mutex_for_version_;
std::map<uint64_t, uint64_t> PullDenseWorker::last_versions_;
std::map<uint64_t, uint64_t> PullDenseWorker::current_version_;
std::map<uint64_t, std::vector<uint64_t>> PullDenseWorker::training_versions_;
std::map<uint64_t, std::vector<std::string>>
    PullDenseWorker::dense_value_names_;
34 35 36 37

void PullDenseWorker::Initialize(const TrainerDesc& param) {
  running_ = false;
  param_ = param.pull_dense_param();
H
heqiaozhi 已提交
38
  dwp_param_ = param.downpour_param();
39 40 41
  threshold_ = param_.threshold();
  thread_num_ = param_.device_num();
  sleep_time_ms_ = param_.sleep_time_ms();
42 43
  for (int i = 0; i < dwp_param_.program_config(0).pull_dense_table_id_size();
       ++i) {
H
heqiaozhi 已提交
44 45 46 47 48 49 50 51 52
    uint64_t tid = static_cast<uint64_t>(
        dwp_param_.program_config(0).pull_dense_table_id(i));
    TableParameter table;
    for (auto i : param_.dense_table()) {
      if (i.table_id() == tid) {
        table = i;
        break;
      }
    }
53
    // setup dense variables for each table
H
heqiaozhi 已提交
54
    int var_num = table.dense_value_name_size();
55 56
    dense_value_names_[tid].resize(var_num);
    for (int j = 0; j < var_num; ++j) {
57
      dense_value_names_[tid][j] = table.dense_value_name(j);
58 59 60 61 62 63
    }
    // setup training version for each table
    training_versions_[tid].resize(thread_num_, 0);
    last_versions_[tid] = 0;
    current_version_[tid] = 0;
  }
64 65 66 67

#if defined(PADDLE_WITH_PSCORE)
  fleet_ptr_ = paddle::distributed::FleetWrapper::GetInstance();
#else
68
  fleet_ptr_ = FleetWrapper::GetInstance();
69 70
#endif

71
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
T
Thunderbrook 已提交
72
  copy_streams_.clear();
T
Thunderbrook 已提交
73
#endif
74 75
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) || \
    defined(PADDLE_WITH_XPU)
T
Thunderbrook 已提交
76 77 78 79 80 81
  places_.clear();
  thread_scopes_.clear();
#endif
}

void PullDenseWorker::CreatePinVar() {
82 83
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) || \
    defined(PADDLE_WITH_XPU)
T
Thunderbrook 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97
  // for (auto& v : dense_value_names_) {
  //  for (auto& name : v.second) {
  for (int i = 0; i < dwp_param_.program_config(0).pull_dense_table_id_size();
       ++i) {
    uint64_t tid = static_cast<uint64_t>(
        dwp_param_.program_config(0).pull_dense_table_id(i));
    for (size_t j = 0; j < dense_value_names_[tid].size(); j++) {
      auto& name = dense_value_names_[tid][j];
      Variable* var = root_scope_->FindVar(name);

      LoDTensor* tensor = var->GetMutable<LoDTensor>();
      auto* ptr = root_scope_->Var(name + "pin");
      InitializeVariable(ptr, proto::VarType::LOD_TENSOR);
      LoDTensor* pin_tensor = ptr->GetMutable<LoDTensor>();
98
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
T
Thunderbrook 已提交
99 100
      pin_tensor->mutable_data<float>(tensor->dims(),
                                      platform::CUDAPinnedPlace());
T
Thunderbrook 已提交
101 102 103 104
#endif
#ifdef PADDLE_WITH_XPU
      pin_tensor->mutable_data<float>(tensor->dims(), platform::CPUPlace());
#endif
T
Thunderbrook 已提交
105 106 107
    }
  }
#endif
108 109 110 111 112 113 114 115 116 117 118 119
}

void PullDenseWorker::Wait(std::vector<::std::future<int32_t>>* status_vec) {
  for (auto& t : *status_vec) {
    t.wait();
    auto status = t.get();
    if (status != 0) {
      LOG(WARNING) << "Current Pull Dense Thread Failed Times"
                   << ++pull_dense_fail_times_;
    }
  }

120
  size_t MAX_FAIL_NUM = 20;
121
  if (pull_dense_fail_times_ > MAX_FAIL_NUM) {
122 123
    PADDLE_THROW(platform::errors::Fatal(
        "Pull dense failed more than %d times.", MAX_FAIL_NUM));
124 125
    exit(-1);
  }
126
  status_vec->resize(0);
127 128
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) || \
    defined(PADDLE_WITH_XPU)
T
Thunderbrook 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

  for (size_t i = 0; i < places_.size(); ++i) {
    // for (auto& v : dense_value_names_) {
    //  for (auto& name : v.second) {
    for (int x = 0; x < dwp_param_.program_config(0).pull_dense_table_id_size();
         ++x) {
      uint64_t tid = static_cast<uint64_t>(
          dwp_param_.program_config(0).pull_dense_table_id(x));
      for (size_t j = 0; j < dense_value_names_[tid].size(); j++) {
        auto& name = dense_value_names_[tid][j];

        Variable* pin_var = root_scope_->FindVar(name + "pin");
        LoDTensor* pin_tensor = pin_var->GetMutable<LoDTensor>();
        float* pin_w = pin_tensor->data<float>();
        Variable* var = thread_scopes_[i]->FindVar(name);
        LoDTensor* tensor = var->GetMutable<LoDTensor>();
        float* w = tensor->data<float>();
146
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
147
        memory::Copy(places_[i], w, platform::CUDAPinnedPlace(), pin_w,
T
Thunderbrook 已提交
148
                     sizeof(float) * tensor->numel(), copy_streams_[i]);
T
Thunderbrook 已提交
149 150
#endif
#ifdef PADDLE_WITH_XPU
151
        memory::Copy(places_[i], w, platform::CPUPlace(), pin_w,
T
Thunderbrook 已提交
152 153
                     sizeof(float) * tensor->numel());
#endif
T
Thunderbrook 已提交
154 155 156 157
      }
    }
  }
#endif
158 159 160 161 162 163 164 165 166
}

void PullDenseWorker::Stop() {
  if (running_) {
    running_ = false;
    t_.join();
  }
}

167 168
void PullDenseWorker::PullDense(bool force_update) {
  pull_dense_status_.resize(0);
169 170
  for (int i = 0; i < dwp_param_.program_config(0).pull_dense_table_id_size();
       ++i) {
171 172 173
    uint64_t tid = static_cast<uint64_t>(
        dwp_param_.program_config(0).pull_dense_table_id(i));
    if (force_update || CheckUpdateParam(tid)) {
174 175
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) || \
    defined(PADDLE_WITH_XPU)
T
Thunderbrook 已提交
176
      VLOG(3) << "pull dense " << force_update << " " << tid;
177
      fleet_ptr_->PullDenseVarsAsync(*root_scope_, tid, dense_value_names_[tid],
T
Thunderbrook 已提交
178
                                     &pull_dense_status_, false);
179 180 181
#elif defined(PADDLE_WITH_PSCORE)
      fleet_ptr_->PullDenseVarsAsync(*root_scope_, tid, dense_value_names_[tid],
                                     &pull_dense_status_, true);
T
Thunderbrook 已提交
182 183 184 185
#else
      fleet_ptr_->PullDenseVarsAsync(*root_scope_, tid, dense_value_names_[tid],
                                     &pull_dense_status_, true);
#endif
186 187 188 189 190 191 192 193
      ResetThreadVersion(tid);
    }
  }
  if (pull_dense_status_.size() != 0) {
    Wait(&pull_dense_status_);
  }
}

194 195
int PullDenseWorker::Start() {
  running_ = true;
196 197
  // before training, we can pull dense from pserver first.
  PullDense(true);
198 199 200 201 202 203
  t_ = std::thread(&PullDenseWorker::Run, this);
  return 0;
}

void PullDenseWorker::Run() {
  while (running_) {
204
    PullDense(false);
D
dongdaxiang 已提交
205
#ifndef _WIN32
206
    usleep(sleep_time_ms_ * 1000);
D
dongdaxiang 已提交
207
#endif
208 209 210 211 212 213 214 215 216 217 218 219 220
  }
}

void PullDenseWorker::IncreaseThreadVersion(int thread_id, uint64_t table_id) {
  std::lock_guard<std::mutex> lock(mutex_for_version_);
  training_versions_[table_id][thread_id]++;
}

bool PullDenseWorker::CheckUpdateParam(uint64_t table_id) {
  std::lock_guard<std::mutex> lock(mutex_for_version_);
  auto& version = training_versions_[table_id];
  current_version_[table_id] =
      *(std::min_element(version.begin(), version.end()));
221 222
  if (current_version_[table_id] - last_versions_[table_id] <
      static_cast<size_t>(threshold_)) {
223 224 225 226 227 228 229 230 231 232
    return false;
  }
  return true;
}

void PullDenseWorker::ResetThreadVersion(uint64_t table_id) {
  std::lock_guard<std::mutex> lock(mutex_for_version_);
  last_versions_[table_id] = current_version_[table_id];
}

233 234 235 236 237 238 239 240 241 242 243
int PullDenseWorker::GetThreadIdByScope(const Scope* scope) {
  if (scope_to_thread_id_.find(scope) != scope_to_thread_id_.end()) {
    return scope_to_thread_id_[scope];
  }
  return -1;
}

void PullDenseWorker::SetThreadIdByScope(const Scope* scope, int tid) {
  scope_to_thread_id_[scope] = tid;
}

T
Thunderbrook 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
void PullDenseWorker::MergeDenseParam() {
  for (int x = 0; x < dwp_param_.program_config(0).pull_dense_table_id_size();
       ++x) {
    uint64_t tid = static_cast<uint64_t>(
        dwp_param_.program_config(0).pull_dense_table_id(x));
    for (size_t j = 0; j < dense_value_names_[tid].size(); j++) {
      auto& name = dense_value_names_[tid][j];

      Variable* root_var = root_scope_->FindVar(name);
      LoDTensor* root_tensor = root_var->GetMutable<LoDTensor>();
      Variable* var = thread_scopes_[0]->FindVar(name);
      LoDTensor* tensor = var->GetMutable<LoDTensor>();
      TensorCopy((*tensor), root_tensor->place(), root_tensor);
    }
  }
}

261 262
}  // namespace framework
}  // namespace paddle