hyrax.verbs.infer_stream#
Attributes#
Classes#
Streaming inference verb — loads model once, processes batches on demand. |
|
Context manager for streaming inference. |
Module Contents#
- class InferStream(config)[source]#
Bases:
hyrax.verbs.verb_registry.VerbStreaming inference verb — loads model once, processes batches on demand.
Overall initialization for all verbs that saves the config
- description = 'Run streaming inference: load model once and process batches interactively.'[source]#
- run(sample_batch: dict | None = None) InferStreamSession[source]#
Set up the model and return a session for streaming inference.
There are two ways to drive the session:
Data-source driven (
sample_batch=None) — configure a streaming dataset under[data_request.infer_stream](e.g.KafkaStreamDataset). The model is pre-flighted from the stream itself and a DataLoader is built, so the returned session can be iterated directly:with hy.infer_stream() as session: for batch, results in session: ...
Manual — pass a representative
sample_batchand feed batches yourself:with hy.infer_stream(sample_batch=batch) as session: results = session.process(batch)
- Parameters:
sample_batch (dict | None) – A representative batch dict with
"object_id"and model-specific data fields, used to pre-flight the model architecture. WhenNone, the model is pre-flighted from a[data_request.infer_stream]streaming dataset instead.- Returns:
A context manager / session object. Iterate it (data-source driven) or call
session.process(batch)(manual); callsession.close()when done.- Return type:
- Raises:
ValueError – If
sample_batchis None and no[data_request.infer_stream]is configured.
- class InferStreamSession(process_func, save_batch_callback, config, results_dir, close_logger_fn, load_dataset_fn, data_loader=None, provider=None)[source]#
Context manager for streaming inference.
Holds a loaded model and Lance writer. When constructed with a
data_loader(the data-source-driven path), the session is iterable and yields(batch, results)pairs as data arrives; otherwise feed batches yourself withprocess().Warning
process()is not thread-safe. Do not call it concurrently.- __iter__()[source]#
Iterate the configured data source, processing each batch as it arrives.
- Yields:
tuple[dict, np.ndarray] – The collated input
batchand the modelresultsfor it.- Raises:
RuntimeError – If the session was created without a data source (no
[data_request.infer_stream]configuration).
- process(batch: dict) numpy.typing.NDArray[source]#
Run inference on a single batch and save results.
- Parameters:
batch (dict) – Must contain
"object_id"(list of str) and model-specific data fields.- Returns:
Model output on CPU, detached from the computation graph.
- Return type:
np.ndarray
- Raises:
RuntimeError – If the session has already been closed.