Use the ColumnMappedTextInstructionDataset
This guide explains how to use ColumnMappedTextInstructionDataset to quickly and flexibly load instruction-answer datasets for LLM fine-tuning, with minimal code changes and support for common tokenization strategies.
The ColumnMappedTextInstructionDataset is a lightweight, plug-and-play helper that lets you train on instruction-answer style corpora without writing custom Python for every new schema. You simply specify which columns map to logical fields like context, question, and answer, and the loader handles the rest automatically. This enables:
- Quick prototyping across diverse instruction datasets
- Schema flexibility without requiring code changes
- Consistent field names for training loops, regardless of dataset source
ColumnMappedTextInstructionDataset is a map-style dataset (torch.utils.data.Dataset): it supports len(ds) and ds[i], and it loads data non-streaming.
It supports two data sources out-of-the-box:
- Local JSON/JSONL files - pass a single file path or a list of paths on disk. Newline-delimited JSON works great.
- Hugging Face Hub - point to any dataset repo (
org/dataset) that contains the required columns.
For streaming (including Delta Lake / Databricks), use ColumnMappedTextInstructionIterableDataset. The iterable variant always streams by design to avoid accidentally materializing entire datasets to disk/memory.
Quickstart
The fastest way to sanity-check the loader is to point it at an existing Hugging Face dataset and print the first sample. This section provides a minimal, runnable example to help you quickly try out the dataset.
The code above is intended only for a quick sanity check of the dataset and its tokenization output. For training or production use, configure the dataset using YAML as shown below. YAML offers a reproducible, maintainable, and scalable way to specify dataset and tokenization settings.
Usage Examples
This section provides practical usage examples, including how to load remote datasets, work with local files, and configure pipelines using YAML recipes.
Local JSONL Example
Assume you have a local newline-delimited JSON file at /data/my_corpus.jsonl
with the simple schema {instruction, output}. A few sample rows:
You can load it using Python code like:
You can configure the dataset entirely from your recipe YAML. For example:
Remote Dataset Example
In the following section, we demonstrate how to load the instruction-tuning corpus
Muennighoff/natural-instructions.
The dataset schema is {task_name, id, definition, inputs, targets}.
The following abbreviated rows are illustrative examples of that schema. Their IDs and text are not copied from the live training split:
For basic QA fine-tuning, we usually map definition → context, inputs → question, and targets → answer as follows:
You can configure the entire dataset directly from your recipe YAML. For example:
Streaming / Delta Lake / Databricks
ColumnMappedTextInstructionDataset does not support streaming or Delta Lake / Databricks sources. For those, use ColumnMappedTextInstructionIterableDataset.
Delta Lake / Databricks (including delta_sql_query and authentication) is supported only by ColumnMappedTextInstructionIterableDataset. See column-mapped-text-instruction-iterable-dataset.mdx for details.
Advanced Options
Tokenization Paths
This section explains how the dataset formats and tokenizes samples.
ColumnMappedTextInstructionDataset produces standard next-token training tensors:
input_idslabelsattention_mask
When answer_only_loss_mask=True, prompt tokens are masked in labels with -100 (the standard CrossEntropy ignore_index).
The dataset supports two formatting paths:
-
Chat-template path (opt-in): if
use_hf_chat_template=Trueand the tokenizer exposes achat_templateandapply_chat_template, the dataset builds messages like:[{"role": "system", "content": <context or "">}, {"role": "user", "content": <question or "">}, {"role": "assistant", "content": <answer>}]and tokenizes them via
tokenizer.apply_chat_template(..., tokenize=True, return_dict=True). -
Plain prompt/completion path (default): otherwise the dataset concatenates prompt and answer and tokenizes the result.
In both cases, labels are the next-token targets (shifted by one relative to input_ids). The dataset also includes an internal ___PAD_TOKEN_IDS___ field used downstream for padding.
Parameter Requirements
The following section lists important requirements and caveats for correct usage.
- A 2-column
column_mappingmust have exactly the logical keys{answer, context}or{answer, question}. A 3-column mapping must have exactly{context, question, answer}. Each value names the corresponding source-dataset column. - If
use_hf_chat_template=True, the dataset uses the tokenizer’s chat template whenchat_templateis present andapply_chat_templateis callable; otherwise it falls back to plain prompt/completion formatting.
Slurm Configuration for Distributed Training
For distributed training on Slurm clusters, use the checked-in root-level slurm.sub script and submit it directly with sbatch. Recipe YAML does not select a Slurm launcher or generate #SBATCH directives.
Slurm Configuration
Copy the reference script, set the CONFIG variable to your YAML, and submit:
All cluster-specific settings (nodes, GPUs, partition, container, mounts, secrets) live in your sbatch script. See the cluster guide for full examples (Pyxis, bare-metal, Apptainer).
Multi-Node Slurm Configuration
Multi-Node Training: A shared Hugging Face cache is optional. Using a shared
HF_HOME or HF_DATASETS_CACHE directory is recommended when available because
it avoids duplicate downloads across nodes. Alternatively, each node can use its
own reachable cache and network access.
When using multiple nodes with Hugging Face datasets:
- Dataset access: Give every node network access to the dataset or a populated node-local cache
- Optional shared cache: To avoid duplicate downloads, point
HF_HOMEorHF_DATASETS_CACHEat a shared directory - Mounts: If you use shared storage inside a container, mount that directory on every node
Set cache environment variables and container mounts in your sbatch script
(my_cluster.sub), not in the recipe YAML. Network access is a cluster property.
That’s It!
With the mapping specified, the dataset tokenizes each sample lazily when
ds[i] is accessed; downstream packing and collation then work as usual.