ParamUtil.cpp 4.8 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 19 20

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 "ParamUtil.h"

#include <fenv.h>
#include <stdio.h>

#include <iomanip>
Y
Yu Yang 已提交
21
#include <iostream>
Z
zhangjinchao01 已提交
22
#include <limits>
Y
Yu Yang 已提交
23
#include <sstream>
Z
zhangjinchao01 已提交
24 25 26 27

#include <google/protobuf/text_format.h>
#include <paddle/utils/Version.h>

Y
Yu Yang 已提交
28
#include "paddle/utils/GlobalConstants.h"
Z
zhangjinchao01 已提交
29 30 31 32
#include "paddle/utils/PythonUtil.h"
#include "paddle/utils/Stat.h"
#include "paddle/utils/Util.h"

Y
Yu Yang 已提交
33
#include "TesterConfig.h"
Z
zhangjinchao01 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include "paddle/gserver/gradientmachines/NeuralNetwork.h"
#include "paddle/gserver/layers/ValidationLayer.h"

namespace paddle {

ParameterUtil::ParameterUtil(
    const std::shared_ptr<TrainerConfigHelper> &config,
    std::unique_ptr<ParameterUtilConfig> &&intconfig,
    const GradientMachinePtr &gradientMachine,
    const std::shared_ptr<ParameterUpdater> &parameterUpdater) {
  config_ = config;
  intConfig_ = std::move(intconfig);
  gserver_ = gradientMachine;
  pUpdater_ = parameterUpdater;
}

bool ParameterUtil::loadParameters(int passId, bool local, bool remote) {
  constexpr int kBufLen = 100;
  char buf[kBufLen];
  snprintf(buf, kBufLen, "pass-%05d", passId);
  std::string doneFile = path::join(config_->getSaveDir(), buf, "done");
  if (!fileExist(doneFile.c_str())) return false;
  loadParametersWithPath(path::join(config_->getSaveDir(), buf), local, remote);
  return true;
}

60 61 62
void ParameterUtil::loadParametersWithPath(const std::string &dir,
                                           bool local,
                                           bool remote) {
Z
zhangjinchao01 已提交
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
  if (local) {
    gserver_->loadParameters(dir);
  }
  if (remote && pUpdater_) {
    pUpdater_->loadParametersRemote(dir);
  }
}

void ParameterUtil::saveParametersOnePass(int passId, int passInnerId) {
  pUpdater_->apply();
  saveParameters(passId, passInnerId);
  if (intConfig_->save_only_one_ && passId >= intConfig_->saving_period_) {
    deleteParameters(passId - intConfig_->saving_period_);
  }
  pUpdater_->restore();
}

void ParameterUtil::saveParameters(int passId, int passInnerId) {
  constexpr int kBufLen = 100;
  char buf[kBufLen];
  if (passInnerId > 0) {
    snprintf(buf, kBufLen, "pass-%05d-%03d", passId, passInnerId);
  } else {
    snprintf(buf, kBufLen, "pass-%05d", passId);
  }

  std::string basePath = config_->getSaveDir();
90 91 92
  if (basePath.find('/') == std::string::npos) {
    basePath = "./" + basePath;
  }
Z
zhangjinchao01 已提交
93 94 95 96 97 98
  mkDirRecursively(basePath.c_str());

  std::string saveDir = path::join(basePath, buf);
  mkDir(saveDir.c_str());
  if (!intConfig_->load_save_param_pserver_) {
    pUpdater_->getParametersRemote(true /*full parameter*/,
99
                                   true /*after apply*/);
Z
zhangjinchao01 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  }

  gserver_->saveParameters(saveDir);
  if (intConfig_->load_save_param_pserver_) {
    pUpdater_->saveParametersRemote(saveDir);
  }
  std::string doneFile = path::join(saveDir, "done");
  touchFile(doneFile.c_str());
  std::ofstream out(doneFile);
  version::printVersion(out);
  out.close();
  VLOG(1) << "save dir " << saveDir;
  saveConfigWithPath(saveDir);
}

void ParameterUtil::deleteParameters(int passId, int passInnerId) {
  constexpr int kBufLen = 100;
  char buf[kBufLen];
118
  const std::string &saveDir = config_->getSaveDir();
Z
zhangjinchao01 已提交
119
  if (passInnerId > 0) {
120 121 122 123 124
    snprintf(buf,
             kBufLen,
             "%s/pass-%05d-%03d",
             saveDir.c_str(),
             passId,
Z
zhangjinchao01 已提交
125 126 127 128 129 130 131 132 133
             passInnerId);
  } else {
    snprintf(buf, kBufLen, "%s/pass-%05d", saveDir.c_str(), passId);
  }
  mkDir(saveDir.c_str());
  LOG(INFO) << "delete dir " << buf;
  rmDir(buf);
}

134
void ParameterUtil::saveConfigWithPath(const std::string &path) {
Z
zhangjinchao01 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  std::string src;
  // save config in some path
  if (!intConfig_->config_.empty()) {
    src = intConfig_->config_;
  } else {
    bool ok;
    src = config_->getConfigName(&ok);
    if (!ok) {
      return;
    }
  }
  copyFileToPath(src, path);

  // save other import config file name to path.txt
  std::string ss = path::join(path, "path.txt");
  std::ofstream os(ss);
  std::string fileName = path::basename(src);
  CHECK(os.write(fileName.c_str(), fileName.length()))
      << "Fail to write config file name " << ss;
  VLOG(1) << "fileName " << fileName;
  os.close();

  // copy other import config files
  for (int i = 0; i < config_->getConfig().config_files_size(); ++i) {
    copyFileToPath(config_->getConfig().config_files(i), path);
  }
}

}  // namespace paddle