ML-Based Model Selection
This document covers the configuration and experimental results for ML-based model selection techniques implemented in the Semantic Router.
Overview
ML-based model selection uses machine learning algorithms to intelligently route queries to the most appropriate LLM based on query characteristics and historical performance data. This approach provides significant improvements over random or single-model routing strategies.
Supported Algorithms
| Algorithm | Description | Best For |
|---|---|---|
| KNN (K-Nearest Neighbors) | Quality-weighted voting among similar queries | High accuracy, diverse query types |
| KMeans | Cluster-based routing with efficiency optimization | Fast inference, balanced workloads |
| SVM (Support Vector Machine) | RBF kernel decision boundaries | Clear domain separation |
| MLP (Multi-Layer Perceptron) | Neural network with GPU acceleration | High-throughput, GPU-enabled environments |
Reference Papers
- FusionFactory (arXiv:2507.10540) - Query-level fusion via LLM routers
- Avengers-Pro (arXiv:2508.12631) - Performance-efficiency optimized routing
Dashboard GUI Setup
The Semantic Router Dashboard includes a guided 3-step wizard for the full ML model selection pipeline. This is the easiest way to get started — no CLI required.
Accessing the Wizard
Navigate to http://localhost:8700/ml-setup (or your dashboard URL).
Step 1: Benchmark
Upload your models YAML (listing your LLM endpoints) and a queries JSONL file (test queries with ground truth). Configure concurrency and max tokens, then click Run Benchmark. Progress streams in real-time with per-query granularity.
Step 2: Train
Select one or more algorithms:
| Algorithm | GPU Required | Notes |
|---|---|---|
| KNN | No | CPU-only (scikit-learn) |
| K-Means | No | CPU-only (scikit-learn) |
| SVM | No | CPU-only (scikit-learn) |
| MLP | Optional | PyTorch — Device selector (CPU/CUDA) appears when selected |
Adjust hyperparameters via the Advanced Settings panel, then click Train Models. Trained model files (knn_model.json, kmeans_model.json, etc.) are saved to a fixed ml-train/ directory.
Step 3: Generate Config
Define routing decisions — each with a name, priority, algorithm, domain conditions, and target model names. Click Generate Config to produce a deployment-ready ml-model-selection-values.yaml.
The generated YAML follows the semantic-router config schema. Merge the model_selection and decisions sections into your main config.yaml, or use it as a standalone config file for the router.
Example Generated Config
config:
model_selection:
enabled: true
ml:
models_path: /data/ml-pipeline/ml-train
embedding_dim: 1024
knn:
k: 5
pretrained_path: /data/ml-pipeline/ml-train/knn_model.json
strategy: priority
decisions:
- name: math-decision
priority: 100
rules:
operator: OR
conditions:
- type: domain
name: math
algorithm:
type: knn
modelRefs:
- model: llama3.2:3b
use_reasoning: false
Configuration
Basic Configuration
Enable ML-based model selection in your config.yaml:
# Enable ML model selection
model_selection:
ml:
enabled: true
models_path: ".cache/ml-models" # Path to trained model files
# Embedding model for query representation
embedding_models:
qwen3_model_path: "models/mom-embedding-pro" # Qwen3-Embedding-0.6B
Per-Decision Algorithm Configuration
Configure different algorithms for different decision types:
decisions:
# Math queries - use KNN for quality-weighted selection
- name: "math_decision"
description: "Mathematics and quantitative reasoning"
priority: 100
rules:
operator: "AND"
conditions:
- type: "domain"
name: "math"
algorithm:
type: "knn"
knn:
k: 5
modelRefs:
- model: "llama-3.2-1b"
- model: "llama-3.2-3b"
- model: "mistral-7b"
# Coding queries - use SVM for clear boundaries
- name: "code_decision"
description: "Programming and software development"
priority: 100
rules:
operator: "AND"
conditions:
- type: "domain"
name: "computer science"
algorithm:
type: "svm"
svm:
kernel: "rbf"
gamma: 1.0
modelRefs:
- model: "codellama-7b"
- model: "llama-3.2-3b"
- model: "mistral-7b"
# General queries - use KMeans for efficiency
- name: "general_decision"
description: "General knowledge queries"
priority: 50
rules:
operator: "AND"
conditions:
- type: "domain"
name: "other"
algorithm:
type: "kmeans"
kmeans:
num_clusters: 8
modelRefs:
- model: "llama-3.2-1b"
- model: "llama-3.2-3b"
- model: "mistral-7b"
# High-throughput queries - use MLP with GPU acceleration
- name: "gpu_accelerated_decision"
description: "High-volume inference with GPU"
priority: 100
rules:
operator: "AND"
conditions:
- type: "domain"
name: "engineering"
algorithm:
type: "mlp"
mlp:
device: "cuda" # or "cpu", "metal"
modelRefs:
- model: "llama-3.2-1b"
- model: "llama-3.2-3b"
- model: "mistral-7b"
- model: "codellama-7b"
Algorithm Parameters
KNN Parameters
algorithm:
type: "knn"
knn:
k: 5 # Number of neighbors (default: 5)
KMeans Parameters
algorithm:
type: "kmeans"
kmeans:
num_clusters: 8 # Number of clusters (default: 8)
SVM Parameters
algorithm:
type: "svm"
svm:
kernel: "rbf" # Kernel type: rbf, linear (default: rbf)
gamma: 1.0 # RBF kernel gamma (default: 1.0)
MLP Parameters
algorithm:
type: "mlp"
mlp:
device: "cuda" # Device: cpu, cuda, metal (default: cpu)
The MLP (Multi-Layer Perceptron) algorithm uses a neural network classifier with GPU acceleration via the Candle Rust framework. It provides high-throughput inference suitable for production deployments with GPU resources.
Device Options:
| Device | Description | Requirements |
|---|---|---|
cpu | CPU inference (default) | No special hardware |
cuda | NVIDIA GPU acceleration | CUDA-capable GPU, CUDA toolkit |
metal | Apple Silicon GPU | macOS with M1/M2/M3 chip |
Experimental Results
Benchmark Setup
- Test queries: 109 queries across multiple domains
- Models evaluated: 4 LLMs (codellama-7b, llama-3.2-1b, llama-3.2-3b, mistral-7b)
- Embedding model: Qwen3-Embedding-0.6B (1024 dimensions)
- Validation data: Real benchmark queries with ground truth performance scores
Performance Comparison
| Strategy | Avg Quality | Avg Latency | Best Model % |
|---|---|---|---|
| Oracle (best possible) | 0.495 | 10.57s | 100.0% |
| KMEANS Selection | 0.252 | 20.23s | 23.9% |
| Always llama-3.2-3b | 0.242 | 25.08s | 15.6% |
| SVM Selection | 0.233 | 25.83s | 14.7% |
| Always mistral-7b | 0.215 | 70.08s | 13.8% |
| Always llama-3.2-1b | 0.212 | 3.65s | 26.6% |
| KNN Selection | 0.196 | 36.62s | 13.8% |
| Random Selection | 0.174 | 40.12s | 9.2% |
| Always codellama-7b | 0.161 | 53.78s | 4.6% |
ML Routing Benefit Over Random Selection
| Algorithm | Quality Improvement | Best Model Selection |
|---|---|---|
| KMEANS | +45.5% | 2.6x more often |
| SVM | +34.4% | 1.6x more often |
| KNN | +13.1% | 1.5x more often |
Key Findings
- All ML methods outperform random selection - Significant quality improvements across all algorithms
- KMEANS provides best quality - 45% improvement over random with good latency
- SVM offers balanced performance - 34% improvement with clear decision boundaries
- KNN provides diverse model selection - Uses all available models based on query similarity
- MLP enables GPU acceleration - Neural network inference with CUDA/Metal support for high-throughput
MLP GPU Acceleration
The MLP algorithm leverages the Candle Rust framework for GPU-accelerated inference:
| Device | Inference Latency | Throughput |
|---|---|---|
| CPU | ~5-10ms | ~100-200 QPS |
| CUDA (NVIDIA) | ~0.5-1ms | ~1000+ QPS |
| Metal (Apple) | ~1-2ms | ~500+ QPS |
When to use MLP:
- High-volume production deployments with GPU resources
- Latency-sensitive applications requiring sub-millisecond inference
- Environments where model selection overhead must be minimized
Architecture
┌─────────────────────────────────────────────────────────────────────┐
│ ONLINE INFERENCE │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ Request (model="auto") │