Retrieval Dataset
NeMo AutoModel supports retrieval model fine-tuning using a retrieval-style dataset: each training example is a query paired with one positive document and one or more negative documents.
This dataset is used by the retrieval recipes in examples/retrieval/bi_encoder/ and
examples/retrieval/cross_encoder/, together with the retrieval collators. For an end-to-end training workflow, refer to
Retrieval Fine-Tuning.
AutoModel supports three raw retrieval input formats: local corpus ID-based JSON, AutoModel-format Hugging Face
datasets, and inline JSONL. It also supports prepared normalized Arrow bundles for large corpus-backed datasets.
Regardless of how the source data is stored, AutoModel converts it into the following common format for training. Later
sections explain how to use the raw formats with RetrievalDatasetConfig and normalized Arrow with
NormalizedRetrievalDatasetConfig.
Training-Time Record Format
With model_type: bi_encoder, both dataset configs build a Hugging Face datasets.Dataset and transform each stored
record into the same training-time schema:
question: query stringdoc_text: list of document texts in the order[positive, negative_1, negative_2, ...]doc_image: list of images (or empty strings), aligned withdoc_textdoc_id: list of document identifiers aligned withdoc_text. Corpus-backed andhf://sources preserve document IDs. Pure inline JSONL produces empty IDs, so use a corpus-backed source or custom preprocessing when reliable same-document masking is required.query_instruction/passage_instruction: optional, used whenuse_dataset_instruction: trueand the corpus provides instructions through metadata
With model_type: cross_encoder, the configs consume the same retrieval records but flatten each query with its
positive and negative passages. CrossEncoderCollator then serializes each query-passage pair for reranking.
For each query, both configs select exactly one positive passage. They use the first item in pos_doc by default. Set
cycle_positive_docs: true to rotate through multiple positives deterministically by epoch
(epoch % len(pos_doc)). For guidance about expanding multi-positive data and preventing sibling positives from
becoming negatives, refer to
Convert Qrels and Multi-Positive Data.
Raw Input Formats
RetrievalDatasetConfig selects the loader for the three raw formats from the source URI or file extension. Use
.jsonl for inline records, .json for local corpus-backed data, and hf:// for AutoModel-format Hugging Face
sources.
The following table compares the three supported source types:
Corpus ID-Based JSON
Use this format when documents live in a separate corpus and training examples reference documents by ID.
The following example shows one training JSON file:
For the text retrieval workflow in this guide, configure the corpus as a TextQADataset. The corpus directory must
contain a merlin_metadata.json file and a Hugging Face-loadable train split with id and text columns.
AutoModel calls datasets.load_dataset(<corpus path>)["train"], then resolves pos_doc and neg_doc IDs against
that split.
Other registered corpus classes support image-oriented data and have different column requirements. Use the matching retrieval example when you configure one of those classes.
The following example is a minimal merlin_metadata.json file:
The corresponding minimal local layout is:
The corpus_id in merlin_metadata.json must match the corpus_id in each training record. Relative corpus paths in
train.json are resolved relative to the JSON file.
The following behavior applies to corpus-backed records:
pos_docandneg_doccan be lists of{"id": ...}dicts or raw IDs (they are normalized internally).- To train with corpus instructions, set
use_dataset_instruction: trueon both the dataset and the bi-encoder collator. The dataset surfacesquery_instructionandpassage_instructionfrommerlin_metadata.json. The collator prepends them before tokenization.
Hugging Face Sources
Direct hf:// loading expects the AutoModel retrieval schema, not arbitrary Hugging Face retrieval datasets. The URI
format is:
Each subset must provide:
<subset>/dataset_metadata.jsonwithcorpus_idmetadata andids_onlyset tofalseor omitted- a
<subset>_corpustrain split withidandtextcolumns - a
<subset>train split withquestionandpos_doc, plus at least oneneg_docwhenn_passages > 1
Datasets in BEIR, DPR, MS MARCO, MIRACL, or other layouts need preprocessing unless they have already been converted to the AutoModel schema above.
Inline-Text JSONL (No Corpus Required)
This is convenient for custom fine-tuning pipelines where the documents are included inline.
The following JSONL example contains one record per line:
The following behavior applies to inline records:
queryis accepted (questionis also accepted as an alias).pos_docandneg_doccan be either:- strings (interpreted as document text), or
- lists of strings, or
- dicts with at least
text.
- The current LLM retrieval collators tokenize text only. Do not rely on inline
imageor OCR fields unless you add a custom preprocessing and collator path. - If
corpus_idis not provided, it defaults to__inline__. - Keep
use_dataset_instruction: falsefor pure inline records. Dataset instructions come from corpus metadata, which inline JSONL does not provide.
Raw Record Requirements
The following requirements apply to all three raw input types:
pos_docmust be non-empty.- Local JSON and JSONL records must include
neg_doc; it may be empty only whenn_passages: 1. - An
hf://source may omitneg_doconly whenn_passages: 1. - With
n_passages > 1, every record must provide at least one negative. If it provides fewer thann_passages - 1, AutoModel repeats the available negatives to fill the requested passage count.
n_passages: 1 is supported, but it is not the usual training setup. The standard bi-encoder and cross-encoder recipes
need at least one negative candidate for meaningful contrastive or reranking supervision, unless you add a custom
negative strategy such as qrels-aware in-batch negatives.
Choose How to Load the Data
Load any of the three raw input types directly with RetrievalDatasetConfig. For large corpus-ID datasets, you can
instead prepare a normalized Arrow bundle on CPU and load it with NormalizedRetrievalDatasetConfig.
Normalized Arrow is not a fourth raw input format. It is a prepared version of corpus ID-based JSON data that keeps the same query-to-document relationships in portable local Arrow shards.
Normalized Arrow for Large VL Datasets
A normalized Arrow bundle is a prepared version of corpus-backed retrieval data. Query rows keep their document ID references, while local Arrow shards store the referenced text and image documents. Training reads those prepared shards instead of loading the source corpora and building their Hugging Face dataset caches after GPUs have been allocated.
Use normalized Arrow for full-scale or image-heavy vision-language (VL) retrieval. For typical text-only retrieval,
loading the source data directly with RetrievalDatasetConfig is usually simpler. Normalization can still help when a
large text corpus has expensive startup or when you need a portable prepared dataset.
The CPU preparation tool currently accepts corpus ID-based JSON sources. For hf:// or inline JSONL sources, continue
using RetrievalDatasetConfig unless you first convert them to corpus ID-based JSON.
Prepare the bundle before launching GPU training:
Then train with the normalized dataset config:
For large datasets on a Slurm cluster, use the CPU normalized dataset preparation scripts to process sources in parallel. See the retrieval data preparation tools for local and Slurm commands, source selection, append and resume behavior, and the cache-warming alternative.
Configure the Dataset and Collator in YAML
Use RetrievalDatasetConfig with the appropriate collator for every supported raw source. The following corpus-backed
example also works with an hf:// source:
For all supported source types, do_shuffle: true shuffles rows only when max_train_samples is set, before
subsampling. Otherwise, the dataloader or distributed sampler controls training order.
For inline JSONL, keep the same public config and change the source path:
For cross-encoder training, keep the same dataset config, set model_type: cross_encoder, and use
CrossEncoderCollator arguments:
Convert Qrels and Multi-Positive Data
The training dataset does not consume query relevance judgments (qrels) as a separate input. Convert qrels-style data into retrieval records before training:
- Put every passage in a corpus split with stable
idandtextvalues. - For each query, write one or more records with
question_id,question,corpus_id,pos_doc, andneg_doc. Use uniquequestion_idvalues within each mining file because hard-negative mining writes results back by ID. - Put the canonical positive first in
pos_doc. Setcycle_positive_docs: trueto rotate through all positives by epoch, or expand the query into multiple records if every positive must be supervised in the same epoch. - For hard-negative mining, include every known positive document ID for the query in
pos_doc. The miner excludes only IDs in the input record, not an external qrels file. - If you expand a query into multiple records, keep sibling-positive records out of the same in-batch-negative training batch or add a custom known-positive mask.
Preserve the complete qrels separately for offline full-corpus retrieval evaluation and use them to audit mined negatives before reusing the output for training.