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

[DOCS] add cache_encryption_callbacks docs #26484

31 changes: 31 additions & 0 deletions docs/articles_en/assets/snippets/ov_caching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions docs/articles_en/assets/snippets/ov_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@
# 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]
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,27 @@ 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
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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``.

.. 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 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.
8 changes: 4 additions & 4 deletions src/bindings/c/include/openvino/c/ov_property.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ OPENVINO_C_VAR(const char*)
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
* @brief Write-only property<ov_encryption_callbacks*> to set encryption and decryption function for model cache.
* If ov_property_key_cache_encryption_callbacks is set, model topology will be encrypted when saving to the cache and
* decrypted when loading from the cache. This property is set in ov_core_compile_model_* only
* @ingroup ov_property_c_api
*/
OPENVINO_C_VAR(const char*)
Expand Down Expand Up @@ -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;
ov_property_key_auto_batch_timeout;
4 changes: 2 additions & 2 deletions src/inference/include/openvino/runtime/properties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 will be encrypted when saving to the cache and decrypted
* when loading from the cache. This property is set 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
Expand Down
Loading