TrainerConfigHelper.h 5.4 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 16 17 18

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 <paddle/utils/Logging.h>
#include <paddle/utils/Util.h>
Y
Yu Yang 已提交
19
#include <memory>
Z
zhangjinchao01 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

namespace paddle {

class TrainerConfig;
class OptimizationConfig;
struct TrainerConfigHelperPrivate;
class ModelConfig;
class DataConfig;

/**
 * @brief TrainerConfig Helper. A class wrap protobuf's TrainerConfig Object,
 * simplize the usage for TrainerConfig.
 *
 * The all operation to TrainerConfig object should use this object. It remove
 * many copy & paste code in trainer.
 *
 * @TODO(yuyang18): Make cmake check compiler support keyword 'final' or not.
 * Define a macro to unify 'final' keyword
 */
class TrainerConfigHelper /*final*/ {
W
Wu Yi 已提交
40
 public:
Z
zhangjinchao01 已提交
41 42 43 44 45 46
  DISABLE_COPY(TrainerConfigHelper);

  /**
   * @brief Ctor, Create a TrainerConfig from config file
   * @param configFilePath Config file path.
   */
47
  explicit TrainerConfigHelper(const std::string& configFilePath);
Z
zhangjinchao01 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  explicit TrainerConfigHelper(const TrainerConfig& config);

  /**
   * Dtor
   * @warning this class is a final class. Should not be inherited.
   */
  ~TrainerConfigHelper();

  /**
   * @brief Get Trainer Config itself.
   */
  const TrainerConfig& getConfig() const;

  TrainerConfig& getMutableConfig();

  /**
   * @brief Get Optimizer Config.
   */
  const OptimizationConfig& getOptConfig() const;

  /**
   * @brief Get Model Config.
   */
  const ModelConfig& getModelConfig() const;

  /**
   * @brief Get Train Data Config Pointer.
   * @return nullptr if there is no train data. Else will return pointer
   */
  const DataConfig* getDataConfigPtr() const;

  /**
   * @brief Get Tain Data Config.
   * @warning Core when there is no train data.
   */
  const DataConfig& getDataConfig() const {
    CHECK(this->hasDataConfig());
    auto conf = this->getDataConfigPtr();
    return *conf;
  }

  /**
   * @brief Get test data config
   * @warning Core when there is no test data.
   */
  const DataConfig& getTestDataConfig() const;

  /**
   * @brief Has train data config or not.
   * @return true if has train data.
   */
  bool hasDataConfig() const;

  /**
   * @brief Has test data config or not.
   * @return true if has test data.
   */
  bool hasTestDataConfig() const;

  /**
   * @brief Update trainer config from command line flags.
   *        Override config's (save_dir, init_model_path, start_pass) if command
   *        flags is existed.
   */
  void updateConfigFromFlags();

  /**
   * @brief Disable optimization's sparse remote update.
   */
  void disableRemoteSparseUpdater();

  /**
   * @brief Disable optimization and each parameter's sparse remote update.
   */
  void disableRemoteSparseUpdaterForEachParams();

  /**
   * @brief implicit conversion.
   */
127
  inline operator const TrainerConfig&() const { return this->getConfig(); }
Z
zhangjinchao01 已提交
128 129 130 131 132 133 134 135 136 137 138

  /**
   * @brief implicit conversion.
   */
  inline operator const OptimizationConfig&() const {
    return this->getOptConfig();
  }

  /**
   * @brief implicit conversion.
   */
139
  inline operator const DataConfig&() const { return this->getDataConfig(); }
Z
zhangjinchao01 已提交
140 141 142 143

  /**
   * @brief implicit conversion.
   */
144
  inline operator const ModelConfig&() const { return this->getModelConfig(); }
Z
zhangjinchao01 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

  /**
   * @brief Get mutable optimization config.
   */
  OptimizationConfig& getOptConfig();

  /**
   * @brief set model save directory.
   * @param saveDir Directory path.
   */
  void setSaveDir(const std::string& saveDir);

  /**
   * @brief get model save directory.
   * @return save directory path.
   */
  const std::string& getSaveDir() const;

  /**
   * @brief Get config file name from model path.
   *
   * Paddle save model to a directory, and write a file 'path.txt' which save
   * config filename.
   *
   * @param modelPath model saved directory.
   * @return config file name.
   */
  static std::string getConfigNameFromPath(const std::string& modelPath);

  /**
   * @brief Get config file name from this config instance.
   * @param[out] ok true if no error.
   * @return config file name.
   */
  std::string getConfigName(bool* ok = nullptr) const;

  /**
   * @brief Try to create TrainerConfigHelper from all command line flags.
   *        Try to load from --config, --init_model_path, --start_pass one by
   *        one. Return nullptr if cannot load TrainerConfigHelper from all
   *        these place.
   * @return nullptr if cannot load, otherwise return a TrainerConfigHelper.
   */
  static std::shared_ptr<TrainerConfigHelper> createFromFlags();

  /**
   * @brief Try to create TrainerConfigHelper only from '--config' flag.
   * @return nullptr if cannot load, otherwise return a TrainerConfigHelper.
   */
  static std::shared_ptr<TrainerConfigHelper> createFromFlagConfig();

W
Wu Yi 已提交
196
 private:
Z
zhangjinchao01 已提交
197 198 199 200 201 202 203 204 205
  static std::string getConfigNameFromPassId(int passId,
                                             const std::string& modelPath);

  TrainerConfigHelperPrivate* m;
};

typedef std::shared_ptr<TrainerConfigHelper> TrainerConfigHelperPtr;

}  // namespace paddle