Trainer.cpp 5.1 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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 "PaddleAPI.h"
E
emailweixu 已提交
16
#include "PaddleAPIPrivate.h"
Z
zhangjinchao01 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

#include <stdlib.h>
#include <memory>
#include <atomic>

#include "paddle/trainer/ParamUtil.h"
#include "paddle/trainer/Trainer.h"
#include "paddle/gserver/gradientmachines/NeuralNetwork.h"
#include "paddle/trainer/TrainerInternal.h"
#include "paddle/utils/Flags.h"

using paddle::real;

P_DECLARE_string(config);
P_DECLARE_string(init_model_path);
P_DECLARE_int32(start_pass);

struct TrainerPrivate : public paddle::Trainer {
E
emailweixu 已提交
35 36 37 38 39 40 41 42 43
  bool _trainOneBatch(size_t batchSize);
  bool forwardOneBatch(size_t batchSize);
  void forwardOneDataBatch(const std::vector<paddle::Argument>& inArgs);
  void setBatchSize(size_t batchSize);
  std::vector<paddle::Argument>& getForwardOutput();

  void startTestPeriod();
  void finishTestPeriod();
  void testOneDataBatch(const paddle::DataBatch& dataBatch);
Z
zhangjinchao01 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  TrainerPrivate() : paddle::Trainer() {}
};

Trainer::Trainer() : m(new TrainerPrivate()) {
  auto conf = paddle::TrainerConfigHelper::createFromFlags();
  if (conf != nullptr) {
    m->init(conf);
  }
}

Trainer::~Trainer() { delete m; }

Trainer* Trainer::createByCommandLine() throw(IOError) {
  auto retv = new Trainer();
  if (retv->m->getConfig().IsInitialized()) {
    return retv;
  } else {
    throw IOError();
  }
}

E
emailweixu 已提交
65 66 67 68
Trainer::Trainer(TrainerConfig* config, GradientMachine* gm)
    : m(new TrainerPrivate()) {
  m->init(config->m->conf, /* testing= */false, gm ? gm->m->machine : nullptr);
}
Z
zhangjinchao01 已提交
69

E
emailweixu 已提交
70 71 72 73 74 75 76 77 78 79
Trainer* Trainer::create(TrainerConfig* config, GradientMachine* gm)
    throw(IOError)
{
  auto retv = new Trainer(config, gm);
  if (retv->m->getConfig().IsInitialized()) {
    return retv;
  } else {
    retv->m->getConfig().CheckInitialized();
    throw IOError();
  }
Z
zhangjinchao01 已提交
80 81
}

E
emailweixu 已提交
82
void Trainer::startTrain() { m->startTrain(); }
Z
zhangjinchao01 已提交
83

E
emailweixu 已提交
84
void Trainer::finishTrain() { m->finishTrain(); }
Z
zhangjinchao01 已提交
85 86 87 88 89

void Trainer::startTrainPass() { m->startTrainPass(); }

void Trainer::finishTrainPass() { m->finishTrainPass(); }

E
emailweixu 已提交
90 91 92 93 94
void Trainer::trainOneDataBatch(size_t batchSize, const Arguments& inArgs) {
  paddle::DataBatch dataBatch;
  dataBatch.getStreams() = inArgs.m->outputs;
  dataBatch.setSize(batchSize);
  m->trainOneDataBatch(dataBatch);
Z
zhangjinchao01 已提交
95 96
}

E
emailweixu 已提交
97 98
bool Trainer::trainOneBatch(size_t batchSize) {
  return m->_trainOneBatch(batchSize);
Z
zhangjinchao01 已提交
99 100
}

E
emailweixu 已提交
101 102 103 104 105 106
bool TrainerPrivate::_trainOneBatch(size_t batchSize) {
  paddle::DataBatch dataBatch;
  CHECK(dataProvider_) << "data_provider is not specified";
  int num = dataProvider_->getNextBatch(batchSize, &dataBatch);
  if (num == 0) {
    return false;
Z
zhangjinchao01 已提交
107
  }
E
emailweixu 已提交
108 109
  trainOneDataBatch(dataBatch);
  return false;
Z
zhangjinchao01 已提交
110 111
}

E
emailweixu 已提交
112 113 114
void TrainerPrivate::startTestPeriod() {
  if (!tester_) {
    createTester();
Z
zhangjinchao01 已提交
115
  }
E
emailweixu 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129
  tester_->startTestPeriod();
}

void Trainer::startTestPeriod() { m->startTestPeriod(); }

void TrainerPrivate::testOneDataBatch(const paddle::DataBatch& dataBatch) {
  tester_->testOneDataBatch(dataBatch, &forwardOutput_);
}

void Trainer::testOneDataBatch(size_t batchSize, const Arguments& args) {
  paddle::DataBatch dataBatch;
  dataBatch.getStreams() = args.m->outputs;
  dataBatch.setSize(batchSize);
  m->testOneDataBatch(dataBatch);
Z
zhangjinchao01 已提交
130 131
}

E
emailweixu 已提交
132 133 134
void TrainerPrivate::finishTestPeriod() { tester_->finishTestPeriod(); }
void Trainer::finishTestPeriod() { m->finishTestPeriod(); }

Z
zhangjinchao01 已提交
135 136 137 138 139 140 141 142
Matrix* Trainer::getLayerOutput(const std::string& layerName) {
  auto nn = std::dynamic_pointer_cast<paddle::NeuralNetwork>(
          this->m->getGradientMachine());
  CHECK(nn) << "trainerInternal_.getGradientMachine() is not NeuralNetwork";
  auto m = nn->getLayerOutput(layerName);
  return Matrix::createByPaddleMatrixPtr(&m);
}

E
emailweixu 已提交
143 144 145 146 147 148 149 150
void Trainer::forwardOneBatch(size_t batchSize) { m->forwardOneBatch(batchSize); }

bool TrainerPrivate::forwardOneBatch(size_t batchSize)  {
  CHECK(dataProvider_) << "data_provider is not specified";
  paddle::DataBatch dataBatch;
  int num = dataProvider_->getNextBatch(batchSize, &dataBatch);
  if (num == 0) {
    return false;
Z
zhangjinchao01 已提交
151 152
  }

E
emailweixu 已提交
153 154
  forwardOneDataBatch(dataBatch.getStreams());
  return true;
Z
zhangjinchao01 已提交
155 156
}

E
emailweixu 已提交
157 158
void TrainerPrivate::forwardOneDataBatch(
    const std::vector<paddle::Argument>& inArgs) {
Z
zhangjinchao01 已提交
159

E
emailweixu 已提交
160
  std::vector<paddle::Argument>& outArgs = forwardOutput_;
Z
zhangjinchao01 已提交
161 162

  if (config_->getOptConfig().use_sparse_remote_updater()) {
E
emailweixu 已提交
163 164
    trainerInternal_.getGradientMachine()->prefetch(inArgs);
    trainerInternal_.getParameterUpdater()->getParametersRemote();
Z
zhangjinchao01 已提交
165
  }
E
emailweixu 已提交
166 167 168 169 170 171
  trainerInternal_.getGradientMachine()->forward(
      inArgs, &outArgs, paddle::PASS_TEST);
}

Arguments* Trainer::getForwardOutput() {
  return Arguments::createByPaddleArgumentVector(&m->getForwardOutput());
Z
zhangjinchao01 已提交
172 173
}

E
emailweixu 已提交
174 175
std::vector<paddle::Argument>& TrainerPrivate::getForwardOutput() {
  return forwardOutput_;
Z
zhangjinchao01 已提交
176
}