read_ahead
Reading an inference step's next files on a reader thread.
A model inference step reads and decodes one file, then runs the model on it. Read-ahead overlaps the two: a daemon reader thread reads the step's files in the order the step will ask for them, while the model runs on the current one, and hands each file's records to the step when the step asks for that file.
with open_read_ahead(datasource, files, depth=1, max_bytes=..., thread_name=...) as ra:
with read_ahead_scope(ra):
... # reads of `datasource` in this context take from `ra`
The reader holds at most depth files the step has not yet taken, and reads
no further while those hold max_bytes or more; a file is always admitted
when nothing is held, however large. Every read, the reader's and any the step
makes itself after a take misses, runs under FileReadAhead.process_lock, so
a step never has two reads of its datasource running at once.
The reader runs in a copy of the context open_read_ahead was called in, so
it records into the step's inference tally and stops when the step's run is
cancelled. It never sees a read-ahead scope. The open_read_ahead exit stops
the reader and waits for it, however long its current read takes, so nothing
it does outlives the step.
Module
Functions
current_read_ahead
def current_read_ahead() ‑> FileReadAhead | None:Return the read-ahead active in the current context, if any.
open_read_ahead
def open_read_ahead( datasource: _ProcessesFiles, files: Sequence[str], *, depth: int, max_bytes: int, thread_name: str,) ‑> Iterator[FileReadAhead | None]:Read files from datasource ahead of the step on a reader thread.
The reader runs in a copy of the calling context, taken here so that it
carries the step's tally and cancellation but no read-ahead scope. When a
cancellation is bound, the reader is registered with it. On exit, normal
or not, the reader is stopped and waited for, with a warning every
_JOIN_WARN_SECONDS while it is still reading, and then every outcome
it stored that no take consumed is dropped.
Arguments
datasource: The datasource to read from.files: The files in the order the step will take them. A repeated file is read once, at its first position.depth: How many files the reader may hold untaken; 0 or less is off.max_bytes: Decoded bytes held untaken at or above which the reader reads no further.thread_name: Name of the reader thread.
read_ahead_scope
def read_ahead_scope(read_ahead: FileReadAhead | None) ‑> Iterator[None]:Make read_ahead the active read-ahead for the block; None is a no-op.
Classes
FileReadAhead
class FileReadAhead( datasource: _ProcessesFiles, files: Sequence[str], *, depth: int, max_bytes: int,):Files a reader thread reads ahead of an inference step, and their hand-off.
Built by open_read_ahead, which runs its reader. Files are keyed by their
position in the plan, which holds each file once in the order first given.
A take for a plan file moves the reader on to at least that file and drops
what is held for the files between the last take and that one: the reader
does not start a read of those files, an outcome it already holds for one
is discarded, and a read of one still in progress is discarded when it
finishes.
Arguments
datasource: The datasource the reader reads from.files: The files in the order the step will take them.depth: How many files the reader may hold untaken.max_bytes: Decoded bytes held untaken at or above which the reader reads no further.
Attributes
process_lock: Held across every read of the datasource made for the step, the reader's and the step's own, so that two never overlap.
Methods
serves
def serves(self, datasource: object) ‑> bool:Whether this read-ahead reads from datasource (by identity).
take
def take( self, filename: str,) ‑> list[dict[str, typing.Any]] | Literal[<ReadAheadMiss.MISS: 'MISS'>]:Return the reader's records for filename, waiting for its read.
While waiting, the run's cancellation is checked every
_TAKE_POLL_SECONDS.
Arguments
filename: The file the step needs next.
Returns
The file's records, or MISS when the file is not in the plan, is
behind an earlier take, or was never read because the reader
stopped. On MISS the caller reads the file itself, under
process_lock.
Raises
BaseException: Whatever reading the file on the reader raised, as the same object.RunCancelledError: If the run is cancelled while waiting.
ReadAheadMiss
class ReadAheadMiss(*args, **kwds):The read-ahead cannot serve a file; the caller reads it itself.
Variables
- static
MISS