From dba30b2bf3aa3727d852f65902abe909f98c419b Mon Sep 17 00:00:00 2001 From: Fang Xu <fang.xu@intel.com> Date: Mon, 9 Sep 2024 10:06:10 +0800 Subject: [PATCH 1/5] [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 <peter.chen@intel.com> --- .../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<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 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 From 952aa3e9d5a9bc2fcdd4fd7efecf3464bff174a5 Mon Sep 17 00:00:00 2001 From: Fang Xu <fang.xu@intel.com> Date: Tue, 10 Sep 2024 18:16:34 +0800 Subject: [PATCH 2/5] Update docs/articles_en/openvino-workflow/running-inference/optimize-inference/optimizing-latency/model-caching-overview.rst Co-authored-by: Tatiana Savina <tatiana.savina@intel.com> --- .../optimizing-latency/model-caching-overview.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 f47470d6097a56..13e1365b6e9732 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 @@ -141,8 +141,7 @@ To check in advance if a particular device supports model caching, your applicat 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``. +When 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:: From 20002a82b01bdb358eb36fc7f0388aa722b2e7ba Mon Sep 17 00:00:00 2001 From: Fang Xu <fang.xu@intel.com> Date: Tue, 10 Sep 2024 18:16:42 +0800 Subject: [PATCH 3/5] Update docs/articles_en/openvino-workflow/running-inference/optimize-inference/optimizing-latency/model-caching-overview.rst Co-authored-by: Tatiana Savina <tatiana.savina@intel.com> --- .../optimizing-latency/model-caching-overview.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 13e1365b6e9732..41c05026bce567 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 @@ -161,5 +161,4 @@ When model caching is enabled, the model topology can be encrypted when saving t .. 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. + 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. From da957a0a41210570ef5f2b8e2e2e345d3da73913 Mon Sep 17 00:00:00 2001 From: xufang <fang.xu@intel.com> Date: Tue, 10 Sep 2024 18:31:31 +0800 Subject: [PATCH 4/5] update comments --- src/bindings/c/include/openvino/c/ov_property.h | 6 +++--- src/inference/include/openvino/runtime/properties.hpp | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bindings/c/include/openvino/c/ov_property.h b/src/bindings/c/include/openvino/c/ov_property.h index c2e855f766c883..356a46ee74d1ef 100644 --- a/src/bindings/c/include/openvino/c/ov_property.h +++ b/src/bindings/c/include/openvino/c/ov_property.h @@ -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 topology in cache will be encrypted/decrypted when - * saving/loading model cache. This property 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*) diff --git a/src/inference/include/openvino/runtime/properties.hpp b/src/inference/include/openvino/runtime/properties.hpp index abc765cafe205a..621c0074fc9d1e 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 topology in 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 From c727d83e8aaabc4342b33b36c622942fcd4b65d3 Mon Sep 17 00:00:00 2001 From: Karol Blaszczak <karol.blaszczak@intel.com> Date: Wed, 11 Sep 2024 09:01:58 +0200 Subject: [PATCH 5/5] Update docs/articles_en/openvino-workflow/running-inference/optimize-inference/optimizing-latency/model-caching-overview.rst --- .../optimizing-latency/model-caching-overview.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 41c05026bce567..2d60705f0ab0da 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 @@ -141,7 +141,7 @@ To check in advance if a particular device supports model caching, your applicat Set "cache_encryption_callbacks" config option to enable cache encryption +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -When 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``. +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::