Skip to content

Commit 81c2784

Browse files
xufang-lisapeterchen-inteltsavinakblaszczak-intel
authored
[DOCS] add cache_encryption_callbacks docs (#26484)
### Details: - *add example of cache_encryption_callbacks in document* ### Tickets: - *CVS-123336* --------- Co-authored-by: Chen Peter <peter.chen@intel.com> Co-authored-by: Tatiana Savina <tatiana.savina@intel.com> Co-authored-by: Karol Blaszczak <karol.blaszczak@intel.com>
1 parent 95d6eea commit 81c2784

File tree

5 files changed

+78
-6
lines changed

5 files changed

+78
-6
lines changed

docs/articles_en/assets/snippets/ov_caching.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,43 @@ bool cachingSupported = std::find(caps.begin(), caps.end(), ov::device::capabili
6060
}
6161
}
6262

63+
void part4() {
64+
std::string modelPath = "/tmp/myModel.xml";
65+
std::string device = "CPU";
66+
ov::Core core; // Step 1: create ov::Core object
67+
core.set_property(ov::cache_dir("/path/to/cache/dir")); // Step 1b: Enable caching
68+
auto model = core.read_model(modelPath); // Step 2: Read Model
69+
//! [ov:caching:part4]
70+
ov::AnyMap config;
71+
ov::EncryptionCallbacks encryption_callbacks;
72+
static const char codec_key[] = {0x30, 0x60, 0x70, 0x02, 0x04, 0x08, 0x3F, 0x6F, 0x72, 0x74, 0x78, 0x7F};
73+
auto codec_xor = [&](const std::string& source_str) {
74+
auto key_size = sizeof(codec_key);
75+
int key_idx = 0;
76+
std::string dst_str = source_str;
77+
for (char& c : dst_str) {
78+
c ^= codec_key[key_idx % key_size];
79+
key_idx++;
80+
}
81+
return dst_str;
82+
};
83+
encryption_callbacks.encrypt = codec_xor;
84+
encryption_callbacks.decrypt = codec_xor;
85+
config.insert(ov::cache_encryption_callbacks(encryption_callbacks)); // Step 4: Set device configuration
86+
auto compiled = core.compile_model(model, device, config); // Step 5: LoadNetwork
87+
//! [ov:caching:part4]
88+
if (!compiled) {
89+
throw std::runtime_error("error");
90+
}
91+
}
92+
6393
int main() {
6494
try {
6595
part0();
6696
part1();
6797
part2();
6898
part3();
99+
part4();
69100
} catch (...) {
70101
}
71102
return 0;

docs/articles_en/assets/snippets/ov_caching.py

+17
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,20 @@
4242
# Find 'EXPORT_IMPORT' capability in supported capabilities
4343
caching_supported = 'EXPORT_IMPORT' in core.get_property(device_name, device.capabilities)
4444
# ! [ov:caching:part3]
45+
46+
# ! [ov:caching:part4]
47+
import base64
48+
49+
def encrypt_base64(src):
50+
return base64.b64encode(bytes(src, "utf-8"))
51+
52+
def decrypt_base64(src):
53+
return base64.b64decode(bytes(src, "utf-8"))
54+
55+
core = ov.Core()
56+
core.set_property({props.cache_dir: path_to_cache_dir})
57+
config_cache = {}
58+
config_cache["CACHE_ENCRYPTION_CALLBACKS"] = [encrypt_base64, decrypt_base64]
59+
model = core.read_model(model=model_path)
60+
compiled_model = core.compile_model(model=model, device_name=device_name, config=config_cache)
61+
# ! [ov:caching:part4]

docs/articles_en/openvino-workflow/running-inference/optimize-inference/optimizing-latency/model-caching-overview.rst

+24
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,27 @@ To check in advance if a particular device supports model caching, your applicat
136136
:language: cpp
137137
:fragment: [ov:caching:part3]
138138

139+
Set "cache_encryption_callbacks" config option to enable cache encryption
140+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
141+
142+
If model caching is enabled, the model topology can be encrypted when saving to the cache and decrypted when loading from the cache. This property can currently be set only in ``compile_model``.
143+
144+
.. tab-set::
145+
146+
.. tab-item:: Python
147+
:sync: py
148+
149+
.. doxygensnippet:: docs/articles_en/assets/snippets/ov_caching.py
150+
:language: py
151+
:fragment: [ov:caching:part4]
152+
153+
.. tab-item:: C++
154+
:sync: cpp
155+
156+
.. doxygensnippet:: docs/articles_en/assets/snippets/ov_caching.cpp
157+
:language: cpp
158+
:fragment: [ov:caching:part4]
159+
160+
.. important::
161+
162+
Currently, this property is supported only by the CPU plugin. For other HW plugins, setting this property will not encrypt/decrypt the model topology in cache and will not affect performance.

src/bindings/c/include/openvino/c/ov_property.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ OPENVINO_C_VAR(const char*)
108108
ov_property_key_cache_mode;
109109

110110
/**
111-
* @brief Write-only property<ov_encryption_callbacks*> to set encryption/decryption function for model cache.
112-
* If ov_property_key_cache_encryption_callbacks is set, model cache will be encrypted/decrypted when saving/loading
113-
* model cache. ov_property_key_cache_encryption_callbacks is enabled in ov_core_compile_model_* only
111+
* @brief Write-only property<ov_encryption_callbacks*> to set encryption and decryption function for model cache.
112+
* If ov_property_key_cache_encryption_callbacks is set, model topology will be encrypted when saving to the cache and
113+
* decrypted when loading from the cache. This property is set in ov_core_compile_model_* only
114114
* @ingroup ov_property_c_api
115115
*/
116116
OPENVINO_C_VAR(const char*)
@@ -245,4 +245,4 @@ ov_property_key_enable_mmap;
245245
* @ingroup ov_property_c_api
246246
*/
247247
OPENVINO_C_VAR(const char*)
248-
ov_property_key_auto_batch_timeout;
248+
ov_property_key_auto_batch_timeout;

src/inference/include/openvino/runtime/properties.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,8 @@ struct EncryptionCallbacks {
791791

792792
/**
793793
* @brief Write-only property to set encryption/decryption function for saving/loading model cache.
794-
* If cache_encryption_callbacks is set, the model cache will be encrypted/decrypted when saving/loading cache.
795-
* cache_encryption_callbacks is enabled in core.compile_model only.
794+
* If cache_encryption_callbacks is set, the model topology will be encrypted when saving to the cache and decrypted
795+
* when loading from the cache. This property is set in core.compile_model only.
796796
* - First value of the struct is encryption function.
797797
* - Second value of the struct is decryption function.
798798
* @ingroup ov_runtime_cpp_prop_api

0 commit comments

Comments
 (0)