async_executor.cc 7.9 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 28 29 30 31 32 33 34 35 36 37
/* Copyright (c) 2016 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 "paddle/fluid/framework/async_executor.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"

#include "gflags/gflags.h"
#include "paddle/fluid/framework/data_feed_factory.h"
#include "paddle/fluid/framework/executor_thread_worker.h"
#include "paddle/fluid/framework/feed_fetch_method.h"
#include "paddle/fluid/framework/feed_fetch_type.h"
#include "paddle/fluid/framework/lod_rank_table.h"
#include "paddle/fluid/framework/lod_tensor_array.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/reader.h"
#include "paddle/fluid/inference/io.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/pybind/pybind.h"

namespace paddle {
namespace framework {
AsyncExecutor::AsyncExecutor(Scope* scope, const platform::Place& place)
    : root_scope_(scope), place_(place) {}

H
heqiaozhi 已提交
38
void AsyncExecutor::InitServer(const std::string& dist_desc, int index) {
39 40
  fleet_ptr_ = FleetWrapper::GetInstance();
  fleet_ptr_->InitServer(dist_desc, index);
41 42
}

43 44 45
void AsyncExecutor::InitWorker(const std::string& dist_desc,
                               const std::vector<uint64_t>& host_sign_list,
                               int node_num, int index) {
46 47
  fleet_ptr_ = FleetWrapper::GetInstance();
  fleet_ptr_->InitWorker(dist_desc, host_sign_list, node_num, index);
H
heqiaozhi 已提交
48 49
}

50
uint64_t AsyncExecutor::StartServer() { return fleet_ptr_->RunServer(); }
H
heqiaozhi 已提交
51

52
void AsyncExecutor::StopServer() { fleet_ptr_->StopServer(); }
H
heqiaozhi 已提交
53

H
heqiaozhi 已提交
54 55
void AsyncExecutor::GatherServers(const std::vector<uint64_t>& host_sign_list,
                                  int node_num) {
56
  fleet_ptr_->GatherServers(host_sign_list, node_num);
H
heqiaozhi 已提交
57 58
}

59
void AsyncExecutor::InitModel() {
D
dongdaxiang 已提交
60 61 62 63 64 65
  for (auto table_id : _param_config.dense_table_id) {
    std::vector<paddle::ps::Region> regions;
    for (auto& t : _param_config.dense_variable_name[table_id]) {
      Variable* var = root_scope_->FindVar(t);
      CHECK(var != nullptr) << "var[" << t << "] not found";
      LoDTensor* tensor = var->GetMutable<LoDTensor>();
H
heqiaozhi 已提交
66

D
dongdaxiang 已提交
67 68
      float* g = tensor->data<float>();
      CHECK(g != nullptr) << "var[" << t << "] value not initialized";
69

D
dongdaxiang 已提交
70 71 72
      float init_range = 0.2;
      int rown = tensor->dims()[0];
      init_range /= sqrt(rown);
H
heqiaozhi 已提交
73

D
dongdaxiang 已提交
74 75 76 77
      std::normal_distribution<float> ndistr(0.0, 1.0);
      for (auto i = 0u; i < tensor->numel(); ++i) {
        g[i] = ndistr(local_random_engine()) * init_range;
      }
H
heqiaozhi 已提交
78

D
dongdaxiang 已提交
79 80 81
      paddle::ps::Region reg(g, tensor->numel());
      regions.emplace_back(std::move(reg));
    }
H
heqiaozhi 已提交
82 83 84

    auto push_status = _pslib_ptr->_worker_ptr->push_dense_param(
        regions.data(), regions.size(), table_id);
D
dongdaxiang 已提交
85 86 87 88 89
    push_status.wait();
    auto status = push_status.get();
    if (status != 0) {
      LOG(FATAL) << "push dense param failed, status[" << status << "]";
      exit(-1);
90
    }
D
dongdaxiang 已提交
91
  }
92 93 94
}

void AsyncExecutor::SaveModel(const std::string& path) {
D
dongdaxiang 已提交
95 96 97 98 99 100 101 102 103
  auto ret = _pslib_ptr->_worker_ptr->flush();
  ret.wait();
  ret = _pslib_ptr->_worker_ptr->save(path, 0);
  ret.wait();
  int32_t feasign_cnt = ret.get();
  if (feasign_cnt == -1) {  // (colourful-tree) TODO should be feasign_cnt < 0
    LOG(FATAL) << "save model failed";
    exit(-1);
  }
104 105
}

W
Wang Guibao 已提交
106
void AsyncExecutor::RunFromFile(const ProgramDesc& main_program,
107
<<<<<<< HEAD
W
Wang Guibao 已提交
108 109 110 111
                                const std::string& data_feed_desc_str,
                                const std::vector<std::string>& filelist,
                                const int thread_num,
                                const std::vector<std::string>& fetch_var_names,
H
heqiaozhi 已提交
112
                                const std::string& mode, const bool debug) {
W
Wang Guibao 已提交
113 114 115 116 117
  std::vector<std::thread> threads;

  auto& block = main_program.Block(0);
  for (auto var_name : fetch_var_names) {
    auto var_desc = block.FindVar(var_name);
118
    PADDLE_ENFORCE_NOT_NULL(var_desc, "%s is not found.", var_name);
W
Wang Guibao 已提交
119 120 121 122 123 124 125 126 127 128 129
    auto shapes = var_desc->GetShape();
    PADDLE_ENFORCE(shapes[shapes.size() - 1] == 1,
                   "var %s: Fetched var has wrong shape, "
                   "only variables with the last dimension size 1 supported",
                   var_name);
  }

  DataFeedDesc data_feed_desc;
  google::protobuf::TextFormat::ParseFromString(data_feed_desc_str,
                                                &data_feed_desc);

130
  actual_thread_num = thread_num;
W
Wang Guibao 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  int file_cnt = filelist.size();
  PADDLE_ENFORCE(file_cnt > 0, "File list cannot be empty");

  if (actual_thread_num > file_cnt) {
    VLOG(1) << "Thread num = " << thread_num << ", file num = " << file_cnt
            << ". Changing thread_num = " << file_cnt;
    actual_thread_num = file_cnt;
  }

  /*
    readerDesc: protobuf description for reader initlization
    argument: class_name, batch_size, use_slot, queue_size, buffer_size,
    padding_index

    reader:
    1) each thread has a reader, reader will read input data and
    put it into input queue
    2) each reader has a Next() iterface, that can fetch an instance
    from the input queue
   */
  // todo: should be factory method for creating datafeed
  std::vector<std::shared_ptr<DataFeed>> readers;
  PrepareReaders(readers, actual_thread_num, data_feed_desc, filelist);
H
heqiaozhi 已提交
154
#ifdef PADDLE_WITH_PSLIB
H
heqiaozhi 已提交
155
  PrepareDenseThread(mode);
H
heqiaozhi 已提交
156
#endif
W
Wang Guibao 已提交
157 158 159
  std::vector<std::shared_ptr<ExecutorThreadWorker>> workers;
  workers.resize(actual_thread_num);
  for (auto& worker : workers) {
H
heqiaozhi 已提交
160
#ifdef PADDLE_WITH_PSLIB
H
heqiaozhi 已提交
161
    if (mode == "mpi") {
H
heqiaozhi 已提交
162
      worker.reset(new AsyncExecutorThreadWorker);
H
heqiaozhi 已提交
163
    } else {
H
heqiaozhi 已提交
164
      worker.reset(new ExecutorThreadWorker);
H
heqiaozhi 已提交
165
    }
H
heqiaozhi 已提交
166 167 168
#else
    worker.reset(new ExecutorThreadWorker);
#endif
W
Wang Guibao 已提交
169 170 171 172 173 174 175 176 177 178
  }

  // prepare thread resource here
  for (int thidx = 0; thidx < actual_thread_num; ++thidx) {
    CreateThreads(workers[thidx].get(), main_program, readers[thidx],
                  fetch_var_names, root_scope_, thidx, debug);
  }

  // start executing ops in multiple threads
  for (int thidx = 0; thidx < actual_thread_num; ++thidx) {
179 180 181 182 183 184 185
    if (debug) {
      threads.push_back(std::thread(&ExecutorThreadWorker::TrainFilesWithTimer,
                                    workers[thidx].get()));
    } else {
      threads.push_back(
          std::thread(&ExecutorThreadWorker::TrainFiles, workers[thidx].get()));
    }
W
Wang Guibao 已提交
186 187 188 189 190
  }

  for (auto& th : threads) {
    th.join();
  }
H
heqiaozhi 已提交
191
#ifdef PADDLE_WITH_PSLIB
H
heqiaozhi 已提交
192 193 194
  if (mode == "mpi") {
    _pull_dense_thread->stop();
  }
H
heqiaozhi 已提交
195
#endif
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
=======
                                const std::string& trainer_desc_str,
                                const bool debug) {
  TrainerDesc trainer_desc;
  google::protobuf::TextFormat::ParseFromString(trainer_desc_str,
                                                &trainer_desc);
  std::shared_ptr<TrainerBase> trainer;
  trainer = TrainerFactory::CreateTrainer(trainer_desc.class_name());
  // initialize trainer
  trainer->Initialize(trainer_desc);
  // trainer->SetRootScope(root_scope_);
  trainer->SetDebug(debug);
  // prepare training environment and helper environment
  trainer->InitTrainerEnv(main_program, place_);
  trainer->InitOtherEnv(main_program);
  // training and finalize training
  trainer->Run();
  trainer->Finalize();
>>>>>>> add dist_multi_trainer for distributed training, add trainer_factory and device_worker_factory so that we can easily extend new training mode, add pull dense worker which is a singleton for parameter fetching
W
Wang Guibao 已提交
215 216 217 218 219 220 221
  root_scope_->DropKids();

  return;
}

}  // einit_modelnd namespace framework
}  // end namespace paddle