未验证 提交 d919ed4c 编写于 作者: S sangoly 提交者: GitHub

[Java API] add getVersion() interface to get c++ lib's version information...

[Java API] add getVersion() interface to get c++ lib's version information test=develop (#2063) (#2072)
上级 08f6217b
......@@ -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, "<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,
const float *buf,
int64_t len) {
......
......@@ -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<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
Java_com_baidu_paddle_lite_PaddlePredictor_saveOptimizedModel(
JNIEnv *env, jobject jpaddle_predictor, jstring model_dir) {
......
......@@ -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
......
......@@ -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}
*
......
......@@ -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;
......
......@@ -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()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册