GradientMachineMode.h 4.5 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 15 16 17 18 19 20 21

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

#pragma once
#include "GradientMachine.h"
#include "unordered_map"

namespace paddle {

class IGradientMachineMode {
W
Wu Yi 已提交
22
 public:
Z
zhangjinchao01 已提交
23 24
  virtual ~IGradientMachineMode() {}

W
Wu Yi 已提交
25 26 27 28 29
 public:  // interfaces
          /**
           * @brief create current mode's gradient machine by model config.
           * @param config model config
           */
Z
zhangjinchao01 已提交
30 31 32 33 34 35 36 37 38 39
  virtual GradientMachine* create(const ModelConfig& config) = 0;

  /**
   * @brief shouldBeMe the current mode of GradientMachine should be this mode.
   * @param algo training algorithm name.
   * @param trainerCount trainer count.
   * @param isLocal is local mode (without pserver)
   * @param isGpu is using gpu.
   * @return true if mode should be this mode.
   */
40 41 42 43
  virtual bool shouldBeMe(const std::string& algo,
                          size_t trainerCount,
                          bool isLocal,
                          bool isGpu) const = 0;
Z
zhangjinchao01 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57

  /**
   * @brief Is data must be in cpu even if using gpu mode.
   * @param trainerCount trainer count
   * @return true if data must be gpu.
   */
  virtual bool isDataMustInCpu(size_t trainerCount) const = 0;

  /**
   * @brief Need not to use mini-batch method, and should train all data in one
   * batch in one pass.
   */
  virtual bool needTrainWholeDataInOneBatch() const = 0;

W
Wu Yi 已提交
58 59 60 61 62 63 64 65
 public:  // static methods.
          /**
           * @brief register a custom gradient machine mode.
           * @note For user to register a custom gradient machine mode, id should >=
           * kCustom.
           * @param mode mode id.
           * @param ptr mode description object.
           */
Z
zhangjinchao01 已提交
66 67 68 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
  static void regGradientMachineMode(
      int32_t mode, std::unique_ptr<IGradientMachineMode>&& ptr) {
    modes_.insert(std::make_pair(mode, std::move(ptr)));
  }

  /**
   * @brief get custom mode from mode id.
   * @param mode mode id
   * @return mode description object.
   */
  static IGradientMachineMode* mode(int32_t mode) {
    if (modes_.find(mode) != modes_.end()) {
      return modes_[mode].get();
    } else {
      return nullptr;
    }
  }

  /**
   * @brief helper function to test trainWholeDataInOneBatch or not for mode
   */
  static bool trainWholeDataInOneBatch(int32_t mode) {
    if (modes_.find(mode) != modes_.end()) {
      return modes_[mode]->needTrainWholeDataInOneBatch();
    } else {
      return false;
    }
  }

  /**
   * @brief Try to get custom mode if we can.
   * @param [out] mode the custom mode id.
   * @param [in] algo algorithm name
   * @param [in] trainerCount trainer count.
   * @param [in] isLocal is local or not
   * @param [in] isGpu using gpu or not.
   * @return true if there is a custom mode fit these conditions.
   */
104 105
  static bool tryGetMode(int* mode,
                         const std::string& algo,
Z
zhangjinchao01 已提交
106
                         int32_t trainerCount,
107 108
                         bool isLocal,
                         bool isGpu) {
Z
zhangjinchao01 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    for (auto it = modes_.begin(); it != modes_.end(); ++it) {
      if (it->second->shouldBeMe(algo, trainerCount, isLocal, isGpu)) {
        *mode = it->first;
        return true;
      }
    }
    return false;
  }

  /**
   * @brief helper function for data must in cpu
   */
  static bool dataMustInCpu(int32_t mode, size_t trainerCount) {
    if (modes_.find(mode) != modes_.end()) {
      return modes_[mode]->isDataMustInCpu(trainerCount);
    } else {
      // provide data to cpu if using synchronized multi-gpu gradient machine.
      return trainerCount > 1;
    }
  }

  /**
   * @brief try to create gradient machine by mode & config.
   * @return nullptr if we cannot create a gradient machine by such mode.
   */
134 135
  static GradientMachine* tryCreateGradientMachine(int32_t mode,
                                                   const ModelConfig& config) {
Z
zhangjinchao01 已提交
136 137 138 139 140 141 142 143
    auto m = IGradientMachineMode::mode(mode);
    if (m) {
      return m->create(config);
    } else {
      return nullptr;
    }
  }

W
Wu Yi 已提交
144
 private:
Z
zhangjinchao01 已提交
145
  static std::unordered_map<int32_t, std::unique_ptr<IGradientMachineMode>>
146
      modes_;
Z
zhangjinchao01 已提交
147 148 149
};

}  // namespace paddle