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

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

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

XGBoost Booster Multiclass Classification Softprob Example

The following tutorial demonstrates deploying and serving an XGBoost Booster Multiclass Classification Softprob 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 Softprob model.

Resources

This tutorial provides the following:

  • Models:
    • ./models/xgb_booster_multi_classification_softprob.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
import pyarrow as pa
from wallaroo.framework import Framework

import pickle
from sklearn.datasets import load_breast_cancer
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(). If logging in externally, update the wallarooPrefix and wallarooSuffix variables with the proper DNS information. For more information on Wallaroo DNS settings, see the Wallaroo DNS Integration Guide.

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'xgb-booster-multi-classification-softprob'
pipeline_name = f'xgb-booster-multi-classification-softprob'

model_name = 'xgb-booster-multi-classification-softprob'
model_file_name = './models/xgb_booster_multi_classification_softprob.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=30))
])

output_schema = pa.schema([
    pa.field('probabilities', 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
Namexgb-booster-multi-classification-softprob
Version29958e48-1237-4eed-81bb-fbff3027c44b
File Namexgb_booster_multi_classification_softprob.pkl
SHAeefed02b648166eda12c0d617c160d9aa8c560fdf54a5c42fe9934d473151034
Statusready
Image PathNone
Architecturex86
Accelerationnone
Updated At2024-11-Apr 20:31:43

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)
pipeline.status()
{'status': 'Running',
 'details': [],
 'engines': [{'ip': '10.28.0.161',
   'name': 'engine-5f9b57db6c-lm5pv',
   'status': 'Running',
   'reason': None,
   'details': [],
   'pipeline_statuses': {'pipelines': [{'id': 'xgb-booster-multi-classification-softprob',
      'status': 'Running'}]},
   'model_statuses': {'models': [{'name': 'xgb-booster-multi-classification-softprob',
      'sha': 'eefed02b648166eda12c0d617c160d9aa8c560fdf54a5c42fe9934d473151034',
      'status': 'Running',
      'version': '29958e48-1237-4eed-81bb-fbff3027c44b'}]}}],
 'engine_lbs': [{'ip': '10.28.1.117',
   'name': 'engine-lb-d7cc8fc9c-tvckk',
   'status': 'Running',
   'reason': None,
   'details': []}],
 'sidekicks': []}

Run Sample Inference

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

dataset = load_breast_cancer()
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()})
dataframe
inputs
0[12.470000267028809, 18.600000381469727, 81.08...
1[18.940000534057617, 21.309999465942383, 123.5...
2[15.460000038146973, 19.479999542236328, 101.6...
3[12.399999618530273, 17.68000030517578, 81.470...
4[11.539999961853027, 14.4399995803833, 74.6500...
......
95[9.777000427246094, 16.989999771118164, 62.5, ...
96[20.1299991607666, 28.25, 131.1999969482422, 1...
97[13.8100004196167, 23.75, 91.55999755859375, 5...
98[11.149999618530273, 13.079999923706055, 70.87...
99[17.270000457763672, 25.420000076293945, 112.4...

100 rows × 1 columns

results = pipeline.infer(dataframe)
results
timein.inputsout.probabilitiesanomaly.count
02024-04-11 20:32:01.183[12.470000267, 18.6000003815, 81.0899963379, 4...[0.087060094, 0.0922827, 0.82065725]0
12024-04-11 20:32:01.183[18.9400005341, 21.3099994659, 123.5999984741,...[0.087060094, 0.0922827, 0.82065725]0
22024-04-11 20:32:01.183[15.4600000381, 19.4799995422, 101.6999969482,...[0.087060094, 0.0922827, 0.82065725]0
32024-04-11 20:32:01.183[12.3999996185, 17.6800003052, 81.4700012207, ...[0.087060094, 0.0922827, 0.82065725]0
42024-04-11 20:32:01.183[11.5399999619, 14.4399995804, 74.6500015259, ...[0.087060094, 0.0922827, 0.82065725]0
...............
952024-04-11 20:32:01.183[9.7770004272, 16.9899997711, 62.5, 290.200012...[0.087060094, 0.0922827, 0.82065725]0
962024-04-11 20:32:01.183[20.1299991608, 28.25, 131.1999969482, 1261.0,...[0.087060094, 0.0922827, 0.82065725]0
972024-04-11 20:32:01.183[13.8100004196, 23.75, 91.5599975586, 597.7999...[0.087060094, 0.0922827, 0.82065725]0
982024-04-11 20:32:01.183[11.1499996185, 13.0799999237, 70.8700027466, ...[0.087060094, 0.0922827, 0.82065725]0
992024-04-11 20:32:01.183[17.2700004578, 25.4200000763, 112.4000015259,...[0.087060094, 0.0922827, 0.82065725]0

100 rows × 4 columns

Undeploy the Pipeline

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

pipeline.undeploy()
namexgb-booster-multi-classification-softprob
created2024-04-11 20:31:33.925389+00:00
last_updated2024-04-11 20:31:46.278875+00:00
deployedFalse
archx86
accelnone
tags
versions97b6cc60-6668-44d9-90bf-ef2a3771b8ba, aaf34548-9a3f-4301-998d-4c616e992043
stepsxgb-booster-multi-classification-softprob
publishedFalse