paddle_mobile_wrap.h 3.3 KB
Newer Older
Y
Yanzhan Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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. */

#pragma once

#include <cstdint>
#include <map>
19
#include <memory>
Y
Yanzhan Yang 已提交
20 21 22 23 24 25 26 27 28 29 30
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

namespace paddle_mobile {
namespace wrap {

#ifndef PADDLE_MOBILE_FPGA

// device type
31
__attribute__((__visibility__("default"))) enum DeviceTypeEnum {
Y
Yanzhan Yang 已提交
32
  kCPU = 0,
33
  kGPU_CL = 1
Y
Yanzhan Yang 已提交
34 35 36 37 38
};

// ddim class
class DDim {
 public:
39 40 41
  __attribute__((__visibility__("default"))) int size();
  __attribute__((__visibility__("default"))) int64_t &operator[](int idx);
  __attribute__((__visibility__("default"))) int64_t operator[](int idx) const;
Y
Yanzhan Yang 已提交
42

43
  __attribute__((__visibility__("default"))) std::vector<int64_t> dims;
Y
Yanzhan Yang 已提交
44
};
45 46
__attribute__((__visibility__("default"))) DDim make_ddim(
    const std::vector<int64_t> &dims);
Y
Yanzhan Yang 已提交
47 48 49 50

// tensor class
class Tensor {
 public:
51
  __attribute__((__visibility__("default"))) Tensor(float *data, DDim ddim);
Y
Yanzhan Yang 已提交
52

53 54
  __attribute__((__visibility__("default"))) float *data() const;
  __attribute__((__visibility__("default"))) DDim dims() const;
Y
Yanzhan Yang 已提交
55

56
 private:
Y
Yanzhan Yang 已提交
57 58 59 60 61 62 63
  float *data_;
  DDim ddim_;
};

// net class
class Net {
 public:
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  __attribute__((__visibility__("default"))) Net(DeviceTypeEnum device);
  __attribute__((__visibility__("default"))) ~Net();
  __attribute__((__visibility__("default"))) void SetThreadNum(int thread_num);
  __attribute__((__visibility__("default"))) void SetCLPath(std::string path);
  __attribute__((__visibility__("default"))) bool Load(
      const std::string &dirname, const bool optimize = false,
      const bool quantification = false, const int batch_size = 1,
      const bool lod_mode = false);
  __attribute__((__visibility__("default"))) bool Load(
      const std::string &model_path, const std::string &para_path,
      const bool optimize = false, const bool quantification = false,
      const int batch_size = 1, const bool lod_mode = false);
  __attribute__((__visibility__("default"))) bool LoadCombinedMemory(
      size_t model_len, const uint8_t *model_buf, size_t combined_params_len,
      uint8_t *combined_params_buf, bool optimize = false,
      bool quantification = false, int batch_size = 1, bool lod_mode = false);
  __attribute__((__visibility__("default"))) std::vector<float> Predict(
      const std::vector<float> &input, const std::vector<int64_t> &dims);
  __attribute__((__visibility__("default"))) bool Predict();
  __attribute__((__visibility__("default"))) bool Predict(const Tensor &input);
  __attribute__((__visibility__("default"))) void Feed(
      const std::string &var_name, const Tensor &input);
  __attribute__((__visibility__("default"))) std::shared_ptr<Tensor> Fetch(
      const std::string &var_name);

 private:
Y
Yanzhan Yang 已提交
90
  void *engine_ = nullptr;
91
  DeviceTypeEnum device_;
Y
Yanzhan Yang 已提交
92 93 94 95 96 97
};

#endif

}  // namespace wrap
}  // namespace paddle_mobile