program.cc 2.3 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 60 61 62 63 64 65 66
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// 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/fluid/lite/core/program.h"
#include "paddle/fluid/lite/core/optimizer.h"

namespace paddle {
namespace lite {

void RuntimeProgram::PersistModel(const std::string &dir,
                                  const framework::proto::ProgramDesc &desc) {
  // Persist model.
  const std::string model_path = dir + "/__model__";
  std::ofstream model_ostream(model_path, std::ios_base::binary);
  CHECK(model_ostream.is_open());
  const std::string pb_str = SerializeProgram(desc);
  model_ostream.write(pb_str.c_str(), pb_str.size());
  model_ostream.close();

  // Persist params.
  framework::proto::ProgramDesc latest_program;
  latest_program.ParseFromString(pb_str);
  SaveParams(dir, latest_program);
}

std::string RuntimeProgram::SerializeProgram(
    const framework::proto::ProgramDesc &desc) {
  auto program_dummy = desc;
  program_dummy.mutable_blocks(0)->clear_ops();
  for (auto &node : instructions_) {
    pb::OpDesc pb_desc;
    TransformOpDescCppToPb(*node.op()->op_info(), &pb_desc);
    pb_desc.SetAttr(kKernelTypeAttr, node.kernel()->SerializedKernelType());
    // append new opdesc
    *program_dummy.mutable_blocks(0)->add_ops() = *pb_desc.Proto();
  }
  return program_dummy.SerializeAsString();
}

void RuntimeProgram::SaveParams(const std::string &dir,
                                const framework::proto::ProgramDesc &desc) {
  CHECK(exec_scope_);
  for (auto &item : desc.blocks(0).vars()) {
    const std::string path = dir + "/" + item.name();
    if (item.name() == "feed" || item.name() == "fetch") continue;
    if (item.persistable()) {
      std::ofstream file(path, std::ios::binary);
      SerializeTensor(file, *exec_scope_, item.name());
      file.close();
    }
  }
}

}  // namespace lite
}  // namespace paddle