inference_phases
Per-invocation tally of where an inference step's time goes.
An InferencePhaseTally accumulates four phases of one inference invocation:
reading and decoding files, preparing records for the model, running the model,
and loading the model. The code that does that work records into whichever tally
is active in its context, and records nothing when none is.
The active tally lives in a ContextVar, so only code running in the context
that entered inference_phase_scope records into it. The executor runs each DAG
step on its own thread with a copy of the context, so work done on behalf of
another step (for example EHR reads through a shared datasource) never reaches
an inference step's tally. A reader thread pool started inside an inference
step does not inherit the context automatically; it must run its tasks under
contextvars.copy_context() for its reads to be counted.
Recording never raises into the step.
Module
Functions
current_inference_tally
def current_inference_tally( ,) ‑> InferencePhaseTally | None:Return the tally active in the current context, if any.
decoded_nbytes
def decoded_nbytes(records: Iterable[Mapping[str, Any]]) ‑> int:Return the total size of the numpy arrays among the records' values.
inference_phase_scope
def inference_phase_scope( tally: InferencePhaseTally,) ‑> Iterator[InferencePhaseTally]:Make tally the active tally for the current context.
Classes
InferencePhaseTally
class InferencePhaseTally():Thread-safe accumulator of phase time for one inference invocation.
Variables
model_load : ModelLoadRecord | None- The recorded model load, orNoneif none was recorded.
Methods
add_cache_lookup
def add_cache_lookup( self, *, hit: bool, error: bool, seconds: float, bytes_read: int,) ‑> None:Record one consulted cache lookup.
Arguments
hit: Whether the record was served from the cache. A miss is anything else, including a load that failed.error: Whether the lookup failed.seconds: Time spent on the lookup: a hit, a plain miss or a failed load.bytes_read: Bytes read from the cache; zero for a miss.
add_cache_store
def add_cache_store( self, *, stored: bool, reason: str | None, error: bool, seconds: float, bytes_written: int,) ‑> None:Record the outcome of storing a record after a cache miss.
Arguments
stored: Whether the record was written to the cache.reason: Why it was not stored; ignored when it was stored. ANoneor empty reason on a not-stored outcome is counted as"unknown".error: Whether the store failed.seconds: Time spent on the store.bytes_written: Bytes written to the cache; zero when not stored.
add_predict
def add_predict(self, seconds: float, cpu_seconds: float = 0.0) ‑> None:Record time spent in the model's prediction call.
The call's lazy reads and preparation are recorded separately and are included in this time.
Arguments
seconds: Wall time of the call.cpu_seconds: CPU time of the calling thread across the call.
add_prep
def add_prep(self, seconds: float) ‑> None:Record time spent preparing records for the model.
add_read
def add_read(self, wall: float, cpu: float, decoded_bytes: int) ‑> None:Record one file read: wall time, thread CPU time and decoded size.
add_read_ahead_fallback
def add_read_ahead_fallback(self) ‑> None:Record one file read synchronously after a read-ahead take missed.
add_read_ahead_read
def add_read_ahead_read(self, *, wall: float, cpu: float) ‑> None:Record one read on the read-ahead reader thread.
Arguments
wall: Wall time of the read.cpu: CPU time of the reader thread across the read.
add_read_ahead_take
def add_read_ahead_take(self, *, ready: bool, wait_seconds: float) ‑> None:Record one file served by a read-ahead take.
Arguments
ready: Whether the file was already read when the take began.wait_seconds: Time spent in the take.
note_read_ahead_held_bytes
def note_read_ahead_held_bytes(self, nbytes: int) ‑> None:Note the decoded bytes the reader holds untaken; the maximum is kept.
record_model_load
def record_model_load(self, record: ModelLoadRecord) ‑> None:Record how the model was loaded; the last record wins.
snapshot
def snapshot(self) ‑> PhaseTotals:Return the cumulative totals so far.
ModelLoadRecord
class ModelLoadRecord( weights_source: WeightsSource, weights_bytes: int | None, download_seconds: float | None, shared_bytes: int | None = None, store: WeightsStoreOutcome | None = None, store_reason: WeightsStoreReason | None = None,):How a model's weights were obtained for an inference invocation.
weights_bytes and download_seconds are None when not known, and
download_seconds is always None for a memo hit or a shared-store hit.
Attributes
weights_source:"memo"when the weights were already in memory,"shared"when read from the shared encrypted store,"download"when fetched from the Hub.weights_bytes: Size of the downloaded weights.download_seconds: Time the fetch from the Hub took.shared_bytes: Size of the weights read from the shared store.store: What the shared store did:"hit","stored"the download,"not_stored"it,"bypassed"the store, or"error".store_reason: Why the download was not stored, or the store bypassed.
Variables
-
download_seconds : float | None
-
shared_bytes : int | None
-
store : WeightsStoreOutcome | None
-
store_reason : WeightsStoreReason | None
-
weights_bytes : int | None
-
weights_source : WeightsSource
PhaseTotals
class PhaseTotals( read_seconds: float, read_cpu_seconds: float, prep_seconds: float, predict_seconds: float, files_read: int, decoded_bytes: int, cache_hits: int = 0, cache_misses: int = 0, cache_not_stored: int = 0, cache_errors: int = 0, cache_read_seconds: float = 0.0, cache_write_seconds: float = 0.0, cache_bytes_read: int = 0, cache_bytes_written: int = 0, cache_not_stored_reasons: tuple[tuple[str, int], ...] = (), read_ahead_files: int = 0, read_ahead_ready_files: int = 0, read_ahead_fallback_files: int = 0, read_ahead_read_seconds: float = 0.0, read_ahead_cpu_seconds: float = 0.0, read_ahead_wait_seconds: float = 0.0, read_ahead_peak_bytes: int = 0, predict_cpu_seconds: float = 0.0,):Cumulative phase totals at a point in time.
Subtracting an earlier snapshot from a later one gives the totals for the window between them.
predict_seconds times the whole prediction call, so it includes the lazy
reads and preparation recorded while it runs. The time spent in the model
proper is therefore predict_seconds - read_seconds - prep_seconds, and
the time outside the prediction call is the enclosing wall time less
predict_seconds; neither read nor prep is subtracted from that.
The cache_* fields describe the wave's decoded-record cache. Every
consulted lookup is one hit or one miss, and a failed load is a miss and an
error. cache_read_seconds covers every consulted lookup: hits, plain misses
and failed loads. cache_bytes_read counts hits only. Every miss is followed
by one store outcome, either stored or not stored for a reason, unless the
read itself raises; a store that failed also counts in cache_errors.
cache_not_stored_reasons is cumulative, sorted by reason, and is not
subtracted. Under read-ahead, a file the reader reads consults the cache on
the reader thread, so its cache_* timings are measured there.
read_seconds, read_cpu_seconds and files_read are measured on the
step's thread: under read-ahead they cover the wait in a take, not the
reader's own read. The read_ahead_* fields describe the read-ahead:
read_ahead_files: files served by a take, on the step's thread.read_ahead_ready_files: of those, files already read when the take began, so the step did not wait.read_ahead_fallback_files: files read synchronously on the step's thread after a take missed.read_ahead_read_secondsandread_ahead_cpu_seconds: wall and CPU time of the reader's reads, measured on the reader thread.read_ahead_wait_seconds: time spent in takes on the step's thread; a subset ofread_seconds.read_ahead_peak_bytes: the most decoded bytes the reader held untaken. It is a running maximum over the step, not windowed: subtracting totals keeps it unchanged.
predict_cpu_seconds is the CPU time of the step's thread across the
model's prediction call.
Variables
-
cache_bytes_read : int
-
cache_bytes_written : int
-
cache_errors : int
-
cache_hits : int
-
cache_misses : int
-
cache_not_stored : int
-
cache_not_stored_reasons : tuple[tuple[str, int], ...]
-
cache_read_seconds : float
-
cache_write_seconds : float
-
decoded_bytes : int
-
files_read : int
-
predict_cpu_seconds : float
-
predict_seconds : float
-
prep_seconds : float
-
read_ahead_cpu_seconds : float
-
read_ahead_fallback_files : int
-
read_ahead_files : int
-
read_ahead_peak_bytes : int
-
read_ahead_read_seconds : float
-
read_ahead_ready_files : int
-
read_ahead_wait_seconds : float
-
read_cpu_seconds : float
-
read_seconds : float