io.h 2.0 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 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. */
朔-望's avatar
朔-望 已提交
14 15 16

#pragma once

L
liuruilong 已提交
17
#include <memory.h>
朔-望's avatar
朔-望 已提交
18
#include <string>
L
liuruilong 已提交
19
#include <vector>
朔-望's avatar
朔-望 已提交
20 21 22

#include "common/types.h"
#include "framework/lod_tensor.h"
L
liuruilong 已提交
23
#include "framework/operator.h"
L
liuruilong 已提交
24
#include "framework/paddle_mobile_object.h"
L
liuruilong 已提交
25 26
#include "framework/program/program.h"
#include "framework/tensor.h"
朔-望's avatar
朔-望 已提交
27 28 29

namespace paddle_mobile {

朔-望's avatar
朔-望 已提交
30 31
template <typename Dtype, Precision P = Precision::FP32>
class Loader : PaddleMobileObject {
朔-望's avatar
朔-望 已提交
32
 public:
33
  const framework::Program<Dtype, P> Load(const std::string &dirname);
朔-望's avatar
朔-望 已提交
34

朔-望's avatar
朔-望 已提交
35
 private:
36
  void LoadVar(framework::LoDTensor *tensor, const std::string &file_path);
朔-望's avatar
朔-望 已提交
37
};
朔-望's avatar
朔-望 已提交
38

L
liuruilong 已提交
39 40 41 42 43 44 45 46 47 48 49
template <typename Dtype, Precision P = Precision::FP32>
class Executor {
 public:
  typedef typename PrecisionTrait<P>::ptype Ptype;

  Executor() = default;

  Executor(const framework::Program<Dtype> p);

  std::shared_ptr<framework::Tensor> predict(framework::Tensor &t);

L
liuruilong 已提交
50 51
  std::vector<Ptype> predict(const std::vector<Ptype> &input,
                             const std::vector<int64_t> &dims);
L
liuruilong 已提交
52 53 54 55 56 57 58 59

 protected:
  void InitMemory();
  void LoadMemory(framework::LoDTensor *tensor, const std::string &file_path);
  const framework::Program<Dtype> program_;
  std::shared_ptr<framework::ProgramDesc> to_predict_program_;
  void predict(const framework::Tensor &t, int block_id);
  std::map<framework::BlockDesc,
L
liuruilong 已提交
60 61
           std::vector<std::shared_ptr<framework::OperatorBase<Dtype>>>>
      ops_of_block_;
L
liuruilong 已提交
62 63 64
  bool use_optimize_ = false;
};

朔-望's avatar
朔-望 已提交
65
}  // namespace paddle_mobile