model_parser.h 3.6 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 36 37 38 39 40 41 42 43
// 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(
    const std::string& path);

// 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 46 47
void LoadCombinedParamsPb(const std::string& path,
                          lite::Scope* scope,
                          const cpp::ProgramDesc& prog);

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

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

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

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 已提交
81 82 83 84
void SaveCombinedParamsNaive(const std::string& path,
                             const lite::Scope& exec_scope,
                             const cpp::ProgramDesc& cpp_prog);

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

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

Y
Yan Chunwei 已提交
95 96 97 98
void LoadCombinedParamsNaive(const std::string& path,
                             lite::Scope* scope,
                             const cpp::ProgramDesc& cpp_prog);

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);
Y
Yan Chunwei 已提交
103 104 105

}  // namespace lite
}  // namespace paddle