quantlab.model.torch_data.prefetch#
- quantlab.model.torch_data.prefetch(batches, device)[source]#
Yield
batchesondevice, each readied ahead by a background thread.A thread takes the next items of
batches(aDataLoader, or any iterable ofBatch-shaped tuples) in order and moves them todevicewhile the caller works on the current one, keeping at mostPREFETCH_DEPTHwaiting besides the one it is readying. For a CUDAdeviceit copies each batch into pinned memory and queues the copy to the GPU on a side stream; the caller’s stream waits for that copy before the batch is handed over, so the transfer overlaps the GPU’s work on the previous batch. The batches come out exactly asbatchesgives them, in its order.An error raised by
batchesis raised here, at the batch it stopped at. Closing the iterator early (close(), or an error in the caller’s loop undercontextlib.closing) stops and joins the thread.- Parameters:
- batchesiterable
The batches, each a
Batchor a tuple of its fields.- devicestr or torch.device
Where to hand them over.
- Yields:
- Batch
The next batch, on
device.
Examples
>>> [int(b.where[0][0]) for b in prefetch([batch, batch], "cpu")] [4, 4]