paddle_mobile_wrap.cpp 10.2 KB
Newer Older
Y
Yanzhan Yang 已提交
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
/* 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. */

#include "io/paddle_mobile_wrap.h"

#include "io/api_paddle_mobile.h"
#include "io/paddle_mobile.h"

namespace paddle_mobile {
namespace wrap {

#ifndef PADDLE_MOBILE_FPGA

// ddim class
int DDim::size() { return dims.size(); }

int64_t &DDim::operator[](int idx) {
  if (0 <= idx && idx < dims.size()) {
    return dims[idx];
  }
  int64_t non_exist = 0;
  return non_exist;
}

int64_t DDim::operator[](int idx) const {
  if (0 <= idx && idx < dims.size()) {
    return dims[idx];
  }
  return 0;
}

DDim make_ddim(const std::vector<int64_t> &dims) {
  DDim ddim;
  for (auto dim : dims) {
    ddim.dims.push_back(dim);
  }
  return ddim;
}

// tensor class
52

Y
Yanzhan Yang 已提交
53 54 55 56 57
Tensor::Tensor(float *data, DDim ddim) {
  this->data_ = data;
  this->ddim_ = ddim;
}

58
float *Tensor::data() const { return this->data_; }
Y
Yanzhan Yang 已提交
59 60 61 62

DDim Tensor::dims() const { return this->ddim_; }

// net class
63 64 65 66 67 68 69 70

void Net::SetThreadNum(int threads) {
  if (this->device_ == kCPU) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
    if (engine != nullptr) {
      engine->SetThreadNum(threads);
    }
Y
Yanzhan Yang 已提交
71 72 73
  }
}

74 75 76 77 78
void Net::SetCLPath(std::string path) {
  if (this->device_ == kGPU_CL) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
    engine->SetCLPath(path);
Y
Yanzhan Yang 已提交
79
  }
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

bool Net::Load(const std::string &dirname, const bool optimize,
               const bool quantification, const int batch_size,
               const bool lod_mode) {
  if (this->device_ == kCPU) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
    if (engine != nullptr) {
      paddle_mobile::PMStatus status =
          engine->Load(dirname, optimize, quantification, batch_size, lod_mode);
      return status == paddle_mobile::PMSuccess;
    }
  } else if (this->device_ == kGPU_CL) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
    if (engine != nullptr) {
      paddle_mobile::PMStatus status =
          engine->Load(dirname, optimize, quantification, batch_size, lod_mode);
      return status == paddle_mobile::PMSuccess;
    }
Y
Yanzhan Yang 已提交
101
  }
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  return false;
}

bool Net::Load(const std::string &model_path, const std::string &para_path,
               const bool optimize, const bool quantification,
               const int batch_size, const bool lod_mode) {
  if (this->device_ == kCPU) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
    if (engine != nullptr) {
      paddle_mobile::PMStatus status =
          engine->Load(model_path, para_path, optimize, quantification,
                       batch_size, lod_mode);
      return status == paddle_mobile::PMSuccess;
    }
  } else if (this->device_ == kGPU_CL) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
    if (engine != nullptr) {
      paddle_mobile::PMStatus status =
          engine->Load(model_path, para_path, optimize, quantification,
                       batch_size, lod_mode);
      return status == paddle_mobile::PMSuccess;
    }
Y
Yanzhan Yang 已提交
126 127 128 129
  }
  return false;
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
bool Net::LoadCombinedMemory(size_t model_len, const uint8_t *model_buf,
                             size_t combined_params_len,
                             uint8_t *combined_params_buf, bool optimize,
                             bool quantification, int batch_size,
                             bool lod_mode) {
  if (this->device_ == kCPU) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
    if (engine != nullptr) {
      bool status = engine->LoadCombinedMemory(
          model_len, model_buf, combined_params_len, combined_params_buf,
          optimize, quantification, batch_size, lod_mode);
      return status;
    }
  } else if (this->device_ == kGPU_CL) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
    if (engine != nullptr) {
      bool status = engine->LoadCombinedMemory(
          model_len, model_buf, combined_params_len, combined_params_buf,
          optimize, quantification, batch_size, lod_mode);
      return status;
    }
Y
Yanzhan Yang 已提交
153
  }
154
  return false;
Y
Yanzhan Yang 已提交
155 156
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
std::vector<float> Net::Predict(const std::vector<float> &input,
                                const std::vector<int64_t> &dims) {
  if (this->device_ == kCPU) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
    if (engine != nullptr) {
      auto result = engine->Predict(input, dims);
      return result;
    }
  } else if (this->device_ == kGPU_CL) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
    if (engine != nullptr) {
      auto result = engine->Predict(input, dims);
      return result;
    }
Y
Yanzhan Yang 已提交
173 174 175 176
  }
  return std::vector<float>();
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
bool Net::Predict() {
  if (this->device_ == kCPU) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
    if (engine != nullptr) {
      paddle_mobile::PMStatus status = engine->Predict();
      return status == paddle_mobile::PMSuccess;
    }
  } else if (this->device_ == kGPU_CL) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
    if (engine != nullptr) {
      paddle_mobile::PMStatus status = engine->Predict();
      return status == paddle_mobile::PMSuccess;
    }
  }
  return false;
}

bool Net::Predict(const Tensor &input) {
  if (this->device_ == kCPU) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
    if (engine != nullptr) {
      auto input_data = input.data();
      auto input_dims = input.dims();
      std::vector<int64_t> input_dims_as_vector = input_dims.dims;
      paddle_mobile::framework::Tensor input_inner(
          input_data,
          paddle_mobile::framework::make_ddim(input_dims_as_vector));
      paddle_mobile::PMStatus status = engine->Predict(input_inner);
      return status == paddle_mobile::PMSuccess;
    }
  } else if (this->device_ == kGPU_CL) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
    if (engine != nullptr) {
      auto input_data = input.data();
      auto input_dims = input.dims();
      std::vector<int64_t> input_dims_as_vector = input_dims.dims;
      paddle_mobile::framework::Tensor input_inner(
          input_data,
          paddle_mobile::framework::make_ddim(input_dims_as_vector));
      paddle_mobile::PMStatus status = engine->Predict(input_inner);
      return status == paddle_mobile::PMSuccess;
    }
Y
Yanzhan Yang 已提交
223
  }
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
  return false;
}

void Net::Feed(const std::string &var_name, const Tensor &input) {
  if (this->device_ == kCPU) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
    if (engine != nullptr) {
      auto input_data = input.data();
      auto input_dims = input.dims();
      std::vector<int64_t> input_dims_as_vector = input_dims.dims;
      paddle_mobile::framework::Tensor input_inner(
          input_data,
          paddle_mobile::framework::make_ddim(input_dims_as_vector));
      engine->Feed(var_name, input_inner);
    }
  } else if (this->device_ == kGPU_CL) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
    if (engine != nullptr) {
      auto input_data = input.data();
      auto input_dims = input.dims();
      std::vector<int64_t> input_dims_as_vector = input_dims.dims;
      paddle_mobile::framework::Tensor input_inner(
          input_data,
          paddle_mobile::framework::make_ddim(input_dims_as_vector));
      engine->Feed(var_name, input_inner);
    }
Y
Yanzhan Yang 已提交
252 253 254
  }
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
std::shared_ptr<Tensor> Net::Fetch(const std::string &var_name) {
  if (this->device_ == kCPU) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
    if (engine != nullptr) {
      auto output_inner = engine->Fetch(var_name);
      auto ddim_inner = output_inner->dims();
      std::vector<int64_t> ddim_as_vector;
      for (int i = 0; i < ddim_inner.size(); i++) {
        ddim_as_vector.push_back(ddim_inner[i]);
      }
      auto ddim = make_ddim(ddim_as_vector);
      auto output_data = output_inner->data<float>();
      std::shared_ptr<Tensor> ptr(new Tensor(output_data, ddim));
      return ptr;
    }
  } else if (this->device_ == kGPU_CL) {
    auto engine =
        (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
    if (engine != nullptr) {
      auto output_inner = engine->Fetch(var_name);
      auto ddim_inner = output_inner->dims();
      std::vector<int64_t> ddim_as_vector;
      for (int i = 0; i < ddim_inner.size(); i++) {
        ddim_as_vector.push_back(ddim_inner[i]);
      }
      auto ddim = make_ddim(ddim_as_vector);
      auto output_data = output_inner->data<float>();
      std::shared_ptr<Tensor> ptr(new Tensor(output_data, ddim));
      return ptr;
    }
Y
Yanzhan Yang 已提交
286 287 288 289
  }
  return nullptr;
}

290
Net::Net(DeviceTypeEnum device) {
Y
Yanzhan Yang 已提交
291 292
  if (this->engine_ == nullptr) {
    PaddleMobileConfigInternal config;
293 294 295 296 297 298 299 300
    this->device_ = device;
    if (this->device_ == kCPU) {
      this->engine_ =
          new paddle_mobile::PaddleMobile<paddle_mobile::CPU>(config);
    } else if (this->device_ == kGPU_CL) {
      this->engine_ =
          new paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL>(config);
    }
Y
Yanzhan Yang 已提交
301 302 303
  }
}

304
Net::~Net() {
Y
Yanzhan Yang 已提交
305
  if (this->engine_ != nullptr) {
306 307 308 309 310 311 312 313 314 315 316
    if (this->device_ == kCPU) {
      auto engine =
          (paddle_mobile::PaddleMobile<paddle_mobile::CPU> *)this->engine_;
      delete engine;
      this->engine_ = nullptr;
    } else if (this->device_ == kGPU_CL) {
      auto engine =
          (paddle_mobile::PaddleMobile<paddle_mobile::GPU_CL> *)this->engine_;
      delete engine;
      this->engine_ = nullptr;
    }
Y
Yanzhan Yang 已提交
317 318 319 320 321 322 323
  }
}

#endif

}  // namespace wrap
}  // namespace paddle_mobile