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. KV caching namespaces each request by its ordered media identities and cache_salt, so a short placeholder collision cannot reuse another image’s KV state. Requests with identical media can still share prefixes; requests with different media do not share even their common text prefix. This can increase prefill cost for workloads with long shared instructions and changing images.
  • 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