Python Data Science Program
📓 Abrir notebook en GitHub

Clase 151 — vLLM y TGI: serving de LLMs en producción

Parte: 2 — Deep Learning · Fuente: Kwon et al. (2023) vLLM + HuggingFace TGI docs. ⏱️ Duración estimada: 80 min.

🎯 Objetivo

Servir LLMs eficientemente en producción con vLLM (Berkeley) o TGI (HuggingFace). Cubrir: PagedAttention, continuous batching, prefill/decode, quantization (AWQ, GPTQ, FP8), structured output (JSON, function calling), streaming, OpenAI-compatible API.

📚 Resultados de aprendizaje

Al finalizar, el estudiante podrá:

🗺️ Temas

📖 Definiciones y características

📂 Dataset / recursos

🧪 Ejercicios

  1. vLLM básico: python -m vllm.entrypoints.openai.api_server --model mistralai/Mistral-7B-Instruct-v0.2. Cliente OpenAI.
  2. Continuous batching benchmark: 100 requests paralelos vs 1 a la vez. Comparar throughput.
  3. AWQ quantization: cargar TheBloke/Mistral-7B-Instruct-v0.2-AWQ. VRAM ~5 GB vs 14 GB fp16.
  4. Structured JSON output: extra_body={'guided_json': {schema}} → forced JSON valid.
  5. TGI: docker run --gpus all ghcr.io/huggingface/text-generation-inference:latest --model-id X.

📝 Homework verificable

Servir Mistral 7B Instruct AWQ con vLLM + cliente OpenAI:

  1. Levantar server vLLM.
  2. 50 prompts batch en paralelo desde cliente.
  3. Medir throughput (tokens/sec total).
  4. Comparar contra transformers.pipeline generate.
  5. Activar guided_json para una tarea específica.

Criterio de aceptación: vLLM ≥ 5× throughput vs HF pipeline; structured output válido al 100 %.

⚠️ Errores comunes

Síntoma / mensaje Causa y cómo arreglar
OOM al cargar Modelo + KV cache no entra. Fix: quantization o --max-model-len 2048.
Throughput bajo en una sola request Con batch=1, vLLM es similar a HF. Fix: paralelizar requests.
LoRA adapter no carga Fix: vLLM soporta LoRA con --enable-lora --lora-modules name=path.
Structured output formato inválido guided_json schema mal. Fix: JSON Schema válido.
TGI lento Versión vieja. Fix: usar imagen latest.

❓ Preguntas frecuentes

❓ vLLM vs TGI?

vLLM ligeramente más rápido en benchmarks; mejor LoRA support. TGI más integrado con HF Hub. Ambos buenos.

❓ Ollama / llama.cpp?

llama.cpp / Ollama: CPU-friendly, GGUF format. Para desktop, local, low traffic. vLLM/TGI para servers con GPU.

❓ Estructura JSON garantizada?

Sí con guided_json (vLLM uses outlines library). Mucho más confiable que prompt engineering.

❓ Speculative decoding?

--speculative-model X --num-speculative-tokens 5. 2-3× decode speedup con calidad idéntica.

❓ Multi-GPU?

--tensor-parallel-size N para shard del modelo en N GPUs.

🔗 Referencias

📥 Material descargable

➡️ Siguiente clase

Clase 152 — RAG básico y embeddings (+ hybrid search, re-ranking, MCP)