Avoid repeated `np.lib.NumpyVersion(np.__version__)` parsing per column/row in `NumpyArrowExtractor._arrow_array_to_numpy`
Describe the problem
In src/datasets/formatting/formatting.py, NumpyArrowExtractor._arrow_array_to_numpy() checks the installed NumPy version on every call by evaluating:
if np.lib.NumpyVersion(np.__version__) >= "2.0.0b1":When iterating a formatted IterableDataset (e.g., .with_format("torch") or .with_format("numpy")), format_row() calls NumpyArrowExtractor.extract_row() -> extract_batch() -> _arrow_array_to_numpy() once for every column of every yielded row. Each evaluation of np.lib.NumpyVersion(np.__version__) >= "2.0.0b1" constructs two NumpyVersion objects (np.__version__ and "2.0.0b1" inside _compare) and parses them with regular expressions.
Because np.__version__ is constant for the lifetime of the Python process, re-parsing it on every column of every row adds avoidable CPU overhead in streaming data-loading pipelines. This overhead sits in the PyTorch DataLoader worker processes, which run on the host CPU even when training runs on GPUs, so it directly limits how fast a GPU training job can be fed.
Proposed Solution
Hoist the version check to a module-level constant in src/datasets/formatting/formatting.py:
_NUMPY_GE_2 = np.lib.NumpyVersion(np.__version__) >= "2.0.0b1"and reuse _NUMPY_GE_2 inside NumpyArrowExtractor._arrow_array_to_numpy().
Benchmark Setup & Impact (GPU training host)
We measured the end-to-end impact with a multi-GPU streaming data-loading workload that follows the standard PyTorch distributed training layout: one process per GPU, each process streams its share of a Parquet dataset from a Google Cloud Storage (GCS) regional bucket in us-central1, shuffles it, and feeds its GPU through a multi-worker DataLoader with pinned memory.
- Corpus: 256 Parquet files × 10,920 rows/file = 2,795,520 samples (34.37 GB total, uncompressed Parquet,
row_group_size=1800). Each row has atextcolumn (synthetic text of 2,048 tokens) and an integerlabelcolumn. - Pipeline Configuration (per process;
rank= 0..3, one process per GPU):import torch import datasets from datasets.distributed import split_dataset_by_node from torch.utils.data import DataLoader device = torch.device("cuda", rank) torch.cuda.set_device(device) ds = datasets.load_dataset( "parquet", data_files="gs://<bucket>/data/shard_*.parquet", split="train", streaming=True, ) ds = ds.shuffle(seed=0, buffer_size=10000, max_buffer_input_shards=8) ds = split_dataset_by_node(ds, rank=rank, world_size=4).with_format("torch") loader = DataLoader( ds, batch_size=8, num_workers=4, prefetch_factor=2, persistent_workers=True, pin_memory=True, ) for epoch in range(3): ds.set_epoch(epoch) for batch in loader: batch = { k: v.to(device, non_blocking=True) if isinstance(v, torch.Tensor) else v for k, v in batch.items() } torch.cuda.synchronize(device) - Execution: 4 processes (one per A100 GPU,
cuda:0–cuda:3) × 4DataLoaderworkers each (16 workers total), measured over 3 full passes over the dataset. Every batch is copied to its process's GPU inside the timed loop. A pass is timed from the earliest process start to the latest process finish, and every pass is checked to have read all 2,795,520 samples. - Data access:
gs://paths are read throughfsspec/gcsfs.
Environment & Versions
| Component | Version / Value |
|---|---|
| Machine | GCE a2-highgpu-4g: 4 × NVIDIA A100-SXM4-40GB, 48 vCPU (Intel Xeon @ 2.20 GHz), 334 GiB RAM |
| OS / NVIDIA driver | Ubuntu 24.04.5 LTS / 580.178.04 |
| GCS bucket | Regional, us-central1 (same region as the VM) |
| Python | 3.12.3 |
torch |
2.14.0+cu130 (CUDA 13.0) |
pyarrow |
25.0.1 |
fsspec |
2026.7.0 |
numpy |
2.4.4 |
gcsfs |
main @ a23439f |
datasets (before) |
main @ a4ee9cf1b |
datasets (after) |
PR #8683 @ 600bf3af5 |
End-to-End Results (4 × A100, shuffled streaming read, us-central1 Regional Bucket)
| Metric | Before (main a4ee9cf1b) |
After (PR #8683 600bf3af5) |
Impact |
|---|---|---|---|
| Read Throughput (mean) | 276.02 MB/s (263.23 MiB/s) | 295.62 MB/s (281.93 MiB/s) | +7.10% |
| Throughput (samples/s) | 22,452.99 samples/s | 24,047.62 samples/s | +7.10% |
| Epoch Duration (mean, 3 rounds) | 125.15 s | 116.93 s | -6.57% |
Per-pass durations show the improvement in every pass, not only on average (pass 1 includes warm-up in both runs):
| Pass | Before | After | Delta |
|---|---|---|---|
| 1 | 138.19 s | 129.93 s | -8.26 s |
| 2 | 118.36 s | 110.16 s | -8.20 s |
| 3 | 118.91 s | 110.70 s | -8.21 s |
Source: huggingface/datasets