OpenVINO GenAI Documentation

repository·master·Indexed 20 days ago

https://github.com/openvinotoolkit/openvino.genai

A library of optimized generative AI model pipelines and execution methods for PCs and laptops, running on the OpenVINO Runtime. It provides high-level C++, Python, and Node.js APIs for LLMs, VLMs, Diffusion models, Whisper speech recognition, and text embeddings. Features include LoRA support, speculative decoding, KVCache token eviction, sparse attention, and continuous batching with prefix caching.

Tokens
113.1K
Snippets
340
Records
463
Agent score
66%

What's inside OpenVINO GenAI

  1. What is the Who What Benchmark (WWB)?

    master

    The Who What Benchmark (WWB) is a simple accuracy benchmarking tool for Generative AI models. Its primary purpose is to estimate the similarity score between embeddings computed from data generated by two different models (e.g., a baseline model vs. an optimized/quantized model).

    Supported Use Cases

    • LLMs (Text Generation): Supports HuggingFace Transformers (including Bitsandbytes, GPTQ), OpenVINO/NNCF via Optimum-Intel, OpenVINO GenAI, and Llama.cpp via BigDL-LLM.
    • Text-to-Image Pipelines: Validates pipelines by computing similarity scores between images generated by Diffusers, Optimum-Intel, and OpenVINO GenAI using the Text2ImageEvaluator class.
    • Visual Language Pipelines (VLMs): Validates pipelines using the VisualTextEvaluator class.
    • Text-to-Video: Evaluates video generation (defaults guidance_scale to 1; higher values enable CFG).
  2. What is OpenVINO GenAI?

    master

    OpenVINO™ GenAI is a library of optimized generative AI model pipelines, execution methods, and samples built on top of the OpenVINO Runtime. It provides simplified APIs that abstract the complexities of the generation process (such as tokenization, scheduling, and generation loops) to allow for easy integration of state-of-the-art models with minimal code.

    Key characteristics include:

    • Lightweight: Designed for efficient inference on PCs and laptops with a minimal memory footprint and no external dependencies (it includes core functionality like tokenization via openvino-tokenizers).
    • Optimized: Hardware-specific optimizations for CPU, GPU, and NPU devices.
    • Multi-language: Supports Python, C++, and Node.js.
  3. Overview of Misaki C++ sample implementations

    master

    The Misaki C++ port includes small API examples demonstrating core functionality:

    • basic_usage.cpp: Demonstrates creating an English engine, calling phonemize_with_tokens(...), and printing phoneme output along with token fields.
    • fallback_and_unknown.cpp: Demonstrates handling unknown words by using set_unknown_token(...) and installing a set_fallback_hook(...) to resolve fallback logic.
  4. Supported Generative AI Scenarios in OpenVINO GenAI

    master

    OpenVINO GenAI provides lightweight C++, Python, and Node.js APIs for several generative AI workflows:

    • Text Generation (LLMs): Chat with models like Llama, Phi, and Qwen.
    • Visual Processing (VLMs): Analyze images and videos using models like LLaVa and MiniCPM-V.
    • Image Generation (Diffusers): Generate images using Stable Diffusion and Flux models.
    • Speech Recognition (Whisper): Convert speech to text.
    • Speech Generation (SpeechT5): Convert text to speech (TTS).
    • Semantic Search (Text Embedding): Compute embeddings for RAG (Retrieval-Augmented Generation) workflows.
    • Text Rerank: Analyze relevance for RAG workflows.

    LoRA Support

    The library supports LoRA adapters for both Text and Image generation, allowing you to:

    • Load multiple adapters per model.
    • Select active adapters for each generation.
    • Mix multiple adapters using alpha blending coefficients.
  5. Supported Generative AI scenarios in OpenVINO GenAI Node.js

    master

    The package provides pre-built pipelines for several AI scenarios:

    • Text generation (LLMs): Chat with models like Llama, Phi, Qwen, etc.
    • Visual processing (VLMs): Analyze images/videos with LLaVa, MiniCPM-V, etc.
    • Image generation (Diffusers): Generate images with Stable Diffusion & Flux.
    • Speech recognition (Whisper): Convert speech to text.
    • Speech generation (SpeechT5): Convert text to speech.
    • Semantic search (Text Embedding): Compute embeddings for RAG workflows.
    • Text Rerank (RAG): Analyze relevance of documents and queries for RAG.
  6. What is Speculative Decoding in OpenVINO GenAI

    master

    Speculative decoding (or assisted-generation) is a latency optimization for autoregressive text generation. It uses a small, computationally cheap drafter model to propose candidate tokens, which are then validated by a larger target model in a single parallelized forward pass. This replaces multiple sequential decoding steps with fewer batched steps, providing speedups on memory-bandwidth-bound decoding.

    OpenVINO GenAI supports three main strategies:

    1. Speculative Decoding (Fast Draft): Uses a smaller off-the-shelf LLM as the drafter. Best for general-purpose speedup without retraining.
    2. Prompt Lookup Decoding: Uses n-gram matching against the input prompt instead of a second model. Best for RAG, summarization, or code editing where the output often repeats parts of the input.
    3. EAGLE3: Uses a custom draft head trained on the target's hidden states. Offers the highest acceptance rate.

    All three strategies are available via LLMPipeline (Python/C++). Prompt Lookup and EAGLE3 are also supported in VLMPipeline (e.g., for Qwen3-VL).

  7. What is Low-Rank Adaptation (LoRA)?

    master

    LoRA (Low-Rank Adaptation) is a lightweight fine-tuning technique for Large Language Models (LLMs) and Stable Diffusion models. Instead of retraining all parameters of a massive model, LoRA adds a small number of new weights using low-rank matrix factorization.

    Key benefits for developers:

    • Efficiency: Drastically reduces the number of trainable parameters, making fine-tuning faster and more memory-efficient.
    • Portability: Produces small weight files (typically a few hundred MBs) that are easy to store and share.
    • Task Switching: Since the original pre-trained weights remain frozen, you can maintain multiple lightweight LoRA adapters for different tasks and switch between them easily.
    • No Inference Latency: LoRA weights can be merged with the base model, ensuring that using an adapter does not slow down model inference.
  8. LoRA support across GenAI pipelines

    master

    LoRA (Low-Rank Adaptation) support varies depending on the pipeline type:

    • LLMs: Supported.
    • VLMs: Supported only for LoRA adapters applied to the language-model (LLM) part. Adapters targeting the vision encoder or other multimodal components are not supported.
    • Speech Recognition: Not supported.
    • Speech Generation: Not supported.
    • Embedding Models: Not supported.
    • Text Rerank Models: Not supported.
  9. Configure SchedulerConfig for hybrid-attention models

    master

    The interpretation of SchedulerConfig depends on the pipeline constructor and the specific fields you set. Use these rules to guide your configuration:

    1. Constructor Defaults

    • LLMPipeline / VLMPipeline (Continuous Batching): Optimized for local, non-concurrent latency. Defaults to max_num_batched_tokens = std::numeric_limits<size_t>::max() and enable_prefix_caching = true.
    • ContinuousBatchingPipeline: Does not assume a latency profile; uses the provided SchedulerConfig object directly.

    2. Automatic Linear-Attention Sizing

    If you leave num_linear_attention_blocks = 0, the runtime derives capacity automatically:

    • No prefix caching + unlimited max_num_batched_tokens: Starts with 1 linear-attention block.
    • No prefix caching + bounded max_num_batched_tokens: Derived from max_num_seqs.
    • With prefix caching: Derived as ceil(num_kv_blocks / cache_interval_multiplier).

    3. Shared Memory Budget

    If you set cache_size > 0 and num_kv_blocks == 0, the runtime treats cache_size as a shared budget for all cache types (KV and linear-attention) under one combined memory limit.

    4. Fully Dynamic Mode

    If both num_kv_blocks == 0 and cache_size == 0, the cache starts with no preallocated capacity and grows on demand. This is flexible but less deterministic.

    5. Manual Override

    Setting num_linear_attention_blocks > 0 overrides all automatic sizing logic. Use this only if the required capacity is already known.

  10. Tune prefix-cache granularity with cache_interval_multiplier

    master

    When enable_prefix_caching=true is used in hybrid models, the cache_interval_multiplier controls the trade-off between memory usage and prefix-cache reuse granularity.

    • Smaller multiplier: Creates more frequent checkpoints. This results in finer-grained reuse (better for chat/repeated prompts) but consumes more linear-attention memory.
    • Larger multiplier: Creates fewer checkpoints. This results in coarser reuse but reduces linear-attention memory usage.

    If unset, the runtime derives the multiplier adaptively so that one checkpoint costs roughly one KV block (with a minimum baseline of 8).

  11. Run Vision Language Models with VLMPipeline

    master

    OpenVINO GenAI provides the VLMPipeline for multimodal text-generation Vision Language Models (VLMs). This pipeline allows you to generate text responses based on a text prompt combined with image and/or video inputs. The pipeline is available for Python, C++, and JavaScript (Node.js) implementations and supports execution on both CPU and GPU devices.

    # Conceptual usage of VLMPipeline
    # The pipeline handles multimodal inputs (text + images/videos)
    from openvino_genai import VLMPipeline
    
    pipeline = VLMPipeline(model_path, device="CPU")
    result = pipeline.generate("prompt", image=image_data)
  12. Compare OpenVINO GenAI with alternatives

    master

    OpenVINO GenAI is positioned as a middle ground between the low-level OpenVINO Runtime and high-level frameworks like Hugging Face Optimum Intel:

    FeatureOpenVINO GenAIBase OpenVINOHugging Face Optimum Intel
    Easy-to-use APIs
    Low footprint
    C++ support
    Node.js bindings
    Pre-built pipelines
    Model varietyMediumHighHigh

    Key Takeaways:

    • Vs. Base OpenVINO: OpenVINO GenAI provides ready-to-use components (generation loops, tokenization, scheduling) that must otherwise be manually implemented in base OpenVINO.
    • Vs. Hugging Face Optimum Intel: OpenVINO GenAI offers a smaller footprint, fewer dependencies, and better optimization options, particularly for C++ applications.