Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA_API] Add JNI method for SaveModel #894

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions modules/java_api/src/main/cpp/openvino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ JNIEXPORT void JNICALL Java_org_intel_openvino_Openvino_serialize(JNIEnv *env, j
serialize(*model, xml_path, bin_path);
)
}

JNIEXPORT void JNICALL Java_org_intel_openvino_Openvino_SaveModel(JNIEnv *env, jobject obj, jlong modelAddr, jstring outputModel, jboolean compressToFp16)
{
JNI_METHOD("SaveModel",
std::string n_output_model = jstringToString(env, outputModel);
std::shared_ptr<const Model> *model = reinterpret_cast<std::shared_ptr<const Model> *>(modelAddr);
save_model(*model, n_output_model, (bool) compressToFp16);
)
}
1 change: 1 addition & 0 deletions modules/java_api/src/main/cpp/openvino_java.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C"
#endif
//ov
JNIEXPORT void JNICALL Java_org_intel_openvino_Openvino_serialize(JNIEnv *, jobject, jlong, jstring, jstring);
JNIEXPORT void JNICALL Java_org_intel_openvino_Openvino_SaveModel(JNIEnv *, jobject, jlong, jstring, jboolean);

// ov::Core
JNIEXPORT jlong JNICALL Java_org_intel_openvino_Core_GetCore(JNIEnv *, jobject);
Expand Down
19 changes: 19 additions & 0 deletions modules/java_api/src/main/java/org/intel/openvino/Openvino.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,27 @@ public static void serialize(Model model, final String xmlPath, final String bin
serialize(model.nativeObj, xmlPath, binPath);
}

/**
* Save model into IR files (xml and bin).
*
* <p>This method saves a model to IR applying all necessary transformations that are usually
* applied in model conversion flow provided by mo tool. Particularly, floating point weights
* are compressed to FP16, debug information in model nodes are cleaned up, etc.
*
* @param model Model which will be converted to IR representation.
* @param outputModel Path to output model file.
* @param compressToFp16 Whether to compress floating point weights to FP16.
*/
public static void save_model(
Model model, final String outputModel, final boolean compressToFp16) {
SaveModel(model.nativeObj, outputModel, compressToFp16);
}

/*----------------------------------- native methods -----------------------------------*/

private static native void serialize(
long modelAddr, final String xmlPath, final String binPath);

private static native void SaveModel(
long modelAddr, final String outputModel, final boolean compressToFp16);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ public void testSerialize() throws IOException {
Openvino.serialize(model, xmlPath.getAbsolutePath(), binPath.getAbsolutePath());
assertTrue(xmlPath.exists() && binPath.exists());
}

@Test
public void testSaveModel() throws IOException {
File tmp = Files.createTempDirectory("ovSaveModelTest").toFile();
File xmlPath = Paths.get(tmp.getAbsolutePath(), "saved_model.xml").toFile();
File binPath = Paths.get(tmp.getAbsolutePath(), "saved_model.bin").toFile();
xmlPath.deleteOnExit();
binPath.deleteOnExit();
tmp.deleteOnExit();

Openvino.save_model(model, xmlPath.getAbsolutePath(), false);
assertTrue(xmlPath.exists() && binPath.exists());
}
}
Loading