hogwild_worker.cc 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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 "paddle/fluid/framework/device_worker.h"
16
#include "paddle/fluid/framework/device_worker_factory.h"
17
#include "paddle/fluid/platform/cpu_helper.h"
D
dongdaxiang 已提交
18
#include "paddle/fluid/platform/lodtensor_printer.h"
19 20 21 22

namespace paddle {
namespace framework {

D
dongdaxiang 已提交
23 24 25 26 27 28 29 30
void HogwildWorker::Initialize(const TrainerDesc& desc) {
  fetch_var_names_.resize(desc.fetch_var_names_size());
  for (size_t i = 0; i < desc.fetch_var_names_size(); ++i) {
    fetch_var_names_[i] = desc.fetch_var_names(i);
  }
  batch_cnt_per_print_ = static_cast<int>(desc.batch_per_print());
}

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
void HogwildWorker::CreateThreadOperators(const ProgramDesc& program) {
  auto& block = program.Block(0);
  op_names_.clear();
  for (auto& op_desc : block.AllOps()) {
    std::unique_ptr<OperatorBase> local_op = OpRegistry::CreateOp(*op_desc);
    op_names_.push_back(op_desc->Type());
    OperatorBase* local_op_ptr = local_op.release();
    ops_.push_back(local_op_ptr);
    continue;
  }
}

void HogwildWorker::CreateThreadScope(const ProgramDesc& program) {
  auto& block = program.Block(0);

  PADDLE_ENFORCE_NOT_NULL(
      root_scope_, "root_scope should be set before creating thread scope");

  thread_scope_ = &root_scope_->NewScope();
  for (auto& var : block.AllVars()) {
    if (var->Persistable()) {
      auto* ptr = root_scope_->Var(var->Name());
      InitializeVariable(ptr, var->GetType());
    } else {
      auto* ptr = thread_scope_->Var(var->Name());
      InitializeVariable(ptr, var->GetType());
    }
  }
}

void HogwildWorker::BindingDataFeedMemory() {
  const std::vector<std::string>& input_feed =
63
      device_reader_->GetUseSlotAlias();
64
  for (auto name : input_feed) {
65
    device_reader_->AddFeedVar(thread_scope_->Var(name), name);
66 67 68 69 70 71 72 73 74 75
  }
}

void HogwildWorker::CreateDeviceResource(const ProgramDesc& main_prog) {
  CreateThreadScope(main_prog);
  CreateThreadOperators(main_prog);
}

void HogwildWorker::TrainFilesWithProfiler() {
  platform::SetNumThreads(1);
76
  device_reader_->Start();
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  std::vector<double> op_total_time;
  std::vector<std::string> op_name;
  for (auto& op : ops_) {
    op_name.push_back(op->Type());
  }
  op_total_time.resize(ops_.size());
  for (size_t i = 0; i < op_total_time.size(); ++i) {
    op_total_time[i] = 0.0;
  }
  platform::Timer timeline;
  double total_time = 0.0;
  double read_time = 0.0;
  int cur_batch;
  int batch_cnt = 0;
  timeline.Start();
D
dongdaxiang 已提交
92
  uint64_t total_inst = 0;
93
  while ((cur_batch = device_reader_->Next()) > 0) {
94
    VLOG(3) << "read a batch in thread " << thread_id_;
95 96 97 98 99
    timeline.Pause();
    read_time += timeline.ElapsedSec();
    total_time += timeline.ElapsedSec();
    for (size_t i = 0; i < ops_.size(); ++i) {
      timeline.Start();
100
      VLOG(3) << "Going to run op " << op_name[i];
101
      ops_[i]->Run(*thread_scope_, place_);
102
      VLOG(3) << "Op " << op_name[i] << " Finished";
103 104 105 106
      timeline.Pause();
      op_total_time[i] += timeline.ElapsedSec();
      total_time += timeline.ElapsedSec();
    }
D
dongdaxiang 已提交
107
    total_inst += cur_batch;
108 109 110 111 112 113 114 115 116
    ++batch_cnt;
    thread_scope_->DropKids();
    if (thread_id_ == 0) {
      if (batch_cnt > 0 && batch_cnt % 100 == 0) {
        for (size_t i = 0; i < ops_.size(); ++i) {
          fprintf(stderr, "op_name:[%zu][%s], op_mean_time:[%fs]\n", i,
                  op_name[i].c_str(), op_total_time[i] / batch_cnt);
        }
        fprintf(stderr, "mean read time: %fs\n", read_time / batch_cnt);
D
dongdaxiang 已提交
117
        fprintf(stderr, "IO percent: %f\n", read_time / total_time * 100);
D
dongdaxiang 已提交
118
        fprintf(stderr, "%6.2f instances/s\n", total_inst / total_time);
119 120 121 122 123 124 125 126 127 128
      }
    }
    timeline.Start();
  }
}

void HogwildWorker::TrainFiles() {
  platform::SetNumThreads(1);

  // how to accumulate fetched values here
129
  device_reader_->Start();
130 131
  int cur_batch;
  int batch_cnt = 0;
132
  while ((cur_batch = device_reader_->Next()) > 0) {
133 134 135 136 137 138 139 140 141
    for (auto& op : ops_) {
      op->Run(*thread_scope_, place_);
    }

    ++batch_cnt;
    thread_scope_->DropKids();
  }
}

D
dongdaxiang 已提交
142 143 144 145 146 147 148 149 150 151 152
void HogwildWorker::PrintFetchVars(int batch_cnt) {
  if (thread_id_ == 0) {
    if (batch_cnt > 0 && batch_cnt % batch_cnt_per_print_ == 0) {
      int fetch_var_num = fetch_var_names_.size();
      for (int i = 0; i < fetch_var_num; ++i) {
        platform::PrintVar(thread_scope_, fetch_var_names_[i], "None");
      }
    }
  }
}

153 154
}  // end namespace framework
}  // end namespace paddle