This notebook illustrates a simple client-server interaction to perform inference on a TensorFlow model using the Python SDK for Konduit Serving.
import numpy as np
import os
This page documents two ways to create Konduit Serving configurations with the Python SDK:
Using Python to create a configuration, and
Writing the configuration as a YAML file, then serving it using the Python SDK.
These approaches are documented in separate tabs throughout this page. For example, the following code block shows the imports for each approach in separate tabs:
from konduit import ParallelInferenceConfig, ServingConfig, TensorFlowConfig, \
ModelConfigType, TensorDataTypesConfig, ModelStep, InferenceConfiguration
from konduit.server import Server
from konduit.client import Client
from konduit.load import server_from_file, client_from_file
Overview
Konduit Serving works by defining a series of steps. These include operations such as
Pre- or post-processing steps
One or more machine learning models
Transforming the output in a way that can be understood by humans
If deploying your model does not require pre- nor post-processing, only one step - a machine learning model - is required. This configuration is defined using a single ModelStep.
Before running this notebook, run the build_jar.py script or the konduit init command. Refer to the Building from source page for details.
Start by downloading the model weights to the data folder.
from urllib.request import urlretrieve
from zipfile import ZipFile
dl_path = "../data/bert/bert.zip"
if not os.path.isfile(dl_path):
urlretrieve("https://deeplearning4jblob.blob.core.windows.net/testresources/bert_mrpc_frozen_v1.zip",
dl_path)
with ZipFile(dl_path, 'r') as zipObj:
zipObj.extractall()
Configure the step
Define the TensorFlow configuration as a TensorFlowConfig object.
tensor_data_types_config: The TensorFlowConfig object requires a dictionary input_data_types. Its keys should represent column names, and the values should represent data types as strings, e.g. "INT32". See here for a list of supported data types.
model_config_type: This argument requires a ModelConfigType object. Specify model_type as TENSORFLOW, and model_loading_path to point to the location of TensorFlow weights saved in the PB file format.
input_names, output_names: names of the input and output nodes respectively. Important: specify input_names and output_namesas lists.
input_data_types: maps each of the input_names to the corresponding data type. The values should represent data types as strings, e.g. INT32. See here for a list of supported data types.
parallel_inference_config: specify the number of workers to run in parallel. Here, we specify workers=1.
Names of input and output nodes
In TensorFlow, you can find the names of your input and output nodes by iterating throughmodel.inputsand model.outputsrespectively and printing the .os.nameattribute of each. For more details, please refer to this StackOverflow answer.
Configure the server
Specify the following:
http_port: select a random port.
input_data_format, output_data_format: Specify input and output data formats as strings.
Accepted input and output data formats are as follows:
Input: JSON, ARROW, IMAGE, ND4J (not yet implemented) and NUMPY.
Output: NUMPY, JSON, ND4J (not yet implemented) and ARROW.
By default, Server() looks for the Konduit Serving JAR konduit.jar in the directory the script is run in. To change this default, use the jar_path argument.
Start the server
Start the server:
server.start()
Starting server.................
Server has started successfully.
<subprocess.Popen at 0x21acae42f60>
konduit_yaml_path = "../yaml/tensorflow-bert.yaml"
server = server_from_file(konduit_yaml_path)
server.start()
Configure the client
To configure the client, create a Client object specifying the port number:
client = Client(port=port)
Add the following to your YAML configuration file:
client:
port: 1337
Create a Client object using the client_from_file function:
NDARRAY inputs to ModelSteps must be specified with a preceding batchSize dimension. For batches with a single observation, this can be done by using numpy.expand_dims() to add an additional dimension to your array.
Load some sample data from NumPy files. Note that these are NumPy arrays, each with shape (4, 128):