From d919ed4cb1f8940170be31472e121ecd66f07ae2 Mon Sep 17 00:00:00 2001 From: sangoly Date: Thu, 19 Sep 2019 17:09:18 +0800 Subject: [PATCH] [Java API] add getVersion() interface to get c++ lib's version information test=develop (#2063) (#2072) --- .../api/android/jni/native/convert_util_jni.h | 21 +++++++++++++++++++ .../api/android/jni/native/paddle_lite_jni.cc | 10 +++++++++ lite/api/android/jni/native/paddle_lite_jni.h | 8 +++++++ .../baidu/paddle/lite/PaddlePredictor.java | 7 +++++++ .../com/baidu/paddle/lite/MainActivity.java | 14 ++++++++++++- .../cmake_tools/parse_kernel_registry.py | 1 - 6 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lite/api/android/jni/native/convert_util_jni.h b/lite/api/android/jni/native/convert_util_jni.h index ae987c330d..c9e8e2e4a6 100644 --- a/lite/api/android/jni/native/convert_util_jni.h +++ b/lite/api/android/jni/native/convert_util_jni.h @@ -49,6 +49,27 @@ inline std::string jstring_to_cpp_string(JNIEnv *env, jstring jstr) { return ret; } +inline jstring cpp_string_to_jstring(JNIEnv *env, std::string str) { + auto *data = str.c_str(); + jclass strClass = env->FindClass("java/lang/String"); + jmethodID strClassInitMethodID = + env->GetMethodID(strClass, "", "([BLjava/lang/String;)V"); + + jbyteArray bytes = env->NewByteArray(strlen(data)); + env->SetByteArrayRegion( + bytes, 0, strlen(data), reinterpret_cast(data)); + + jstring encoding = env->NewStringUTF("UTF-8"); + jstring res = (jstring)( + env->NewObject(strClass, strClassInitMethodID, bytes, encoding)); + + env->DeleteLocalRef(strClass); + env->DeleteLocalRef(encoding); + env->DeleteLocalRef(bytes); + + return res; +} + inline jfloatArray cpp_array_to_jfloatarray(JNIEnv *env, const float *buf, int64_t len) { diff --git a/lite/api/android/jni/native/paddle_lite_jni.cc b/lite/api/android/jni/native/paddle_lite_jni.cc index aa4ece6818..d0d2d603a9 100644 --- a/lite/api/android/jni/native/paddle_lite_jni.cc +++ b/lite/api/android/jni/native/paddle_lite_jni.cc @@ -50,6 +50,16 @@ JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_lite_PaddlePredictor_run( return JNI_TRUE; } +JNIEXPORT jstring JNICALL Java_com_baidu_paddle_lite_PaddlePredictor_getVersion( + JNIEnv *env, jobject jpaddle_predictor) { + std::shared_ptr *predictor = + getPaddlePredictorPointer(env, jpaddle_predictor); + if (predictor == nullptr || (*predictor == nullptr)) { + return cpp_string_to_jstring(env, ""); + } + return cpp_string_to_jstring(env, (*predictor)->GetVersion()); +} + JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_lite_PaddlePredictor_saveOptimizedModel( JNIEnv *env, jobject jpaddle_predictor, jstring model_dir) { diff --git a/lite/api/android/jni/native/paddle_lite_jni.h b/lite/api/android/jni/native/paddle_lite_jni.h index 913e9a4c3a..f447ce105a 100644 --- a/lite/api/android/jni/native/paddle_lite_jni.h +++ b/lite/api/android/jni/native/paddle_lite_jni.h @@ -37,6 +37,14 @@ namespace lite_api { JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_lite_PaddlePredictor_run(JNIEnv *, jobject); +/* + * Class: com_baidu_paddle_lite_PaddlePredictor + * Method: getVersion + * Signature: ()Z + */ +JNIEXPORT jstring JNICALL +Java_com_baidu_paddle_lite_PaddlePredictor_getVersion(JNIEnv *, jobject); + /* * Class: com_baidu_paddle_lite_PaddlePredictor * Method: saveOptimizedModel diff --git a/lite/api/android/jni/src/com/baidu/paddle/lite/PaddlePredictor.java b/lite/api/android/jni/src/com/baidu/paddle/lite/PaddlePredictor.java index d022fd7d61..efd35d23a1 100644 --- a/lite/api/android/jni/src/com/baidu/paddle/lite/PaddlePredictor.java +++ b/lite/api/android/jni/src/com/baidu/paddle/lite/PaddlePredictor.java @@ -82,6 +82,13 @@ public class PaddlePredictor { */ public native boolean run(); + /** + * Get c++ lib's version information. + * + * @return C++ lib's version information. + */ + public native String getVersion(); + /** * Saves the optimized model. It is available only for {@link CxxConfig} * diff --git a/lite/demo/java/android/PaddlePredictor/app/src/main/java/com/baidu/paddle/lite/MainActivity.java b/lite/demo/java/android/PaddlePredictor/app/src/main/java/com/baidu/paddle/lite/MainActivity.java index e8eb01bd55..84bebe6f2a 100644 --- a/lite/demo/java/android/PaddlePredictor/app/src/main/java/com/baidu/paddle/lite/MainActivity.java +++ b/lite/demo/java/android/PaddlePredictor/app/src/main/java/com/baidu/paddle/lite/MainActivity.java @@ -20,9 +20,13 @@ public class MainActivity extends AppCompatActivity { setContentView(R.layout.activity_main); String textOutput = ""; + + String version = getVersionInfo("lite_naive_model_opt.nb", this); + textOutput += "Version: " + version + "\n"; + Tensor output; output = setInputAndRunNaiveModel("lite_naive_model_opt.nb", this); - textOutput += "lite_naive_model output: " + output.getFloatData()[0] + ", " + textOutput += "\nlite_naive_model output: " + output.getFloatData()[0] + ", " + output.getFloatData()[1] + "\n"; textOutput += "expected: 50.2132, -28.8729\n"; @@ -54,6 +58,14 @@ public class MainActivity extends AppCompatActivity { textView.setText(textOutput); } + public static String getVersionInfo(String modelName, Context context) { + String modelPath = copyFromAssetsToCache(modelName, context); + MobileConfig config = new MobileConfig(); + config.setModelDir(modelPath); + PaddlePredictor predictor = PaddlePredictor.createPaddlePredictor(config); + return predictor.getVersion(); + } + public static String copyFromAssetsToCache(String modelPath, Context context) { String newPath = context.getCacheDir() + "/" + modelPath; // String newPath = "/sdcard/" + modelPath; diff --git a/lite/tools/cmake_tools/parse_kernel_registry.py b/lite/tools/cmake_tools/parse_kernel_registry.py index b9bfbb2692..50d2886414 100644 --- a/lite/tools/cmake_tools/parse_kernel_registry.py +++ b/lite/tools/cmake_tools/parse_kernel_registry.py @@ -30,7 +30,6 @@ with open(ops_list_path) as f: paths = set([path for path in f]) for path in paths: with open(path.strip()) as g: - print 'path: ', path c = g.read() kernel_parser = RegisterLiteKernelParser(c) kernel_parser.parse() -- GitLab