paddle_api.cc 12.2 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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 "lite/api/paddle_api.h"
16 17 18

#include <utility>

19
#include "lite/core/context.h"
20
#include "lite/core/device_info.h"
S
sangoly 已提交
21
#include "lite/core/target_wrapper.h"
Y
Yan Chunwei 已提交
22 23
#include "lite/core/tensor.h"

S
sangoly 已提交
24 25 26
#ifdef LITE_WITH_CUDA
#include "lite/backends/cuda/target_wrapper.h"
#endif
27 28 29
#ifdef LITE_WITH_XPU
#include "lite/backends/xpu/target_wrapper.h"
#endif
S
sangoly 已提交
30

31 32 33 34
#ifdef LITE_WITH_MLU
#include "lite/backends/mlu/target_wrapper.h"
#endif

35 36 37 38
#ifdef LITE_WITH_OPENCL
#include "lite/backends/opencl/cl_runtime.h"
#endif

Y
Yan Chunwei 已提交
39 40 41
namespace paddle {
namespace lite_api {

42 43
bool IsOpenCLBackendValid() {
  bool opencl_valid = false;
44

45
#ifdef LITE_WITH_OPENCL
46 47 48 49 50 51 52 53 54 55 56 57
  bool opencl_lib_found = paddle::lite::CLWrapper::Global()->OpenclLibFound();
#ifdef LITE_WITH_LOG
  LOG(INFO) << "opencl_lib_found:" << opencl_lib_found;
#endif
  if (opencl_lib_found == false) return false;

  bool dlsym_success = paddle::lite::CLWrapper::Global()->DlsymSuccess();
#ifdef LITE_WITH_LOG
  LOG(INFO) << "dlsym_success:" << dlsym_success;
#endif
  if (dlsym_success == false) return false;

58 59
  opencl_valid = paddle::lite::CLRuntime::Global()->OpenCLAvaliableForDevice();
#endif
60 61

#ifdef LITE_WITH_LOG
62
  LOG(INFO) << "opencl_valid:" << opencl_valid;
63
#endif
64 65 66
  return opencl_valid;
}

Y
Yan Chunwei 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80
Tensor::Tensor(void *raw) : raw_tensor_(raw) {}

// TODO(Superjomn) refine this by using another `const void* const_raw`;
Tensor::Tensor(const void *raw) { raw_tensor_ = const_cast<void *>(raw); }

lite::Tensor *tensor(void *x) { return static_cast<lite::Tensor *>(x); }
const lite::Tensor *ctensor(void *x) {
  return static_cast<const lite::Tensor *>(x);
}

void Tensor::Resize(const shape_t &shape) {
  tensor(raw_tensor_)->Resize(shape);
}

81
// Tensor::data
Y
Yan Chunwei 已提交
82 83 84 85 86 87 88 89
template <>
const float *Tensor::data() const {
  return ctensor(raw_tensor_)->data<float>();
}
template <>
const int8_t *Tensor::data() const {
  return ctensor(raw_tensor_)->data<int8_t>();
}
90
template <>
91 92 93 94
const uint8_t *Tensor::data() const {
  return ctensor(raw_tensor_)->data<uint8_t>();
}
template <>
95 96 97
const int64_t *Tensor::data() const {
  return ctensor(raw_tensor_)->data<int64_t>();
}
S
sangoly 已提交
98 99 100 101 102
template <>
const int32_t *Tensor::data() const {
  return ctensor(raw_tensor_)->data<int32_t>();
}

103
// Tensor::mutable_data
104
template <>
105 106
int *Tensor::mutable_data(TargetType type) const {
  return tensor(raw_tensor_)->mutable_data<int>(type);
107
}
Y
Yan Chunwei 已提交
108
template <>
109 110
float *Tensor::mutable_data(TargetType type) const {
  return tensor(raw_tensor_)->mutable_data<float>(type);
Y
Yan Chunwei 已提交
111 112
}
template <>
113 114
int8_t *Tensor::mutable_data(TargetType type) const {
  return tensor(raw_tensor_)->mutable_data<int8_t>(type);
Y
Yan Chunwei 已提交
115
}
116
template <>
117 118 119 120
uint8_t *Tensor::mutable_data(TargetType type) const {
  return tensor(raw_tensor_)->mutable_data<uint8_t>(type);
}
template <>
121 122 123
int64_t *Tensor::mutable_data(TargetType type) const {
  return tensor(raw_tensor_)->mutable_data<int64_t>(type);
}
Y
Yan Chunwei 已提交
124

S
sangoly 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
template <typename T, TargetType type>
void Tensor::CopyFromCpu(const T *src_data) {
  T *data = tensor(raw_tensor_)->mutable_data<T>(type);
  int64_t num = tensor(raw_tensor_)->numel();
  CHECK(num > 0) << "You should call Resize interface first";
  if (type == TargetType::kHost || type == TargetType::kARM) {
    lite::TargetWrapperHost::MemcpySync(
        data, src_data, num * sizeof(T), lite::IoDirection::HtoH);
  } else if (type == TargetType::kCUDA) {
#ifdef LITE_WITH_CUDA
    lite::TargetWrapperCuda::MemcpySync(
        data, src_data, num * sizeof(T), lite::IoDirection::HtoD);
#else
    LOG(FATAL) << "Please compile the lib with CUDA.";
139 140 141 142 143 144 145
#endif
  } else if (type == TargetType::kMLU) {
#ifdef LITE_WITH_MLU
    lite::TargetWrapperMlu::MemcpySync(
        data, src_data, num * sizeof(T), lite::IoDirection::HtoD);
#else
    LOG(FATAL) << "Please compile the lib with MLU.";
S
sangoly 已提交
146 147 148 149 150 151
#endif
  } else {
    LOG(FATAL) << "The CopyFromCpu interface just support kHost, kARM, kCUDA";
  }
}
template <typename T>
152
void Tensor::CopyToCpu(T *data) const {
S
sangoly 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165
  const T *src_data = tensor(raw_tensor_)->data<T>();
  int64_t num = tensor(raw_tensor_)->numel();
  CHECK(num > 0) << "You should call Resize interface first";
  auto type = tensor(raw_tensor_)->target();
  if (type == TargetType::kHost || type == TargetType::kARM) {
    lite::TargetWrapperHost::MemcpySync(
        data, src_data, num * sizeof(T), lite::IoDirection::HtoH);
  } else if (type == TargetType::kCUDA) {
#ifdef LITE_WITH_CUDA
    lite::TargetWrapperCuda::MemcpySync(
        data, src_data, num * sizeof(T), lite::IoDirection::DtoH);
#else
    LOG(FATAL) << "Please compile the lib with CUDA.";
166 167 168 169 170 171 172
#endif
  } else if (type == TargetType::kMLU) {
#ifdef LITE_WITH_MLU
    lite::TargetWrapperMlu::MemcpySync(
        data, src_data, num * sizeof(T), lite::IoDirection::DtoH);
#else
    LOG(FATAL) << "Please compile the lib with MLU.";
S
sangoly 已提交
173 174 175 176 177 178 179 180 181
#endif
  } else {
    LOG(FATAL) << "The CopyToCpu interface just support kHost, kARM, kCUDA";
  }
}

template void Tensor::CopyFromCpu<int, TargetType::kHost>(const int *);
template void Tensor::CopyFromCpu<float, TargetType::kHost>(const float *);
template void Tensor::CopyFromCpu<int8_t, TargetType::kHost>(const int8_t *);
182
template void Tensor::CopyFromCpu<uint8_t, TargetType::kHost>(const uint8_t *);
S
sangoly 已提交
183 184 185 186

template void Tensor::CopyFromCpu<int, TargetType::kARM>(const int *);
template void Tensor::CopyFromCpu<float, TargetType::kARM>(const float *);
template void Tensor::CopyFromCpu<int8_t, TargetType::kARM>(const int8_t *);
187 188
template void Tensor::CopyFromCpu<uint8_t, TargetType::kARM>(const uint8_t *);

S
sangoly 已提交
189
template void Tensor::CopyFromCpu<int, TargetType::kCUDA>(const int *);
190
template void Tensor::CopyFromCpu<int64_t, TargetType::kCUDA>(const int64_t *);
S
sangoly 已提交
191 192 193
template void Tensor::CopyFromCpu<float, TargetType::kCUDA>(const float *);
template void Tensor::CopyFromCpu<int8_t, TargetType::kCUDA>(const int8_t *);

194 195 196 197 198
template void Tensor::CopyFromCpu<int, TargetType::kMLU>(const int *);
template void Tensor::CopyFromCpu<int64_t, TargetType::kMLU>(const int64_t *);
template void Tensor::CopyFromCpu<float, TargetType::kMLU>(const float *);
template void Tensor::CopyFromCpu<int8_t, TargetType::kMLU>(const int8_t *);

199 200
template void Tensor::CopyToCpu(float *) const;
template void Tensor::CopyToCpu(int *) const;
201 202
template void Tensor::CopyToCpu(int8_t *) const;
template void Tensor::CopyToCpu(uint8_t *) const;
S
sangoly 已提交
203

Y
Yan Chunwei 已提交
204 205 206 207
shape_t Tensor::shape() const {
  return ctensor(raw_tensor_)->dims().Vectorize();
}

S
sangoly 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
TargetType Tensor::target() const {
  auto type = ctensor(raw_tensor_)->target();
  if (type == TargetType::kUnk) {
    CHECK(false) << "This tensor was not initialized.";
  }
  return type;
}

PrecisionType Tensor::precision() const {
  auto precision = ctensor(raw_tensor_)->precision();
  if (precision == PrecisionType::kUnk) {
    CHECK(false) << "This tensor was not initialized.";
  }
  return precision;
}

Y
Yan Chunwei 已提交
224 225 226 227
lod_t Tensor::lod() const { return ctensor(raw_tensor_)->lod(); }

void Tensor::SetLoD(const lod_t &lod) { tensor(raw_tensor_)->set_lod(lod); }

228 229 230 231 232 233 234 235 236 237 238 239 240 241
std::unique_ptr<Tensor> PaddlePredictor::GetMutableTensor(
    const std::string &name) {
  LOG(FATAL)
      << "The GetMutableTensor API is only supported by CxxConfig predictor.";
  return nullptr;
}

std::vector<std::string> PaddlePredictor::GetParamNames() {
  std::vector<std::string> null_result = {};
  LOG(FATAL)
      << "The GetParamNames API is only supported by CxxConfig predictor.";
  return null_result;
}

Y
Yan Chunwei 已提交
242
void PaddlePredictor::SaveOptimizedModel(const std::string &model_dir,
243 244
                                         LiteModelType model_type,
                                         bool record_info) {
Y
Yan Chunwei 已提交
245 246 247 248 249 250 251 252 253
  LOG(FATAL)
      << "The SaveOptimizedModel API is only supported by CxxConfig predictor.";
}

template <typename ConfigT>
std::shared_ptr<PaddlePredictor> CreatePaddlePredictor(const ConfigT &) {
  return std::shared_ptr<PaddlePredictor>();
}

254 255 256 257 258 259 260 261 262
ConfigBase::ConfigBase(PowerMode mode, int threads) {
#ifdef LITE_WITH_ARM
  lite::DeviceInfo::Init();
  lite::DeviceInfo::Global().SetRunMode(mode, threads);
  mode_ = lite::DeviceInfo::Global().mode();
  threads_ = lite::DeviceInfo::Global().threads();
#endif
}

263 264 265 266 267 268 269 270 271 272 273 274
void ConfigBase::set_opencl_tune(bool enable_tune) {
#ifdef LITE_WITH_OPENCL
  if (paddle::lite_api::IsOpenCLBackendValid()) {
    enable_opencl_tune_ = enable_tune;
    paddle::lite::CLRuntime::Global()->set_auto_tune(enable_opencl_tune_);
#ifdef LITE_WITH_OPENCL
    LOG(INFO) << "auto_tune:" << paddle::lite::CLRuntime::Global()->auto_tune();
#endif
  }
#endif
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
void ConfigBase::set_power_mode(paddle::lite_api::PowerMode mode) {
#ifdef LITE_WITH_ARM
  lite::DeviceInfo::Global().SetRunMode(mode, threads_);
  mode_ = lite::DeviceInfo::Global().mode();
  threads_ = lite::DeviceInfo::Global().threads();
#endif
}

void ConfigBase::set_threads(int threads) {
#ifdef LITE_WITH_ARM
  lite::DeviceInfo::Global().SetRunMode(mode_, threads);
  mode_ = lite::DeviceInfo::Global().mode();
  threads_ = lite::DeviceInfo::Global().threads();
#endif
}

291 292 293 294 295 296 297 298 299 300
#ifdef LITE_WITH_MLU
void CxxConfig::set_mlu_core_version(lite_api::MLUCoreVersion core_version) {
  mlu_core_version_ = core_version;
}
void CxxConfig::set_mlu_core_number(int core_number) {
  mlu_core_number_ = core_number;
}
void CxxConfig::set_mlu_input_layout(DataLayoutType layout) {
  mlu_input_layout_ = layout;
}
301 302
void CxxConfig::set_mlu_firstconv_param(const std::vector<float> &mean,
                                        const std::vector<float> &std) {
303 304 305 306 307 308 309 310
  mlu_first_conv_mean_ = mean;
  mlu_first_conv_std_ = std;
}
lite_api::MLUCoreVersion CxxConfig::mlu_core_version() const {
  return mlu_core_version_;
}
int CxxConfig::mlu_core_number() const { return mlu_core_number_; }
DataLayoutType CxxConfig::mlu_input_layout() const { return mlu_input_layout_; }
311 312 313
std::pair<std::vector<float>, std::vector<float>>
CxxConfig::mlu_firstconv_param() const {
  return std::make_pair(mlu_first_conv_mean_, mlu_first_conv_std_);
314 315 316
}
#endif

317 318
void CxxConfig::set_xpu_workspace_l3_size_per_thread(int l3_size) {
#ifdef LITE_WITH_XPU
319
  lite::TargetWrapperXPU::workspace_l3_size_per_thread = l3_size;
320 321 322 323 324 325 326 327 328
#else
  LOG(WARNING) << "The invoking of the function "
                  "'set_xpu_workspace_l3_size_per_thread' is ignored, please "
                  "rebuild it with LITE_WITH_XPU=ON.";
#endif
}

void CxxConfig::set_xpu_dev_per_thread(int dev_no) {
#ifdef LITE_WITH_XPU
329
  lite::TargetWrapperXPU::SetDev(dev_no);
330 331 332 333 334 335
#else
  LOG(WARNING) << "The invoking of the function 'set_xpu_dev_per_thread' is "
                  "ignored, please rebuild it with LITE_WITH_XPU=ON.";
#endif
}

336 337
void CxxConfig::set_xpu_multi_encoder_precision(const std::string &precision) {
#ifdef LITE_WITH_XPU
338
  lite::TargetWrapperXPU::multi_encoder_precision = precision;
339 340 341 342 343 344 345
#else
  LOG(WARNING) << "The invoking of the function "
                  "'set_xpu_multi_encoder_precision' is "
                  "ignored, please rebuild it with LITE_WITH_XPU=ON.";
#endif
}

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
// set model data in combined format, `set_model_from_file` refers to loading
// model from file, set_model_from_buffer refers to loading model from memory
// buffer
void MobileConfig::set_model_from_file(const std::string &x) {
  lite_model_file_ = x;
}
void MobileConfig::set_model_from_buffer(const std::string &x) {
  lite_model_file_ = x;
  model_from_memory_ = true;
}
void MobileConfig::set_model_buffer(const char *model_buffer,
                                    size_t model_buffer_size,
                                    const char *param_buffer,
                                    size_t param_buffer_size) {
  LOG(WARNING) << "warning: `set_model_buffer` will be abandened in "
                  "release/v3.0.0, new method `set_model_from_buffer(const "
                  "std::string &x)` is recommended.";
  model_buffer_ = std::string(model_buffer, model_buffer + model_buffer_size);
  param_buffer_ = std::string(param_buffer, param_buffer + param_buffer_size);
  model_from_memory_ = true;
}

Y
Yan Chunwei 已提交
368 369
}  // namespace lite_api
}  // namespace paddle