Skip to content

Commit 9590782

Browse files
olpipimryzhov
andauthored
Add cache encryption to vlm sample (openvinotoolkit#2038)
[CVS-162990](https://jira.devtools.intel.com/browse/CVS-162990) Co-authored-by: Mikhail Ryzhov <mikhail.ryzhov@intel.com>
1 parent 70b767e commit 9590782

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

samples/cpp/visual_language_chat/encrypted_model_vlm.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ std::pair<std::string, ov::Tensor> decrypt_model(const std::filesystem::path& mo
1111
std::ifstream model_file(model_dir / model_file_name);
1212
std::ifstream weights_file();
1313
if (!model_file.is_open()) {
14-
throw std::runtime_error("Cannot open model or weights file");
14+
throw std::runtime_error("Cannot open model file");
1515
}
1616
std::string model_str((std::istreambuf_iterator<char>(model_file)), std::istreambuf_iterator<char>());
1717

@@ -30,6 +30,39 @@ ov::genai::Tokenizer decrypt_tokenizer(const std::filesystem::path& models_path)
3030
return ov::genai::Tokenizer(tok_model_str, tok_weights_tensor, detok_model_str, detok_weights_tensor);
3131
}
3232

33+
static const char codec_key[] = {0x30, 0x60, 0x70, 0x02, 0x04, 0x08, 0x3F, 0x6F, 0x72, 0x74, 0x78, 0x7F};
34+
35+
std::string codec_xor(const std::string& source_str) {
36+
auto key_size = sizeof(codec_key);
37+
int key_idx = 0;
38+
std::string dst_str = source_str;
39+
for (char& c : dst_str) {
40+
c ^= codec_key[key_idx % key_size];
41+
key_idx++;
42+
}
43+
return dst_str;
44+
}
45+
46+
std::string encryption_callback(const std::string& source_str) {
47+
return codec_xor(source_str);
48+
}
49+
50+
std::string decryption_callback(const std::string& source_str) {
51+
return codec_xor(source_str);
52+
}
53+
54+
auto get_config_for_cache_encryption() {
55+
ov::AnyMap config;
56+
config.insert({ov::cache_dir("llm_cache")});
57+
ov::EncryptionCallbacks encryption_callbacks;
58+
//use XOR-based encryption as an example
59+
encryption_callbacks.encrypt = encryption_callback;
60+
encryption_callbacks.decrypt = decryption_callback;
61+
config.insert(ov::cache_encryption_callbacks(encryption_callbacks));
62+
config.insert(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
63+
return config;
64+
}
65+
3366
bool print_subword(std::string&& subword) {
3467
return !(std::cout << subword << std::flush);
3568
}
@@ -61,7 +94,7 @@ int main(int argc, char* argv[]) try {
6194
if (device == "GPU") {
6295
// Cache compiled models on disk for GPU to save time on the
6396
// next run. It's not beneficial for CPU.
64-
enable_compile_cache.insert({ov::cache_dir("vlm_cache")});
97+
enable_compile_cache = get_config_for_cache_encryption();
6598
}
6699
ov::genai::VLMPipeline pipe(models_map, tokenizer, models_path, device, enable_compile_cache);
67100

samples/python/visual_language_chat/encrypted_model_vlm.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ def read_images(path: str) -> list[Tensor]:
7474
return [read_image(path)]
7575

7676

77+
# here is example how to make cache de-encryption based on base64
78+
import base64
79+
80+
def encrypt_base64(src: bytes):
81+
return base64.b64encode(src)
82+
83+
84+
def decrypt_base64(src: bytes):
85+
return base64.b64decode(src)
86+
87+
88+
def get_config_for_cache_encryption():
89+
config_cache = dict()
90+
config_cache["CACHE_DIR"] = "llm_cache"
91+
config_cache["CACHE_ENCRYPTION_CALLBACKS"] = [encrypt_base64, decrypt_base64]
92+
config_cache["CACHE_MODE"] = "OPTIMIZE_SIZE"
93+
return config_cache
94+
95+
7796
def main():
7897
parser = argparse.ArgumentParser()
7998
parser.add_argument('model_dir')
@@ -100,7 +119,7 @@ def main():
100119
if "GPU" == device:
101120
# Cache compiled models on disk for GPU to save time on the
102121
# next run. It's not beneficial for CPU.
103-
enable_compile_cache["CACHE_DIR"] = "vlm_cache"
122+
enable_compile_cache = get_config_for_cache_encryption()
104123

105124
pipe = openvino_genai.VLMPipeline(models_map, tokenizer, args.model_dir, device, **enable_compile_cache)
106125

0 commit comments

Comments
 (0)