Isuruigi/adaptive-lora-framework

Adaptive Multi-Adapter LLM System with Intelligent Routing

Python

0

0 commits

updated Dec 25, 2025

See the code

README

πŸ€– Adaptive Multi-Adapter LLM System

Novel approach to LLM fine-tuning using specialized LoRA adapters with learned routing

Python PyTorch License Tests

🎯 Overview

Traditional fine-tuning creates a single model good at one task. This project implements a multi-adapter architecture where:

  • πŸŽ“ Multiple specialized adapters handle different types of queries (reasoning, code, creative, analysis)
  • 🧠 Learned router network dynamically selects optimal adapters using BERT-based classification
  • πŸ“Š Self-evaluation system monitors quality with multi-metric assessment and uncertainty quantification
  • πŸ”„ Continuous improvement through targeted synthetic data generation

πŸ—οΈ Architecture

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   User Query    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  Router Network β”‚ ← BERT-based classifier
                    β”‚  (Complexity +  β”‚   with Gumbel-Softmax
                    β”‚   Task Type)    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β–Ό           β–Ό               β–Ό           β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚Reasoningβ”‚ β”‚  Code   β”‚   β”‚Creative β”‚ β”‚Analysis β”‚
    β”‚ Adapter β”‚ β”‚ Adapter β”‚   β”‚ Adapter β”‚ β”‚ Adapter β”‚
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
         β”‚           β”‚             β”‚           β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  Generation   β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
                    β”‚ Self-Evaluatorβ”‚ ← Multi-metric scoring
                    β”‚ + Uncertainty β”‚   MC Dropout, calibration
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“Š Results

MetricBaseline (Single Adapter)This SystemImprovement
Task Accuracy72.3%84.7%+17.1%
Response Quality0.680.82+20.6%
Routing AccuracyN/A87.3%-
Inference Time2.3s2.1s+8.7%
GPU Memory16GB4GB-75%

πŸš€ Quick Start

Installation

git clone https://github.com/isuruigi/adaptive-lora-framework.git
cd adaptive-lora-framework
pip install -r requirements.txt

Run Demo

# Set API key (get free from https://console.groq.com)
export GROQ_API_KEY="your-key"

# Start Gradio server
python scripts/run_gradio_server.py
# Generate training data
python scripts/prepare_data.py --adapter reasoning --samples 1000

# Train adapter
python -m src.adapters.lora_trainer \
    --adapter_config configs/adapters/reasoning_adapter.yaml \
    --output_dir models/reasoning

Train Router

python experiments/train_router.py \
    --config configs/router_config.yaml \
    --output_dir models/router

πŸ“ Project Structure

adaptive-lora-framework/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ adapters/          # LoRA adapter training & management
β”‚   β”‚   β”œβ”€β”€ lora_trainer.py
β”‚   β”‚   └── adapter_manager.py
β”‚   β”œβ”€β”€ router/            # Intelligent routing system
β”‚   β”‚   β”œβ”€β”€ router_model.py    # BERT-based router with Gumbel-Softmax
β”‚   β”‚   β”œβ”€β”€ router_trainer.py  # Multi-objective training
β”‚   β”‚   └── reinforcement.py   # RL-based improvement
β”‚   β”œβ”€β”€ evaluation/        # Quality assessment
β”‚   β”‚   β”œβ”€β”€ self_evaluator.py  # Multi-metric evaluation
β”‚   β”‚   β”œβ”€β”€ uncertainty.py     # MC Dropout, calibration
β”‚   β”‚   └── llm_judge.py       # GPT-4/Claude evaluation
β”‚   β”œβ”€β”€ data/              # Data processing
β”‚   β”‚   └── synthetic_generator.py
β”‚   β”œβ”€β”€ serving/           # Production API
β”‚   β”‚   β”œβ”€β”€ api.py         # FastAPI endpoints
β”‚   β”‚   └── inference_engine.py
β”‚   └── monitoring/        # Observability
β”‚       β”œβ”€β”€ metrics.py
β”‚       └── alerting.py
β”œβ”€β”€ configs/               # YAML configurations
β”œβ”€β”€ experiments/           # Training scripts
β”œβ”€β”€ tests/                 # Unit, integration, load tests
β”œβ”€β”€ k8s/                   # Kubernetes manifests
β”œβ”€β”€ deploy/                # Deployment configs
└── notebooks/             # Jupyter notebooks

πŸ”¬ Key Innovations

1. Learned Router Network

Unlike rule-based routing, uses a trainable network that learns from actual performance data:

from src.router import AdapterRouter

router = AdapterRouter(
    encoder_name="sentence-transformers/all-MiniLM-L6-v2",
    num_adapters=4,
    hidden_dim=256,
    use_gumbel=True  # Differentiable selection
)

# Predict best adapter
adapter_probs, complexity = router(query_embedding)

2. Multi-Metric Self-Evaluation

Goes beyond accuracy with relevance, coherence, factuality, and safety scoring:

from src.evaluation import SelfEvaluator

evaluator = SelfEvaluator()
metrics = evaluator.evaluate(
    query="Explain quantum computing",
    response="Quantum computing uses qubits...",
    adapter_used="reasoning"
)
# Returns: {relevance: 0.9, coherence: 0.85, completeness: 0.8}

3. Uncertainty Quantification

MC Dropout and ensemble methods for confidence estimation:

from src.evaluation import UncertaintyQuantifier

quantifier = UncertaintyQuantifier(model, n_samples=10)
mean, uncertainty = quantifier.predict_with_uncertainty(input_ids)

πŸ’» Technical Stack

ComponentTechnology
Base ModelLlama 3 / Phi-3 / Mistral
Fine-tuningQLoRA (4-bit NF4)
RouterBERT + Gumbel-Softmax
TrainingPyTorch, Transformers, PEFT
ServingFastAPI, Gradio
DeploymentDocker, Kubernetes
MonitoringPrometheus, Grafana

πŸ§ͺ Testing

# Run all tests
pytest tests/ -v

# Unit tests only
pytest tests/unit/ -v

# Integration tests
pytest tests/integration/ -v

# Load tests
pytest tests/load/ -v

πŸ“ˆ Training Details

Adapter Training:

  • Rank: 16-32 (varies by adapter)
  • Learning rate: 2e-4 with cosine schedule
  • Batch size: 32 (gradient accumulation)
  • Training time: ~2 hours per adapter (A100)

Router Training:

  • Encoder: all-MiniLM-L6-v2
  • Dataset: 5000 synthetic examples
  • Loss: Cross-entropy + complexity regularization
  • Training time: 15 minutes

πŸš€ Deployment Options

OptionCostBest For
Local GradioFreeDevelopment
HuggingFace SpacesFreeDemo
Modal (Serverless GPU)$0.001/reqProduction
Kubernetes$100+/moEnterprise

πŸ“ Citation

@misc{adaptive-lora-2024,
  author = {Your Name},
  title = {Adaptive Multi-Adapter LLM System},
  year = {2024},
  url = {https://github.com/isuruigi/adaptive-lora-framework}
}

πŸ“„ License

MIT License - see LICENSE for details.


Built with ❀️ as a portfolio project demonstrating modern MLOps practices

Isuruigi/adaptive-lora-framework

Adaptive Multi-Adapter LLM System with Intelligent Routing

Python

0

0 commits

updated Dec 25, 2025

See the code

README

πŸ€– Adaptive Multi-Adapter LLM System

Novel approach to LLM fine-tuning using specialized LoRA adapters with learned routing

Python PyTorch License Tests

🎯 Overview

Traditional fine-tuning creates a single model good at one task. This project implements a multi-adapter architecture where:

  • πŸŽ“ Multiple specialized adapters handle different types of queries (reasoning, code, creative, analysis)
  • 🧠 Learned router network dynamically selects optimal adapters using BERT-based classification
  • πŸ“Š Self-evaluation system monitors quality with multi-metric assessment and uncertainty quantification
  • πŸ”„ Continuous improvement through targeted synthetic data generation

πŸ—οΈ Architecture

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   User Query    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  Router Network β”‚ ← BERT-based classifier
                    β”‚  (Complexity +  β”‚   with Gumbel-Softmax
                    β”‚   Task Type)    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β–Ό           β–Ό               β–Ό           β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚Reasoningβ”‚ β”‚  Code   β”‚   β”‚Creative β”‚ β”‚Analysis β”‚
    β”‚ Adapter β”‚ β”‚ Adapter β”‚   β”‚ Adapter β”‚ β”‚ Adapter β”‚
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
         β”‚           β”‚             β”‚           β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  Generation   β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
                    β”‚ Self-Evaluatorβ”‚ ← Multi-metric scoring
                    β”‚ + Uncertainty β”‚   MC Dropout, calibration
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“Š Results

MetricBaseline (Single Adapter)This SystemImprovement
Task Accuracy72.3%84.7%+17.1%
Response Quality0.680.82+20.6%
Routing AccuracyN/A87.3%-
Inference Time2.3s2.1s+8.7%
GPU Memory16GB4GB-75%

πŸš€ Quick Start

Installation

git clone https://github.com/isuruigi/adaptive-lora-framework.git
cd adaptive-lora-framework
pip install -r requirements.txt

Run Demo

# Set API key (get free from https://console.groq.com)
export GROQ_API_KEY="your-key"

# Start Gradio server
python scripts/run_gradio_server.py
# Generate training data
python scripts/prepare_data.py --adapter reasoning --samples 1000

# Train adapter
python -m src.adapters.lora_trainer \
    --adapter_config configs/adapters/reasoning_adapter.yaml \
    --output_dir models/reasoning

Train Router

python experiments/train_router.py \
    --config configs/router_config.yaml \
    --output_dir models/router

πŸ“ Project Structure

adaptive-lora-framework/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ adapters/          # LoRA adapter training & management
β”‚   β”‚   β”œβ”€β”€ lora_trainer.py
β”‚   β”‚   └── adapter_manager.py
β”‚   β”œβ”€β”€ router/            # Intelligent routing system
β”‚   β”‚   β”œβ”€β”€ router_model.py    # BERT-based router with Gumbel-Softmax
β”‚   β”‚   β”œβ”€β”€ router_trainer.py  # Multi-objective training
β”‚   β”‚   └── reinforcement.py   # RL-based improvement
β”‚   β”œβ”€β”€ evaluation/        # Quality assessment
β”‚   β”‚   β”œβ”€β”€ self_evaluator.py  # Multi-metric evaluation
β”‚   β”‚   β”œβ”€β”€ uncertainty.py     # MC Dropout, calibration
β”‚   β”‚   └── llm_judge.py       # GPT-4/Claude evaluation
β”‚   β”œβ”€β”€ data/              # Data processing
β”‚   β”‚   └── synthetic_generator.py
β”‚   β”œβ”€β”€ serving/           # Production API
β”‚   β”‚   β”œβ”€β”€ api.py         # FastAPI endpoints
β”‚   β”‚   └── inference_engine.py
β”‚   └── monitoring/        # Observability
β”‚       β”œβ”€β”€ metrics.py
β”‚       └── alerting.py
β”œβ”€β”€ configs/               # YAML configurations
β”œβ”€β”€ experiments/           # Training scripts
β”œβ”€β”€ tests/                 # Unit, integration, load tests
β”œβ”€β”€ k8s/                   # Kubernetes manifests
β”œβ”€β”€ deploy/                # Deployment configs
└── notebooks/             # Jupyter notebooks

πŸ”¬ Key Innovations

1. Learned Router Network

Unlike rule-based routing, uses a trainable network that learns from actual performance data:

from src.router import AdapterRouter

router = AdapterRouter(
    encoder_name="sentence-transformers/all-MiniLM-L6-v2",
    num_adapters=4,
    hidden_dim=256,
    use_gumbel=True  # Differentiable selection
)

# Predict best adapter
adapter_probs, complexity = router(query_embedding)

2. Multi-Metric Self-Evaluation

Goes beyond accuracy with relevance, coherence, factuality, and safety scoring:

from src.evaluation import SelfEvaluator

evaluator = SelfEvaluator()
metrics = evaluator.evaluate(
    query="Explain quantum computing",
    response="Quantum computing uses qubits...",
    adapter_used="reasoning"
)
# Returns: {relevance: 0.9, coherence: 0.85, completeness: 0.8}

3. Uncertainty Quantification

MC Dropout and ensemble methods for confidence estimation:

from src.evaluation import UncertaintyQuantifier

quantifier = UncertaintyQuantifier(model, n_samples=10)
mean, uncertainty = quantifier.predict_with_uncertainty(input_ids)

πŸ’» Technical Stack

ComponentTechnology
Base ModelLlama 3 / Phi-3 / Mistral
Fine-tuningQLoRA (4-bit NF4)
RouterBERT + Gumbel-Softmax
TrainingPyTorch, Transformers, PEFT
ServingFastAPI, Gradio
DeploymentDocker, Kubernetes
MonitoringPrometheus, Grafana

πŸ§ͺ Testing

# Run all tests
pytest tests/ -v

# Unit tests only
pytest tests/unit/ -v

# Integration tests
pytest tests/integration/ -v

# Load tests
pytest tests/load/ -v

πŸ“ˆ Training Details

Adapter Training:

  • Rank: 16-32 (varies by adapter)
  • Learning rate: 2e-4 with cosine schedule
  • Batch size: 32 (gradient accumulation)
  • Training time: ~2 hours per adapter (A100)

Router Training:

  • Encoder: all-MiniLM-L6-v2
  • Dataset: 5000 synthetic examples
  • Loss: Cross-entropy + complexity regularization
  • Training time: 15 minutes

πŸš€ Deployment Options

OptionCostBest For
Local GradioFreeDevelopment
HuggingFace SpacesFreeDemo
Modal (Serverless GPU)$0.001/reqProduction
Kubernetes$100+/moEnterprise

πŸ“ Citation

@misc{adaptive-lora-2024,
  author = {Your Name},
  title = {Adaptive Multi-Adapter LLM System},
  year = {2024},
  url = {https://github.com/isuruigi/adaptive-lora-framework}
}

πŸ“„ License

MIT License - see LICENSE for details.


Built with ❀️ as a portfolio project demonstrating modern MLOps practices