model_parser.h 3.1 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// 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);

// Read a model and files of parameters in pb format.
void LoadModelPb(const std::string& model_dir,
                 Scope* scope,
                 cpp::ProgramDesc* prog);

// Save a model and files of parameters in pb format.
void SaveModelPb(const std::string& model_dir,
                 const Scope& scope,
                 const cpp::ProgramDesc& prog);

// 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 已提交
69 70 71 72
void SaveCombinedParamsNaive(const std::string& path,
                             const lite::Scope& exec_scope,
                             const cpp::ProgramDesc& cpp_prog);

Y
Yan Chunwei 已提交
73 74
void SaveModelNaive(const std::string& model_dir,
                    const Scope& exec_scope,
Y
Yan Chunwei 已提交
75 76
                    const cpp::ProgramDesc& cpp_prog,
                    bool combined = true);
Y
Yan Chunwei 已提交
77 78 79 80 81 82
#endif

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

Y
Yan Chunwei 已提交
83 84 85 86
void LoadCombinedParamsNaive(const std::string& path,
                             lite::Scope* scope,
                             const cpp::ProgramDesc& cpp_prog);

Y
Yan Chunwei 已提交
87 88
void LoadModelNaive(const std::string& model_dir,
                    lite::Scope* scope,
Y
Yan Chunwei 已提交
89 90
                    cpp::ProgramDesc* prog,
                    bool combined = true);
Y
Yan Chunwei 已提交
91 92 93

}  // namespace lite
}  // namespace paddle