Skip to content

Commit 657c68b

Browse files
Private/tkrupa/cache encryption docs (#28035)
### Details: - Replacement for #27993 (can't modify it, not from fork). - Docs for #27912. Added separately because docs addition breaks CI (no GPU Plugin in Python API tests currently), ### Tickets: - https://jira.devtools.intel.com/browse/CVS-158140 --------- Co-authored-by: Sebastian Golebiewski <[email protected]>
1 parent 59984e9 commit 657c68b

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

docs/articles_en/assets/snippets/ov_caching.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,42 @@ auto compiled = core.compile_model(model, device, config); // Step 5:
9090
}
9191
}
9292

93+
void part5() {
94+
std::string modelPath = "/tmp/myModel.xml";
95+
std::string device = "GPU";
96+
ov::Core core; // Step 1: create ov::Core object
97+
core.set_property(ov::cache_dir("/path/to/cache/dir")); // Step 1b: Enable caching
98+
//! [ov:caching:part5]
99+
static const char codec_key[] = {0x30, 0x60, 0x70, 0x02, 0x04, 0x08, 0x3F, 0x6F, 0x72, 0x74, 0x78, 0x7F};
100+
auto codec_xor = [&](const std::string& source_str) {
101+
auto key_size = sizeof(codec_key);
102+
int key_idx = 0;
103+
std::string dst_str = source_str;
104+
for (char& c : dst_str) {
105+
c ^= codec_key[key_idx % key_size];
106+
key_idx++;
107+
}
108+
return dst_str;
109+
};
110+
auto compiled = core.compile_model(modelPath,
111+
device,
112+
ov::cache_encryption_callbacks(ov::EncryptionCallbacks{codec_xor, codec_xor}),
113+
ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE)); // Step 5: Compile model
114+
//! [ov:caching:part5]
115+
if (!compiled) {
116+
throw std::runtime_error("error");
117+
}
118+
}
119+
93120
int main() {
94121
try {
95122
part0();
96123
part1();
97124
part2();
98125
part3();
99126
part4();
127+
part5();
100128
} catch (...) {
101129
}
102130
return 0;
103-
}
131+
}

docs/articles_en/assets/snippets/ov_caching.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,20 @@ def decrypt_base64(src):
5959
model = core.read_model(model=model_path)
6060
compiled_model = core.compile_model(model=model, device_name=device_name, config=config_cache)
6161
# ! [ov:caching:part4]
62+
63+
# ! [ov:caching:part5]
64+
import base64
65+
66+
def encrypt_base64(src):
67+
return base64.b64encode(bytes(src, "utf-8"))
68+
69+
def decrypt_base64(src):
70+
return base64.b64decode(bytes(src, "utf-8"))
71+
72+
core = ov.Core()
73+
core.set_property({props.cache_dir: path_to_cache_dir})
74+
config_cache = {}
75+
config_cache["CACHE_ENCRYPTION_CALLBACKS"] = [encrypt_base64, decrypt_base64]
76+
config_cache["CACHE_MODE"] = "OPTIMIZE_SIZE"
77+
compiled_model = core.compile_model(model=model_path, device_name='GPU', config=config_cache)
78+
# ! [ov:caching:part5]

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ To check in advance if a particular device supports model caching, your applicat
139139
Set "cache_encryption_callbacks" config option to enable cache encryption
140140
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
141141

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``.
142+
If model caching is enabled in the CPU Plugin, the model topology can be encrypted while it is saved to the cache and decrypted when it is loaded from the cache. Currently, this property can be set only in ``compile_model``.
143143

144144
.. tab-set::
145145

@@ -157,6 +157,24 @@ If model caching is enabled, the model topology can be encrypted when saving to
157157
:language: cpp
158158
:fragment: [ov:caching:part4]
159159

160+
If model caching is enabled in the GPU Plugin, the model topology can be encrypted while it is saved to the cache and decrypted when it is loaded from the cache. Full encryption only works when the ``CacheMode`` property is set to ``OPTIMIZE_SIZE``.
161+
162+
.. tab-set::
163+
164+
.. tab-item:: Python
165+
:sync: py
166+
167+
.. doxygensnippet:: docs/articles_en/assets/snippets/ov_caching.py
168+
:language: py
169+
:fragment: [ov:caching:part5]
170+
171+
.. tab-item:: C++
172+
:sync: cpp
173+
174+
.. doxygensnippet:: docs/articles_en/assets/snippets/ov_caching.cpp
175+
:language: cpp
176+
:fragment: [ov:caching:part5]
177+
160178
.. important::
161179

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.
180+
Currently, this property is supported only by the CPU and GPU plugins. For other HW plugins, setting this property will not encrypt/decrypt the model topology in cache and will not affect performance.

0 commit comments

Comments
 (0)