How microsoft/kblam works
Project’s Core Purpose and Type
• KBLaM is a research project (for ICLR 2025) that augments large language models (LLMs) with external knowledge using specialized adapter modules. It integrates with pre-trained models from the Hugging Face hub (e.g., Meta-Llama, Phi-3).
• It aims to eliminate the need for external retrieval modules by directly incorporating knowledge base (KB) tokens into the LLM workflow. It also includes synthetic dataset generation, training, and evaluation, which are all central to understanding the system.
Break Down Main Components Based on the File Structure
a. Data generation and dataset preparation:
• Folder: dataset_generation
• Contains scripts like gen_synthetic_data.py (to create synthetic KB data) and generate_kb_embeddings.py (to convert KB information into embeddings).
• Folder: datasets
• Contains example datasets (both real and synthetic) and dataset metadata.
b. Experimentation, Training, and Evaluation Components:
• Folder: experiments
• Includes training scripts (train.py) and evaluation tools (eval.py, output_scorer.py) that replicate experiments described in the paper.
• Notebook: eval_acc.ipynb for interactive evaluation analysis.
c. Core Library/Processing Modules:
• Folder: src/kblam
• The main library that implements KBLaM functionality.
• Contains submodules:
• gpt_session.py: Likely handles session management with the GPT or transformer models.
• kb_encoder.py: Implements the encoding logic to integrate the KB into LLM inputs.
• models: Contains multiple adapter modules and configuration classes:
• kblam_config.py and kblam_processor.py: Define processing configurations and core adapters.
• Specific model adapters like llama3_model.py and phi3_model.py that interface with supported LLMs.
• utils: Contains helper scripts for data conversion, evaluation utilities, model training utilities, etc.
d. Development & Integration Support:
• Folders such as .github (workflows, CI/CD configs), tests (unit tests) and additional documentation like CODE_OF_CONDUCT.md, LICENSE, and SUPPORT.md help maintain project integrity.
Relationships and Data/Control Flow
• Data Generation to Dataset Construction:
• Synthetic KB creation and embedding generation modules (in dataset_generation) produce structured datasets that feed into training modules.
• Model Adapters and Core LLM Integration:
• The core adapter modules (in src/kblam/models) act as intermediaries between the external LLM (from Hugging Face) and the augmented KB tokens generated from the KB encoder.
• The kb_encoder (in src/kblam) preprocesses the KB data that is then formatted by model-specific adapter scripts.
• Training and Experimentation:
• Scripts in experiments use processed data (possibly from the datasets) and call functions from src/kblam (including session management and training utilities) to run the training pipelines.
• Evaluation:
• The evaluation scripts and notebooks (eval.py, eval_acc.ipynb, and scorer scripts) assess performance in terms of retrieval accuracy, refusal rate, precision, and recall.
• External Dependencies:
• An element for the “Hugging Face Hub” and external services (e.g., Azure OpenAI for dataset generation).
Architectural Patterns and Design Principles
• Modular/Layered Architecture: Separate layers for data ingestion, model adapter logic, training, and evaluation.
• Adapter Pattern: The model adapters (e.g., llama3_model.py, phi3_model.py) serve as plug-in components that can be extended, representing the extensibility of the system.
• Separation of Concerns: Clear distinct modules for dataset generation, core library functionality (encoding & model processing), training experiments, and testing.