Python pipeline steps
You can integrate Python into a Konduit Serving instance by defining PythonConfig objects as steps to PythonPipelineStep.
Last updated
You can integrate Python into a Konduit Serving instance by defining PythonConfig objects as steps to PythonPipelineStep.
Last updated
Konduit Serving uses JavaCPP Presets to execute Python scripts using the CPython API. This allows you to build custom Konduit Serving pipeline steps by writing Python scripts to be run within a Konduit Serving Java process.
PythonConfig
Define the configuration for a PythonStep
.
A PythonConfig
takes on the following parameters:
python_path
: Optional. The search path for Python modules, as defined by sys.path
. See the Python modules section for details.
python_code_path
: Optional. Specify the location of your Python script.
python_code
: Optional. A string that contains Python commands.
python_inputs
: A dictionary with input names as keys and corresponding value types as values. Value types should be specified as one of the following strings: "INT"
, "STR"
, "FLOAT"
, "BOOL"
, "NDARRAY"
.
python_outputs
: A dictionary with output names as keys and corresponding value types as values. Values types should be specified as per python_inputs
.
extra_inputs
: potential extra input variables. Specify value types as per python_inputs
.
return_all_inputs
: Boolean. Whether or not to return all inputs in addition to outputs.
setup_and_run
: Boolean. Whether or not to use the setup-and-run schematics. Defaults to False.
NumPy array subclasses and some NumPy array data types are not supported.
Unsupported NumPy array data types are as follows:np.uint8
, np.uint16
, np.uint32
, np.uint64
, np.uintp
, np.complex64
, np.complex128
, np.int8
, np.int16
, np.bool
, np.byte
, np.ubyte
, np.ushort
, np.uintc
, np.uint
, np.ulonglong
, np.half
, np.csingle
, np.cdouble
, np.clongdouble
.
For output type NDARRAY, convert your output to a regular NumPy array and supported data type using np.array()/np.ndarray()
and/or the astype()
method. Also, ensure that the output is a NumPy array and not a NumPy scalar: see the documentation for np.isscalar
for details.
PythonStep
For most use cases,PythonStep
can be set up as follows:
Note that by default, the default name for each step is default
. You will need this when specifying your data inputs via the .predict()
method of the Client
class.
Finally, the PythonStep
object can be passed to an InferenceConfiguration
object, which is used to configure the Konduit Serving instance:
Some models may require the server to transform more than one set of inputs. For instance, to serve object detection models, annotations and images may have to be transformed in a single PythonStep
. This requires a unique name to be specified for each PythonConfig
:
Python steps can take any argument that can be passed to PythonConfig
.The following is a basic example of specifying a Python step in a YAML configuration:
type
: specify this as PYTHON
.
python_code
: if you want to specify your Python code directly in your YAML file. The following documentation may be helpful for specifying multi-line Python code, specifically the section on literal block scalars.
python_code_path
: specify the path of a Python .py
script.
python_inputs
: name-value pairs specifying the data types for each of the inputs referenced in the script. Data types should be one of the following: INT
, STR
, FLOAT
, BOOL
, NDARRAY
.
python_outputs
: name-value pairs specifying the data types for each of the outputs referenced in the script. Data types should be one of the following: INT
, STR
, FLOAT
, BOOL
, NDARRAY
.
extra_inputs
: potential extra input variables. Specify value types as per python_inputs
.
return_all_inputs
: Boolean. Whether or not to return all inputs in addition to output.
setup_and_run
: Boolean. Whether or not to use the setup-and-run schematics. Defaults to False
.
python_path
: location of the Python modules. Generally, if your script only requires NumPy, setting a custom python_path
is not necessary. Refer to the Python modules documentation on setting a custom Python path with additional modules.
The names referenced in python_inputs
and python_outputs
correspond with inputColumnNames
and outputColumnNames
. Modifying python_inputs
and python_outputs
does not modify the input and output name of the step. input_names
and output_names
are arguments to PythonStep
which cannot be accessed through the YAML configuration, and default to the name default
.
pythonPath
argument If the pythonPath
is not specified, you will still be able to import modules cached for NumPy by JavaCPP Presets in your Python script(s).
In Java, you can find the location of the default modules by printing
where cachePackages
is imported as a static variable:
If you require additional modules, you can set a custompythonPath
by running the following command in your Python environment and setting the output as your pythonPath
:
Custom pythonPath
follows the format defined by sys.path.
The first element is the location of the script used to invoke the Python interpreter, and the remaining elements specify where Python should search for modules.
To list the modules that you can access, run help("modules")
in your Python interpreter.