Skip to content

Commit a845222

Browse files
[JAVA_API] Add JNI method for SaveModel (#894)
Co-authored-by: Anna Likholat <anna.likholat@intel.com>
1 parent ea2a29e commit a845222

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

modules/java_api/src/main/cpp/openvino.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ JNIEXPORT void JNICALL Java_org_intel_openvino_Openvino_serialize(JNIEnv *env, j
2020
serialize(*model, xml_path, bin_path);
2121
)
2222
}
23+
24+
JNIEXPORT void JNICALL Java_org_intel_openvino_Openvino_SaveModel(JNIEnv *env, jobject obj, jlong modelAddr, jstring outputModel, jboolean compressToFp16)
25+
{
26+
JNI_METHOD("SaveModel",
27+
std::string n_output_model = jstringToString(env, outputModel);
28+
std::shared_ptr<const Model> *model = reinterpret_cast<std::shared_ptr<const Model> *>(modelAddr);
29+
save_model(*model, n_output_model, (bool) compressToFp16);
30+
)
31+
}

modules/java_api/src/main/cpp/openvino_java.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C"
99
#endif
1010
//ov
1111
JNIEXPORT void JNICALL Java_org_intel_openvino_Openvino_serialize(JNIEnv *, jobject, jlong, jstring, jstring);
12+
JNIEXPORT void JNICALL Java_org_intel_openvino_Openvino_SaveModel(JNIEnv *, jobject, jlong, jstring, jboolean);
1213

1314
// ov::Core
1415
JNIEXPORT jlong JNICALL Java_org_intel_openvino_Core_GetCore(JNIEnv *, jobject);

modules/java_api/src/main/java/org/intel/openvino/Openvino.java

+19
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,27 @@ public static void serialize(Model model, final String xmlPath, final String bin
2424
serialize(model.nativeObj, xmlPath, binPath);
2525
}
2626

27+
/**
28+
* Save model into IR files (xml and bin).
29+
*
30+
* <p>This method saves a model to IR applying all necessary transformations that are usually
31+
* applied in model conversion flow provided by mo tool. Particularly, floating point weights
32+
* are compressed to FP16, debug information in model nodes are cleaned up, etc.
33+
*
34+
* @param model Model which will be converted to IR representation.
35+
* @param outputModel Path to output model file.
36+
* @param compressToFp16 Whether to compress floating point weights to FP16.
37+
*/
38+
public static void save_model(
39+
Model model, final String outputModel, final boolean compressToFp16) {
40+
SaveModel(model.nativeObj, outputModel, compressToFp16);
41+
}
42+
2743
/*----------------------------------- native methods -----------------------------------*/
2844

2945
private static native void serialize(
3046
long modelAddr, final String xmlPath, final String binPath);
47+
48+
private static native void SaveModel(
49+
long modelAddr, final String outputModel, final boolean compressToFp16);
3150
}

modules/java_api/src/test/java/org/intel/openvino/OpenvinoTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,17 @@ public void testSerialize() throws IOException {
3232
Openvino.serialize(model, xmlPath.getAbsolutePath(), binPath.getAbsolutePath());
3333
assertTrue(xmlPath.exists() && binPath.exists());
3434
}
35+
36+
@Test
37+
public void testSaveModel() throws IOException {
38+
File tmp = Files.createTempDirectory("ovSaveModelTest").toFile();
39+
File xmlPath = Paths.get(tmp.getAbsolutePath(), "saved_model.xml").toFile();
40+
File binPath = Paths.get(tmp.getAbsolutePath(), "saved_model.bin").toFile();
41+
xmlPath.deleteOnExit();
42+
binPath.deleteOnExit();
43+
tmp.deleteOnExit();
44+
45+
Openvino.save_model(model, xmlPath.getAbsolutePath(), false);
46+
assertTrue(xmlPath.exists() && binPath.exists());
47+
}
3548
}

0 commit comments

Comments
 (0)