diff --git a/mindspore/lite/examples/quick_start_server_inference_java/build.sh b/mindspore/lite/examples/quick_start_server_inference_java/build.sh
new file mode 100644
index 00000000000..59f8af502f8
--- /dev/null
+++ b/mindspore/lite/examples/quick_start_server_inference_java/build.sh
@@ -0,0 +1,43 @@
+#!/bin/bash
+# Copyright 2021 Huawei Technologies Co., Ltd
+#
+# 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.
+# ============================================================================
+
+BASEPATH=$(cd "$(dirname $0)" || exit; pwd)
+get_version() {
+ VERSION_MAJOR=$(grep "const int ms_version_major =" ${BASEPATH}/../../include/version.h | tr -dc "[0-9]")
+ VERSION_MINOR=$(grep "const int ms_version_minor =" ${BASEPATH}/../../include/version.h | tr -dc "[0-9]")
+ VERSION_REVISION=$(grep "const int ms_version_revision =" ${BASEPATH}/../../include/version.h | tr -dc "[0-9]")
+ VERSION_STR=${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION}
+}
+get_version
+MODEL_DOWNLOAD_URL="https://download.mindspore.cn/model_zoo/official/lite/quick_start/mobilenetv2.ms"
+MINDSPORE_FILE_NAME="mindspore-lite-${VERSION_STR}-linux-x64"
+MINDSPORE_FILE="${MINDSPORE_FILE_NAME}.tar.gz"
+MINDSPORE_LITE_DOWNLOAD_URL="https://ms-release.obs.cn-north-4.myhuaweicloud.com/${VERSION_STR}/MindSpore/lite/release/linux/x86_64/${MINDSPORE_FILE}"
+
+mkdir -p build
+mkdir -p lib
+mkdir -p model
+if [ ! -e ${BASEPATH}/model/mobilenetv2.ms ]; then
+ wget -c -O ${BASEPATH}/model/mobilenetv2.ms --no-check-certificate ${MODEL_DOWNLOAD_URL}
+fi
+if [ ! -e ${BASEPATH}/build/${MINDSPORE_FILE} ]; then
+ wget -c -O ${BASEPATH}/build/${MINDSPORE_FILE} --no-check-certificate ${MINDSPORE_LITE_DOWNLOAD_URL}
+fi
+tar xzvf ${BASEPATH}/build/${MINDSPORE_FILE} -C ${BASEPATH}/build/
+cp -r ${BASEPATH}/build/${MINDSPORE_FILE_NAME}/runtime/lib/* ${BASEPATH}/lib
+cd ${BASEPATH}/ || exit
+
+mvn package
diff --git a/mindspore/lite/examples/quick_start_server_inference_java/pom.xml b/mindspore/lite/examples/quick_start_server_inference_java/pom.xml
new file mode 100644
index 00000000000..f1aebed5cf4
--- /dev/null
+++ b/mindspore/lite/examples/quick_start_server_inference_java/pom.xml
@@ -0,0 +1,55 @@
+
+
+ 4.0.0
+
+ com.mindspore.lite.demo
+ quick_start_java
+ 1.0
+
+
+ 8
+ 8
+
+
+
+
+
+ com.mindspore.lite
+ mindspore-lite-java
+ 1.0
+ system
+ ${project.basedir}/lib/mindspore-lite-java.jar
+
+
+
+
+ ${project.name}
+
+
+ org.apache.maven.plugins
+ maven-assembly-plugin
+
+
+
+ com.mindspore.lite.demo.Main
+
+
+
+ jar-with-dependencies
+
+
+
+
+ make-assemble
+ package
+
+ single
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/mindspore/lite/examples/quick_start_server_inference_java/src/main/java/com/mindspore/lite/demo/Main.java b/mindspore/lite/examples/quick_start_server_inference_java/src/main/java/com/mindspore/lite/demo/Main.java
new file mode 100644
index 00000000000..a089d38fa30
--- /dev/null
+++ b/mindspore/lite/examples/quick_start_server_inference_java/src/main/java/com/mindspore/lite/demo/Main.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2022 Huawei Technologies Co., Ltd
+ *
+ * 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.mindspore.lite.demo;
+import com.mindspore.config.DataType;
+import com.mindspore.config.DeviceType;
+import com.mindspore.config.MSContext;
+import com.mindspore.config.RunnerConfig;
+import com.mindspore.ModelParallelRunner;
+import com.mindspore.MSTensor;
+import com.mindspore.Model;
+import com.mindspore.config.ModelType;
+import com.mindspore.config.Version;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.FloatBuffer;
+import java.util.Random;
+
+import java.nio.ByteBuffer;
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class Main {
+ public static float[] generateArray(int len) {
+ Random rand = new Random();
+ float[] arr = new float[len];
+ for (int i = 0; i < arr.length; i++) {
+ arr[i] = rand.nextFloat();
+ }
+ return arr;
+ }
+
+ private static ByteBuffer floatArrayToByteBuffer(float[] floats) {
+ if (floats == null) {
+ return null;
+ }
+ ByteBuffer buffer = ByteBuffer.allocateDirect(floats.length * Float.BYTES);
+ buffer.order(ByteOrder.LITTLE_ENDIAN);
+ FloatBuffer floatBuffer = buffer.asFloatBuffer();
+ floatBuffer.put(floats);
+ return buffer;
+ }
+
+ public static void main(String[] args) {
+ System.out.println(Version.version());
+ if (args.length < 1) {
+ System.err.println("The model path parameter must be passed.");
+ return;
+ }
+ String modelPath = args[0];
+
+ // use default param init context
+ MSContext context = new MSContext();
+ context.init(1,0);
+ boolean ret = context.addDeviceInfo(DeviceType.DT_CPU, false, 0);
+ if (!ret) {
+ System.err.println("init context failed");
+ context.free();
+ return ;
+ }
+
+ // init runner config
+ RunnerConfig config = new RunnerConfig();
+ config.init(context);
+ config.setWorkersNum(2);
+
+ // init ModelParallelRunner
+ ModelParallelRunner runner = new ModelParallelRunner();
+ ret = runner.init(modelPath, config);
+ if (!ret) {
+ System.err.println("ModelParallelRunner init failed.");
+ runner.free();
+ return;
+ }
+
+ // init input tensor
+ List inputs = new ArrayList<>();
+ MSTensor input = runner.getInputs().get(0);
+ if (input.getDataType() != DataType.kNumberTypeFloat32) {
+ System.err.println("Input tensor data type is not float, the data type is " + input.getDataType());
+ return;
+ }
+ // Generator Random Data.
+ int elementNums = input.elementsNum();
+ float[] randomData = generateArray(elementNums);
+ ByteBuffer inputData = floatArrayToByteBuffer(randomData);
+ // create input MSTensor
+ MSTensor inputTensor = MSTensor.createTensor(input.tensorName(), DataType.kNumberTypeFloat32,input.getShape(), inputData);
+ inputs.add(inputTensor);
+
+ // init output
+ List outputs = new ArrayList<>();
+
+ // runner do predict
+ ret = runner.predict(inputs,outputs);
+ if (!ret) {
+ System.err.println("MindSpore Lite predict failed.");
+ runner.free();
+ return;
+ }
+ System.err.println("========== model parallel runner predict success ==========");
+ runner.free();
+ }
+}
diff --git a/mindspore/lite/java/native/CMakeLists.txt b/mindspore/lite/java/native/CMakeLists.txt
index 1d7356b915e..561408c997f 100644
--- a/mindspore/lite/java/native/CMakeLists.txt
+++ b/mindspore/lite/java/native/CMakeLists.txt
@@ -36,6 +36,10 @@ set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
+if(DEFINED ENV{MSLITE_ENABLE_SERVER_INFERENCE})
+ set(MSLITE_ENABLE_SERVER_INFERENCE $ENV{MSLITE_ENABLE_SERVER_INFERENCE})
+endif()
+
if(ENABLE_VERBOSE)
set(CMAKE_VERBOSE_MAKEFILE on)
endif()
@@ -79,7 +83,8 @@ set(JNI_SRC
)
if(MSLITE_ENABLE_SERVER_INFERENCE)
- set(JNI_TRAIN_SRC
+ set(JNI_SRC
+ ${JNI_SRC}
${NEW_NATIVE_DIR}/runner_config.cpp
${NEW_NATIVE_DIR}/model_parallel_runner.cpp
)
diff --git a/mindspore/lite/java/src/main/java/com/mindspore/config/RunnerConfig.java b/mindspore/lite/java/src/main/java/com/mindspore/config/RunnerConfig.java
index b5a445099a6..83d1ef898a0 100644
--- a/mindspore/lite/java/src/main/java/com/mindspore/config/RunnerConfig.java
+++ b/mindspore/lite/java/src/main/java/com/mindspore/config/RunnerConfig.java
@@ -48,12 +48,12 @@ public class RunnerConfig {
}
/**
- * Set num models
+ * Set workers num
*
- * @param numModel The number of parallel models.
+ * @param workersNum The number of parallel models.
*/
- public void setNumModel(int numModel) {
- setNumModel(runnerConfigPtr, numModel);
+ public void setWorkersNum(int workersNum) {
+ setWorkersNum(runnerConfigPtr, workersNum);
}
/**
@@ -67,6 +67,6 @@ public class RunnerConfig {
private native long createRunnerConfig(long msContextPtr);
- private native void setNumModel(long runnerConfigPtr, int numModel);
+ private native void setWorkersNum(long runnerConfigPtr, int workersNum);
}
diff --git a/mindspore/lite/java/src/main/native/model_parallel_runner.cpp b/mindspore/lite/java/src/main/native/model_parallel_runner.cpp
index bdaf3b9a6e3..7408612553a 100644
--- a/mindspore/lite/java/src/main/native/model_parallel_runner.cpp
+++ b/mindspore/lite/java/src/main/native/model_parallel_runner.cpp
@@ -126,11 +126,11 @@ extern "C" JNIEXPORT jobject JNICALL Java_com_mindspore_ModelParallelRunner_pred
}
extern "C" JNIEXPORT void JNICALL Java_com_mindspore_ModelParallelRunner_free(JNIEnv *env, jobject thiz,
- jlong model_parallel_runner_ptr) {
-auto *pointer = reinterpret_cast(model_parallel_runner_ptr);
-if (pointer == nullptr) {
-MS_LOGE("ModelParallelRunner pointer from java is nullptr");
-return;
-}
-delete pointer;
+ jlong model_parallel_runner_ptr) {
+ auto *pointer = reinterpret_cast(model_parallel_runner_ptr);
+ if (pointer == nullptr) {
+ MS_LOGE("ModelParallelRunner pointer from java is nullptr");
+ return;
+ }
+ delete pointer;
}
diff --git a/mindspore/lite/java/src/main/native/runner_config.cpp b/mindspore/lite/java/src/main/native/runner_config.cpp
index c4fea466319..ccc2b0c2a99 100644
--- a/mindspore/lite/java/src/main/native/runner_config.cpp
+++ b/mindspore/lite/java/src/main/native/runner_config.cpp
@@ -38,17 +38,17 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_config_RunnerConfig_create
return (jlong) nullptr;
}
context.reset(c_context_ptr);
- runner_config->model_ctx = context;
+ runner_config->context = context;
return (jlong)runner_config;
}
-extern "C" JNIEXPORT void JNICALL Java_com_mindspore_config_RunnerConfig_setNumModel(JNIEnv *env, jobject thiz,
- jstring runner_config_ptr,
-jint num_model) {
-auto *pointer = reinterpret_cast(runner_config_ptr);
-if (pointer == nullptr) {
-MS_LOGE("runner config pointer from java is nullptr");
-return;
-}
-pointer->num_model = num_model;
+extern "C" JNIEXPORT void JNICALL Java_com_mindspore_config_RunnerConfig_setWorkersNum(JNIEnv *env, jobject thiz,
+ jstring runner_config_ptr,
+ jint workers_num) {
+ auto *pointer = reinterpret_cast(runner_config_ptr);
+ if (pointer == nullptr) {
+ MS_LOGE("runner config pointer from java is nullptr");
+ return;
+ }
+ pointer->workers_num = workers_num;
}
diff --git a/mindspore/lite/src/pack_weight_manager.cc b/mindspore/lite/src/pack_weight_manager.cc
index 7aba4199a5e..3aca479ccf5 100644
--- a/mindspore/lite/src/pack_weight_manager.cc
+++ b/mindspore/lite/src/pack_weight_manager.cc
@@ -198,12 +198,12 @@ void PackWeightManager::FreePackedWeight(ModelConstWeight *weight) {
PackWeightManager::~PackWeightManager() {
for (auto &item : path_model_weight_) {
FreePackedWeight(item.second);
- path_model_weight_.erase(item.first);
}
+ path_model_weight_.clear();
for (auto &item : buf_model_weight_) {
FreePackedWeight(item.second);
- buf_model_weight_.erase(item.first);
}
+ buf_model_weight_.clear();
}
} // namespace mindspore::lite
#endif
diff --git a/mindspore/lite/src/pack_weight_manager.h b/mindspore/lite/src/pack_weight_manager.h
index 3e03d36cf3c..0c476f0d46c 100644
--- a/mindspore/lite/src/pack_weight_manager.h
+++ b/mindspore/lite/src/pack_weight_manager.h
@@ -57,7 +57,7 @@ class PackWeightManager {
void FreePackedWeight(ModelConstWeight *weight);
std::map path_model_weight_;
- std::map buf_model_weight_;
+ std::map buf_model_weight_;
std::map> path_model_buf_;
std::mutex mtx_weight_;
};