InferRoute / docs /academic_foundations.md
Ypeng12's picture
docs: add project description subtitle to README and academic foundations
fe86a0f
|
Raw
History Blame Contribute Delete
11.9 kB
# πŸ“– Academic Foundations: FrugalGPT & RouterBench Integration
InferRoute is built upon robust theoretical frameworks for cost-performance trade-offs and multi-tier cascading inference.
InferRoute's core routing architecture is built on the theoretical and mathematical foundations of two landmark papers in LLM cost-performance optimization:
1. **FrugalGPT**: *"FrugalGPT: How to Use Large Language Models While Reducing Cost and Improving Performance"* (Chen et al., Stanford University, 2023).
2. **RouterBench**: *"RouterBench: A Benchmark for Multi-LLM Routing System"* (Li et al., Martian, 2024).
This document outlines the theoretical concepts introduced in these papers and details how they are implemented within the InferRoute gateway codebase.
---
## πŸ”„ 1. FrugalGPT: Cascading Inference & Prompt Adaptation
The primary goal of FrugalGPT is to minimize the cost of querying Large Language Models while maintaining (or even improving) the quality of responses by leveraging heterogeneous APIs. The paper identifies three main pillars of cost-efficiency:
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Incoming Query / Request β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
[ 1. Prompt Adaptation ]
(Trim few-shot examples for cheap backends)
β”‚
β–Ό
[ 2. LLM Approximation (Cache) ]
─────────────► Exact Cache Hit? ────────────► Return Answer
β”‚ (Miss)
β–Ό
[ 3. LLM Cascade ]
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Cheap Model β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
Reliability Judge
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (Score < Ο„) β”‚ (Score β‰₯ Ο„)
β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Escalated Model β”‚ β”‚ Accept Response β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
### 1.1 Prompt Adaptation
* **Theory**: Premium models (e.g., GPT-4o) handle complex prompts with many few-shot examples well, but charging per-token on long prompts is expensive. Cheap models (e.g., Llama-3-8B) cannot leverage long few-shot examples effectively anyway, so sending them full prompts wastes money. Prompt Adaptation dynamically reduces the prompt size (e.g., by pruning examples or vocabulary) for low-cost models.
* **Code Implementation**:
* Located in [prompt_adapter.py](file:///c:/Users/pengy/OneDrive/Desktop/InferRoute/inferroute/prompt_adapter.py).
* The method `compress_few_shot_examples` matches few-shot patterns (like `Example 1: ...`, `Q: ... A: ...`) and trims the prompt down to at most 1 example if the target model is cheap (`ollama` or `vllm`).
* The method `adapt_prompt` automatically decides whether to compress prompt messages based on target backend tiering.
### 1.2 LLM Approximation (Completion Cache)
* **Theory**: Caching and retrieving historical model completions for exact or semantically identical queries avoids model execution fees entirely.
* **Code Implementation**:
* Located in `inferroute/cache.py` (Redis exact completions cache layer).
### 1.3 LLM Cascade
* **Theory**: Queries are routed sequentially from the cheapest model to the most expensive model. At each step, a **Reliability Judge** checks if the response is correct/acceptable with a confidence threshold $\tau \in [0, 1]$. If accepted, the cascade terminates and returns the response. Otherwise, it escalates to the next model tier.
* **Code Implementation**:
* **Routing Policy**: Registered as `"cascade"` in [router.py](file:///c:/Users/pengy/OneDrive/Desktop/InferRoute/inferroute/router.py). Generates a cost-sorted list of healthy models (e.g. `ollama` βž” `vllm` βž” `gemini` βž” `openai`) and parses the threshold parameter `acceptance_threshold` ($\tau$).
* **Blocking Cascade Flow**: Handled by `handle_cascade_blocking_flow` in [main.py](file:///c:/Users/pengy/OneDrive/Desktop/InferRoute/inferroute/main.py). It queries backends sequentially, calls `reliability_scorer.evaluate_reliability`, logs intermediate step scores, and stops upon meeting the threshold, accumulating cumulative costs.
* **Streaming Cascade Flow**: Handled by `handle_cascade_streaming_flow` in [main.py](file:///c:/Users/pengy/OneDrive/Desktop/InferRoute/inferroute/main.py). To maintain SSE streaming compatibility without leaking garbage output, the gateway buffers all streamed tokens inside an internal generator, scores the full content, and only pumps the SSE stream to the client if quality checks pass.
* **Reliability Judge**: Implemented as `ReliabilityScorer` in [validator.py](file:///c:/Users/pengy/OneDrive/Desktop/InferRoute/inferroute/validator.py). It performs keyword matching, math digit evaluations, python syntax AST parses, and JSON schema structural validation, alongside repetition loop penalties.
---
## 🧠 2. RouterBench: Predictive Utility & Frontier Evaluation
RouterBench formalizes the multi-LLM routing system as a mathematical optimization framework. It introduces the utility trade-off curve and defines standard routing baselines.
### 2.1 Mathematical Utility Optimization
Choosing which model $m$ should process a prompt $x$ is framed as maximizing a utility score:
$$\text{Score}(m, x) = \lambda \cdot \text{Quality}_{\text{pred}}(m, x) - \text{Cost}(m)$$
Where:
* **$\lambda$ (lambda)**: The user's *willingness to pay* (cost-quality trade-off parameter). A high $\lambda$ (e.g., 5.0) heavily weights quality, guiding routing to cloud models. A low $\lambda$ (e.g., 0.1) weights cost savings, guiding routing to local nodes.
* **$\text{Quality}_{\text{pred}}(m, x)$**: The predicted performance rating of model $m$ on query $x$, scaled between $0.0$ and $1.0$.
* **$\text{Cost}(m)$**: The estimated economic fee of model $m$ (cost per million tokens).
In InferRoute, this is implemented inside the routing policy loops in [router.py](file:///c:/Users/pengy/OneDrive/Desktop/InferRoute/inferroute/router.py).
### 2.2 Standardized Routing Policies
InferRoute implements the exact policy types benchmarked in RouterBench:
1. **Zero Router Baseline (`zero`)**: Randomly assigns a fraction $p$ of queries to premium cloud backends and $1-p$ to local backends. Sweeping $p \in [0, 1]$ forms the random baseline performance curve.
2. **Rule-Based Router (`rule`)**: Inspects query intent (e.g., code snippets, math symbols, prompt length) to direct traffic using heuristic rules.
3. **KNN-Based Router (`knn`)**: Finds historical benchmark queries with high Jaccard similarity. Computes average quality scores per model for those nearest neighbors, and applies the utility formula.
4. **MLP-Based Router (`mlp`)**: Employs a content-aware classifier model. Extracts query features (`is_code`, `is_math`, `is_json`, `is_long`) to predict quality score probabilities, maximizing the utility formula.
5. **Oracle Router Upper Bound (`oracle`)**: A theoretical router with perfect offline knowledge of whether each model will succeed. It selects the cheapest backend that achieves a quality score $\ge 0.8$.
### 2.3 Evaluation Metric: AIQ (Area under the Curve)
* **Theory**: To compare different routers globally rather than at a single budget, RouterBench computes the **AIQ (Area under the cost-quality trade-off curve)**. A higher AIQ indicates a more cost-effective Pareto frontier:
$$\text{AIQ} = \int_{c_{\min}}^{c_{\max}} Q(c) \, dc \approx \sum_{i=0}^{n-1} \frac{q_i + q_{i+1}}{2} \cdot (c_{i+1} - c_i)$$
* **Code Implementation**:
* Handled in [plot_results.py](file:///c:/Users/pengy/OneDrive/Desktop/InferRoute/benchmarks/plot_results.py) via `calculate_auc()`, using the trapezoidal rule over swept scenarios.
---
## πŸš€ 3. Emerging LLM Routing Paradigms (2025-2026)
Recent academic work has advanced LLM routing beyond static scoring policies into preference learning, decision-aware training, output-length optimization, and agentic multi-round execution.
### 3.1 Preference-Based Routing (RouteLLM - ICLR 2025)
* **Theory**: Instead of regressing absolute performance scores, RouteLLM trains a classifier on pairwise human preference data (e.g., LMSYS Chatbot Arena) to predict the probability that a cheap model $M_{\text{cheap}}$ is sufficient compared to a strong model $M_{\text{strong}}$:
$$\text{Pr}(M_{\text{strong}} \succ M_{\text{cheap}} \mid x) = \sigma(f(x))$$
Requests are routed to $M_{\text{strong}}$ if the probability exceeds a budget-controlled threshold $\theta \in [0, 1]$.
### 3.2 Decision-Aware Routing & Routing Collapse (EquiRouter - 2026)
* **Theory**: Traditional routers trained via regression loss often exhibit "routing collapse" at high budgets, defaulting to expensive models even when cheap ones are sufficient. EquiRouter replaces score regression with a **Decision-Aware Ranking Loss**:
$$\mathcal{L}_{\text{rank}} = -\log \sigma\left(\text{Utility}(M_i, x) - \text{Utility}(M_j, x)\right)$$
This directly optimizes the discrete decision margin, recovering balanced routing and achieving up to 17% cost savings.
### 3.3 Output-Length-Aware Routing (R2-Router - ICML 2026)
* **Theory**: A model's cost and quality are not static point-profiles; they vary as a function of the generated response length $L$. R2-Router couples model selection with dynamic length constraints, solving the joint optimization problem:
$$\max_{m, L} \left[ \text{Quality}(m, x, L) - \lambda \cdot \text{Cost}(m, L) \right]$$
By appending system instructions specifying length constraints, it achieves up to 4-5x cost reductions.
### 3.4 Agentic Multi-Round Routing (Router-R1 - 2025)
* **Theory**: Treats routing as a sequential, multi-round decision process orchestrated by a lightweight reasoning LLM router. Trained via Reinforcement Learning (RL), the router emits thinking blocks (`<think>`) and routes sub-tasks dynamically:
$$R = R_{\text{accuracy}} + R_{\text{format}} - \beta \cdot \text{Cost}_{\text{inference}}$$
This allows the gateway to resolve complex multi-step queries by combining cheap and expensive model calls.
### 3.5 Standardized Large-Scale Benchmarking (LLMRouterBench - 2026)
* **Theory**: A unified evaluation framework comprising over 400K instances across diverse tasks. It addresses the evaluation inconsistencies in prior literature, offering a standardized testbed to assess the stability and scaling laws of dynamic routers across multiple LLM pools.
### 3.6 Unified Taxonomy of Resource Optimization (Survey: Doing More with Less - 2025)
* **Theory**: A systemic survey categorizing routing schemes (similarity, classification, reinforcement learning) and decision timing (pre-generation routing vs. post-generation validation cascades). It defines structural patterns for balancing inference latency, financial costs, and output quality across LLM-based gateways.