Wallaroo SDK Upload and Deploy Tutorial: XGBoost Booster Multiclass Classification Softmax Tutorial

How to upload a XGBoost Booster Multiclass Classification Softmax model to Wallaroo

This tutorial and the assets can be downloaded as part of the Wallaroo Tutorials repository.

XGBoost Booster Multiclass Classification Softmax Example

The following tutorial demonstrates deploying and serving an XGBoost Booster Multiclass Classification Softmax model to Wallaroo.

The following XGBoost model types are supported by Wallaroo. XGBoost models not supported by Wallaroo are supported via the Arbitrary Python models, also known as Bring Your Own Predict (BYOP).

XGBoost Model TypeWallaroo Auto Packaging Supported
XGBClassifier
XGBRegressor
Booster Classifier
Booster Classifier
Booster Regressor
Booster Random Forest Regressor
Booster Random Forest Classifier
XGBRFClassifier
XGBRFRegressor
XGBRanker*X
  • XGBRanker XGBoost models are currently supported via converting them to BYOP models.

Goal

Upload, deploy, and serve a sample XGBoost Booster Multiclass Classification Softmax model.

Resources

This tutorial provides the following:

  • Models:
    • ./models/booster_multi_classification_softmax.pkl: The sample XGBoost model that receives the sklearn.datasets.load_iris dataset.

Prerequisites

  • A deployed Wallaroo instance with Edge Registry Services and Edge Observability enabled.
  • The following Python libraries installed:
    • wallaroo: The Wallaroo SDK. Included with the Wallaroo JupyterHub service by default.
    • pandas: Pandas, mainly used for Pandas DataFrame
  • A X64 Docker deployment to deploy the model on an edge location.
  • The notebook “Wallaroo Run Anywhere Model Drift Observability with Assays: Preparation” has been run, and the model edge deployments executed.

Steps

Import Libraries

The first step is to import the libraries we will need. See ./requirements.txt for a list of additional libraries used with this tutorial.

import wallaroo
from wallaroo.deployment_config import DeploymentConfigBuilder
from wallaroo.pipeline import Pipeline
import pyarrow as pa
from wallaroo.framework import Framework

import pickle
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from xgboost import train, DMatrix

Open a Connection to Wallaroo

The next step is connect to Wallaroo through the Wallaroo client. The Python library is included in the Wallaroo install and available through the Jupyter Hub interface provided with your Wallaroo environment.

This is accomplished using the wallaroo.Client() command, which provides a URL to grant the SDK permission to your specific Wallaroo environment. When displayed, enter the URL into a browser and confirm permissions. Store the connection into a variable that can be referenced later.

If logging into the Wallaroo instance through the internal JupyterHub service, use wl = wallaroo.Client(). For more details on logging in through Wallaroo, see the Wallaroo SDK Essentials Guide: Client Connection.

wl = wallaroo.Client()

Set Variables

We’ll set the name of our workspace, pipeline, models and files. Workspace names must be unique across the Wallaroo workspace. For this, we’ll add in a randomly generated 4 characters to the workspace name to prevent collisions with other users’ workspaces. If running this tutorial, we recommend hard coding the workspace name so it will function in the same workspace each time it’s run.

workspace_name = f'xgboost-booster-multiclass-classification-softmax'
pipeline_name = f'xgboost-booster-multiclass-classification-softmax'

model_name = 'booster-multiclass-classification-softmax'
model_file_name = './models/booster_multi_classification_softmax.pkl'

Create Workspace and Pipeline

We will now create the Wallaroo workspace to store our model and set it as the current workspace. Future commands will default to this workspace for pipeline creation, model uploads, etc. We’ll create our Wallaroo pipeline to deploy our model.

workspace = wl.get_workspace(name=workspace_name, create_if_not_exist=True)
wl.set_current_workspace(workspace)

pipeline = wl.build_pipeline(pipeline_name)

Upload XGBoost Model

XGBoost models are uploaded to Wallaroo through the wallaroo.client.Client.upload_model method.

Upload XGBoost Model Parameters

The following parameters are available for XGBoost models.

ParameterTypeDescription
namestring (Required)The name of the model. Model names are unique per workspace. Models that are uploaded with the same name are assigned as a new version of the model.
pathstring (Required)The path to the model file being uploaded.
frameworkstring (Required)Set as Framework.XGBOOST.
input_schemapyarrow.lib.Schema (Required)The input schema in Apache Arrow schema format.
output_schemapyarrow.lib.Schema (Required)The output schema in Apache Arrow schema format.
convert_waitbool (Optional) (Default: True)
  • True: Waits in the script for the model conversion completion.
  • False: Proceeds with the script without waiting for the model conversion process to display complete.

Once the upload process starts, the model is containerized by the Wallaroo instance. This process may take up to 10 minutes.

Upload XGBoost Model Return

The following is returned with a successful model upload and conversion.

FieldTypeDescription
namestringThe name of the model.
versionstringThe model version as a unique UUID.
file_namestringThe file name of the model as stored in Wallaroo.
image_pathstringThe image used to deploy the model in the Wallaroo engine.
last_update_timeDateTimeWhen the model was last updated.

Configure Input and Output Schemas

First we configure the input and output schemas in PyArrow format.

input_schema = pa.schema([
    pa.field('inputs', pa.list_(pa.float32(), list_size=4))
])

output_schema = pa.schema([
    pa.field('predictions', pa.float32()),
])

Upload Model Example

With the input and output schemas defined, we now upload the XGBoost model.

model = wl.upload_model(model_name, 
                        model_file_name, 
                        framework=Framework.XGBOOST, 
                        input_schema=input_schema, 
                        output_schema=output_schema)
model
Waiting for model loading - this will take up to 10.0min.
Model is pending loading to a native runtime.
Model is attempting loading to a native runtime.successful

Ready
Namebooster-multiclass-classification-softmax
Versiond7d80765-0bf9-42ee-b9ad-47102ecc44db
File Namebooster_multi_classification_softmax.pkl
SHA6ace49bb2a3514724937de8ec023a14c94ac71dde7dce220ad27e44d296b4b25
Statusready
Image PathNone
Architecturex86
Accelerationnone
Updated At2024-19-Jul 16:35:38
Workspace id42
Workspace namexgboost-booster-multiclass-classification-softmax

Deploy Pipeline

With the model uploaded and packaged, we add the model as a pipeline model step. For our deployment, we will set a minimum deployment configuration - this is the amount of resources the deployed pipeline uses from the cluster.

Once set, we deploy the pipeline, which allocates the assigned resources for the cluster and makes it available for inference requests.

pipeline.add_model_step(model)

deployment_config = DeploymentConfigBuilder() \
    .cpus(0.25).memory('1Gi') \
    .build()
pipeline.deploy(deployment_config=deployment_config)
namexgboost-booster-multiclass-classification-softmax
created2024-07-19 16:35:28.828304+00:00
last_updated2024-07-19 16:35:41.666829+00:00
deployedTrue
workspace_id42
workspace_namexgboost-booster-multiclass-classification-softmax
archx86
accelnone
tags
versions5303f2dd-cffb-44d2-bbea-ae94465dd9ce, aa56d2f6-72d9-4194-9c7f-58c444f43d03
stepsbooster-multiclass-classification-softmax
publishedFalse

Run Sample Inference

The dataset is from the sklearn.datasets.load_iris examples. These are converted to a pandas DataFrame, that is submitted to the deployed model in Wallaroo for an inference request.

dataset = load_iris()
X, y = dataset.data, dataset.target
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)
dtrain = DMatrix(X_train, label=y_train)
dtest = DMatrix(X_test, label=y_test)

data = dtest.get_data().todense()[:100]
import pandas as pd

dataframe = pd.DataFrame({"inputs": data.tolist()})

results = pipeline.infer(dataframe)
results
timein.inputsout.predictionsanomaly.count
02024-07-19 16:35:56.479[6.0999999046, 2.7999999523, 4.6999998093, 1.2...1.00
12024-07-19 16:35:56.479[5.6999998093, 3.7999999523, 1.7000000477, 0.3...0.00
22024-07-19 16:35:56.479[7.6999998093, 2.5999999046, 6.9000000954, 2.2...2.00
32024-07-19 16:35:56.479[6.0, 2.9000000954, 4.5, 1.5]1.00
42024-07-19 16:35:56.479[6.8000001907, 2.7999999523, 4.8000001907, 1.3...1.00
52024-07-19 16:35:56.479[5.4000000954, 3.4000000954, 1.5, 0.400000006]0.00
62024-07-19 16:35:56.479[5.5999999046, 2.9000000954, 3.5999999046, 1.2...1.00
72024-07-19 16:35:56.479[6.9000000954, 3.0999999046, 5.0999999046, 2.2...2.00
82024-07-19 16:35:56.479[6.1999998093, 2.2000000477, 4.5, 1.5]1.00
92024-07-19 16:35:56.479[5.8000001907, 2.7000000477, 3.9000000954, 1.2...1.00
102024-07-19 16:35:56.479[6.5, 3.2000000477, 5.0999999046, 2.0]2.00
112024-07-19 16:35:56.479[4.8000001907, 3.0, 1.3999999762, 0.1000000015]0.00
122024-07-19 16:35:56.479[5.5, 3.5, 1.2999999523, 0.200000003]0.00
132024-07-19 16:35:56.479[4.9000000954, 3.0999999046, 1.5, 0.1000000015]0.00
142024-07-19 16:35:56.479[5.0999999046, 3.7999999523, 1.5, 0.3000000119]0.00
152024-07-19 16:35:56.479[6.3000001907, 3.2999999523, 4.6999998093, 1.6...1.00
162024-07-19 16:35:56.479[6.5, 3.0, 5.8000001907, 2.2000000477]2.00
172024-07-19 16:35:56.479[5.5999999046, 2.5, 3.9000000954, 1.1000000238]1.00
182024-07-19 16:35:56.479[5.6999998093, 2.7999999523, 4.5, 1.2999999523]1.00
192024-07-19 16:35:56.479[6.4000000954, 2.7999999523, 5.5999999046, 2.2...2.00
202024-07-19 16:35:56.479[4.6999998093, 3.2000000477, 1.6000000238, 0.2...0.00
212024-07-19 16:35:56.479[6.0999999046, 3.0, 4.9000000954, 1.7999999523]2.00
222024-07-19 16:35:56.479[5.0, 3.4000000954, 1.6000000238, 0.400000006]0.00
232024-07-19 16:35:56.479[6.4000000954, 2.7999999523, 5.5999999046, 2.0...2.00
242024-07-19 16:35:56.479[7.9000000954, 3.7999999523, 6.4000000954, 2.0]2.00
252024-07-19 16:35:56.479[6.6999998093, 3.0, 5.1999998093, 2.2999999523]2.00
262024-07-19 16:35:56.479[6.6999998093, 2.5, 5.8000001907, 1.7999999523]2.00
272024-07-19 16:35:56.479[6.8000001907, 3.2000000477, 5.9000000954, 2.2...2.00
282024-07-19 16:35:56.479[4.8000001907, 3.0, 1.3999999762, 0.3000000119]0.00
292024-07-19 16:35:56.479[4.8000001907, 3.0999999046, 1.6000000238, 0.2...0.00

Undeploy the Pipeline

With the tutorial complete, we undeploy the pipeline and return the resources back to the cluster.

pipeline.undeploy()
namexgboost-booster-multiclass-classification-softmax
created2024-07-19 16:35:28.828304+00:00
last_updated2024-07-19 16:35:41.666829+00:00
deployedFalse
workspace_id42
workspace_namexgboost-booster-multiclass-classification-softmax
archx86
accelnone
tags
versions5303f2dd-cffb-44d2-bbea-ae94465dd9ce, aa56d2f6-72d9-4194-9c7f-58c444f43d03
stepsbooster-multiclass-classification-softmax
publishedFalse