model_parser.h 5.4 KB
Newer Older
Y
Yan Chunwei 已提交
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) 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.

// This file contains model format related operations, such as load a model,
// parse an operator definitions and so on.

#pragma once
#include <memory>
#include <string>
#include <vector>
#ifndef LITE_ON_TINY_PUBLISH
#include "lite/core/framework.pb.h"
24
#include "lite/model_parser/naive_buffer/proto/framework.nb.h"
Y
Yan Chunwei 已提交
25 26 27 28 29 30 31 32 33 34 35
#endif
#include "lite/core/scope.h"
#include "lite/core/variable.h"
#include "lite/model_parser/compatible_pb.h"

namespace paddle {
namespace lite {

#ifndef LITE_ON_TINY_PUBLISH
// Read a __model__ file.
std::unique_ptr<framework::proto::ProgramDesc> LoadProgram(
36
    const std::string& path, bool program_from_memory = false);
Y
Yan Chunwei 已提交
37

38 39 40 41 42 43 44 45 46 47
template <typename T>
void ReadModelDataFromFile(T* data,
                           const std::string& prog_path,
                           uint64_t* offset,
                           const uint64_t& size);

void AppendToFile(const std::string& filename,
                  const void* src,
                  size_t byte_size);

Y
Yan Chunwei 已提交
48 49 50 51 52 53
// Read a single file containing all the parameters.
void LoadParams(const std::string& path);

// Load a single parameter to an output tensor.
void LoadParam(const std::string& path, Variable* out);

54 55
void LoadCombinedParamsPb(const std::string& path,
                          lite::Scope* scope,
56 57
                          const cpp::ProgramDesc& prog,
                          bool params_from_memory = false);
58

Y
Yan Chunwei 已提交
59 60
// Read a model and files of parameters in pb format.
void LoadModelPb(const std::string& model_dir,
61 62
                 const std::string& model_file,
                 const std::string& param_file,
Y
Yan Chunwei 已提交
63
                 Scope* scope,
64
                 cpp::ProgramDesc* prog,
65 66
                 bool combined = false,
                 bool model_from_memory = false);
Y
Yan Chunwei 已提交
67 68 69 70

// Save a model and files of parameters in pb format.
void SaveModelPb(const std::string& model_dir,
                 const Scope& scope,
71 72 73 74 75 76
                 const cpp::ProgramDesc& prog,
                 bool combined = false);

void SaveCombinedParamsPb(const std::string& path,
                          const lite::Scope& exec_scope,
                          const cpp::ProgramDesc& prog);
Y
Yan Chunwei 已提交
77 78 79 80 81 82 83 84

// Serialize tensors to ostream.
void SerializeTensor(std::ostream& os,
                     const lite::Scope& scope,
                     const std::string& var);

// LoDTensor to ostream
void TensorToStream(std::ostream& os, const lite::Tensor& tensor);
85
void TensorFromStream(std::istream& is, lite::Tensor* tensor);
Y
Yan Chunwei 已提交
86 87 88 89 90 91 92
void ReadBinaryFile(const std::string& filename, std::string* contents);

// For naive buffer
void SaveParamNaive(const std::string& path,
                    const lite::Scope& exec_scope,
                    const std::string& var_name);

Y
Yan Chunwei 已提交
93 94 95 96
void SaveCombinedParamsNaive(const std::string& path,
                             const lite::Scope& exec_scope,
                             const cpp::ProgramDesc& cpp_prog);

Y
Yan Chunwei 已提交
97 98
void SaveModelNaive(const std::string& model_dir,
                    const Scope& exec_scope,
99
                    const cpp::ProgramDesc& cpp_prog);
100 101 102 103

void SaveModelFbs(const std::string& model_dir,
                  const Scope& exec_scope,
                  const cpp::ProgramDesc& cpp_prog);
Y
Yan Chunwei 已提交
104 105 106 107

void LoadParamNaive(const std::string& path,
                    lite::Scope* scope,
                    const std::string& name);
108 109
// warning:this old inference will be abandened in release/v3.0.0
// and LoadModelNaiveFromFile is suggested.
Y
Yan Chunwei 已提交
110 111
void LoadModelNaive(const std::string& model_dir,
                    lite::Scope* scope,
Y
Yan Chunwei 已提交
112 113
                    cpp::ProgramDesc* prog,
                    bool combined = true);
114 115 116
void LoadModelNaiveV0FromFile(const std::string& filename,
                              Scope* scope,
                              cpp::ProgramDesc* cpp_prog);
117 118 119 120
void LoadModelNaiveFromMemory(const std::string& model_buffer,
                              const std::string& param_buffer,
                              lite::Scope* scope,
                              cpp::ProgramDesc* cpp_prog);
121
void LoadModelNaiveV0FromMemory(const std::string& model_buffer,
122 123
                                Scope* scope,
                                cpp::ProgramDesc* cpp_prog);
124 125 126 127
#endif  // LITE_ON_TINY_PUBLISH
void LoadModelFbsFromFile(const std::string& filename,
                          Scope* scope,
                          cpp::ProgramDesc* cpp_prog);
128

129
void LoadModelNaiveFromFile(const std::string& filename,
130
                            lite::Scope* scope,
131 132 133 134 135 136
                            cpp::ProgramDesc* prog);

void LoadModelNaiveFromMemory(const std::string& model_buffer,
                              lite::Scope* scope,
                              cpp::ProgramDesc* cpp_prog);
void LoadModelNaiveV1FromMemory(const std::string& model_buffer,
137 138
                                Scope* scope,
                                cpp::ProgramDesc* cpp_prog);
Y
Yan Chunwei 已提交
139 140
}  // namespace lite
}  // namespace paddle