|
| 1 | +// Copyright (C) 2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include <filesystem> |
| 5 | +#include <fstream> |
| 6 | + |
| 7 | +#include "load_image.hpp" |
| 8 | +#include "openvino/genai/visual_language/pipeline.hpp" |
| 9 | + |
| 10 | +std::pair<std::string, ov::Tensor> decrypt_model(const std::string& model_path, const std::string& weights_path) { |
| 11 | + std::ifstream model_file(model_path); |
| 12 | + std::ifstream weights_file(weights_path, std::ios::binary); |
| 13 | + if (!model_file.is_open() || !weights_file.is_open()) { |
| 14 | + throw std::runtime_error("Cannot open model or weights file"); |
| 15 | + } |
| 16 | + |
| 17 | + // User can add file decryption of model_file and weights_file in memory here. |
| 18 | + |
| 19 | + |
| 20 | + std::string model_str((std::istreambuf_iterator<char>(model_file)), std::istreambuf_iterator<char>()); |
| 21 | + |
| 22 | + weights_file.seekg(0, std::ios::end); |
| 23 | + auto weight_size = static_cast<unsigned>(weights_file.tellg()); |
| 24 | + weights_file.seekg(0, std::ios::beg); |
| 25 | + auto weights_tensor = ov::Tensor(ov::element::u8, {weight_size}); |
| 26 | + if (!weights_file.read(static_cast<char*>(weights_tensor.data()), weight_size)) { |
| 27 | + throw std::runtime_error("Cannot read weights file"); |
| 28 | + } |
| 29 | + |
| 30 | + return {model_str, weights_tensor}; |
| 31 | +} |
| 32 | + |
| 33 | +ov::genai::Tokenizer decrypt_tokenizer(const std::string& models_path) { |
| 34 | + std::string tok_model_path = models_path + "/openvino_tokenizer.xml"; |
| 35 | + std::string tok_weights_path = models_path + "/openvino_tokenizer.bin"; |
| 36 | + auto [tok_model_str, tok_weights_tensor] = decrypt_model(tok_model_path, tok_weights_path); |
| 37 | + |
| 38 | + std::string detok_model_path = models_path + "/openvino_detokenizer.xml"; |
| 39 | + std::string detok_weights_path = models_path + "/openvino_detokenizer.bin"; |
| 40 | + auto [detok_model_str, detok_weights_tensor] = decrypt_model(detok_model_path, detok_weights_path); |
| 41 | + |
| 42 | + return ov::genai::Tokenizer(tok_model_str, tok_weights_tensor, detok_model_str, detok_weights_tensor); |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +bool print_subword(std::string&& subword) { |
| 47 | + return !(std::cout << subword << std::flush); |
| 48 | +} |
| 49 | + |
| 50 | +int main(int argc, char* argv[]) try { |
| 51 | + if (4 != argc) { |
| 52 | + throw std::runtime_error(std::string{"Usage "} + argv[0] + " <MODEL_DIR> <IMAGE_FILE OR DIR_WITH_IMAGES> <PROMPT>"); |
| 53 | + } |
| 54 | + |
| 55 | + //read and encrypt models |
| 56 | + std::string models_path = argv[1]; |
| 57 | + auto language_model = decrypt_model(models_path + "/openvino_language_model.xml", models_path + "/openvino_language_model.bin"); |
| 58 | + auto resampler_model = decrypt_model(models_path + "/openvino_resampler_model.xml", models_path + "/openvino_resampler_model.bin"); |
| 59 | + auto text_embeddings_model = decrypt_model(models_path + "/openvino_text_embeddings_model.xml", models_path + "/openvino_text_embeddings_model.bin"); |
| 60 | + auto vision_embeddings_model = decrypt_model(models_path + "/openvino_vision_embeddings_model.xml", models_path + "/openvino_vision_embeddings_model.bin"); |
| 61 | + |
| 62 | + ov::genai::ModelsMap models_map; |
| 63 | + models_map.emplace("language", std::move(language_model)); |
| 64 | + models_map.emplace("resampler", std::move(resampler_model)); |
| 65 | + models_map.emplace("text_embeddings", std::move(text_embeddings_model)); |
| 66 | + models_map.emplace("vision_embeddings", std::move(vision_embeddings_model)); |
| 67 | + ov::genai::Tokenizer tokenizer = decrypt_tokenizer(models_path); |
| 68 | + |
| 69 | + std::vector<ov::Tensor> rgbs = utils::load_images(argv[2]); |
| 70 | + |
| 71 | + // GPU and NPU can be used as well. |
| 72 | + // Note: If NPU selected, only language model will be run on NPU |
| 73 | + std::string device = "CPU"; |
| 74 | + ov::AnyMap enable_compile_cache; |
| 75 | + if (device == "GPU") { |
| 76 | + // Cache compiled models on disk for GPU to save time on the |
| 77 | + // next run. It's not beneficial for CPU. |
| 78 | + enable_compile_cache.insert({ov::cache_dir("vlm_cache")}); |
| 79 | + } |
| 80 | + ov::genai::VLMPipeline pipe(models_map, tokenizer, models_path, device, enable_compile_cache); |
| 81 | + |
| 82 | + ov::genai::GenerationConfig generation_config; |
| 83 | + generation_config.max_new_tokens = 100; |
| 84 | + |
| 85 | + std::string prompt = argv[3]; |
| 86 | + pipe.generate(prompt, |
| 87 | + ov::genai::images(rgbs), |
| 88 | + ov::genai::generation_config(generation_config), |
| 89 | + ov::genai::streamer(print_subword)); |
| 90 | + |
| 91 | +} catch (const std::exception& error) { |
| 92 | + try { |
| 93 | + std::cerr << error.what() << '\n'; |
| 94 | + } catch (const std::ios_base::failure&) {} |
| 95 | + return EXIT_FAILURE; |
| 96 | +} catch (...) { |
| 97 | + try { |
| 98 | + std::cerr << "Non-exception object thrown\n"; |
| 99 | + } catch (const std::ios_base::failure&) {} |
| 100 | + return EXIT_FAILURE; |
| 101 | +} |
0 commit comments