Packaging models as an inference pipeline

AI SDK Operation Manual

Portfolio
Industrial AI
Product
AI Software Development Kit
Software version
v2.4
Edition
04/2025
Language
English

The AI SDK provides the functionality to create a pipeline configuration package that encapsulates trained models. These models can be converted to an edge configuration package using the AI SDK. Then it can be uploaded and run on the AI Inference Server on an Industrial Edge Device. The related functions can be found within the simaticai.deployment module.

Component types

AI SDK defines component as a pipeline step that consumes input data, processes said data by using a model, and produces the output data. The model can mean, in a narrower sense, an ML model such as a neural network or the random forest algorithm, or simply an aggregator or other pre- or postprocessing logic.

In every case, a Python script, hereinafter referred to as the 'entrypoint', establishes the connection between the model and the AI Inference Server. The server needs to receive information about what environment is required to execute the code, including the required Python packages or file resources.

AI SDK supports components for both CPU and GPU based execution. These components can be found in the simaticai.deployment module and are as follows

  • deployment.PythonComponent for CPU based execution

  • deployment.GPURuntimeComponent for GPU based execution

Given these components have dedicated runtime environments, they have different information needs for their execution.

For the PythonComponent these are

  • The entrypoint that receives the input data

  • The model that needs to be executed

  • The Python version required to run the Python script

  • The input and output variables of the component

For GPURuntimeComponent these are

  • The ONNX model that needs to be executed on a GPU enabled hardware

  • The ONNX model configuration that instruments the GPU based execution

You might notice that in case of GPU based execution there is no entrypoint defined. This is because GPU based execution requires a dedicated GPU Runtime step, which handles the execution of the model. In this case pre- and post-processing PythonComponents are required to take care of pipeline inputs and outputs.

At the time of writing, only ONNX models are supported, but with the appropriate configuration, it is possible to use the TensorRT backend and optimize the model during the loading process.

For the GPU Runtime input and output mapping is optional as the component expects input and output variables with names defined in the model. All you have to take care is that the pre- and post-processing components has outputs (in pre-processing) and inputs (in postprocessing) that matches with input/output names defined in the model.

The optional input/outputs serves to handle pass-through variables that are not processed by the model on GPU, but should travel with other, processed data. One good example for such data is image metadata.

The GPU runtime needs a model configuration, a .pbtxt file. AI SDK automatically generates a default configuration, for the selected model. The mandatory max_batch_size parameter is added to the autogenerated configuration, which limits the size of input batches. It's value must be a positive integer, and for models without batching capabilities it must be ˙0`.

Single or multiple components

From a deployment perspective, the inference pipeline can consist of one or more components. This is independent of the logical structure of the inference pipeline. For example, you can package a typical time series pipeline that consists of multiple scikit-learn pipeline elements into a single pipeline component for deployment:


single component

Alternatively, you can deploy the same pipeline as two components:


multi-component

To keep the deployment simple and less error-prone, you should deploy your inference pipeline with as few components as possible.

Pipelines running on GPU enabled AI Inference Server always require pre- and post-processing steps

In many cases, a single component is sufficient. However, there may be reasons why you might consider using separate components, such as:

  • You need a different Python environment for different parts of your processing - for instance, you have components that require conflicting package versions.

  • You want to exploit parallelism between components without implementing multithreading.

  • You want to modularize your pipeline and build it from a pool of component variants that you can flexibly combine.

Creating an inference pipeline package

The AI SDK allows you to create pipeline components implemented in Python and assemble linear pipelines from one or more such components.

The API is designed to anticipate future possible types of components that could be based on a technology other than Python, such as ONNX or native TensorFlow serving. However, only Python is currently supported. The workflow for creating an inference pipeline package is as follows:

  1. Write the Python code that wraps your trained model as an inference pipeline component.

  2. Define the pipeline component.

  3. Repeat the above steps if you have multiple components.

  4. Configure the pipeline.

  5. Save the pipeline configuration in an edge configuration package.

Creating pipeline components implemented in Python

Implementing an inference pipeline component in Python is a comprehensive topic in itself and will be described in detail in the next chapter, "Guideline for writing pipeline components (Page 26)". A component consists of files and metadata.

Files contain:

  • Python scripts

  • trained models

Metadata includes:

  • component name and component version

  • required Python version and Python packages

  • input and output variables

  • the number of parallel executors

  • the entry point

You can create your own arrangement of project files. We recommend that you follow the end-to-end tutorials for the AI SDK. Here the source code and stored trained models are organized in a predefined structure. If you keep the same relative structure on the AI Inference Server, you can use the same relative references from the source code to the stored models or other files.

Usually, there should be at least one Python script for the entry point, the inference wrapper, and the saved model. You can create the pipeline component by running a Python script or notebook that provides the following functionality:

  • creates a Python component object with a specific name, component version, and required Python version

  • defines required Python packages

  • defines input and output variables

  • defines custom metrics

  • defines the number of parallel executors

  • adds Python scripts and saved models

  • defines the entry point under the Python scripts

You can find detailed examples in the Examples section of this document

All this takes place with the corresponding functionality of the simaticai.deployment module. For concrete examples, refer to the packaging notebooks in the end-to-end tutorials. Please refer to the AI SDK API reference manual for more information and advanced options.

Consider the following limitations:

  • The AI SDK allows you to select a Python version that is required for executing your model on the AI Inference Server.

  • Make sure you select a Python version that is supported by the version installed on your Industrial Edge target device. For the current AI SDK version, this is Python version 3.10 and 3.1

  • The required Python packages must either be added as wheel files to the pipeline component or be available for download via pip for the target Inference Server.

  • The entrypoint script must be in the root folder of the package in the current AI SDK version.

  • AI Inference Server supports a maximum of 8 parallel executors.

Creating pipeline components for GPU based execution

The AI Inference Server can run pipeline steps requiring GPU access by providing ML models in ONNX (Open Neural Network Exchange) format.

ONNX is a good choice for performance and portability at the same time (see https://onnx.ai/). Converters are readily available (see https://onnx.ai/onnx/intro/converters.html).

Please note that an ONNX model can contain custom layers, although the AI Inference Server can run that model it may have performance implications.

As an example, we take a common scenario with 3 components for an ONNX pipeline configuration package following the end-to-end object_detection AI SDK Tutorial.

  • Pre-processing Component

  • Model (object detection) Component

  • Post-processing Component

The responsibility of the Pre-processing Component is to receive and transform the input as required by the GPURuntimeComponent. The required steps for that are the usual ones as follows:

  • Define the component with deployment.PythonComponent

  • Add resources with add_resources

  • Set the entry point code file with set_entrypoint

  • Set the requirements by installing dependencies with requirements.txt

  • Add input(s) with add_input

  • Add output(s) with add_output

from simaticai import deployment

preprocessing = deployment.PythonComponent(name='preprocessing', python_version='3.10')
preprocessing.add_resources('../src/preprocessing/', 'preprocessing.py')
preprocessing.set_entrypoint('preprocessing.py')
preprocessing.set_requirements('../src/preprocessing/requirements.txt')
preprocessing.add_input("vision_payload", "ImageSet")
preprocessing.add_output("input", "Float32Array")
preprocessing.add_output("iuid", "String")

preprocessing

The responsibility of the Model Component (Detection GPU Runtime Component in our example) is to produce a prediction from the given input and pass boxes (coordinates of object bounding rectangles), labels (class/name of detected objects), and scores (confidence level of the predictions).

First, we add the model to the component (*use_model*), and the listed information will be added automatically.

  • Model’s config.pbtxt (for each model, users can define a model configuration. This configuration, at minimum, needs to define: the backend, name, shape, and datatype of model inputs and outputs.)

  • Component inputs required by the model

  • Component outputs required by the model

from simaticai.deployment import model_config

detection = deployment.GPURuntimeComponent(name='detection')
detection.use_model("../src/detection/1/model.onnx",
                    max_batch_size=1,
                    optimization=model_config.TensorRTOptimization(),  # Optional
                    warmup=model_config.Warmup.RANDOM_DATA)

 adding extra inputs and outputs to connect the payload with the result
detection.add_input("iuid", "String")
detection.add_output("iuid", "String")

detection

With the code above, the AI SDK generates a default model_config for your model, containing the required information as follows:

  • platform information

  • input tensor(s) name, type and shape

  • output tensor(s) name, type and shape

  • TensorRT acceleration

  • model_warmup

To change this configuration, you can save the component and fine-tune the generated config.pbtxt file (*use_config*). For further details on Model Configuration and fine-tuning refer to https://github.com/triton-inference-server/tutorials/blob/main/Conceptual_Guide/Part_1-model_deployment/README.md.At the time of writing, AI Inference Server supports TensorRT acceleration with ONNX model.

# saving component 
detection.save("../packages/components/")

 [...] editing ../packages/components/detection/config.pbtxt file, e.g adding TensorRT optimization
detection.use_config("../packages/components/detection/config.pbtxt") # using the fine-tuned config file

The responsibility of the Post-processing Component is to receive the predictions from the the Detection GPU Runtime Component and make a decision based on the results.

postprocessing = deployment.PythonComponent(name='postprocessing', python_version='3.10')

postprocessing.add_resources('../src/postprocessing/', 'postprocessing.py')
postprocessing.set_entrypoint('postprocessing.py')
postprocessing.set_requirements('../src/postprocessing/requirements.txt')

postprocessing.add_input("iuid", "String")
postprocessing.add_input("boxes", "Float32Array")
postprocessing.add_input("labels", "Int64Array")
postprocessing.add_input("scores", "Float32Array")

postprocessing.add_output("prediction", "String")
postprocessing.add_output("result", "String")

postprocessing

All these steps lead us to the final one: Pipeline Creation.

The code using from_components is self-explanatory:

pipeline = deployment.Pipeline.from_components( [preprocessing, detection, postprocessing],
                                                name='BoardObjectDetection',
                                                desc="3 Steps Pipeline for Object model to spot extra or missing holes, not expected damages on the a workpiece")

pipeline

Once the pipeline is created, it can be saved and then tested. The deployable Edge Runtime Package can be deployed to the AI Model Manager or imported into the AI Inference Server.

edge_package_path = pipeline.export('../packages')
edge_package_path

The best way to validate your configuration is to test it using AI SDK’s tools.

There is an option to test the pipeline by components. Using the *run_component* method of the AI SDK LocalPipelineRunner instead of run_pipeline gives you step-by-step validation/testing capabilities.

Please refer to the 40-TestPipelineLocally.ipynb tutorial notebook under the Object Detection E2E tutorial.

NOTICE

The `simaticai.deployment.Pipeline.export()` method is available from AI SDK v2.1.0.

Configuring and saving the pipeline

After you create the component(s), you must combine them into a pipeline using a Pipeline object.

If the pipeline only consists of one single component, it has the same input variables and output variables as its single component. You only need to specify a pipeline name and version from which the file name is derived when you save the package. For example, refer to the end of the package creation notebooks in the end-to-end tutorials.

To create a linear pipeline of multiple components, you can still mostly rely on the constructor of Pipeline, which attempts to automatically connect the components passed as a list:

  • connecting pipeline input to the first component

  • connecting inputs and outputs of subsequent components if the variable name matches

  • connecting the last component to the pipeline output

In general, you should pass data from one component to another component in a single variable of type String and serialize and deserialize any data you have through a string.

The low-level methods of the Pipeline class allow you to connect any components, pipeline inputs and outputs. However, the AI SDK cannot guarantee that the result will behave as intended on the AI Inference Server.

For information about pipeline input and output with different data types and defining custom metrics, refer to the Guideline for writing runtime components (Page 26). It describes how input and output data is passed between the AI Inference Server and your entry point. It also explains special considerations that apply to a continuous stream of time series data or for bulk data.

Whether you created the pipeline with a single constructor call or with low-level methods, you must save it for the creation of the pipeline. This step creates the pipeline configuration package as a .zip file and leaves the contents of the .zip file in the file system. You can explore it to troubleshoot or see how your package creation calls are reflected in the contents of files and directories.

Pipeline packages are identified by their package ID and version attributes, and are grouped by package ID in the AI Inference Server and other Edge applications.

When saving a pipeline – with the export() method – you can specify a package ID in a UUID 4-compliant format or an automatically generated one is assigned.

If no package ID is defined in the expor() method, and AI SDK finds an already assigned package ID in a previously generated and similarly named package, the package ID found in the latest package is used.

AI SDK automatically assigns and increments the version number of a pipeline each time a package is saved unless a new package ID is assigned in the export() method, or an explicit version number without a package ID is defined in the export() method, or the pipeline constructor.

Restrictions

  • You cannot overwrite a previously saved package with the same package ID if the package ID is explicitly assigned in the export() method

  • Existing packages without a package ID will be overwritten

  • If a new package ID is assigned to an existing version of the package, the old one will be overwritten

  • If no predecessor of a package is found, AI SDK assigns version 1 to the created package

  • The version defined in the export() method takes precedence over the version assigned at the constructor level