#8705·datasets

在 `map_nested` 和 `_single_map_nested` 中跳过构建无效的 `hf_tqdm` 进度条

作者: zhixiangli创建于 2026年9月28日更新于 2026年9月28日

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.

内容来源: huggingface/datasets