• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <jni.h>
18 #include <fstream>
19 #include "common/ms_log.h"
20 #include "include/model.h"
21 #include "common/jni_utils.h"
22 
Java_com_mindspore_lite_Model_loadModel(JNIEnv * env,jobject thiz,jobject buffer)23 extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModel(JNIEnv *env, jobject thiz, jobject buffer) {
24   if (buffer == nullptr) {
25     MS_LOGE("Buffer from java is nullptr");
26     return reinterpret_cast<jlong>(nullptr);
27   }
28   jlong buffer_len = env->GetDirectBufferCapacity(buffer);
29   auto *model_buffer = static_cast<char *>(env->GetDirectBufferAddress(buffer));
30 
31   auto model = mindspore::lite::Model::Import(model_buffer, buffer_len);
32   if (model == nullptr) {
33     MS_LOGE("Import model failed");
34     return reinterpret_cast<jlong>(nullptr);
35   }
36   return reinterpret_cast<jlong>(model);
37 }
38 
Java_com_mindspore_lite_Model_loadModelByPath(JNIEnv * env,jobject thiz,jstring model_path)39 extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModelByPath(JNIEnv *env, jobject thiz,
40                                                                                  jstring model_path) {
41   auto model_path_char = RealPath(env->GetStringUTFChars(model_path, JNI_FALSE));
42   if (model_path_char.empty()) {
43     MS_LOGE("model_path_char is nullptr");
44     return reinterpret_cast<jlong>(nullptr);
45   }
46   std::ifstream ifs(model_path_char);
47   if (!ifs.good()) {
48     MS_LOGE("file: %s is not exist", model_path_char.c_str());
49     return reinterpret_cast<jlong>(nullptr);
50   }
51 
52   if (!ifs.is_open()) {
53     MS_LOGE("file: %s open failed", model_path_char.c_str());
54     return reinterpret_cast<jlong>(nullptr);
55   }
56 
57   ifs.seekg(0, std::ios::end);
58   auto size = ifs.tellg();
59   auto buf = new (std::nothrow) char[size];
60   if (buf == nullptr) {
61     MS_LOGE("malloc buf failed, file: %s", model_path_char.c_str());
62     ifs.close();
63     return reinterpret_cast<jlong>(nullptr);
64   }
65 
66   ifs.seekg(0, std::ios::beg);
67   ifs.read(buf, size);
68   ifs.close();
69   auto model = mindspore::lite::Model::Import(buf, size);
70   delete[] buf;
71   if (model == nullptr) {
72     MS_LOGE("Import model failed");
73     return reinterpret_cast<jlong>(nullptr);
74   }
75   return reinterpret_cast<jlong>(model);
76 }
77 
Java_com_mindspore_lite_Model_free(JNIEnv * env,jobject thiz,jlong model_ptr)78 extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_Model_free(JNIEnv *env, jobject thiz, jlong model_ptr) {
79   auto *pointer = reinterpret_cast<void *>(model_ptr);
80   if (pointer == nullptr) {
81     MS_LOGE("Model pointer from java is nullptr");
82     return;
83   }
84   auto *lite_model_ptr = static_cast<mindspore::lite::Model *>(pointer);
85   delete (lite_model_ptr);
86 }
87 
Java_com_mindspore_lite_Model_freeBuffer(JNIEnv * env,jobject thiz,jlong model_ptr)88 extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_Model_freeBuffer(JNIEnv *env, jobject thiz, jlong model_ptr) {
89   auto *pointer = reinterpret_cast<void *>(model_ptr);
90   if (pointer == nullptr) {
91     MS_LOGE("Model pointer from java is nullptr");
92     return;
93   }
94   auto *lite_model_ptr = static_cast<mindspore::lite::Model *>(pointer);
95   lite_model_ptr->Free();
96 }
97