提交 71b05c94 编写于 作者: S sangoly 提交者: Yan Chunwei

[Java API] add getVersion() interface to get c++ lib's version information test=develop (#2063)

上级 3569483a
...@@ -58,7 +58,7 @@ if (LITE_WITH_LIGHT_WEIGHT_FRAMEWORK AND LITE_WITH_ARM) ...@@ -58,7 +58,7 @@ if (LITE_WITH_LIGHT_WEIGHT_FRAMEWORK AND LITE_WITH_ARM)
endif(LITE_WITH_NPU) endif(LITE_WITH_NPU)
if (LITE_WITH_FPGA) if (LITE_WITH_FPGA)
set(INFER_LITE_PUBLISH_ROOT "${INFER_LITE_PUBLISH_ROOT}.fpga") set(INFER_LITE_PUBLISH_ROOT "${INFER_LITE_PUBLISH_ROOT}.fpga")
endif(LITE_WITH_NPU) endif(LITE_WITH_FPGA)
message(STATUS "publish inference lib to ${INFER_LITE_PUBLISH_ROOT}") message(STATUS "publish inference lib to ${INFER_LITE_PUBLISH_ROOT}")
# The final target for publish lite lib # The final target for publish lite lib
......
...@@ -49,6 +49,27 @@ inline std::string jstring_to_cpp_string(JNIEnv *env, jstring jstr) { ...@@ -49,6 +49,27 @@ inline std::string jstring_to_cpp_string(JNIEnv *env, jstring jstr) {
return ret; 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, "<init>", "([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(data));
env->SetByteArrayRegion(
bytes, 0, strlen(data), reinterpret_cast<const jbyte *>(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, inline jfloatArray cpp_array_to_jfloatarray(JNIEnv *env,
const float *buf, const float *buf,
int64_t len) { int64_t len) {
......
...@@ -50,6 +50,16 @@ JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_lite_PaddlePredictor_run( ...@@ -50,6 +50,16 @@ JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_lite_PaddlePredictor_run(
return JNI_TRUE; return JNI_TRUE;
} }
JNIEXPORT jstring JNICALL Java_com_baidu_paddle_lite_PaddlePredictor_getVersion(
JNIEnv *env, jobject jpaddle_predictor) {
std::shared_ptr<PaddlePredictor> *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 JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_saveOptimizedModel( Java_com_baidu_paddle_lite_PaddlePredictor_saveOptimizedModel(
JNIEnv *env, jobject jpaddle_predictor, jstring model_dir) { JNIEnv *env, jobject jpaddle_predictor, jstring model_dir) {
......
...@@ -37,6 +37,14 @@ namespace lite_api { ...@@ -37,6 +37,14 @@ namespace lite_api {
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_com_baidu_paddle_lite_PaddlePredictor_run(JNIEnv *, jobject); 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 * Class: com_baidu_paddle_lite_PaddlePredictor
* Method: saveOptimizedModel * Method: saveOptimizedModel
......
...@@ -82,6 +82,13 @@ public class PaddlePredictor { ...@@ -82,6 +82,13 @@ public class PaddlePredictor {
*/ */
public native boolean run(); 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} * Saves the optimized model. It is available only for {@link CxxConfig}
* *
......
...@@ -20,9 +20,13 @@ public class MainActivity extends AppCompatActivity { ...@@ -20,9 +20,13 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
String textOutput = ""; String textOutput = "";
String version = getVersionInfo("lite_naive_model_opt.nb", this);
textOutput += "Version: " + version + "\n";
Tensor output; Tensor output;
output = setInputAndRunNaiveModel("lite_naive_model_opt.nb", this); 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"; + output.getFloatData()[1] + "\n";
textOutput += "expected: 50.2132, -28.8729\n"; textOutput += "expected: 50.2132, -28.8729\n";
...@@ -54,6 +58,14 @@ public class MainActivity extends AppCompatActivity { ...@@ -54,6 +58,14 @@ public class MainActivity extends AppCompatActivity {
textView.setText(textOutput); 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) { public static String copyFromAssetsToCache(String modelPath, Context context) {
String newPath = context.getCacheDir() + "/" + modelPath; String newPath = context.getCacheDir() + "/" + modelPath;
// String newPath = "/sdcard/" + modelPath; // String newPath = "/sdcard/" + modelPath;
......
...@@ -30,7 +30,6 @@ with open(ops_list_path) as f: ...@@ -30,7 +30,6 @@ with open(ops_list_path) as f:
paths = set([path for path in f]) paths = set([path for path in f])
for path in paths: for path in paths:
with open(path.strip()) as g: with open(path.strip()) as g:
print 'path: ', path
c = g.read() c = g.read()
kernel_parser = RegisterLiteKernelParser(c) kernel_parser = RegisterLiteKernelParser(c)
kernel_parser.parse() kernel_parser.parse()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册