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

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. */

Y
Yu Yang 已提交
15
#include <algorithm>
X
Xin Pan 已提交
16 17
#include "paddle/legacy/utils/Stat.h"
#include "paddle/legacy/utils/Util.h"
Z
zhangjinchao01 已提交
18 19 20 21 22 23 24 25

#include "MultiNetwork.h"

#include "NeuralNetwork.h"
#include "ParallelNeuralNetwork.h"

namespace paddle {

26 27
void MultiNetwork::init(const ModelConfig& config,
                        ParamInitCallback callback,
Z
zhangjinchao01 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
                        const std::vector<ParameterType>& parameterTypes,
                        bool useGpu) {
  CHECK_GT(config.sub_models_size(), 1) << "sub_models_size should GT 1";
  // check submodel[0] is root
  CHECK_EQ("root", config.sub_models(0).name())
      << "sub_models(0) should be root";
  // ignore root
  subNetworks_.resize(config.sub_models_size() - 1);
  // base class
  NeuralNetwork::init(config, callback, parameterTypes, useGpu);
  // sub networks
  for (int i = 1; i < config.sub_models_size(); ++i) {
    std::string subModelName = config.sub_models(i).name();
    if (FLAGS_parallel_nn) {
      subNetworks_[i - 1] = std::unique_ptr<ParallelNeuralNetwork>(
43
          new ParallelNeuralNetwork(subModelName, this));
Z
zhangjinchao01 已提交
44 45
    } else {
      subNetworks_[i - 1] = std::unique_ptr<NeuralNetwork>(
46
          NeuralNetwork::newNeuralNetwork(subModelName, this));
Z
zhangjinchao01 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    }
    subNetworks_[i - 1]->init(config);
  }
}

void MultiNetwork::prefetch(const std::vector<Argument>& inArgs) {
  std::vector<std::vector<Argument>> argumentGroups;
  Argument::splitByDataId(inArgs, &argumentGroups);
  // check group size is equal to sub network size
  CHECK_EQ(argumentGroups.size(), subNetworks_.size());
  for (size_t i = 0; i < subNetworks_.size(); i++) {
    if (argumentGroups[i].size() == 1 && argumentGroups[i][0].dataId == -1) {
      // check input args: if dataId is -1, then skip this sub network
      continue;
    }
    subNetworks_[i]->prefetch(argumentGroups[i]);
  }
}

void MultiNetwork::forward(const std::vector<Argument>& inArgs,
67 68
                           std::vector<Argument>* outArgs,
                           PassType passType) {
Z
zhangjinchao01 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  // split inArgs to several vectors
  std::vector<std::vector<Argument>> argumentGroups;
  Argument::splitByDataId(inArgs, &argumentGroups);

  // check group size is equal to sub network size
  CHECK_EQ(argumentGroups.size(), subNetworks_.size());
  std::vector<Argument> tempOutArgs;
  outArgs->clear();

  for (size_t i = 0; i < subNetworks_.size(); i++) {
    tempOutArgs.clear();
    if (argumentGroups[i].size() == 1 && argumentGroups[i][0].dataId == -1) {
      // check input args: if dataId is -1, then skip this sub network
      continue;
    }
    subNetworks_[i]->forward(argumentGroups[i], &tempOutArgs, passType);
    for (const auto& elem : tempOutArgs) {
      outArgs->push_back(elem);
      outArgs->back().dataId = i;
    }
  }
}

void MultiNetwork::backward(const UpdateCallback& callback) {
  for (size_t i = 0; i < subNetworks_.size(); i++) {
    subNetworks_[i]->backward(callback);
  }
}

void MultiNetwork::forwardBackward(const std::vector<Argument>& inArgs,
                                   std::vector<Argument>* outArgs,
                                   PassType passType,
                                   const UpdateCallback& callback) {
  forward(inArgs, outArgs, passType);
  backward(callback);
}

void MultiNetwork::onPassEnd() {
  for (size_t i = 0; i < subNetworks_.size(); i++) {
    subNetworks_[i]->onPassEnd();
  }
}

112
void MultiNetwork::start() {
Z
zhangjinchao01 已提交
113
  for (auto& subNetwork : subNetworks_) {
114
    subNetwork->start();
Z
zhangjinchao01 已提交
115 116 117 118 119 120 121 122 123 124
  }
}

void MultiNetwork::finish() {
  for (size_t i = 0; i < subNetworks_.size(); i++) {
    subNetworks_[i]->finish();
  }
}

class MultiCombinedEvaluator : public Evaluator {
W
Wu Yi 已提交
125
 public:
Z
zhangjinchao01 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  MultiCombinedEvaluator() {}
  void addEvaluator(std::unique_ptr<Evaluator>&& evaluator) {
    evaluators_.emplace_back(std::move(evaluator));
  }
  virtual void start() {
    for (auto& evaluator : evaluators_) {
      evaluator->start();
    }
  }

  virtual void finish() {
    for (auto& evaluator : evaluators_) {
      evaluator->finish();
    }
  }

  virtual void eval(const NeuralNetwork& nn) {
    const MultiNetwork& multiNetwork = dynamic_cast<const MultiNetwork&>(nn);
    CHECK_EQ(evaluators_.size(), multiNetwork.getSubNetworks().size());
    int size = evaluators_.size();
    for (int i = 0; i < size; i++) {
      // one evaluator for one subNetwork
      evaluators_[i]->eval(*multiNetwork.getSubNetworks()[i]);
    }
  }

  virtual real evalImp(std::vector<Argument>& arguments) {
    (void)arguments;
    return -1;
  }

Y
Yu Yang 已提交
157
  virtual void printStats(std::ostream& os) const {
Z
zhangjinchao01 已提交
158 159 160 161 162 163 164 165 166 167 168 169
    for (auto& evaluator : evaluators_) {
      evaluator->printStats(os);
      os << ' ';
    }
  }

  virtual void distributeEval(ParameterClient2* client) {
    for (auto& evaluator : evaluators_) {
      evaluator->distributeEval(client);
    }
  }

W
Wu Yi 已提交
170
 protected:
Z
zhangjinchao01 已提交
171 172 173
  std::vector<std::unique_ptr<Evaluator>> evaluators_;
};

Y
Yu Yang 已提交
174
Evaluator* MultiNetwork::makeEvaluator() const {
Z
zhangjinchao01 已提交
175 176 177 178 179 180 181 182
  MultiCombinedEvaluator* multiCombinedEvaluator = new MultiCombinedEvaluator();
  for (size_t i = 0; i < subNetworks_.size(); i++) {
    std::unique_ptr<Evaluator> evaluator(subNetworks_[i]->makeEvaluator());
    multiCombinedEvaluator->addEvaluator(std::move(evaluator));
  }
  return multiCombinedEvaluator;
}

Y
Yu Yang 已提交
183
void MultiNetwork::eval(Evaluator* evaluator) const { evaluator->eval(*this); }
Z
zhangjinchao01 已提交
184 185

}  // namespace paddle