Why every DataLoader worker holds a copy of your dataset
PyTorch issue #13246, “DataLoader num_workers > 0 causes CPU memory from parent process to be replicated in all worker processes”, has been open since October 2018. On the 665,298-record LLaVA annotation mix with 4 workers, a plain list took 16.0 GB of memory across the machine, and the first batch arrived after 63 seconds. With packmem 0.2.0 the same loader took 1,144 MB, and the first batch arrived in 1.0 s.
Why every worker ends up with a copy
Vision-language fine-tuning scripts in the LLaVA tradition load the whole annotation file in the dataset's constructor: self.list_data_dict = json.load(open(path)). That is a list of dicts with nested conversation turns, millions of Python objects.
- With fork, workers start out sharing the parent's memory pages, copy-on-write. But Python writes to every object it reads: indexing a dict changes its reference count, and the garbage collector writes to it when it runs. Each page touched that way is copied into the worker, and over an epoch every worker builds its own copy.
- With spawn or forkserver, the default on macOS and, from Python 3.14, on Linux, nothing is shared at all: the dataset is pickled and sent to each worker. Here that was 687 MB of pickle per worker, sent before the first batch could start.
The usual advice in the issue is to hold the data as NumPy arrays or tensors, which works for numbers but not for nested conversation text. gc.freeze() stops the collector's writes but not the reference counts, and does nothing under spawn. An open pull request, #191555, proposes a SharedList for PyTorch itself: it pickles each record into one shared-memory buffer that workers read without copying. Its maintainers have asked for a design review first.
The fix: one memory-mapped file for every worker
packmem keeps records as packed columns and reads them without unpacking. In 0.2.0, a table saved with save() is memory-mapped by packmem.open(), which takes milliseconds whatever its size. Each worker receives a handle of 134 bytes instead of a pickle, and the parent and every worker read the same pages. Reading a row builds a dict on demand, so there are no long-lived Python objects to copy. The change is in the dataset's constructor:
pip install packmemimport os
import packmem
from torch.utils.data import Dataset
class Conversations(Dataset):
def __init__(self, path):
# was: self.rows = json.load(open(path))
saved = path + ".pkm"
if not os.path.exists(saved): # once, like an Arrow cache
packmem.load_json(path).save(saved)
self.rows = packmem.open(saved) # memory-mapped
def __len__(self):
return len(self.rows)
def __getitem__(self, i):
return self.rows[i] # a dict, built on demandHugging Face datasets also memory-maps its data, and it is a good answer. packmem used 37% less memory across the machine (1,144 MB against 1,810 MB). Most of the difference is that every worker imports the library it reads with: 56 MB for datasets against 0.2 MB for packmem. The file is also 2.5 times smaller (309 MB against a 785 MB Arrow cache), and packmem takes the JSON as it is, with no schema to declare. With fork workers the machine held 549 MB with packmem against 994 MB with datasets and 3.6 GB with a list.
What this does not show
- It measures the annotations only: no images, model or GPU. It is what holding and serving the records costs, which is the part that runs out of memory.
- Memory held to within about 1 MB across runs; the server was shared, with a load of 10 to 27, so timings are indicative.
datasetsis about twice as fast for batched access (ds[i:i+64]), and parsing JSON into packmem the first time is slower thanjson.load. The saved file is reused after that.- Every variant produced identical batches. The scripts, raw results and the full write-up are in the repository: RESULT_0.2.0.md.