forked from mindspore-Ecosystem/mindspore
!15846 add java api: CreateSession with three arguments
From: @hangangqiang Reviewed-by: @zhanghaibo5,@zhaizhiqiang,@HilbertDavid,@jpc_chenjianping Signed-off-by: @HilbertDavid
This commit is contained in:
commit
dfae126152
|
@ -16,6 +16,7 @@
|
|||
|
||||
package com.mindspore.lite;
|
||||
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -29,17 +30,39 @@ public class LiteSession {
|
|||
System.loadLibrary("mindspore-lite-jni");
|
||||
}
|
||||
|
||||
private long sessionPtr;
|
||||
private long sessionPtr = 0;
|
||||
|
||||
// Deprecated, please use "public static LiteSession createSession(final MSConfig config)" instead.
|
||||
public LiteSession() {
|
||||
this.sessionPtr = 0;
|
||||
}
|
||||
|
||||
// Deprecated, please use "public static LiteSession createSession(final MSConfig config)" instead.
|
||||
public boolean init(MSConfig config) {
|
||||
this.sessionPtr = createSession(config.getMSConfigPtr());
|
||||
return this.sessionPtr != 0;
|
||||
}
|
||||
|
||||
public static LiteSession createSession(final MSConfig config) {
|
||||
LiteSession liteSession = new LiteSession();
|
||||
liteSession.sessionPtr = liteSession.createSession(config.getMSConfigPtr());
|
||||
if (liteSession.sessionPtr == 0) {
|
||||
return null;
|
||||
} else {
|
||||
return liteSession;
|
||||
}
|
||||
}
|
||||
|
||||
public static LiteSession createSession(final MappedByteBuffer buffer, final MSConfig config) {
|
||||
LiteSession liteSession = new LiteSession();
|
||||
liteSession.sessionPtr = liteSession.createSessionWithModel(buffer, config.getMSConfigPtr());
|
||||
if (liteSession.sessionPtr == 0) {
|
||||
return null;
|
||||
} else {
|
||||
return liteSession;
|
||||
}
|
||||
}
|
||||
|
||||
public long getSessionPtr() {
|
||||
return sessionPtr;
|
||||
}
|
||||
|
@ -124,6 +147,8 @@ public class LiteSession {
|
|||
|
||||
private native long createSession(long msConfigPtr);
|
||||
|
||||
private native long createSessionWithModel(MappedByteBuffer buffer, long msConfigPtr);
|
||||
|
||||
private native boolean compileGraph(long sessionPtr, long modelPtr);
|
||||
|
||||
private native void bindThread(long sessionPtr, boolean if_bind);
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package com.mindspore.lite;
|
||||
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -29,17 +30,39 @@ public class LiteSession {
|
|||
System.loadLibrary("mindspore-lite-jni");
|
||||
}
|
||||
|
||||
private long sessionPtr;
|
||||
private long sessionPtr = 0;
|
||||
|
||||
// Deprecated, please use "public static LiteSession createSession(final MSConfig config)" instead.
|
||||
public LiteSession() {
|
||||
this.sessionPtr = 0;
|
||||
}
|
||||
|
||||
// Deprecated, please use "public static LiteSession createSession(final MSConfig config)" instead.
|
||||
public boolean init(MSConfig config) {
|
||||
this.sessionPtr = createSession(config.getMSConfigPtr());
|
||||
return this.sessionPtr != 0;
|
||||
}
|
||||
|
||||
public static LiteSession createSession(final MSConfig config) {
|
||||
LiteSession liteSession = new LiteSession();
|
||||
liteSession.sessionPtr = liteSession.createSession(config.getMSConfigPtr());
|
||||
if (liteSession.sessionPtr == 0) {
|
||||
return null;
|
||||
} else {
|
||||
return liteSession;
|
||||
}
|
||||
}
|
||||
|
||||
public static LiteSession createSession(final MappedByteBuffer buffer, final MSConfig config) {
|
||||
LiteSession liteSession = new LiteSession();
|
||||
liteSession.sessionPtr = liteSession.createSessionWithModel(buffer, config.getMSConfigPtr());
|
||||
if (liteSession.sessionPtr == 0) {
|
||||
return null;
|
||||
} else {
|
||||
return liteSession;
|
||||
}
|
||||
}
|
||||
|
||||
public long getSessionPtr() {
|
||||
return sessionPtr;
|
||||
}
|
||||
|
@ -124,6 +147,8 @@ public class LiteSession {
|
|||
|
||||
private native long createSession(long msConfigPtr);
|
||||
|
||||
private native long createSessionWithModel(MappedByteBuffer buffer, long msConfigPtr);
|
||||
|
||||
private native boolean compileGraph(long sessionPtr, long modelPtr);
|
||||
|
||||
private native void bindThread(long sessionPtr, boolean if_bind);
|
||||
|
|
|
@ -15,9 +15,35 @@
|
|||
*/
|
||||
|
||||
#include <jni.h>
|
||||
#include <fstream>
|
||||
#include "common/ms_log.h"
|
||||
#include "include/lite_session.h"
|
||||
#include "include/errorcode.h"
|
||||
extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_LiteSession_createSessionWithModel(JNIEnv *env, jobject thiz,
|
||||
jobject model_buffer,
|
||||
jlong ms_config_ptr) {
|
||||
// decode model buffer and buffer size
|
||||
if (model_buffer == nullptr) {
|
||||
MS_LOGE("Buffer from java is nullptr");
|
||||
return reinterpret_cast<jlong>(nullptr);
|
||||
}
|
||||
jlong buffer_len = env->GetDirectBufferCapacity(model_buffer);
|
||||
auto *model_buf = static_cast<char *>(env->GetDirectBufferAddress(model_buffer));
|
||||
// decode ms context
|
||||
auto *pointer = reinterpret_cast<void *>(ms_config_ptr);
|
||||
if (pointer == nullptr) {
|
||||
MS_LOGE("Context pointer from java is nullptr");
|
||||
return jlong(nullptr);
|
||||
}
|
||||
auto *lite_context_ptr = static_cast<mindspore::lite::Context *>(pointer);
|
||||
// create session
|
||||
auto session = mindspore::session::LiteSession::CreateSession(model_buf, buffer_len, lite_context_ptr);
|
||||
if (session == nullptr) {
|
||||
MS_LOGE("CreateSession failed");
|
||||
return jlong(nullptr);
|
||||
}
|
||||
return jlong(session);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_LiteSession_createSession(JNIEnv *env, jobject thiz,
|
||||
jlong ms_config_ptr) {
|
||||
|
|
Loading…
Reference in New Issue