morpheus.stages.inference.inference_stage.InferenceStage#
- class InferenceStage(c, thread_count=None)[source]#
Bases:
ControlMessageStageThis class serves as the base for any inference stage. Inference stages operate differently than other stages due to the fact that they operate in a separate thread and have their own batch size which is separate from the pipeline batch size. Processing the inference work in a separate thread is necessary to support inference types that may require exclusive use of a single thread (i.e., TensorRT) without blocking the main thread.
Changing batch sizes for the inference stage requires breaking messages into smaller slices, running inference on the smaller slices, then recombining the inference output into the original batch size. This inference base class handles breaking and recombining batches and queing the inference work to be processed on another thread.
Inference stages that derive from this class need to implement the
_get_inference_workermethod which returns an implementation of theInferenceWorkerclass. YourInferenceWorkerclass must implement theprocessandcalc_output_dimsmethods. Theprocessmethods is where you provide implementation details on how to perform inference with theControlMessagebatch. The worker uses thecalc_output_dimsto calculate the output dimensions of the pipeline batch that inference batch results are appended to.To add a C++ implementation for processing inference requests, you must implement the
_get_cpp_inference_nodemethod and implementsupports_cpp_nodein your worker to return True. Your pipeline can then use your C++ implementation by settinguse_cppto True in your pipeline configuration. See developer documentation for more details.- Parameters:
- c
morpheus.config.Config Pipeline configuration instance.
- thread_countint, optional
Number of threads to use for inference. If not provided, the
num_threadsattribute of theConfigobject will be used.
- c
- Attributes:
has_multi_input_portsIndicates if this stage has multiple input ports.
has_multi_output_portsIndicates if this stage has multiple output ports.
input_portsInput ports to this stage.
is_builtIndicates if this stage has been built.
is_pre_builtIndicates if this stage has been built.
nameThe name of the stage.
output_portsOutput ports from this stage.
unique_nameUnique name of stage.
Methods
Accepted input types to this stage.
build(builder[, do_propagate])Build this stage.
can_build([check_ports])Determines if all inputs have been built allowing this node to be built.
can_pre_build([check_ports])Determines if all inputs have been built allowing this node to be built.
compute_schema(schema)Compute the schema for this stage based on the incoming schema from upstream stages.
Get all input stages to this stage.
Get all input senders to this stage.
Get all output stages from this stage.
Get all output receivers from this stage.
Stages which need to have columns inserted into the dataframe, should populate the
self._needed_columnsdictionary with mapping of column names tomorpheus.common.TypeId.join()On all inference worker threads, this function applies join.
This function is called along with on_start during stage initialization.
stop()Stops the inference workers and closes the inference queue.
Returns a tuple of supported execution modes of this stage.
Specifies whether this Stage is capable of creating C++ nodes.
- _build(builder, input_nodes)[source]#
This function is responsible for constructing this stage’s internal
mrc.SegmentObjectobject. The input of this function contains the returned value from the upstream stage.The input values are the
mrc.Builderfor this stage and a list of parent nodes.- Parameters:
- builder
mrc.Builder mrc.Builderobject for the pipeline. This should be used to construct/attach the internalmrc.SegmentObject.- input_nodes
list[mrc.SegmentObject] List containing the input
mrc.SegmentObjectobjects.
- builder
- Returns:
list[mrc.SegmentObject]List of tuples containing the output
mrc.SegmentObjectobject from this stage.
- _get_inference_worker(inf_queue)[source]#
Returns the main inference worker which manages requests possibly in another thread depending on which mode the pipeline is currently operating in.
- Parameters:
- inf_queue
morpheus.utils.producer_consumer_queue.ProducerConsumerQueue Inference request queue.
- inf_queue
- Returns:
InferenceWorkerInference worker implementation for stage.
- build(builder, do_propagate=True)[source]#
Build this stage.
- Parameters:
- builder
mrc.Builder MRC segment for this stage.
- do_propagatebool, optional
Whether to propagate to build output stages, by default True.
- builder
- can_build(check_ports=False)[source]#
Determines if all inputs have been built allowing this node to be built.
- Parameters:
- check_portsbool, optional
Check if we can build based on the input ports, by default False.
- Returns:
- bool
True if we can build, False otherwise.
- can_pre_build(check_ports=False)[source]#
Determines if all inputs have been built allowing this node to be built.
- Parameters:
- check_portsbool, optional
Check if we can build based on the input ports, by default False.
- Returns:
- bool
True if we can build, False otherwise.
- compute_schema(schema)[source]#
Compute the schema for this stage based on the incoming schema from upstream stages.
Incoming schema and type information from upstream stages is available via the
schema.input_schemasandschema.input_typesproperties.Derived classes need to override this method, can set the output type(s) on
schemaby callingset_typefor all output ports. For example a simple pass-thru stage might perform the following:>>> for (port_idx, port_schema) in enumerate(schema.input_schemas): ... schema.output_schemas[port_idx].set_type(port_schema.get_type()) >>>
If the port types in
upstream_schemaare incompatible the stage should raise aRuntimeError.
- get_all_input_stages()[source]#
Get all input stages to this stage.
- Returns:
- list[
morpheus.pipeline.pipeline.StageBase] All input stages.
- list[
- get_all_inputs()[source]#
Get all input senders to this stage.
- Returns:
- list[
morpheus.pipeline.pipeline.Sender] All input senders.
- list[
- get_all_output_stages()[source]#
Get all output stages from this stage.
- Returns:
- list[
morpheus.pipeline.pipeline.StageBase] All output stages.
- list[
- get_all_outputs()[source]#
Get all output receivers from this stage.
- Returns:
- list[
morpheus.pipeline.pipeline.Receiver] All output receivers.
- list[
- get_needed_columns()[source]#
Stages which need to have columns inserted into the dataframe, should populate the
self._needed_columnsdictionary with mapping of column names tomorpheus.common.TypeId. This will ensure that the columns are allocated and populated with null values.
- property has_multi_input_ports: bool#
Indicates if this stage has multiple input ports.
- Returns:
- bool
True if stage has multiple input ports, False otherwise.
- property has_multi_output_ports: bool#
Indicates if this stage has multiple output ports.
- Returns:
- bool
True if stage has multiple output ports, False otherwise.
- property input_ports: list[Receiver]#
Input ports to this stage.
- Returns:
- list[
morpheus.pipeline.pipeline.Receiver] Input ports to this stage.
- list[
- property is_built: bool#
Indicates if this stage has been built.
- Returns:
- bool
True if stage is built, False otherwise.
- property is_pre_built: bool#
Indicates if this stage has been built.
- Returns:
- bool
True if stage is built, False otherwise.
- property name: str#
The name of the stage. Used in logging. Each derived class should override this property with a unique name.
- Returns:
- str
Name of a stage.
- property output_ports: list[Sender]#
Output ports from this stage.
- Returns:
- list[
morpheus.pipeline.pipeline.Sender] Output ports from this stage.
- list[
- async start_async()[source]#
This function is called along with on_start during stage initialization. Allows stages to utilize the asyncio loop if needed.
- supported_execution_modes()[source]#
Returns a tuple of supported execution modes of this stage. By default this returns
(ExecutionMode.GPU,). Subclasses can override this method to specify different execution modes.For most stages the values will be static, and this can be accomplished by making use of either the
CpuOnlyMixinorGpuAndCpuMixinmixins.However, complex stages may choose to make this decision at runtime, in which case this method should be overridden. directly within the stage class.
- supports_cpp_node()[source]#
Specifies whether this Stage is capable of creating C++ nodes. During the build phase, this value will be combined with
CppConfig.get_should_use_cpp()to determine whether or not a C++ node is created. This is an instance method to allow runtime decisions and derived classes to override base implementations.