pipeline_trainer.cc 5.5 KB
Newer Older
H
hutuxian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 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.

15
#if defined(PADDLE_WITH_NCCL)
16
#include <map>
H
hutuxian 已提交
17 18 19 20 21 22 23 24 25 26
#include "paddle/fluid/framework/data_feed_factory.h"
#include "paddle/fluid/framework/device_worker_factory.h"
#include "paddle/fluid/framework/trainer.h"
#include "paddle/fluid/framework/trainer_desc.pb.h"

namespace paddle {
namespace framework {

void PipelineTrainer::Initialize(const TrainerDesc& trainer_desc,
                                 Dataset* dataset) {
L
lilong12 已提交
27 28 29 30
  const auto& section_params = trainer_desc.section_param();
  num_microbatches_ = section_params.num_microbatches();
  VLOG(3) << "Number of microbatches per minibatch: " << num_microbatches_;
  trainer_desc_ = trainer_desc;
S
sandyhouse 已提交
31
  auto cpu_core_id = section_params.start_cpu_core_id();
H
hutuxian 已提交
32

H
hutuxian 已提交
33
  ParseDumpConfig(trainer_desc);
S
sandyhouse 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  const auto& section_config = section_params.section_config();
  int place_id = section_config.place_id();
  PADDLE_ENFORCE_GE(place_id, 0,
                    platform::errors::InvalidArgument(
                        "The place_id value for CUDAPlace shoud be "
                        "non-negative, but the value given is %d.",
                        place_id));
  place_ = platform::CUDAPlace(place_id);
  worker_ = DeviceWorkerFactory::CreateDeviceWorker(
      trainer_desc.device_worker_name());
  auto this_worker =
      std::dynamic_pointer_cast<paddle::framework::SectionWorker>(worker_);
  this_worker->SetPlace(place_);
  this_worker->Initialize(trainer_desc);
  this_worker->SetMicrobatchNum(num_microbatches_);
S
sandyhouse 已提交
49
  this_worker->SetStartCpuCoreId(cpu_core_id);
H
hutuxian 已提交
50 51 52 53 54

  // set debug here
  SetDebug(trainer_desc.debug());
}

H
hutuxian 已提交
55 56 57 58 59 60 61 62 63 64 65 66
void PipelineTrainer::InitOtherEnv(const ProgramDesc& main_program) {
  if (need_dump_field_) {
    InitDumpEnv();
  }
}

std::string PipelineTrainer::GetDumpPath(int tid) {
  return string::format_string("%s/part-%05d", dump_fields_path_.c_str(), tid);
}

void PipelineTrainer::InitDumpEnv() {
  queue_ = paddle::framework::MakeChannel<std::string>();
L
lilong12 已提交
67
  // TODO(sandyhouse): should make it as a config
H
hutuxian 已提交
68 69 70 71 72 73 74
  dump_thread_num_ = 1;
  for (int i = 0; i < dump_thread_num_; i++) {
    dump_thread_.push_back(
        std::thread(std::bind(&TrainerBase::DumpWork, this, i)));
  }
}

S
sandyhouse 已提交
75
void PipelineTrainer::CopyParameters(int microbatch_id,
L
lilong12 已提交
76 77 78
                                     const ProgramDesc& program,
                                     const platform::Place& place) {
  auto& global_block = program.Block(0);
79
  std::map<std::string, int> param_map;
L
lilong12 已提交
80
  for (auto& var : global_block.AllVars()) {
81 82 83 84
    if (var->Persistable()) {
      param_map[var->Name()] = 1;
    }
  }
S
sandyhouse 已提交
85

86 87 88 89 90 91 92 93 94
  for (auto& var : global_block.AllVars()) {
    bool is_param_grad = false;
    size_t pos = 0;
    if ((pos = var->Name().find(kGradVarSuffix)) != std::string::npos) {
      auto prefix_name = var->Name().substr(0, pos);
      if (param_map.find(prefix_name) != param_map.end()) {
        is_param_grad = true;
      }
    }
S
sandyhouse 已提交
95 96 97 98 99
    if (is_param_grad && microbatch_id == 0) {
      auto* ptr = minibatch_scope_->Var(var->Name());
      InitializeVariable(ptr, var->GetType());
      VLOG(3) << "Create grad for persistable var: " << var->Name()
              << ", which pointer is " << ptr;
100
    } else if (!var->Persistable() && !is_param_grad) {
S
sandyhouse 已提交
101
      auto* ptr = microbatch_scopes_[microbatch_id]->Var(var->Name());
S
sandyhouse 已提交
102
      VLOG(3) << "Create variable " << var->Name() << " for microbatch "
S
sandyhouse 已提交
103
              << microbatch_id << ", which pointer is " << ptr;
L
lilong12 已提交
104
      InitializeVariable(ptr, var->GetType());
H
hutuxian 已提交
105 106 107 108 109 110
    }
  }
}

void PipelineTrainer::InitTrainerEnv(const ProgramDesc& main_program,
                                     const platform::Place& place) {
S
sandyhouse 已提交
111 112 113
  PADDLE_ENFORCE_NOT_NULL(root_scope_, platform::errors::InvalidArgument(
                                           "root_scope_ can not be nullptr"));
  microbatch_scopes_.resize(num_microbatches_);
H
hutuxian 已提交
114

S
sandyhouse 已提交
115
  VLOG(3) << "Create minibatch and microbatch scopes...";
S
sandyhouse 已提交
116 117 118 119 120 121 122
  minibatch_scope_ = &root_scope_->NewScope();
  std::shared_ptr<framework::ProgramDesc> program;
  program.reset(new ProgramDesc(
      trainer_desc_.section_param().section_config().program_desc()));
  for (int j = 0; j < num_microbatches_; ++j) {
    microbatch_scopes_[j] = &minibatch_scope_->NewScope();
    CopyParameters(j, *program, place_);
H
hutuxian 已提交
123 124
  }

S
sandyhouse 已提交
125 126 127 128 129
  auto this_worker =
      std::dynamic_pointer_cast<paddle::framework::SectionWorker>(worker_);
  this_worker->SetRootScope(root_scope_);
  this_worker->SetMinibatchScope(minibatch_scope_);
  this_worker->SetMicrobatchScopes(microbatch_scopes_);
H
hutuxian 已提交
130 131 132
}

void PipelineTrainer::Run() {
S
sandyhouse 已提交
133
  VLOG(5) << "Going to run PipelineTrainer::Run()";
S
sandyhouse 已提交
134 135 136 137 138
  if (!debug_) {
    section_thread_ = std::thread(&DeviceWorker::TrainFiles, worker_.get());
  } else {
    section_thread_ =
        std::thread(&DeviceWorker::TrainFilesWithProfiler, worker_.get());
H
hutuxian 已提交
139 140 141 142
  }
}

void PipelineTrainer::Finalize() {
S
sandyhouse 已提交
143
  section_thread_.join();
H
hutuxian 已提交
144 145 146
  if (need_dump_field_) {
    FinalizeDumpEnv();
  }
H
hutuxian 已提交
147 148 149
  root_scope_->DropKids();
}

150
Scope* PipelineTrainer::GetWorkerScope(int thread_id) {
S
sandyhouse 已提交
151
  return microbatch_scopes_[0];
152 153
}

H
hutuxian 已提交
154 155 156
}  // end namespace framework
}  // end namespace paddle
#endif