api_paddle_mobile.cc 9.2 KB
Newer Older
N
nhzlx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#include "io/api_paddle_mobile.h"
J
jameswu2014 已提交
16
#include <memory>
H
hjchen2 已提交
17
#include <string>
J
jameswu2014 已提交
18
#include <utility>
N
nhzlx 已提交
19
#include <vector>
20
#include "common/enforce.h"
21
#include "common/type_define.h"
N
nhzlx 已提交
22
#include "framework/tensor.h"
23 24 25
#ifdef PADDLE_MOBILE_FPGA
#include <fpga/common/fpga_common.h>
#endif
N
nhzlx 已提交
26 27 28

namespace paddle_mobile {

29 30
template <typename Device, typename T>
PaddleMobilePredictor<Device, T>::PaddleMobilePredictor(
N
nhzlx 已提交
31 32 33 34 35 36
    const PaddleMobileConfig &config) {
  PADDLE_MOBILE_ENFORCE(Init(config) == true,
                        "paddle mobile predictor init failed!");
  config_ = config;
}

37 38
template <typename Device, typename T>
bool PaddleMobilePredictor<Device, T>::Init(const PaddleMobileConfig &config) {
39 40 41
  PaddleMobileConfigInternal configInternal;
  configInternal.load_when_predict = config.load_when_predict;
  paddle_mobile_.reset(new PaddleMobile<Device, T>(configInternal));
Y
yangfei 已提交
42 43 44
#ifdef PADDLE_MOBILE_CL
  paddle_mobile_->SetCLPath(config.cl_path);
#endif
xiebaiyuan's avatar
xiebaiyuan 已提交
45 46
  if (config.memory_pack.from_memory) {
    DLOG << "load from memory!";
47 48 49 50 51
    paddle_mobile_->LoadCombinedMemory(
        config.memory_pack.model_size, config.memory_pack.model_buf,
        config.memory_pack.combined_params_size,
        config.memory_pack.combined_params_buf, config.optimize,
        config.quantification, config.batch_size, config.lod_mode);
xiebaiyuan's avatar
xiebaiyuan 已提交
52
  } else if (!config.model_dir.empty()) {
N
nhzlx 已提交
53
    paddle_mobile_->Load(config.model_dir, config.optimize,
Z
zhangyang0701 已提交
54 55
                         config.quantification, config.batch_size,
                         config.lod_mode);
N
nhzlx 已提交
56 57
  } else if (!config.prog_file.empty() && !config.param_file.empty()) {
    paddle_mobile_->Load(config.prog_file, config.param_file, config.optimize,
Z
zhangyang0701 已提交
58 59
                         config.quantification, config.batch_size,
                         config.lod_mode);
N
nhzlx 已提交
60 61 62 63 64 65 66 67
  } else {
    LOG(kLOG_ERROR) << "fail to load inference model!";
    return false;
  }
  // If the openmp is open, set the thread num
  paddle_mobile_->SetThreadNum(config.thread_num);
  return true;
}
68 69
template <typename Device, typename T>
bool PaddleMobilePredictor<Device, T>::Run(
N
nhzlx 已提交
70 71 72 73 74 75 76 77
    const std::vector<PaddleTensor> &inputs,
    std::vector<PaddleTensor> *output_data, int batch_size) {
  if (inputs.empty()) {
    LOG(kLOG_ERROR) << "At least one output should be set with tensors' names.";
    return false;
  }
  auto input = inputs[0];

78
  if (input.lod.size() == 0 && input.shape.size() != 4) {
N
nhzlx 已提交
79 80 81 82 83 84 85 86 87
    LOG(kLOG_ERROR) << "input shape not equal to 4!";
    return false;
  }
  std::vector<int64_t> dims;
  for (auto d : input.shape) {
    dims.push_back(static_cast<int64_t>(d));
  }

  // use tensor
88
  framework::DDim ddim = framework::make_ddim(dims);
N
nhzlx 已提交
89 90

  framework::Tensor input_tensor;
91 92 93 94 95 96 97
  framework::LoDTensor input_lod_tensor;
  paddle_mobile::framework::LoD lod{{}};
  for (int i = 0; i < input.lod.size(); ++i) {
    lod[0].push_back(input.lod[i]);
  }
  input_lod_tensor.set_lod(lod);

N
nhzlx 已提交
98
  int input_length = framework::product(ddim);
99 100 101 102 103 104 105 106 107 108 109
  if (input.lod.size() > 0) {
    input_lod_tensor.Resize(ddim);
    memcpy(input_lod_tensor.mutable_data<T>(),
           static_cast<T *>(input.data.data()), input_length * sizeof(T));
    paddle_mobile_->Predict(input_lod_tensor);
  } else {
    input_tensor.Resize(ddim);
    memcpy(input_tensor.mutable_data<T>(), static_cast<T *>(input.data.data()),
           input_length * sizeof(T));
    paddle_mobile_->Predict(input_tensor);
  }
N
nhzlx 已提交
110

111
  auto output_tensor = paddle_mobile_->Fetch();
N
nhzlx 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

  if (output_data->empty()) {
    LOG(kLOG_ERROR) << "At least one output should be set with tensors' names.";
    return false;
  }

  auto &output = (*output_data)[0];
  int output_length = output_tensor->numel();
  std::vector<int64_t> tensor_shape =
      framework::vectorize(output_tensor->dims());

  for (auto d : tensor_shape) {
    output.shape.push_back(static_cast<int>(d));
  }

127 128
  if (output.data.length() < output_length * sizeof(T)) {
    output.data.Resize(output_length * sizeof(T));
N
nhzlx 已提交
129 130
  }

131 132
  memcpy(output.data.data(), output_tensor->template data<T>(),
         output_length * sizeof(T));
N
nhzlx 已提交
133 134 135 136

  return true;
}

Z
zhangyang0701 已提交
137
#ifdef PADDLE_MOBILE_FPGA
138 139 140
void ConvertPaddleTensors(const PaddleTensor &src, framework::Tensor *des) {
  des->Resize(framework::make_ddim(src.shape));
  des->external_data = src.data.data();
141
  des->set_type(static_cast<kTypeId_t>(static_cast<int>(src.dtypeid)));
142 143 144
  des->layout =
      src.layout == LAYOUT_HWC ? framework::LAYOUT_HWC : framework::LAYOUT_CHW;
}
Z
zhangyang0701 已提交
145

146 147
void ConvertTensors(const framework::Tensor &src, PaddleTensor *des) {
  des->shape = framework::vectorize2int(src.dims());
148
  des->dtypeid = static_cast<PaddlekTypeId_t>(static_cast<int>(src.type()));
149
  des->layout = src.layout == framework::LAYOUT_HWC ? LAYOUT_HWC : LAYOUT_CHW;
Z
zhangyang0701 已提交
150

151
  auto num = src.numel();
152
  if (src.type() == type_id<float>()) {
153 154
    des->data.Reset(const_cast<float *>(src.data<float>()),
                    num * sizeof(float));
155
  } else if (src.type() == type_id<half>()) {
156 157
    des->data.Reset(const_cast<int16_t *>(src.data<int16_t>()),
                    num * sizeof(int16_t));
158 159 160
  } else {
    des->data.Reset(const_cast<int8_t *>(src.data<int8_t>()),
                    num * sizeof(int8_t));
Z
zhangyang0701 已提交
161
  }
162
}
Z
zhangyang0701 已提交
163

164 165 166 167 168 169
template <typename Device, typename T>
void PaddleMobilePredictor<Device, T>::FeedPaddleTensors(
    const std::vector<PaddleTensor> &inputs) {
  auto num = inputs.size();
  std::vector<framework::Tensor> tensors(num, framework::Tensor());
  for (int i = 0; i < num; i++) {
170 171
    if (static_cast<kTypeId_t>(static_cast<int>(inputs[i].dtypeid)) ==
        type_id<int8_t>().hash_code()) {
172 173 174 175
      tensors[i].init(type_id<int8_t>().hash_code());
    } else {
      tensors[i].init(type_id<float>().hash_code());
    }
176
    ConvertPaddleTensors(inputs[i], &tensors[i]);
Z
zhangyang0701 已提交
177
  }
178
  paddle_mobile_->FeedTensorData(tensors);
179
}
Z
zhangyang0701 已提交
180

181 182 183
template <typename Device, typename T>
void PaddleMobilePredictor<Device, T>::FetchPaddleTensors(
    std::vector<PaddleTensor> *outputs) {
184 185 186 187 188
  //  auto num = outputs->size();
  //  PADDLE_MOBILE_ENFORCE(num > 0, "0 output pointers is not permitted");
  //  std::vector<framework::Tensor *> tensors(num, nullptr);
  outputs->clear();
  std::vector<framework::Tensor *> tensors;
189
  paddle_mobile_->GetTensorResults(&tensors);
190 191
  auto num = tensors.size();
  outputs->resize(num, PaddleTensor());
192 193
  for (int i = 0; i < num; i++) {
    ConvertTensors(*tensors[i], &(*outputs)[i]);
Z
zhangyang0701 已提交
194 195 196
  }
}

J
jameswu2014 已提交
197 198 199 200 201
template <typename Device, typename T>
void PaddleMobilePredictor<Device, T>::FetchPaddleTensors(PaddleTensor *output,
                                                          int id) {
  std::shared_ptr<framework::Tensor> tensor_ptr =
      paddle_mobile_->FetchResult(id);
202 203 204 205 206 207 208 209
  void *data_addr = nullptr;
  int data_sizeof = 1;
  if (tensor_ptr.get()->type() == type_id<half>().hash_code()) {
    data_addr = tensor_ptr.get()->data<half>();
    data_sizeof = sizeof(half);
  } else if (tensor_ptr.get()->type() == type_id<float>().hash_code()) {
    data_addr = tensor_ptr.get()->data<float>();
    data_sizeof = sizeof(float);
210 211 212
  } else if (tensor_ptr.get()->type() == type_id<int8_t>().hash_code()) {
    data_addr = tensor_ptr.get()->data<int8_t>();
    data_sizeof = sizeof(int8_t);
213 214 215 216 217
  } else {
    PADDLE_MOBILE_ENFORCE(0, "output typeid is not supported");
  }
  size_t size = tensor_ptr.get()->numel() * data_sizeof;
  fpga::fpga_invalidate(data_addr, size);
J
jameswu2014 已提交
218 219 220
  ConvertTensors(*(tensor_ptr.get()), output);
  return;
}
221 222 223 224 225
template <typename Device, typename T>
void PaddleMobilePredictor<Device, T>::GetPaddleTensor(const std::string &name,
                                                       PaddleTensor *output) {
  framework::Tensor *t = paddle_mobile_->GetTensorByName(name);
  ConvertTensors(*t, output);
Z
zhangyang0701 已提交
226
}
227

228 229 230 231 232
template <typename Device, typename T>
void PaddleMobilePredictor<Device, T>::Predict_From_To(int start, int end) {
  paddle_mobile_->Predict_From_To(start, end);
}

Z
zhangyang0701 已提交
233
#endif
234 235
template <typename Device, typename T>
PaddleMobilePredictor<Device, T>::~PaddleMobilePredictor() {
L
liuruilong 已提交
236 237 238
  paddle_mobile_->Clear();
}

N
nhzlx 已提交
239 240 241 242 243 244 245 246
// A factory to help create difference predictor.
template <>
std::unique_ptr<PaddlePredictor>
CreatePaddlePredictor<PaddleMobileConfig, PaddleEngineKind::kPaddleMobile>(
    const PaddleMobileConfig &config) {
  std::unique_ptr<PaddlePredictor> x;
  if (config.precision == PaddleMobileConfig::FP32) {
    if (config.device == PaddleMobileConfig::kCPU) {
247
      x.reset(new PaddleMobilePredictor<CPU, float>(config));
N
nhzlx 已提交
248
    } else if (config.device == PaddleMobileConfig::kFPGA) {
249
      x.reset(new PaddleMobilePredictor<FPGA, float>(config));
L
liuruilong 已提交
250
    } else if (config.device == PaddleMobileConfig::kGPU_CL) {
251
      x.reset(new PaddleMobilePredictor<GPU_CL, float>(config));
N
nhzlx 已提交
252 253 254 255 256 257 258 259 260 261 262 263
    } else {
      LOG(kLOG_ERROR) << "unsupport device type!";
      return nullptr;
    }
  } else {
    LOG(kLOG_ERROR) << "unsupport precision type!";
    return nullptr;
  }
  return std::move(x);
}

}  // namespace paddle_mobile