Skip to content

Commit

Permalink
[DOC] add cache_encryption_callbacks example in guide (#26432)
Browse files Browse the repository at this point in the history
### Details:
 - *add example of cache_encryption_callbacks in document*

### Tickets:
 - *CVS-123336*

---------

Co-authored-by: Chen Peter <peter.chen@intel.com>
  • Loading branch information
xufang-lisa and peterchen-intel authored Sep 9, 2024
1 parent 102e875 commit 91b5744
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
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 @@ -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.
6 changes: 3 additions & 3 deletions src/bindings/c/include/openvino/c/ov_property.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*)
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 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
Expand Down

0 comments on commit 91b5744

Please sign in to comment.