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

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

#include <stdlib.h>
#include <atomic>
Y
Yu Yang 已提交
20
#include <memory>
Z
zhangjinchao01 已提交
21

Y
Yu Yang 已提交
22
#include "paddle/gserver/gradientmachines/NeuralNetwork.h"
Z
zhangjinchao01 已提交
23 24 25 26 27 28 29
#include "paddle/trainer/ParamUtil.h"
#include "paddle/trainer/Trainer.h"
#include "paddle/trainer/TrainerInternal.h"
#include "paddle/utils/Flags.h"

using paddle::real;

30 31 32
DECLARE_string(config);
DECLARE_string(init_model_path);
DECLARE_int32(start_pass);
Z
zhangjinchao01 已提交
33 34

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

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

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

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

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

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

E
emailweixu 已提交
88 89 90 91 92
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 已提交
93 94
}

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

E
emailweixu 已提交
99 100 101 102 103 104
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 已提交
105
  }
E
emailweixu 已提交
106 107
  trainOneDataBatch(dataBatch);
  return false;
Z
zhangjinchao01 已提交
108 109
}

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

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

L
liaogang 已提交
133
Arguments* Trainer::getLayerOutput(const std::string& layerName) const {
L
liaogang 已提交
134
  auto nn = this->m->getGradientMachine();
Z
zhangjinchao01 已提交
135
  CHECK(nn) << "trainerInternal_.getGradientMachine() is not NeuralNetwork";
L
liaogang 已提交
136 137
  auto arg = nn->getLayerOutput(layerName);
  return Arguments::createByPaddleArgument(&arg);
Z
zhangjinchao01 已提交
138 139
}

140 141 142
void Trainer::forwardOneBatch(size_t batchSize) {
  m->forwardOneBatch(batchSize);
}
E
emailweixu 已提交
143

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

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

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

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

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

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