Retrieval-augmented pretraining (RAP) for MEDS-style EHR data.
This is a work-in-progress, but the core research stack is implemented.
MedRAP composes a retrieval-augmented patient model from swappable stages
(encode → query → retrieve → retrieval-encode → fuse → pool → predict),
orchestrated by RetrievalAugmentedModel. The pipeline also supports
REALM-style marginalized retrieval (per-document logits marginalized over
the retrieved set, with differentiable document scores for end-to-end retriever
training) via the marginalized_retrieval flag.
Implemented stage components (all in medrap.model):
- Encoders:
MEDSCodeEncoder,TokenEmbeddingEncoder, andTimeDeltaRoPEPatientEncoder(transformer encoder with rotary position embeddings derived from cumulative log time-deltas). - Query projectors:
LinearQueryProjector,SequenceMeanQueryProjector. - Retrievers:
InMemoryRetrieverandHFDatasetRetriever(FAISS over a prepared HF dataset, with optional GPU FAISS, payload caching, and anone/random_docsretrieval ablation mode). - Retrieval encoders:
TokenFeatureRetrievalEncoder,MeanPooledRetrievalEncoder,PerDocMeanPooledRetrievalEncoder,LinearProjectionRetrievalEncoder,KeyEmbeddingRetrievalEncoder. - Fusion:
ReplaceFusion,ConcatFusion,PassthroughFusion(retrieval-ablation baseline),CrossAttentionFusion,PerDocCrossAttentionFusion(per-document cross-attention; required formarginalized_retrievalwith attention-based fusion). - Pooling:
IdentityPooling,MaskedMeanPooling. - Heads:
LinearHead. - Tasks & losses (in
medrap.train): binary, marginalized-binary, and multi-task binary classification, withBinaryClassificationLoss,MarginalizedRetrievalSupervisedLoss,MultiTaskBCELoss, andMultiTaskBCEMarginalizedLoss.
src/medrap/
├── model/ # nn.Module building blocks (shared by all commands)
├── train/ # Lightning training infrastructure (train, eval)
├── prepare_retrieval/ # Offline retrieval dataset preparation
├── preprocess/ # Raw-MEDS rare-code/sparse-subject filtering (pre-tensorization)
├── retrieve/ # Batch retrieval from a trained model
├── get_embeddings/ # Embedding extraction from a trained model
└── predict_probabilities/ # Probability prediction from a trained model
Each subpackage has a README.md describing its contents. types.py,
configs.py, cli.py, and extraction.py live at the top level.
Assemble a retrieval-augmented model from concrete stage components and run a
forward pass over a synthetic MEDSTorchBatch:
import torch
from meds_torchdata import MEDSTorchBatch
from medrap.model.encoders import MEDSCodeEncoder
from medrap.model.fusion import ReplaceFusion
from medrap.model.heads import LinearHead
from medrap.model.model import RetrievalAugmentedModel
from medrap.model.pooling import IdentityPooling
from medrap.model.query_projection import SequenceMeanQueryProjector
from medrap.model.retrieval_encoder import MeanPooledRetrievalEncoder
from medrap.model.retrievers import InMemoryRetriever
model = RetrievalAugmentedModel(
encoder=MEDSCodeEncoder(),
query_projector=SequenceMeanQueryProjector(in_dim=1, out_dim=4),
retriever=InMemoryRetriever(
doc_key_embeddings=torch.FloatTensor(
[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]]
),
doc_tokens=torch.LongTensor([[1, 2], [3, 4]]),
doc_attention_mask=torch.BoolTensor([[True, True], [True, True]]),
),
retrieval_encoder=MeanPooledRetrievalEncoder(vocab_size=8, embedding_dim=2),
fusion=ReplaceFusion(),
pooling=IdentityPooling(),
head=LinearHead(in_dim=2, out_dim=2),
)
batch = MEDSTorchBatch(
code=torch.LongTensor([[101, 0], [42, 7]]),
numeric_value=torch.zeros((2, 2), dtype=torch.float32),
numeric_value_mask=torch.zeros((2, 2), dtype=torch.bool),
time_delta_days=torch.zeros((2, 2), dtype=torch.float32),
)
out = model.forward(batch=batch)
print(tuple(out.logits.shape)) # (2, 2)
print(sorted(out.metadata))
# ['fusion_output', 'query_output', 'retrieval_encoder_output', 'retriever_output']A maintained, doctested copy of this example (plus a marginalized-retrieval
variant) lives in the RetrievalAugmentedModel docstring in model/model.py.
MEDSCodeEncoder accepts meds_torchdata.MEDSTorchBatch directly.
medrap exposes four Hydra-native entrypoints; all accept Hydra overrides and
medrap-train/medrap-eval/medrap-preprocess require an output_dir.
A CPU smoke run on the built-in synthetic datamodule (no external data needed):
uv run medrap-train output_dir=/tmp/medrap_smoke \
training/datamodule=synthetic training/trainer=lightning_demoTrain and evaluate on real (tensorized MEDS) data:
uv run medrap-train output_dir=outputs/run_001 training/datamodule=meds
uv run medrap-eval output_dir=outputs/run_001_eval \
checkpoint_path=outputs/run_001/best_model.ckptRe-running into an existing output_dir is rejected unless you pass
do_overwrite=true or do_resume=true (train), or choose a fresh output_dir
(eval). medrap-eval also accepts eval_mode=validate (default) or
eval_mode=test.
Build a local Hugging Face dataset + FAISS index for retrieval (requires the prep extra, e.g. uv pip install "medrap[prep]"). Example: Hub source, then save under prep.output.output_dir:
uv run medrap-prepare-retrieval-dataset \
prep.source.path=MedRAG/textbooks prep.source.split=train \
prep.document.fields='[title,content]' \
prep.tokenizer.pretrained_model_name_or_path=Qwen/Qwen3-Embedding-0.6B \
prep.embedder.model_name_or_path=Qwen/Qwen3-Embedding-0.6B prep.embedder.device=cuda \
prep.index.source_id_column=id \
prep.output.output_dir=outputs/retrieval_artifact \
prep.index.max_length=256 \
prep.index.tokenization_batch_size=512 \
prep.index.embedding_batch_size=256 \
prep.index.encode_batch_size=32Use prep.embedder.device=cpu when no GPU is available. For a dataset already on disk, switch the source group: prep/source=load_from_disk and set prep.source.dataset_path=.... Like train/eval,
re-running into an existing prep.output.output_dir is rejected unless you pass do_overwrite=true.
Filter a raw MEDS dataset's rare codes and sparse subjects before tensorization (run before MTD_preprocess, see Using MIMIC-IV Data below):
uv run medrap-preprocess meds_data_dir=mimic/MEDS_cohort output_dir=mimic/MEDS_cohort_filtered \
min_subjects_per_code=25 min_events_per_subject=5Codes matching sentinel_code_regex (death/admission/discharge/registration events by default) are never
dropped regardless of frequency. Like the other commands, re-running into an existing output_dir is
rejected unless you pass do_overwrite=true.
These commands are direct Hydra entrypoints (@hydra.main), so Hydra receives
overrides without an intermediate subcommand dispatcher.
Pipeline-stage groups (under src/medrap/conf/):
encoder/—meds_code,rope,token_embedding,token_embedding_128,token_embedding_1024query_projector/—linear,sequence_mean,sequence_mean_1024retriever/—in_memory,in_memory_sanity,in_memory_from_pt,hf_datasetretrieval_encoder/—token_feature,mean_pooled,per_doc_mean_pooled,linear_projection,key_embeddingfusion/—replace,concat,passthrough,cross_attention_medium,cross_attention_perdoc_mediumpooling/—identity,masked_meanhead/—linear,linear_1024_to_2
Training groups (under training/):
training/datamodule/—synthetic,synthetic_marginalized,meds,meds_multitasktraining/loss/—binary_bce,marginalized_retrieval,multitask_binary_bce,multitask_binary_bce_marginalizedtraining/task/—binary_classification,marginalized_binary,multitask_binarytraining/module/—supervised_lightningtraining/trainer/—lightning_default,lightning_demo,lightning_eval,lightning_wandb
Offline retrieval-prep groups (under prep/):
prep/source/—load_dataset,load_from_diskprep/document/—ordered_fieldsprep/tokenizer/—hf_autoprep/embedder/—sentence_transformerprep/index/—defaultprep/output/—default
The default locked dependency is faiss-cpu, so HF dataset retrieval runs on CPU unless configured
otherwise. If your environment provides a CUDA-enabled FAISS build, you can request GPU FAISS search:
retriever=hf_dataset \
retriever.dataset_path=/path/to/retrieval_db \
retriever.device=0GPU FAISS is optional and not installed by default because available wheels depend on CUDA, Python, and GPU
architecture. If GPU loading fails, leave retriever.device=null for CPU retrieval.
To reduce per-batch Hugging Face row materialization overhead, cache retrieval payload columns as tensors:
retriever.cache_payloads=true \
retriever.payload_cache_device=cpuUse retriever.payload_cache_device=cuda only when the retrieval payloads fit in GPU memory.
Build a local image with BuildKit enabled so uv downloads are cached across rebuilds:
DOCKER_BUILDKIT=1 docker build -t medrap:local .GPU training requires Docker with NVIDIA Container Toolkit / NVIDIA container runtime configured on the host. Verify GPU access from the container with:
docker run --rm --gpus all medrap:local python -c "import torch; print(torch.cuda.is_available())"The image is intended to consume prebuilt artifacts. For training, mount:
- a tensorized MEDS cohort directory
- a split task-label directory containing
train.parquet,tuning.parquet, andheld_out.parquet - a prepared retrieval dataset directory with
retrieval.faiss - an output directory for logs and checkpoints
Quick smoke check:
docker run --rm medrap:local medrap-train --helpExample training invocation:
docker run --rm --gpus all \
-v /host/tensorized:/data/tensorized:ro \
-v /host/task_labels:/data/task_labels:ro \
-v /host/retrieval_db:/data/retrieval_db:ro \
-v /host/outputs:/outputs \
medrap:local \
medrap-train \
retriever=hf_dataset \
retriever.dataset_path=/data/retrieval_db \
training/datamodule=meds \
training.datamodule.config.tensorized_cohort_dir=/data/tensorized \
training.datamodule.config.task_labels_dir=/data/task_labels \
output_dir=/outputs/run_001The image runs as a non-root user by default. If you need bind-mounted output files to match your current host user, override the runtime user:
docker run --rm --user "$(id -u):$(id -g)" ...MedRAP works with any data in the MEDS format. This section walks through the full pipeline for MIMIC-IV: downloading the raw data, converting it to MEDS, tensorizing it for PyTorch, creating task labels, and training a model.
- A PhysioNet account with MIMIC-IV credentialed access.
uv(orpip) for package management.
To avoid interfering with the main MedRAP model environment, you can run the
MIMIC-IV download and conversion steps in a dedicated uv virtual environment:
uv venv .venv-mimic
source .venv-mimic/bin/activate
uv pip install MIMIC_IV_MEDS MEDS-DEVAfter finishing data download/conversion/label extraction, deactivate this environment and return to the MedRAP project environment for tensorization and model training:
deactivate
uv sync
source .venv/bin/activateFollow the steps in MIMIC_IV_MEDS package README to download mimic data.
Follow the steps in MEDS-DEV to extract the task labels. For example,
meds-dev-task \
task=mortality/in_icu/first_24h \
dataset=$DATASET_NAME \
output_dir=$LABELS_DIR \
dataset_dir=$MEDS_COHORT_DIRUse meds-torch-data to convert MEDS parquets into an efficient on-disk tensor format:
uv run MTD_preprocess \
MEDS_dataset_dir=mimic/MEDS_cohort \
output_dir=mimic/tensorizedThe output in mimic/tensorized/ is what meds_torchdata.MEDSTorchDataConfig
expects as tensorized_cohort_dir.