|
| 1 | +// Copyright (C) 2023-2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include <cxxopts.hpp> |
| 5 | +#include <filesystem> |
| 6 | + |
| 7 | +#include "load_image.hpp" |
| 8 | +#include <openvino/genai/visual_language/pipeline.hpp> |
| 9 | + |
| 10 | + |
| 11 | +int main(int argc, char* argv[]) try { |
| 12 | + cxxopts::Options options("benchmark_vlm", "Help command"); |
| 13 | + |
| 14 | + options.add_options() |
| 15 | + ("m,model", "Path to model and tokenizers base directory", cxxopts::value<std::string>()->default_value(".")) |
| 16 | + ("p,prompt", "Prompt", cxxopts::value<std::string>()->default_value("What is on the image?")) |
| 17 | + ("i,image", "Image", cxxopts::value<std::string>()->default_value("image.jpg")) |
| 18 | + ("nw,num_warmup", "Number of warmup iterations", cxxopts::value<size_t>()->default_value(std::to_string(1))) |
| 19 | + ("n,num_iter", "Number of iterations", cxxopts::value<size_t>()->default_value(std::to_string(3))) |
| 20 | + ("mt,max_new_tokens", "Maximal number of new tokens", cxxopts::value<size_t>()->default_value(std::to_string(20))) |
| 21 | + ("d,device", "device", cxxopts::value<std::string>()->default_value("CPU")) |
| 22 | + ("h,help", "Print usage"); |
| 23 | + |
| 24 | + cxxopts::ParseResult result; |
| 25 | + try { |
| 26 | + result = options.parse(argc, argv); |
| 27 | + } catch (const cxxopts::exceptions::exception& e) { |
| 28 | + std::cout << e.what() << "\n\n"; |
| 29 | + std::cout << options.help() << std::endl; |
| 30 | + return EXIT_FAILURE; |
| 31 | + } |
| 32 | + |
| 33 | + if (result.count("help")) { |
| 34 | + std::cout << options.help() << std::endl; |
| 35 | + return EXIT_SUCCESS; |
| 36 | + } |
| 37 | + |
| 38 | + std::string prompt = result["prompt"].as<std::string>(); |
| 39 | + const std::string models_path = result["model"].as<std::string>(); |
| 40 | + const std::string image_path = result["image"].as<std::string>(); |
| 41 | + std::string device = result["device"].as<std::string>(); |
| 42 | + size_t num_warmup = result["num_warmup"].as<size_t>(); |
| 43 | + size_t num_iter = result["num_iter"].as<size_t>(); |
| 44 | + ov::Tensor image = utils::load_image(image_path); |
| 45 | + |
| 46 | + ov::genai::GenerationConfig config; |
| 47 | + config.max_new_tokens = result["max_new_tokens"].as<size_t>(); |
| 48 | + |
| 49 | + ov::genai::VLMPipeline pipe(models_path, device); |
| 50 | + |
| 51 | + for (size_t i = 0; i < num_warmup; i++) |
| 52 | + pipe.generate(prompt, ov::genai::image(image), ov::genai::generation_config(config)); |
| 53 | + |
| 54 | + auto res = pipe.generate(prompt, ov::genai::image(image), ov::genai::generation_config(config)); |
| 55 | + auto metrics = res.perf_metrics; |
| 56 | + for (size_t i = 0; i < num_iter - 1; i++) { |
| 57 | + res = pipe.generate(prompt, ov::genai::image(image), ov::genai::generation_config(config)); |
| 58 | + metrics = metrics + res.perf_metrics; |
| 59 | + } |
| 60 | + |
| 61 | + std::cout << std::fixed << std::setprecision(2); |
| 62 | + std::cout << "Load time: " << metrics.get_load_time() << " ms" << std::endl; |
| 63 | + std::cout << "Generate time: " << metrics.get_generate_duration().mean << " ± " << metrics.get_generate_duration().std << " ms" << std::endl; |
| 64 | + std::cout << "Tokenization time: " << metrics.get_tokenization_duration().mean << " ± " << metrics.get_tokenization_duration().std << " ms" << std::endl; |
| 65 | + std::cout << "Detokenization time: " << metrics.get_detokenization_duration().mean << " ± " << metrics.get_detokenization_duration().std << " ms" << std::endl; |
| 66 | + std::cout << "Embeddings preparation time: " << metrics.get_prepare_embeddings_duration().mean << " ± " << metrics.get_prepare_embeddings_duration().std << " ms" << std::endl; |
| 67 | + std::cout << "TTFT: " << metrics.get_ttft().mean << " ± " << metrics.get_ttft().std << " ms" << std::endl; |
| 68 | + std::cout << "TPOT: " << metrics.get_tpot().mean << " ± " << metrics.get_tpot().std << " ms/token " << std::endl; |
| 69 | + std::cout << "Throughput: " << metrics.get_throughput().mean << " ± " << metrics.get_throughput().std << " tokens/s" << std::endl; |
| 70 | + |
| 71 | + return 0; |
| 72 | +} catch (const std::exception& error) { |
| 73 | + try { |
| 74 | + std::cerr << error.what() << '\n'; |
| 75 | + } catch (const std::ios_base::failure&) {} |
| 76 | + return EXIT_FAILURE; |
| 77 | +} catch (...) { |
| 78 | + try { |
| 79 | + std::cerr << "Non-exception object thrown\n"; |
| 80 | + } catch (const std::ios_base::failure&) {} |
| 81 | + return EXIT_FAILURE; |
| 82 | +} |
0 commit comments