Skip to main content
This tutorial demonstrates how to use SGLang’s offline Engine API to query VLMs. We will demonstrate usage with Qwen2.5-VL and Llama 4. This section demonstrates three different calling approaches:
  1. Basic Call: Directly pass images and text.
  2. Processor Output: Use HuggingFace processor for data preprocessing.
  3. Precomputed Embeddings: Pre-calculate image features to improve inference efficiency.

Understanding the Three Input Formats

SGLang supports three ways to pass visual data, each optimized for different scenarios:

1. Raw Images - Simplest approach

  • Pass PIL Images, file paths, URLs, or base64 strings directly
  • SGLang handles all preprocessing automatically
  • Best for: Quick prototyping, simple applications

2. Processor Output - For custom preprocessing

  • Pre-process images with HuggingFace processor
  • Pass the complete processor output dict with format: "processor_output"
  • Best for: Custom image transformations, integration with existing pipelines
  • Requirement: Must use input_ids instead of text prompt

3. Precomputed Embeddings - For maximum performance

  • Pre-calculate visual embeddings using the vision encoder
  • Pass embeddings with format: "precomputed_embedding"
  • Best for: Repeated queries on same images, caching, high-throughput serving
  • Performance gain: Avoids redundant vision encoder computation (30-50% speedup)
Key Rule: Within a single request, use only one format for all images. Don’t mix formats. The examples below demonstrate all three approaches with both Qwen2.5-VL and Llama 4 models.

Multimodal cache identity

SGLang identifies actual media features or supplied embeddings using full 256-bit digests. Cache identities also include tensor shape and dtype, container boundaries, modality, model-specific metadata, and the effective model/processor configuration. CPU and CUDA tensor hashing use BLAKE3; GPU hashing transfers only the digests to the host. These are collision-resistant identifiers, not a mathematical guarantee that collisions are impossible.
  • mm_hashes remains accepted for compatibility, but cannot override cache keys or skip server-side content hashing. External routers must not assume that a supplied scalar hash equals SGLang’s internal KV-cache key.
  • mm_content_hashes describes source content. Keep --trust-mm-content-hashes disabled for untrusted clients; enabling it allows preprocessing-cache lookup before verifying the supplied media.
  • Embedding caching and batch deduplication retain the full identity. In the KV cache key, each media span is padded with a repeating cycle of four 62-bit sentinels derived from the item’s identity, so the radix cache shares earlier turns when media are appended, and the text before media that differ. This is on by default for every natively served multimodal model at any --page-size and span position, including PD and EPD disaggregation and media passed as processor outputs or precomputed embeddings. The existing Qwen2.5-VL cache-only page proof remains unchanged where enabled.
  • Media that do not hold the cycle (a span shorter than four tokens, missing or truncated offsets, model-specific scalar padding) are named in the KV namespace instead, together with every media before them and cache_salt. Such a request shares KV only with requests that start with the same media; media appended after the last such item still share through their spans.
  • A request names all of its media, and so shares KV only with requests that carry the same media list, when:
    • the model cross-attends to an encoder state built from the whole request (Llama 3.2 Vision, Moss-VL, Whisper);
    • the request carries input_embeds or positional embedding overrides, which its prompt tokens do not describe;
    • the request continues a streaming session.
    A processor for a model whose earlier inputs, positions or attention change when media are appended must set has_causal_image_positions=False on its output. Deployments using HiCache or PD must use the same model, processor and settings across participating workers. Set SGLANG_FORCE_MM_KV_NAMESPACE=1 to name every media of every request in the namespace, which removes prefix sharing across different media lists.
  • Pin model revisions when sharing encoder caches. Clear caches or restart when replacing weights or processor implementations in place; a configuration fingerprint is not a checksum of all model weights.
Custom processors must hash the final model-facing features and metadata before exporting CUDA transport handles. The shared frontend finalization path then binds the processor namespace and updates any prebuilt padding together; composite processors must delegate this step to their inner processor. Keep server-created full identities intact across transport and never use a truncated scalar hash as an embedding or KV-cache identity.

Querying Qwen2.5-VL Model

Example
Example

Basic Offline Engine API Call

Example
Example

Call with Processor Output

Using a HuggingFace processor to preprocess text and images, and passing the processor_output directly into Engine.generate.
Example

Call with Precomputed Embeddings

You can pre-calculate image features to avoid repeated visual encoding processes.
Example
Example

Querying Llama 4 Vision Model

Example

Llama 4 Basic Call

Llama 4 requires more computational resources, so it’s configured with multi-GPU parallelism (tp_size=4) and larger context length.
Example

Call with Processor Output

Using HuggingFace processor to preprocess data can reduce computational overhead during inference.
Example

Call with Precomputed Embeddings

Example