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() InferStreamSession[source]#
Set up the model and return a session for streaming inference.
There are two ways to drive the session:
Data-source driven — configure a streaming dataset under
[data_request.infer_stream](e.g.KafkaStreamDataset). A DataLoader is built over it, so the returned session can be iterated directly:with hy.infer_stream() as session: for batch, results in session: ...
Manual — leave
[data_request.infer_stream]unset and feed batches yourself:with hy.infer_stream() as session: results = session.process(batch)
- Returns:
A context manager / session object. Iterate it (data-source driven) or call
session.process(batch)(manual); callsession.close()when done.- Return type:
- 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 | dict[str, np.ndarray]] – The collated input
batchand the modelresultsfor it, in the form returned byprocess().- Raises:
RuntimeError – If the session was created without a data source (no
[data_request.infer_stream]configuration).
- _close_provider()[source]#
Tear down the streaming data source, releasing any connection it holds.
Kept separate from
stop()because stopping is only a signal: a data loader left suspended mid-iteration (the caller hit an exception, or broke out of the loop) never resumes to clean up after itself, so the connection has to be closed from the outside or it lingers for the life of the process.
- process(batch: dict) numpy.typing.NDArray | dict[str, 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. When the model’s
infer_batchreturns a dict of tensors, this is a dict with the same keys whose values are numpy arrays; otherwise it is a single numpy array.- Return type:
np.ndarray | dict[str, np.ndarray]
- Raises:
RuntimeError – If the session has already been closed, or if the model returns something other than a tensor or a dict of tensors.