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

[Java API] add setThreads & setPowerMode interface (#1907)

上级 178a93b9
...@@ -21,6 +21,7 @@ add_jar(PaddlePredictor ...@@ -21,6 +21,7 @@ add_jar(PaddlePredictor
src/com/baidu/paddle/lite/MobileConfig.java src/com/baidu/paddle/lite/MobileConfig.java
src/com/baidu/paddle/lite/PaddleLiteInitializer.java src/com/baidu/paddle/lite/PaddleLiteInitializer.java
src/com/baidu/paddle/lite/PaddlePredictor.java src/com/baidu/paddle/lite/PaddlePredictor.java
src/com/baidu/paddle/lite/PowerMode.java
src/com/baidu/paddle/lite/Place.java src/com/baidu/paddle/lite/Place.java
src/com/baidu/paddle/lite/Tensor.java) src/com/baidu/paddle/lite/Tensor.java)
get_target_property(_jarFile PaddlePredictor JAR_FILE) get_target_property(_jarFile PaddlePredictor JAR_FILE)
......
...@@ -11,6 +11,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -11,6 +11,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#pragma once
#include <jni.h> #include <jni.h>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -19,9 +21,6 @@ limitations under the License. */ ...@@ -19,9 +21,6 @@ limitations under the License. */
#include "lite/api/paddle_api.h" #include "lite/api/paddle_api.h"
#include "lite/api/paddle_place.h" #include "lite/api/paddle_place.h"
#ifndef PADDLE_FLUID_LITE_API_ANDROID_JNI_NATIVE_CONVERT_UTIL_JNI_H_
#define PADDLE_FLUID_LITE_API_ANDROID_JNI_NATIVE_CONVERT_UTIL_JNI_H_
namespace paddle { namespace paddle {
namespace lite_api { namespace lite_api {
...@@ -167,20 +166,32 @@ inline MobileConfig jmobileconfig_to_cpp_mobileconfig(JNIEnv *env, ...@@ -167,20 +166,32 @@ inline MobileConfig jmobileconfig_to_cpp_mobileconfig(JNIEnv *env,
jobject jmobileconfig) { jobject jmobileconfig) {
jclass mobileconfig_jclazz = env->GetObjectClass(jmobileconfig); jclass mobileconfig_jclazz = env->GetObjectClass(jmobileconfig);
jmethodID model_dir_method = env->GetMethodID(
mobileconfig_jclazz, "getModelDir", "()Ljava/lang/String;");
MobileConfig config; MobileConfig config;
// set model dir
jmethodID model_dir_method = env->GetMethodID(
mobileconfig_jclazz, "getModelDir", "()Ljava/lang/String;");
jstring java_model_dir = jstring java_model_dir =
(jstring)env->CallObjectMethod(jmobileconfig, model_dir_method); (jstring)env->CallObjectMethod(jmobileconfig, model_dir_method);
if (java_model_dir != nullptr) { if (java_model_dir != nullptr) {
std::string cpp_model_dir = jstring_to_cpp_string(env, java_model_dir); std::string cpp_model_dir = jstring_to_cpp_string(env, java_model_dir);
config.set_model_dir(cpp_model_dir); config.set_model_dir(cpp_model_dir);
} }
// set threads
jmethodID threads_method =
env->GetMethodID(mobileconfig_jclazz, "getThreads", "()I");
int threads = env->CallIntMethod(jmobileconfig, threads_method);
config.set_threads(threads);
// set power mode
jmethodID power_mode_method =
env->GetMethodID(mobileconfig_jclazz, "getPowerModeInt", "()I");
int power_mode = env->CallIntMethod(jmobileconfig, power_mode_method);
config.set_power_mode(static_cast<paddle::lite_api::PowerMode>(power_mode));
return config; return config;
} }
} // namespace lite_api } // namespace lite_api
} // namespace paddle } // namespace paddle
#endif // PADDLE_FLUID_LITE_API_ANDROID_JNI_NATIVE_CONVERT_UTIL_JNI_H_
...@@ -18,5 +18,52 @@ package com.baidu.paddle.lite; ...@@ -18,5 +18,52 @@ package com.baidu.paddle.lite;
* optimization or other unnecessary stages. * optimization or other unnecessary stages.
*/ */
public class MobileConfig extends ConfigBase { public class MobileConfig extends ConfigBase {
// Empty class
/**
* Set power mode.
*
* @return
*/
public void setPowerMode(PowerMode powerMode) {
this.powerMode = powerMode;
}
/**
* Returns power mode.
*
* @return power mode
*/
public PowerMode getPowerMode() {
return powerMode;
}
/**
* Set threads num.
*
* @return
*/
public void setThreads(int threads) {
this.threads = threads;
}
/**
* Returns threads num.
*
* @return threads num
*/
public int getThreads() {
return threads;
}
/**
* Returns power mode as enum int value.
*
* @return power mode as enum int value
*/
public int getPowerModeInt() {
return powerMode.value();
}
private PowerMode powerMode = PowerMode.LITE_POWER_HIGH;
private int threads = 1;
} }
/* 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. */
package com.baidu.paddle.lite;
/**
* PowerMode is the cpu runing mode for the light weight predictor.
*/
public enum PowerMode {
LITE_POWER_HIGH(0),
LITE_POWER_LOW(1),
LITE_POWER_FULL(2),
LITE_POWER_NO_BIND(3),
LITE_POWER_RAND_HIGH(4),
LITE_POWER_RAND_LOW(5);
public final int value;
private PowerMode(int value) {
this.value = value;
}
public int value() {
return this.value;
}
}
...@@ -91,6 +91,8 @@ public class MainActivity extends AppCompatActivity { ...@@ -91,6 +91,8 @@ public class MainActivity extends AppCompatActivity {
MobileConfig config = new MobileConfig(); MobileConfig config = new MobileConfig();
config.setModelDir(modelPath); config.setModelDir(modelPath);
config.setPowerMode(PowerMode.LITE_POWER_HIGH);
config.setThreads(1);
PaddlePredictor predictor = PaddlePredictor.createPaddlePredictor(config); PaddlePredictor predictor = PaddlePredictor.createPaddlePredictor(config);
Tensor input = predictor.getInput(0); Tensor input = predictor.getInput(0);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册