Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[DOC] add cache_encryption_callbacks example in guide (#26432)

Fang Xu91b574489527
diff --git a/docs/articles_en/assets/snippets/ov_caching.cpp b/docs/articles_en/assets/snippets/ov_caching.cpp
index 891d3e93682..aa08a739261 100644
--- a/docs/articles_en/assets/snippets/ov_caching.cpp
+++ b/docs/articles_en/assets/snippets/ov_caching.cpp
@@ -60,12 +60,43 @@ bool cachingSupported = std::find(caps.begin(), caps.end(), ov::device::capabili
}
}
+void part4() {
+ std::string modelPath = "/tmp/myModel.xml";
+ std::string device = "CPU";
+ ov::Core core; // Step 1: create ov::Core object
+ core.set_property(ov::cache_dir("/path/to/cache/dir")); // Step 1b: Enable caching
+ auto model = core.read_model(modelPath); // Step 2: Read Model
+//! [ov:caching:part4]
+ov::AnyMap config;
+ov::EncryptionCallbacks encryption_callbacks;
+static const char codec_key[] = {0x30, 0x60, 0x70, 0x02, 0x04, 0x08, 0x3F, 0x6F, 0x72, 0x74, 0x78, 0x7F};
+auto codec_xor = [&](const std::string& source_str) {
+ auto key_size = sizeof(codec_key);
+ int key_idx = 0;
+ std::string dst_str = source_str;
+ for (char& c : dst_str) {
+ c ^= codec_key[key_idx % key_size];
+ key_idx++;
+ }
+ return dst_str;
+};
+encryption_callbacks.encrypt = codec_xor;
+encryption_callbacks.decrypt = codec_xor;
+config.insert(ov::cache_encryption_callbacks(encryption_callbacks)); // Step 4: Set device configuration
+auto compiled = core.compile_model(model, device, config); // Step 5: LoadNetwork
+//! [ov:caching:part4]
+ if (!compiled) {
+ throw std::runtime_error("error");
+ }
+}
+
int main() {
try {
part0();
part1();
part2();
part3();
+ part4();
} catch (...) {
}
return 0;
diff --git a/docs/articles_en/assets/snippets/ov_caching.py b/docs/articles_en/assets/snippets/ov_caching.py
index 4ce0b91ccd7..57bd72f3f9b 100644
--- a/docs/articles_en/assets/snippets/ov_caching.py
+++ b/docs/articles_en/assets/snippets/ov_caching.py
@@ -42,3 +42,20 @@ import openvino.properties.device as device
# Find 'EXPORT_IMPORT' capability in supported capabilities
caching_supported = 'EXPORT_IMPORT' in core.get_property(device_name, device.capabilities)
# ! [ov:caching:part3]
+
+# ! [ov:caching:part4]
+import base64
+
+def encrypt_base64(src):
+ return base64.b64encode(bytes(src, "utf-8"))
+
+def decrypt_base64(src):
+ return base64.b64decode(bytes(src, "utf-8"))
+
+core = ov.Core()
+core.set_property({props.cache_dir: path_to_cache_dir})
+config_cache = {}
+config_cache["CACHE_ENCRYPTION_CALLBACKS"] = [encrypt_base64, decrypt_base64]
+model = core.read_model(model=model_path)
+compiled_model = core.compile_model(model=model, device_name=device_name, config=config_cache)
+# ! [ov:caching:part4]
diff --git a/docs/articles_en/openvino-workflow/running-inference/optimize-inference/optimizing-latency/model-caching-overview.rst b/docs/articles_en/openvino-workflow/running-inference/optimize-inference/optimizing-latency/model-caching-overview.rst
index 09701ab97d2..f47470d6097 100644
--- a/docs/articles_en/openvino-workflow/running-inference/optimize-inference/optimizing-latency/model-caching-overview.rst
+++ b/docs/articles_en/openvino-workflow/running-inference/optimize-inference/optimizing-latency/model-caching-overview.rst
@@ -138,3 +138,29 @@ To check in advance if a particular device supports model caching, your applicat
:language: cpp
:fragment: [ov:caching:part3]
+Set "cache_encryption_callbacks" config option to enable cache encryption
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+With model caching enabled, model topology in cache can be encrypted/decrypted when saving/loading model cache.
+This property can currently only be set in ``compile_model``.
+
+.. tab-set::
+
+ .. tab-item:: Python
+ :sync: py
+
+ .. doxygensnippet:: docs/articles_en/assets/snippets/ov_caching.py
+ :language: py
+ :fragment: [ov:caching:part4]
+
+ .. tab-item:: C++
+ :sync: cpp
+
+ .. doxygensnippet:: docs/articles_en/assets/snippets/ov_caching.cpp
+ :language: cpp
+ :fragment: [ov:caching:part4]
+
+.. important::
+
+ Currently, this property is only supported by CPU plugin. For other HW plugins, setting this property will perform
+ normally but do not encrypt/decrypt the model topology in cache.
diff --git a/src/bindings/c/include/openvino/c/ov_property.h b/src/bindings/c/include/openvino/c/ov_property.h
index 905d473fc36..c2e855f766c 100644
--- a/src/bindings/c/include/openvino/c/ov_property.h
+++ b/src/bindings/c/include/openvino/c/ov_property.h
@@ -109,8 +109,8 @@ ov_property_key_cache_mode;
/**
* @brief Write-only property<ov_encryption_callbacks*> to set encryption/decryption function for model cache.
- * If ov_property_key_cache_encryption_callbacks is set, model cache will be encrypted/decrypted when saving/loading
- * model cache. ov_property_key_cache_encryption_callbacks is enabled in ov_core_compile_model_* only
+ * If ov_property_key_cache_encryption_callbacks is set, model topology in cache will be encrypted/decrypted when
+ * saving/loading model cache. This property is enabled in ov_core_compile_model_* only
* @ingroup ov_property_c_api
*/
OPENVINO_C_VAR(const char*)
@@ -245,4 +245,4 @@ ov_property_key_enable_mmap;
* @ingroup ov_property_c_api
*/
OPENVINO_C_VAR(const char*)
-ov_property_key_auto_batch_timeout;
\ No newline at end of file
+ov_property_key_auto_batch_timeout;
diff --git a/src/inference/include/openvino/runtime/properties.hpp b/src/inference/include/openvino/runtime/properties.hpp
index 878cba81590..abc765cafe2 100644
--- a/src/inference/include/openvino/runtime/properties.hpp
+++ b/src/inference/include/openvino/runtime/properties.hpp
@@ -791,8 +791,8 @@ struct EncryptionCallbacks {
/**
* @brief Write-only property to set encryption/decryption function for saving/loading model cache.
- * If cache_encryption_callbacks is set, the model cache will be encrypted/decrypted when saving/loading cache.
- * cache_encryption_callbacks is enabled in core.compile_model only.
+ * If cache_encryption_callbacks is set, the model topology in cache will be encrypted/decrypted when saving/loading
+ * cache. cache_encryption_callbacks is enabled in core.compile_model only.
* - First value of the struct is encryption function.
* - Second value of the struct is decryption function.
* @ingroup ov_runtime_cpp_prop_api