#8705·datasets

Skip constructing disabled `hf_tqdm` progress bars in `map_nested` and `_single_map_nested`

Author: zhixiangliCreated Sep 28, 2026Updated Sep 28, 2026

Describe the problem

map_nested() in src/datasets/utils/py_utils.py is called for every example by the tensor formatters (such as TorchFormatter.recursive_tensorize()) with disable_tqdm=True (the default). However, map_nested() unconditionally wraps its input iterable in hf_tqdm(iterable, disable=disable_tqdm, desc=desc), and _single_map_nested() likewise wraps every nested dict or list in with hf_tqdm(..., disable=disable_tqdm, ...) as pbar: (even though map_nested always invokes _single_map_nested with disable_tqdm=True).

Even when disable=True, constructing and tearing down an hf_tqdm (tqdm_asyncio / std.tqdm) instance acquires TMonitor / position locks (tqdm.__new__ -> _get_free_pos), initializes tqdm_asyncio and std.tqdm attributes, and enters/exits a context manager. Doing this once (or multiple times for nested dicts/lists) per row adds avoidable per-example overhead. This cost is paid 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

In src/datasets/utils/py_utils.py:

  1. In map_nested, only wrap iterable in hf_tqdm(...) when not disable_tqdm.
  2. In _single_map_nested, use nullcontext(pbar_iterable) when disable_tqdm is truthy instead of constructing hf_tqdm(...).

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 a text column (synthetic text of 2,048 tokens) and an integer label column.
  • Pipeline Configuration (per process; rank = 0..3, one process per GPU):
    python
    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) × 4 DataLoader workers 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 through fsspec / 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 #8684 @ f6bb49263

End-to-End Results (4 × A100, shuffled streaming read, us-central1 Regional Bucket)

Metric Before (main a4ee9cf1b) After (PR #8684 f6bb49263) Impact
Read Throughput (mean) 276.02 MB/s (263.23 MiB/s) 299.49 MB/s (285.61 MiB/s) +8.50%
Throughput (samples/s) 22,452.99 samples/s 24,362.24 samples/s +8.50%
Epoch Duration (mean, 3 rounds) 125.15 s 115.54 s -7.68%

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.45 s -8.74 s
2 118.36 s 108.87 s -9.49 s
3 118.91 s 108.30 s -10.61 s