io.cc 4.6 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
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. */

15
#include "paddle/inference/io.h"
16

17
#include <fstream>
18 19
#include "paddle/framework/block_desc.h"
#include "paddle/framework/feed_fetch_type.h"
20 21

namespace paddle {
22
namespace inference {
23

24 25 26 27 28 29 30 31 32 33 34
void ReadBinaryFile(const std::string& filename, std::string& contents) {
  VLOG(3) << "loading model from " << filename;
  std::ifstream inputfs(filename, std::ios::in | std::ios::binary);
  inputfs.seekg(0, std::ios::end);
  contents.clear();
  contents.resize(inputfs.tellg());
  inputfs.seekg(0, std::ios::beg);
  inputfs.read(&contents[0], contents.size());
  inputfs.close();
}

35
bool IsParameter(const framework::VarDesc* var,
K
kexinzhao 已提交
36
                 const framework::ProgramDesc& main_program) {
37
  if (var->Persistable()) {
38
    // There are many unreachable variables in the program
K
kexinzhao 已提交
39 40
    for (size_t i = 0; i < main_program.Size(); ++i) {
      const framework::BlockDesc& block = main_program.Block(i);
41
      for (auto* op : block.AllOps()) {
42
        if (op->Type() == framework::kFeedOpType) {
43 44
          continue;
        }
45 46 47 48 49 50 51 52 53 54 55
        for (auto input_argument_name : op->InputArgumentNames()) {
          if (input_argument_name == var->Name()) {
            return true;
          }
        }
      }
    }
  }
  return false;
}

56 57
void LoadPersistables(framework::Executor& executor,
                      framework::Scope& scope,
58
                      const framework::ProgramDesc& main_program,
59
                      const std::string& dirname,
60
                      const std::string& param_filename) {
K
kexinzhao 已提交
61
  const framework::BlockDesc& global_block = main_program.Block(0);
62

63 64
  framework::ProgramDesc* load_program = new framework::ProgramDesc();
  framework::BlockDesc* load_block = load_program->MutableBlock(0);
65 66
  std::vector<std::string> paramlist;

K
kexinzhao 已提交
67
  for (auto* var : global_block.AllVars()) {
68
    if (IsParameter(var, main_program)) {
L
Liu Yiqun 已提交
69
      VLOG(3) << "parameter's name: " << var->Name();
70 71

      framework::VarDesc* new_var = load_block->Var(var->Name());
F
fengjiayi 已提交
72
      new_var->SetShape(var->GetShape());
73 74 75 76 77
      new_var->SetDataType(var->GetDataType());
      new_var->SetType(var->GetType());
      new_var->SetLoDLevel(var->GetLoDLevel());
      new_var->SetPersistable(true);

78 79 80 81 82 83 84 85 86 87
      if (!param_filename.empty()) {
        paramlist.push_back(new_var->Name());
      } else {
        // append_op
        framework::OpDesc* op = load_block->AppendOp();
        op->SetType("load");
        op->SetOutput("Out", {new_var->Name()});
        op->SetAttr("file_path", {dirname + "/" + new_var->Name()});
        op->CheckAttrs();
      }
88 89
    }
  }
90 91 92 93 94 95 96 97 98 99 100 101

  if (!param_filename.empty()) {
    // sort paramlist to have consistent ordering
    std::sort(paramlist.begin(), paramlist.end());
    // append just the load_combine op
    framework::OpDesc* op = load_block->AppendOp();
    op->SetType("load_combine");
    op->SetOutput("Out", paramlist);
    op->SetAttr("file_path", {param_filename});
    op->CheckAttrs();
  }

102
  executor.Run(*load_program, &scope, 0, true, true);
103 104

  VLOG(3) << "Ran loading successfully";
105
  delete load_program;
106
}
107

K
kexinzhao 已提交
108 109 110
std::unique_ptr<framework::ProgramDesc> Load(framework::Executor& executor,
                                             framework::Scope& scope,
                                             const std::string& dirname) {
111 112
  std::string model_filename = dirname + "/__model__";
  std::string program_desc_str;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  ReadBinaryFile(model_filename, program_desc_str);

  std::unique_ptr<framework::ProgramDesc> main_program(
      new framework::ProgramDesc(program_desc_str));

  LoadPersistables(executor, scope, *main_program, dirname, "");
  return main_program;
}

std::unique_ptr<framework::ProgramDesc> Load(
    framework::Executor& executor,
    framework::Scope& scope,
    const std::string& prog_filename,
    const std::string& param_filename) {
  std::string model_filename = prog_filename;
  std::string program_desc_str;
  ReadBinaryFile(model_filename, program_desc_str);
130

K
kexinzhao 已提交
131 132
  std::unique_ptr<framework::ProgramDesc> main_program(
      new framework::ProgramDesc(program_desc_str));
133

134
  LoadPersistables(executor, scope, *main_program, "", param_filename);
135 136 137 138
  return main_program;
}

}  // namespace inference
139
}  // namespace paddle