Skip to content

Read each paired text-classification dataset row once - #894

Open
snygginghani wants to merge 1 commit into
huggingface:mainfrom
snygginghani:perf/read-paired-dataset-row-once
Open

snygginghani wants to merge 1 commit into
huggingface:mainfrom
snygginghani:perf/read-paired-dataset-row-once

Conversation

@snygginghani

Copy link
Copy Markdown

Summary

The text-classification evaluator supports passing two columns to a pipeline, for example the premise and hypothesis in natural language inference. DatasetColumnPair currently indexes the same row separately for each field. This repeats dataset formatting and Arrow conversion. Read the row once and reuse that path for both indexing and iteration.

Validation

For 15,000 synthetic premise/hypothesis rows in datasets.Dataset, the median time to materialize all pipeline inputs across eight alternating runs on Windows was 806.84 ms before and 412.56 ms after (1.96× faster). Inputs matched exactly. The single-input control was 476.34 ms before and 480.49 ms after. This measures input preparation without model inference. Python 3.13.3, datasets 5.2.0, Intel Core i5-12400F.

Benchmark input and operation
from statistics import median
from time import perf_counter

from datasets import Dataset
from evaluate.evaluator.utils import DatasetColumnPair

n = 15_000
data = Dataset.from_dict({
    "premise": [f"A photo of somebody waiting in a railway station, scene {i}." for i in range(n)],
    "hypothesis": [f"A traveller is waiting for the train in scene {i}." for i in range(n)],
    "label": [i % 3 for i in range(n)],
})
for second_column in ("hypothesis", None):
    inputs = DatasetColumnPair(data, "premise", second_column, "text", "text_pair")
    list(inputs)
    times = []
    for _ in range(8):
        start = perf_counter()
        rows = list(inputs)
        times.append((perf_counter() - start) * 1000)
    print(second_column, median(times))

python -m pytest tests/test_evaluator_utils.py -q passed both cases. The paired case fails on the baseline because the transform runs twice for the same row. Black, isort, flake8, and git diff --check passed on the changed files.

AI assistance was used to prepare the patch, tests, and benchmark.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant