Yiqiao Jin1, Kartik Sharma1, Vineeth Rakesh2, Yingtong Dou2, Menghai Pan2, Mahashweta Das2, Srijan Kumar1
1 Georgia Institute of Technology Β Β·Β 2 Visa Research
@inproceedings{jin2025sara,
title={SARA: Selective and Adaptive Retrieval-augmented Generation with Context Compression},
author={Jin, Yiqiao and Sharma, Kartik and Rakesh, Vineeth and Dou, Yingtong and Pan, Menghai and Das, Mahashweta and Kumar, Srijan},
booktitle={ACL},
year={2026}
}SARA represents retrieved evidence as a mix of k natural-language passages and n β k compressed
retrieval embeddings. The compressed pieces are injected at <COMPRESS> token positions in the prompt
through a small trained projector that maps retriever embeddings into the language model's hidden space,
letting the model attend to many more documents within a fixed context budget.
This repository supports exactly two evaluated methods, sharing one dataset, retriever, model loader, generation code, metrics, and CLI:
- Standard RAG β all
nretrieved documents inserted into the Mistral prompt as ordinary text. - SARA β the same
ncandidates, butkrendered as text andn β kas compressed embeddings.
Both start from the public mistralai/Mistral-7B-Instruct-v0.2 base and are trained with matched budgets,
so the comparison is fair by construction.
- π― Two-method, apples-to-apples: Standard RAG vs SARA under identical data, retriever, prompts, decoding, and seeds.
- π§ Projector +
<COMPRESS>tokens: retriever embeddings projected into the LM hidden space at compress positions. - πͺΆ LoRA-first: adapters + projector only β no merged or full-model checkpoints are ever saved.
- β‘ Fully batched generation and evaluation (dynamic padding, one GPU call per batch).
- π Leakage-safe: document-level (per-paper) train/dev/test splits.
- π οΈ Single CLI:
python -m src.run_sara {finetune, evaluate, report} --method {rag, sara}.
SARA targets Python 3.11 + CUDA 12.x. Install PyTorch from the cu128 wheel index first, then the rest.
# 1) Create and activate the env
conda create -n sara python=3.11 -y
conda activate sara
pip install -U pip setuptools wheel
# 2) Install cu128 PyTorch from the PyTorch wheel index
pip install --index-url https://download.pytorch.org/whl/cu128 torch torchvision torchaudio
# 3) Install SARA + its dependencies (editable)
pip install -e .
# or: pip install -r requirements.txtπ‘ Flash-Attention is optional. The model loads with PyTorch SDPA by default, so no flash-attn build is required.
π Set
HF_TOKENto access the gated Mistral base.WANDB_API_KEYis optional (only if you setuse_wandb: true).
python -c "
import torch, transformers, peft
from src.model import XMistralForCausalLM
print('torch ', torch.__version__, 'cuda_ok:', torch.cuda.is_available())
print('transformers', transformers.__version__)
print('peft ', peft.__version__)
print('SARA model ', XMistralForCausalLM.__name__, 'OK')
"All commands run on GPU 0 (set CUDA_VISIBLE_DEVICES to choose devices). QASPER is the primary benchmark.
python -m src.data.make_qasper_splits --dev-frac 0.1 --seed 42# Standard RAG (LoRA only; all n documents as text)
CUDA_VISIBLE_DEVICES=0 python -m src.run_sara finetune \
--config config/language_modeling/Finetune_RAG_qasper_pubbase.yaml
# SARA (LoRA + projector; k text + (n-k) compressed)
CUDA_VISIBLE_DEVICES=0 python -m src.run_sara finetune \
--config config/language_modeling/Finetune_SARA_qasper_pubbase.yamlπͺ Optional β projector alignment warm-up. Pre-train the SARA projector to reconstruct documents from their compressed embeddings before fine-tuning (set
init_projector_fromin the SARA config to the output):CUDA_VISIBLE_DEVICES=0 python -m src.train.align_projector \ --train data/reformatted/QASPER_paragraphs_train.jsonl \ --out checkpoints/paragraph/sara_qasper_align --steps 600 --batch-size 8 --lr 5e-4
# Standard RAG
CUDA_VISIBLE_DEVICES=0 python -m src.run_sara evaluate --method rag \
--models rag_qasper --retrievers bm25 --datasets qasper --n 10 --batch-size 16
# SARA
CUDA_VISIBLE_DEVICES=0 python -m src.run_sara evaluate --method sara \
--models sara_qasper --retrievers bm25 --datasets qasper --k 5 --n 10 --batch-size 16python -m src.run_sara report \
--rag outputs/rag_qasper/answers_rag_qasper_bm25_qasper_k10_n10_rep1.5.jsonl \
--sara outputs/sara_qasper/answers_sara_qasper_bm25_qasper_k5_n10_rep1.5.jsonl \
--out-dir outputs/docs/sara_vs_rag_qasper| Subcommand | Purpose |
|---|---|
finetune |
Train a LoRA (+ projector for SARA) adapter from the public base. |
evaluate |
Run batched evaluation for a method (--method rag or --method sara). |
report |
Build the Standard RAG vs SARA comparison report + table. |
Evidence allocation is controlled by --k and --n:
| Method | Allocation | Flags |
|---|---|---|
rag |
all n documents as text (k = n) |
--n N |
sara |
k text + n β k compressed (k < n) |
--k K --n N |
Unsupported --method values are rejected with a clear error.
| Component | Default |
|---|---|
| π Primary benchmark | QASPER (long-document scientific QA) |
| π€ Base LM | mistralai/Mistral-7B-Instruct-v0.2 |
| 𧬠Compressor / retriever-embedding | Salesforce/SFR-Embedding-Mistral |
| π Retriever | BM25 |
python -m pytest tests/test_xmistral.py \
tests/test_checkpoint_roundtrip.py \
tests/test_train_sara_sanity.py -qThese cover the <COMPRESS> embedding injection (batched vs single-example parity), checkpoint round-trip
(LoRA + projector + added-token rows reload numerically), and the training gradient/optimizer sanity checks.
Released under the Apache License 2.0.
Built on excellent open-source work: π€ Transformers Β· π€ PEFT Β· π€ Accelerate Β· LlamaIndex.