model_parser.h 4.3 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 24 25 26 27 28 29 30 31 32 33 34 35
// 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"
#endif
#include "lite/core/scope.h"
#include "lite/core/variable.h"
#include "lite/model_parser/compatible_pb.h"
#include "lite/model_parser/naive_buffer/proto/framework.nb.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

// 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);

44 45
void LoadCombinedParamsPb(const std::string& path,
                          lite::Scope* scope,
46 47
                          const cpp::ProgramDesc& prog,
                          bool params_from_memory = false);
48

Y
Yan Chunwei 已提交
49 50
// Read a model and files of parameters in pb format.
void LoadModelPb(const std::string& model_dir,
51 52
                 const std::string& model_file,
                 const std::string& param_file,
Y
Yan Chunwei 已提交
53
                 Scope* scope,
54
                 cpp::ProgramDesc* prog,
55 56
                 bool combined = false,
                 bool model_from_memory = false);
Y
Yan Chunwei 已提交
57 58 59 60

// Save a model and files of parameters in pb format.
void SaveModelPb(const std::string& model_dir,
                 const Scope& scope,
61 62 63 64 65 66
                 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 已提交
67 68 69 70 71 72 73 74

// 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);
75
void TensorFromStream(std::istream& is, lite::Tensor* tensor);
Y
Yan Chunwei 已提交
76 77 78 79 80 81 82
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 已提交
83 84 85 86
void SaveCombinedParamsNaive(const std::string& path,
                             const lite::Scope& exec_scope,
                             const cpp::ProgramDesc& cpp_prog);

Y
Yan Chunwei 已提交
87 88
void SaveModelNaive(const std::string& model_dir,
                    const Scope& exec_scope,
Y
Yan Chunwei 已提交
89 90
                    const cpp::ProgramDesc& cpp_prog,
                    bool combined = true);
Y
Yan Chunwei 已提交
91 92 93 94 95 96
#endif

void LoadParamNaive(const std::string& path,
                    lite::Scope* scope,
                    const std::string& name);

97 98
// warning:this old inference will be abandened in release/v3.0.0
// and LoadModelNaiveFromFile is suggested.
Y
Yan Chunwei 已提交
99 100
void LoadModelNaive(const std::string& model_dir,
                    lite::Scope* scope,
Y
Yan Chunwei 已提交
101 102
                    cpp::ProgramDesc* prog,
                    bool combined = true);
103 104 105
void LoadModelNaiveFromFile(const std::string& filename,
                            lite::Scope* scope,
                            cpp::ProgramDesc* prog);
106 107 108 109
void LoadModelNaiveFromMemory(const std::string& model_buffer,
                              const std::string& param_buffer,
                              lite::Scope* scope,
                              cpp::ProgramDesc* cpp_prog);
110 111 112
void LoadModelNaiveFromMemory(const std::string& model_buffer,
                              lite::Scope* scope,
                              cpp::ProgramDesc* cpp_prog);
113

Y
Yan Chunwei 已提交
114 115
}  // namespace lite
}  // namespace paddle