Skip to main content

Quickstart Guide

This guide will walk you through running your first generative AI model using ONNX Runtime GenAI. We’ll use the Phi-3 model as an example, which is optimized for on-device AI scenarios.
This quickstart uses Python. For C# or C++ examples, see the examples directory.

Prerequisites

Before starting, ensure you have:
  • Python 3.8 or later installed
  • ONNX Runtime GenAI installed (see Installation)
  • At least 4GB of free disk space for the model
  • 8GB+ RAM recommended

Step 1: Download the Model

First, download a pre-optimized ONNX model. We’ll use the Phi-3 Mini model optimized for CPU.
1

Install Hugging Face CLI

2

Download Phi-3 Model

Download the CPU-optimized INT4 quantized model:
This downloads the model to ./cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/
For GPU acceleration, download a GPU-optimized variant:

Alternative: Download via Foundry Local

You can also use Foundry Local to download models:

Step 2: Install Required Packages

Ensure you have the necessary Python packages:

Step 3: Run Your First Model

Create a Python script to run inference with streaming output:

Step 4: Run the Script

Execute your script:

Expected Output

You should see output similar to:

Understanding the Code

Let’s break down the key components:
1

Load Model and Tokenizer

  • Model: Loads the ONNX model from the specified directory
  • Tokenizer: Handles text encoding/decoding using the model’s vocabulary
  • TokenizerStream: Enables streaming token decoding for real-time output
2

Configure Generation Parameters

  • max_length: Maximum number of tokens to generate
  • batch_size: Number of sequences to generate simultaneously
  • Additional options: top_k, top_p, temperature, num_beams
3

Encode Input and Generate

  • Encode text to tokens
  • Create generator with model and parameters
  • Generate tokens one at a time in a loop
  • Decode and print each token for streaming output

Advanced Examples

Continuous Chat with History

For a chat application that maintains conversation history:

Performance Tips

Choose the Right Quantization

  • INT4: Best for CPU, smallest model size
  • FP16: Recommended for GPUs
  • FP32: Highest accuracy, larger size

Use Appropriate Hardware

  • CPU: Good for testing and small models
  • CUDA: Best for NVIDIA GPUs
  • DirectML: Windows GPU acceleration
  • TensorRT: Optimized NVIDIA inference

Batch Processing

Process multiple prompts together to improve throughput:

Adjust Generation Parameters

  • Lower max_length for faster responses
  • Adjust temperature for creativity (0.0-1.0)
  • Use top_k and top_p for quality/speed tradeoff

Common Issues and Solutions

  • Use GPU acceleration if available
  • Download INT4 quantized models for CPU
  • Reduce max_length parameter
  • Close other applications to free up RAM
  • Use smaller batch sizes
  • Download a more quantized model (INT4 vs FP16)
  • Reduce max_length parameter
  • Ensure enough RAM/VRAM for the model
Verify the model path is correct:
The directory should contain:
  • genai_config.json
  • *.onnx files
  • Tokenizer files
  • Verify chat template matches your model
  • Check that input prompt is not empty
  • Ensure max_length is sufficient
  • Try adjusting temperature and sampling parameters

Next Steps

Explore More Models

Browse ONNX models on Hugging Face for different use cases

Advanced Features

Learn about:
  • Multi-LoRA support
  • Constrained decoding for JSON output
  • Vision and audio models
  • Custom model optimization

API Reference

Detailed documentation of all classes and methods in the ONNX Runtime GenAI API

Examples Repository

Complete examples for Python, C#, C++, and more advanced scenarios

Download Models

For a comprehensive guide on downloading and preparing models, see:

Download Models Guide

Learn how to download models via Foundry Local, Hugging Face, or build your own