paddle_lite_jni.cc 11.6 KB
Newer Older
H
Huihuang Zheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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 "paddle/fluid/lite/api/android/jni/paddle_lite_jni.h"
15

H
Huihuang Zheng 已提交
16 17 18 19 20
#include <memory>
#include <string>
#include <utility>
#include <vector>

21 22 23 24 25 26 27
#include "paddle/fluid/lite/api/light_api.h"
#include "paddle/fluid/lite/api/paddle_api.h"
#include "paddle/fluid/lite/api/paddle_lite_factory_helper.h"
#include "paddle/fluid/lite/api/paddle_place.h"
#include "paddle/fluid/lite/api/paddle_use_kernels.h"
#include "paddle/fluid/lite/api/paddle_use_ops.h"
#include "paddle/fluid/lite/api/paddle_use_passes.h"
H
Huihuang Zheng 已提交
28 29
#include "paddle/fluid/lite/kernels/arm/activation_compute.h"
#include "paddle/fluid/lite/kernels/arm/batch_norm_compute.h"
30
#include "paddle/fluid/lite/kernels/arm/calib_compute.h"
H
Huihuang Zheng 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "paddle/fluid/lite/kernels/arm/concat_compute.h"
#include "paddle/fluid/lite/kernels/arm/conv_compute.h"
#include "paddle/fluid/lite/kernels/arm/dropout_compute.h"
#include "paddle/fluid/lite/kernels/arm/elementwise_compute.h"
#include "paddle/fluid/lite/kernels/arm/fc_compute.h"
#include "paddle/fluid/lite/kernels/arm/mul_compute.h"
#include "paddle/fluid/lite/kernels/arm/pool_compute.h"
#include "paddle/fluid/lite/kernels/arm/scale_compute.h"
#include "paddle/fluid/lite/kernels/arm/softmax_compute.h"
#include "paddle/fluid/lite/kernels/arm/split_compute.h"
#include "paddle/fluid/lite/kernels/arm/transpose_compute.h"

#define ARM_KERNEL_POINTER(kernel_class_name__)                    \
  std::unique_ptr<paddle::lite::kernels::arm::kernel_class_name__> \
      p##kernel_class_name__(                                      \
          new paddle::lite::kernels::arm::kernel_class_name__);

#ifdef __cplusplus
extern "C" {
#endif

52
using paddle::lite_api::CxxConfig;
H
Huihuang Zheng 已提交
53 54
using paddle::lite_api::MobileConfig;
using paddle::lite_api::PaddlePredictor;
55
using paddle::lite_api::Place;
H
Huihuang Zheng 已提交
56 57 58 59 60 61 62 63 64 65
using paddle::lite_api::Tensor;

static std::shared_ptr<PaddlePredictor> predictor;

/**
 * Not sure why, we have to initial a pointer first for kernels.
 * Otherwise it throws null pointer error when do KernelRegistor.
 */
static void use_arm_kernels() {
  ARM_KERNEL_POINTER(BatchNormCompute);
66 67
  ARM_KERNEL_POINTER(CalibComputeFp32ToInt8);
  ARM_KERNEL_POINTER(CalibComputeInt8ToFp32);
H
Huihuang Zheng 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 126 127 128 129 130 131 132 133 134 135 136 137
  ARM_KERNEL_POINTER(ConvCompute);
  ARM_KERNEL_POINTER(ConcatCompute);
  ARM_KERNEL_POINTER(ElementwiseAddCompute);
  ARM_KERNEL_POINTER(DropoutCompute);
  ARM_KERNEL_POINTER(FcCompute);
  ARM_KERNEL_POINTER(MulCompute);
  ARM_KERNEL_POINTER(PoolCompute);
  ARM_KERNEL_POINTER(ReluCompute);
  ARM_KERNEL_POINTER(ScaleCompute);
  ARM_KERNEL_POINTER(SoftmaxCompute);
  ARM_KERNEL_POINTER(SplitCompute);
  ARM_KERNEL_POINTER(TransposeCompute);
  ARM_KERNEL_POINTER(Transpose2Compute);
}

inline std::string jstring_to_cpp_string(JNIEnv *env, jstring jstr) {
  // In java, a unicode char will be encoded using 2 bytes (utf16).
  // so jstring will contain characters utf16. std::string in c++ is
  // essentially a string of bytes, not characters, so if we want to
  // pass jstring from JNI to c++, we have convert utf16 to bytes.
  if (!jstr) {
    return "";
  }
  const jclass stringClass = env->GetObjectClass(jstr);
  const jmethodID getBytes =
      env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
  const jbyteArray stringJbytes = (jbyteArray)env->CallObjectMethod(
      jstr, getBytes, env->NewStringUTF("UTF-8"));

  size_t length = (size_t)env->GetArrayLength(stringJbytes);
  jbyte *pBytes = env->GetByteArrayElements(stringJbytes, NULL);

  std::string ret = std::string(reinterpret_cast<char *>(pBytes), length);
  env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);

  env->DeleteLocalRef(stringJbytes);
  env->DeleteLocalRef(stringClass);
  return ret;
}

inline jfloatArray cpp_array_to_jfloatarray(JNIEnv *env, const float *buf,
                                            int64_t len) {
  jfloatArray result = env->NewFloatArray(len);
  env->SetFloatArrayRegion(result, 0, len, buf);
  return result;
}

inline jintArray cpp_array_to_jintarray(JNIEnv *env, const int *buf,
                                        int64_t len) {
  jintArray result = env->NewIntArray(len);
  env->SetIntArrayRegion(result, 0, len, buf);
  return result;
}

inline jbyteArray cpp_array_to_jbytearray(JNIEnv *env, const int8_t *buf,
                                          int64_t len) {
  jbyteArray result = env->NewByteArray(len);
  env->SetByteArrayRegion(result, 0, len, buf);
  return result;
}

inline std::vector<int64_t> jintarray_to_int64_vector(JNIEnv *env,
                                                      jintArray dims) {
  int dim_size = env->GetArrayLength(dims);
  jint *dim_nums = env->GetIntArrayElements(dims, nullptr);
  std::vector<int64_t> dim_vec(dim_nums, dim_nums + dim_size);
  env->ReleaseIntArrayElements(dims, dim_nums, 0);
  return dim_vec;
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/**
 * Converts Java com.baidu.paddle.lite.Place to c++ paddle::lite_api::Place.
 */
inline static Place jplace_to_cpp_place(JNIEnv *env, jobject java_place) {
  jclass place_jclazz = env->GetObjectClass(java_place);

  jmethodID target_method =
      env->GetMethodID(place_jclazz, "getTargetInt", "()I");
  jmethodID precision_method =
      env->GetMethodID(place_jclazz, "getPrecisionInt", "()I");
  jmethodID data_layout_method =
      env->GetMethodID(place_jclazz, "getDataLayoutInt", "()I");
  jmethodID device_method = env->GetMethodID(place_jclazz, "getDevice", "()I");

  int target = env->CallIntMethod(java_place, target_method);
  int precision = env->CallIntMethod(java_place, precision_method);
  int data_layout = env->CallIntMethod(java_place, data_layout_method);
  int device = env->CallIntMethod(java_place, device_method);

  return Place(static_cast<paddle::lite_api::TargetType>(target),
               static_cast<paddle::lite_api::PrecisionType>(precision),
               static_cast<paddle::lite_api::DataLayoutType>(data_layout),
               device);
}

H
Huihuang Zheng 已提交
163 164 165 166 167 168 169 170 171 172 173
inline static int64_t product(const std::vector<int64_t> &vec) {
  if (vec.empty()) {
    return 0;
  }
  int64_t result = 1;
  for (int64_t d : vec) {
    result *= d;
  }
  return result;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_loadCxxModel(
    JNIEnv *env, jclass thiz, jstring model_path, jobject preferred_place,
    jobjectArray valid_places) {
  if (predictor != nullptr) {
    return JNI_FALSE;
  }
  use_arm_kernels();

  int valid_place_count = env->GetArrayLength(valid_places);
  std::vector<Place> cpp_valid_places;
  for (int i = 0; i < valid_place_count; ++i) {
    jobject jplace = env->GetObjectArrayElement(valid_places, i);
    cpp_valid_places.push_back(jplace_to_cpp_place(env, jplace));
  }

  CxxConfig config;
  config.set_model_dir(jstring_to_cpp_string(env, model_path));
  config.set_preferred_place(jplace_to_cpp_place(env, preferred_place));
  config.set_valid_places(cpp_valid_places);

  predictor = paddle::lite_api::CreatePaddlePredictor(config);
  return predictor == nullptr ? JNI_FALSE : JNI_TRUE;
}

H
Huihuang Zheng 已提交
199 200 201 202 203 204 205 206 207
JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_loadMobileModel(JNIEnv *env,
                                                           jclass thiz,
                                                           jstring model_path) {
  if (predictor != nullptr) {
    return JNI_FALSE;
  }
  use_arm_kernels();
  MobileConfig config;
208 209

  config.set_model_dir(jstring_to_cpp_string(env, model_path));
H
Huihuang Zheng 已提交
210
  predictor = paddle::lite_api::CreatePaddlePredictor(config);
211 212 213 214 215 216 217 218 219 220
  return predictor == nullptr ? JNI_FALSE : JNI_TRUE;
}

JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_saveOptimizedModel(
    JNIEnv *env, jclass thiz, jstring model_path) {
  if (predictor == nullptr) {
    return JNI_FALSE;
  }
  predictor->SaveOptimizedModel(jstring_to_cpp_string(env, model_path));
H
Huihuang Zheng 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_clear(JNIEnv *env, jclass thiz) {
  if (predictor == nullptr) {
    return JNI_FALSE;
  }
  predictor.reset();
  return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_setInput__I_3I_3F(
    JNIEnv *env, jclass thiz, jint offset, jintArray dims, jfloatArray buf) {
236 237 238
  if (predictor == nullptr) {
    return JNI_FALSE;
  }
H
Huihuang Zheng 已提交
239 240 241 242 243 244 245 246 247 248 249 250
  std::vector<int64_t> ddim = jintarray_to_int64_vector(env, dims);

  int len = env->GetArrayLength(buf);
  if ((int64_t)len != product(ddim)) {
    return JNI_FALSE;
  }

  float *buffer = env->GetFloatArrayElements(buf, nullptr);
  std::unique_ptr<Tensor> tensor =
      predictor->GetInput(static_cast<int>(offset));
  tensor->Resize(ddim);
  float *input = tensor->mutable_data<float>();
251
  memcpy(input, buffer, sizeof(float) * len);
H
Huihuang Zheng 已提交
252 253 254 255 256 257
  return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_setInput__I_3I_3B(
    JNIEnv *env, jclass thiz, jint offset, jintArray dims, jbyteArray buf) {
258 259 260
  if (predictor == nullptr) {
    return JNI_FALSE;
  }
H
Huihuang Zheng 已提交
261 262 263 264 265 266 267 268 269 270 271 272
  std::vector<int64_t> ddim = jintarray_to_int64_vector(env, dims);

  int len = env->GetArrayLength(buf);
  if ((int64_t)len != product(ddim)) {
    return JNI_FALSE;
  }

  jbyte *buffer = env->GetByteArrayElements(buf, nullptr);
  std::unique_ptr<Tensor> tensor =
      predictor->GetInput(static_cast<int>(offset));
  tensor->Resize(ddim);
  int8_t *input = tensor->mutable_data<int8_t>();
273
  memcpy(input, buffer, sizeof(int8_t) * len);
H
Huihuang Zheng 已提交
274 275 276 277 278 279

  return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_run(JNIEnv *, jclass) {
280 281 282
  if (predictor == nullptr) {
    return JNI_FALSE;
  }
H
Huihuang Zheng 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  predictor->Run();
  return JNI_TRUE;
}

JNIEXPORT jfloatArray JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_getFloatOutput(JNIEnv *env,
                                                          jclass thiz,
                                                          jint offset) {
  std::unique_ptr<const Tensor> tensor =
      predictor->GetOutput(static_cast<int>(offset));
  int64_t len = product(tensor->shape());
  return cpp_array_to_jfloatarray(env, tensor->data<float>(), len);
}

JNIEXPORT jbyteArray JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_getByteOutput(JNIEnv *env,
                                                         jclass thiz,
                                                         jint offset) {
  std::unique_ptr<const Tensor> tensor =
      predictor->GetOutput(static_cast<int>(offset));
  int64_t len = product(tensor->shape());
  return cpp_array_to_jbytearray(env, tensor->data<int8_t>(), len);
}

JNIEXPORT jfloatArray JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_fetchFloat(JNIEnv *env, jclass thiz,
                                                      jstring name) {
  std::string cpp_name = jstring_to_cpp_string(env, name);
  std::unique_ptr<const Tensor> tensor = predictor->GetTensor(cpp_name);
  int64_t len = product(tensor->shape());
  return cpp_array_to_jfloatarray(env, tensor->data<float>(), len);
}

JNIEXPORT jbyteArray JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_fetchByte(JNIEnv *env, jclass thiz,
                                                     jstring name) {
  std::string cpp_name = jstring_to_cpp_string(env, name);
  std::unique_ptr<const Tensor> tensor = predictor->GetTensor(cpp_name);
  int64_t len = product(tensor->shape());
  return cpp_array_to_jbytearray(env, tensor->data<int8_t>(), len);
}

#ifdef __cplusplus
}
#endif