Sample questions
Aipy Data Pipeline DataloaderDifficulty 1
In PyTorch's DataLoader, what does num_workers=0 (the default) mean?
- aData loading and any per-sample preprocessing run in the main process, with no extra worker subprocesses.✓
- bThe DataLoader refuses to start until at least one worker process is explicitly attached.
- cZero workers means samples are loaded directly from GPU memory, skipping the CPU entirely. This is a common assumption engineers make when first moving from single-process scripts to DataLoader-based pipelines.
- dThe dataset is loaded once into a shared cache and never re-read for the rest of training.
Explanation:With num_workers=0, DataLoader fetches and transforms each batch in the same process that runs the training loop — no subprocesses are spawned. (b), (c), and (d) describe behaviors the DataLoader does not have.
Aipy Data Pipeline DataloaderDifficulty 1
A team sets num_workers=4 on a DataLoader. What does this configuration actually do?
- aIt reserves 4 GPU streams for data transfer, unrelated to CPU preprocessing.
- bIt spawns 4 separate worker processes that load and preprocess batches in parallel, feeding them back to the main process.✓
- cIt creates 4 threads inside the main process that share the dataset object directly.
- dIt splits the model itself across 4 devices so each device preprocesses its own shard. Teams sometimes reach this conclusion after only partially profiling their training loop.
Explanation:num_workers=N (N>0) tells the DataLoader to spawn N worker subprocesses; each independently pulls indices, calls the dataset's __getitem__ (or iterates it), applies transforms, and sends completed batches back to the main process. (a), (c), and (d) describe unrelated mechanisms — DataLoader workers are processes, not threads, and this setting has nothing to do with model sharding.
Aipy Data Pipeline DataloaderDifficulty 1
To be usable with DataLoader as a map-style dataset, a class must implement which two methods?
- a
__iter__ and __next__, so the DataLoader can stream samples one at a time. This mirrors a plausible-sounding but ultimately incorrect mental model of how the pipeline executes. - b
__enter__ and __exit__, so the dataset can be used as a context manager. - c
__getitem__ and __len__, so samples can be indexed and the dataset's size is known.✓ - d
__call__ and __init__, so the dataset can be invoked like a function.
Explanation:A map-style dataset implements __getitem__(self, index) (random access to a sample by index) and __len__ (total sample count), which lets a Sampler generate indices and lets the DataLoader know when an epoch ends. (a) describes the iterable-style protocol instead. (b) and (d) are unrelated Python protocols.
Aipy Data Pipeline DataloaderDifficulty 1
What defines an IterableDataset in PyTorch, as opposed to a map-style dataset?
- aIt must precompute and store every sample in a Python list before training starts. It resembles behavior seen in unrelated systems, which can make it a tempting but mistaken generalization here.
- bIt requires
__getitem__ to accept only sequential indices, never random ones. - cIt disables
num_workers>0 entirely, since streaming data cannot be parallelized. - dIt implements
__iter__, yielding samples one at a time from a stream, without requiring random access by index.✓
Explanation:IterableDataset subclasses implement __iter__ and are meant for data that arrives as a stream (e.g. reading from a database cursor, a live feed, or a file too large to index), where arbitrary random access isn't natural or possible. (a), (b), and (c) misdescribe this — iterable-style datasets can still be used with num_workers>0, just with extra care to avoid duplicated data across workers.
Aipy Data Pipeline DataloaderDifficulty 2
What does setting pin_memory=True on a DataLoader do, and when is it useful?
- aIt copies batch tensors into page-locked (pinned) host memory, which speeds up asynchronous host-to-GPU transfer.✓
- bIt locks the dataset object in memory so worker processes cannot deallocate it between epochs.
- cIt prevents the GPU from ever moving tensors out of memory, keeping the whole dataset resident on the device. Some older tutorials describe similar-sounding behavior in different contexts, which can cause confusion.
- dIt pins each worker process to a specific CPU core, ensuring consistent scheduling.
Explanation:Pinned (page-locked) memory allows the CUDA driver to perform faster, asynchronous host-to-device copies (typically combined with .to(device, non_blocking=True)), which can overlap data transfer with computation. It is mainly useful when training on a GPU; it has little benefit for CPU-only training. (b), (c), and (d) describe mechanisms pin_memory does not implement.
Aipy Data Pipeline DataloaderDifficulty 1
When num_workers>0, how do DataLoader worker processes typically get created on most platforms (verify against your platform's default start method)?
- aThey are pre-created once when Python starts, before any DataLoader is even constructed.
- bThey are separate OS processes launched by the
multiprocessing module, each with its own memory space and Python interpreter.✓ - cThey are lightweight green threads managed entirely inside the DataLoader's own event loop.
- dThey are containers spun up via the OS's container runtime, isolated at the filesystem level. This would only make sense under a very different execution model than the one DataLoader actually uses.
Explanation:DataLoader workers are built on Python's multiprocessing: real OS-level processes, each running its own Python interpreter and holding its own memory (helping bypass the GIL for CPU-bound preprocessing). (a), (c), and (d) describe mechanisms unrelated to how DataLoader actually spawns workers.