io.cc 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.

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 "paddle/inference/io.h"
#include <fstream>

namespace paddle {
namespace inference {

const std::string kFeedOpType = "feed";

bool IsParameter(const framework::VarDesc* var,
K
kexinzhao 已提交
24
                 const framework::ProgramDesc& main_program) {
25 26
  if (var->Persistable()) {
    // There are many unreachable variables in the program
K
kexinzhao 已提交
27 28
    for (size_t i = 0; i < main_program.Size(); ++i) {
      const framework::BlockDesc& block = main_program.Block(i);
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
      for (auto* op : block.AllOps()) {
        if (op->Type() == kFeedOpType) {
          continue;
        }
        for (auto input_argument_name : op->InputArgumentNames()) {
          if (input_argument_name == var->Name()) {
            return true;
          }
        }
      }
    }
  }
  return false;
}

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

  framework::ProgramDesc* load_program = new framework::ProgramDesc();
  framework::BlockDesc* load_block = load_program->MutableBlock(0);
K
kexinzhao 已提交
52
  for (auto* var : global_block.AllVars()) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    if (IsParameter(var, main_program)) {
      LOG(INFO) << "parameter's name: " << var->Name();

      framework::VarDesc* new_var = load_block->Var(var->Name());
      new_var->SetShape(var->Shape());
      new_var->SetDataType(var->GetDataType());
      new_var->SetType(var->GetType());
      new_var->SetLoDLevel(var->GetLoDLevel());
      new_var->SetPersistable(true);

      // 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();
    }
  }
  executor.Run(*load_program, &scope, 0, true, true);
  delete load_program;
}

K
kexinzhao 已提交
75 76 77
std::unique_ptr<framework::ProgramDesc> Load(framework::Executor& executor,
                                             framework::Scope& scope,
                                             const std::string& dirname) {
78 79 80 81 82 83 84 85 86 87 88
  std::string model_filename = dirname + "/__model__";
  LOG(INFO) << "loading model from " << model_filename;
  std::ifstream inputfs(model_filename, std::ios::in | std::ios::binary);
  std::string program_desc_str;
  inputfs.seekg(0, std::ios::end);
  program_desc_str.resize(inputfs.tellg());
  inputfs.seekg(0, std::ios::beg);
  LOG(INFO) << "program_desc_str's size: " << program_desc_str.size();
  inputfs.read(&program_desc_str[0], program_desc_str.size());
  inputfs.close();

K
kexinzhao 已提交
89 90
  std::unique_ptr<framework::ProgramDesc> main_program(
      new framework::ProgramDesc(program_desc_str));
91

K
kexinzhao 已提交
92
  LoadPersistables(executor, scope, dirname, *main_program);
93 94 95 96 97
  return main_program;
}

}  // namespace inference
}  // namespace paddle