From 91b57448952799245aca9824e1f1c7c3eb3466bc Mon Sep 17 00:00:00 2001 From: Fang Xu Date: Mon, 9 Sep 2024 10:06:10 +0800 Subject: [PATCH] [DOC] add cache_encryption_callbacks example in guide (#26432) ### Details: - *add example of cache_encryption_callbacks in document* ### Tickets: - *CVS-123336* --------- Co-authored-by: Chen Peter --- .../assets/snippets/ov_caching.cpp | 31 +++++++++++++++++++ .../articles_en/assets/snippets/ov_caching.py | 17 ++++++++++ .../model-caching-overview.rst | 26 ++++++++++++++++ .../c/include/openvino/c/ov_property.h | 6 ++-- .../include/openvino/runtime/properties.hpp | 4 +-- 5 files changed, 79 insertions(+), 5 deletions(-) diff --git a/docs/articles_en/assets/snippets/ov_caching.cpp b/docs/articles_en/assets/snippets/ov_caching.cpp index 891d3e9368292d..aa08a739261b81 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 4ce0b91ccd7506..57bd72f3f9b80b 100644 --- a/docs/articles_en/assets/snippets/ov_caching.py +++ b/docs/articles_en/assets/snippets/ov_caching.py @@ -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] 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 09701ab97d23fd..f47470d6097a56 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 905d473fc36ba8..c2e855f766c883 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 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 878cba81590d98..abc765cafe205a 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