io.cc 4.5 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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
Yi Wang 已提交
15
#include "paddle/fluid/inference/io.h"
16

17
#include <fstream>
Y
Yi Wang 已提交
18 19
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/feed_fetch_type.h"
20 21
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/pybind/pybind.h"
22 23

namespace paddle {
24
namespace inference {
25

26 27 28 29
// Temporarilly add this function for exposing framework::InitDevices() when
// linking the inference shared library.
void Init(bool init_p2p) { framework::InitDevices(init_p2p); }

30
void ReadBinaryFile(const std::string& filename, std::string& contents) {
31 32 33
  std::ifstream fin(filename, std::ios::in | std::ios::binary);
  PADDLE_ENFORCE(static_cast<bool>(fin), "Cannot open file %s", filename);
  fin.seekg(0, std::ios::end);
34
  contents.clear();
35 36 37 38
  contents.resize(fin.tellg());
  fin.seekg(0, std::ios::beg);
  fin.read(&contents[0], contents.size());
  fin.close();
39 40
}

L
Liu Yiqun 已提交
41 42
bool IsPersistable(const framework::VarDesc* var) {
  if (var->Persistable() &&
43 44
      var->GetType() != framework::proto::VarType::FEED_MINIBATCH &&
      var->GetType() != framework::proto::VarType::FETCH_LIST) {
L
Liu Yiqun 已提交
45
    return true;
46 47 48 49
  }
  return false;
}

50
void LoadPersistables(framework::Executor& executor, framework::Scope& scope,
51
                      const framework::ProgramDesc& main_program,
52
                      const std::string& dirname,
53
                      const std::string& param_filename) {
K
kexinzhao 已提交
54
  const framework::BlockDesc& global_block = main_program.Block(0);
55

56 57
  framework::ProgramDesc* load_program = new framework::ProgramDesc();
  framework::BlockDesc* load_block = load_program->MutableBlock(0);
58 59
  std::vector<std::string> paramlist;

K
kexinzhao 已提交
60
  for (auto* var : global_block.AllVars()) {
L
Liu Yiqun 已提交
61 62
    if (IsPersistable(var)) {
      VLOG(3) << "persistable variable's name: " << var->Name();
63 64

      framework::VarDesc* new_var = load_block->Var(var->Name());
F
fengjiayi 已提交
65
      new_var->SetShape(var->GetShape());
66 67 68 69 70
      new_var->SetDataType(var->GetDataType());
      new_var->SetType(var->GetType());
      new_var->SetLoDLevel(var->GetLoDLevel());
      new_var->SetPersistable(true);

71 72 73 74 75 76 77 78 79 80
      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();
      }
81 82
    }
  }
83 84 85 86 87 88 89 90 91 92 93 94

  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();
  }

95
  executor.Run(*load_program, &scope, 0, true, true);
96

97
  delete load_program;
98
}
99

K
kexinzhao 已提交
100 101 102
std::unique_ptr<framework::ProgramDesc> Load(framework::Executor& executor,
                                             framework::Scope& scope,
                                             const std::string& dirname) {
103 104
  std::string model_filename = dirname + "/__model__";
  std::string program_desc_str;
105
  VLOG(3) << "loading model from " << model_filename;
106 107 108 109 110 111 112 113 114 115
  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(
116 117
    framework::Executor& executor, framework::Scope& scope,
    const std::string& prog_filename, const std::string& param_filename) {
118 119 120
  std::string model_filename = prog_filename;
  std::string program_desc_str;
  ReadBinaryFile(model_filename, program_desc_str);
121

K
kexinzhao 已提交
122 123
  std::unique_ptr<framework::ProgramDesc> main_program(
      new framework::ProgramDesc(program_desc_str));
124

125
  LoadPersistables(executor, scope, *main_program, "", param_filename);
126 127 128 129
  return main_program;
}

}  // namespace inference
130
}  // namespace paddle