Wallaroo SDK Upload and Deploy Tutorial: XGBoost Booster Binary Classification Example

How to upload a XGBoost Booster Binary Classification Model to Wallaroo

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

XGBoost Booster Binary Classification Example

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

Resources

This tutorial provides the following:

  • Models:
    • /models/xgb_booster_binary_classification.pkl: The sample XGBoost model that receives the sklearn.datasets.load_breast_cancer 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'xgboost-booster-binary-classification'
pipeline_name = f'xgboost-booster-binary-classification'

model_name = 'xgboost-booster-binary-classification'
model_file_name = './models/xgb_booster_binary_classification.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
Namexgboost-booster-binary-classification
Version4da57231-c8ef-4382-91ff-6ef32c27595d
File Namexgb_booster_binary_classification.pkl
SHAbe6d8c51395196da4720951b45660840fecdd680e7d99003a966023873322d8b
Statusready
Image PathNone
Architecturex86
Accelerationnone
Updated At2024-11-Apr 20:30:25

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-binary-classification
created2024-04-11 20:29:50.398564+00:00
last_updated2024-04-11 20:30:29.467871+00:00
deployedTrue
archx86
accelnone
tags
versions7d19d628-9638-451a-a254-63b41a40b933, c75d9d3d-5c2b-48ee-9ca0-99b03ff7580c
stepsxgboost-booster-binary-classification
publishedFalse

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:30:47.824[12.470000267, 18.6000003815, 81.0899963379, 4...0.8947630
12024-04-11 20:30:47.824[18.9400005341, 21.3099994659, 123.5999984741,...0.1070600
22024-04-11 20:30:47.824[15.4600000381, 19.4799995422, 101.6999969482,...0.1070600
32024-04-11 20:30:47.824[12.3999996185, 17.6800003052, 81.4700012207, ...0.8947630
42024-04-11 20:30:47.824[11.5399999619, 14.4399995804, 74.6500015259, ...0.8947630
...............
952024-04-11 20:30:47.824[9.7770004272, 16.9899997711, 62.5, 290.200012...0.8947630
962024-04-11 20:30:47.824[20.1299991608, 28.25, 131.1999969482, 1261.0,...0.1070600
972024-04-11 20:30:47.824[13.8100004196, 23.75, 91.5599975586, 597.7999...0.1070600
982024-04-11 20:30:47.824[11.1499996185, 13.0799999237, 70.8700027466, ...0.8947630
992024-04-11 20:30:47.824[17.2700004578, 25.4200000763, 112.4000015259,...0.1070600

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()
namexgboost-booster-binary-classification
created2024-04-11 20:29:50.398564+00:00
last_updated2024-04-11 20:30:29.467871+00:00
deployedFalse
archx86
accelnone
tags
versions7d19d628-9638-451a-a254-63b41a40b933, c75d9d3d-5c2b-48ee-9ca0-99b03ff7580c
stepsxgboost-booster-binary-classification
publishedFalse