forked from huawei/mindspore2022
add java api and fix some bug
This commit is contained in:
parent
fdf7aebd78
commit
b71b133ffa
|
|
@ -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
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns=""
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.mindspore.lite.demo</groupId>
|
||||
<artifactId>quick_start_java</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.mindspore.lite</groupId>
|
||||
<artifactId>mindspore-lite-java</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/lib/mindspore-lite-java.jar</systemPath>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.name}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.mindspore.lite.demo.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assemble</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
@ -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<MSTensor> 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<MSTensor> 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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<mindspore::ModelParallelRunner *>(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<mindspore::ModelParallelRunner *>(model_parallel_runner_ptr);
|
||||
if (pointer == nullptr) {
|
||||
MS_LOGE("ModelParallelRunner pointer from java is nullptr");
|
||||
return;
|
||||
}
|
||||
delete pointer;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<mindspore::RunnerConfig *>(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<mindspore::RunnerConfig *>(runner_config_ptr);
|
||||
if (pointer == nullptr) {
|
||||
MS_LOGE("runner config pointer from java is nullptr");
|
||||
return;
|
||||
}
|
||||
pointer->workers_num = workers_num;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ class PackWeightManager {
|
|||
void FreePackedWeight(ModelConstWeight *weight);
|
||||
|
||||
std::map<const std::string, ModelConstWeight *> path_model_weight_;
|
||||
std::map<const std::string, ModelConstWeight *> buf_model_weight_;
|
||||
std::map<const void *, ModelConstWeight *> buf_model_weight_;
|
||||
std::map<const std::string, std::vector<const void *>> path_model_buf_;
|
||||
std::mutex mtx_weight_;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue