Generally, it's not recommended and often problematic to run Uvicorn with workers > 1 when serving a vLLM-based model, primarily due to GPU memory duplication.
Here's a breakdown of why:
- Model Loading: Each Uvicorn worker process is a separate Python process. When you load a vLLM model (which is very large) within your FastAPI application, each worker process will attempt to load its own independent copy of the model into GPU memory.
- GPU Memory Consumption: LLMs require significant amounts of GPU VRAM. If you have, say, 4 workers, you'll likely need roughly 4 times the GPU memory required to load the model just once. This can quickly exceed the available VRAM on your GPU(s), causing out-of-memory errors or forcing the system to use slower system RAM.
- Performance Contention: Even if you have enough memory (e.g., multiple GPUs, one per worker), the workers will compete for GPU compute resources. vLLM is already highly optimized to handle concurrent requests efficiently within a single process using techniques like PagedAttention and continuous batching. Having multiple processes contend for the same GPU resources can lead to context-switching overhead and potentially lower overall throughput compared to a single, well-utilized worker process.
- vLLM's Concurrency Model: vLLM is designed as an asynchronous system. A single vLLM engine instance running within a single Python process (and thus, a single Uvicorn worker) can handle many requests concurrently, making effective use of the GPU.
When Might Multiple Workers Make Sense?
- Multiple GPUs: If you have multiple GPUs (e.g., 2 GPUs) and configure your system (perhaps using environment variables like
CUDA_VISIBLE_DEVICES per worker or specific vLLM multi-GPU configurations) so that each worker exclusively uses a different GPU, then multiple workers could be beneficial. Each worker would manage its own vLLM instance on its dedicated GPU.
- CPU-Bound Tasks: If your FastAPI application performs significant CPU-bound work outside of the actual vLLM inference (e.g., complex request pre-processing, heavy data manipulation unrelated to the LLM call), then multiple workers might help parallelize those CPU tasks across different CPU cores, assuming the GPU isn't the sole bottleneck. However, for most LLM serving scenarios, the GPU is the primary bottleneck.
Recommended Approach:
For serving a vLLM model with FastAPI/Uvicorn, the standard and most efficient practice is usually:
- Use
workers = 1.
- Ensure your vLLM engine is initialized correctly within your application startup logic.
- Leverage the asynchronous capabilities of FastAPI and vLLM to handle concurrent requests within that single process.
If you need to scale beyond what a single GPU and process can handle, consider deploying multiple independent instances of your FastAPI application (each potentially on a different GPU or server) and using a load balancer in front of them.