Skip to content

Commit ae3b71f

Browse files
authored
Modify encryption callback to expect a binary string in Python API (openvinotoolkit#28953)
### Details: - Cache encryption callbacks in GPU Plugin operate on strings that contain binary data which cannot be converted into a valid Python string (C++ strings are encoding-agnostic while Python strings are Unicode). This change modifies encryption callbacks to operate on binary strings (`bytes` type) on Python side.
1 parent c17b247 commit ae3b71f

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

docs/articles_en/assets/snippets/ov_caching.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
# ! [ov:caching:part5]
5757
import base64
5858

59-
def encrypt_base64(src):
60-
return base64.b64encode(bytes(src, "utf-8"))
59+
def encrypt_base64(src: bytes):
60+
return base64.b64encode(src)
6161

62-
def decrypt_base64(src):
63-
return base64.b64decode(bytes(src, "utf-8"))
62+
def decrypt_base64(src: bytes):
63+
return base64.b64decode(src)
6464

6565
core = ov.Core()
6666
core.set_property({props.cache_dir: path_to_cache_dir})
@@ -73,14 +73,14 @@ def decrypt_base64(src):
7373
# ! [ov:caching:part6]
7474
import base64
7575

76-
def encrypt_base64(src):
77-
return base64.b64encode(bytes(src, "utf-8"))
76+
def encrypt_base64(src: bytes):
77+
return base64.b64encode(src)
7878

79-
def decrypt_base64(src):
80-
return base64.b64decode(bytes(src, "utf-8"))
79+
def decrypt_base64(src: bytes):
80+
return base64.b64decode(src)
8181

8282
core = ov.Core()
83-
if "GPU" in core.available_devices:
83+
if any("GPU" in device for device in core.available_devices):
8484
core.set_property({props.cache_dir: path_to_cache_dir})
8585
config_cache = {}
8686
config_cache["CACHE_ENCRYPTION_CALLBACKS"] = [encrypt_base64, decrypt_base64]

src/bindings/python/src/pyopenvino/utils/utils.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,14 @@ std::map<std::string, ov::Any> properties_to_any_map(const std::map<std::string,
298298
[py_encrypt](const std::string& in_str) -> std::string {
299299
// Acquire GIL, execute Python function
300300
py::gil_scoped_acquire acquire;
301-
return (*py_encrypt)(in_str).cast<std::string>();
301+
return (*py_encrypt)(py::bytes(in_str)).cast<std::string>();
302302
};
303303

304304
std::function<std::string(const std::string&)> decrypt_func =
305305
[py_decrypt](const std::string& in_str) -> std::string {
306306
// Acquire GIL, execute Python function
307307
py::gil_scoped_acquire acquire;
308-
return (*py_decrypt)(in_str).cast<std::string>();
308+
return (*py_decrypt)(py::bytes(in_str)).cast<std::string>();
309309
};
310310
ov::EncryptionCallbacks encryption_callbacks{encrypt_func, decrypt_func};
311311
properties_to_cpp[property.first] = encryption_callbacks;

src/bindings/python/tests/utils/helpers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ def generate_relu_compiled_model(
198198

199199

200200
def encrypt_base64(src):
201-
return base64.b64encode(bytes(src, "utf-8"))
201+
return base64.b64encode(src)
202202

203203

204204
def decrypt_base64(src):
205-
return base64.b64decode(bytes(src, "utf-8"))
205+
return base64.b64decode(src)
206206

207207

208208
def generate_relu_compiled_model_with_config(

0 commit comments

Comments
 (0)