Skip to main content
Microsoft’s Phi vision models are compact yet powerful multi-modal models that combine visual understanding with language capabilities. ONNX Runtime GenAI supports Phi-3 Vision, Phi-3.5 Vision, and Phi-4 Multi-Modal models.

Supported Models

Phi-3 Vision

128k context length vision model for image understanding

Phi-3.5 Vision

Enhanced vision capabilities with improved accuracy

Phi-4 Multi-Modal

Latest model supporting both vision and audio inputs

Model Architecture

Phi vision models are multi-modal models consisting of several internal components:
  • Vision Encoder: Processes images and extracts visual features
  • Image Embedding: Converts visual features into embeddings compatible with the language model
  • Language Model: Core transformer model for text generation
  • Fusion Layers: Combine visual and text embeddings
For ONNX Runtime GenAI, each internal component is exported as a separate ONNX model for optimal performance.

Building Phi Vision Models

Phi-3 Vision (128k Context)

1

Download PyTorch Model

2

Download Modified Files

3

Replace Modeling Files

4

Build ONNX Models

5

Add Configuration Files

Download the required JSON configuration files:

Using Phi Vision Models

Basic Image Understanding

Multi-Image Processing

Chat Template Integration

Image Input Handling

Supported Image Formats

Phi vision models support common image formats:
  • JPEG/JPG
  • PNG
  • BMP
  • TIFF

Image Preprocessing

The processor automatically handles:
  1. Resizing: Images are resized to the model’s expected dimensions
  2. Normalization: Pixel values are normalized
  3. Patch Extraction: Images are divided into patches
  4. Embedding: Visual patches are converted to embeddings

Image Resolution

Advanced Usage

Batch Processing

Custom Generation Parameters

Performance Optimization

Choose the right precision for your hardware:
  • FP32: Best accuracy, slower, works on all devices
  • FP16: Good balance, requires GPU with FP16 support
  • INT4: Fastest, smallest memory footprint, slight accuracy loss
For large images or long sequences:

Fine-Tuning Support

You can use your own fine-tuned Phi vision models:
1

Fine-tune with PyTorch

Fine-tune the model using your preferred training framework.
2

Replace Weights

3

Build ONNX Models

4

Update Configurations

Modify genai_config.json and processor_config.json if your fine-tuning changed model architecture or tokenizer.

Troubleshooting

If you encounter OOM errors:
  1. Reduce image resolution before processing
  2. Use INT4 quantization instead of FP16
  3. Reduce max_length parameter
  4. Process images one at a time instead of batching
If you see flash attention errors:

Example Application

Here’s a complete example script for document analysis:

Next Steps

Qwen Vision

Explore Qwen’s advanced vision models

Gemma Vision

Learn about Google’s Gemma vision models

Whisper Audio

Add audio processing capabilities

Model Quantization

Optimize models with quantization