Wallaroo SDK Upload and Deploy Tutorial: XGBoost Booster Regression

How to upload a XGBoost Booster Regression model to Wallaroo

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

XGBoost Booster Regression Example

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

Resources

This tutorial provides the following:

  • Models:
    • ./models/xgb_booster_regression.pkl: The sample XGBoost model that receives the sklearn.datasets.load_diabetes 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.pipeline import Pipeline
from wallaroo.deployment_config import DeploymentConfigBuilder
import pyarrow as pa
from wallaroo.framework import Framework

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

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-regression-conversion'
pipeline_name = f'xgb-booster-regression-conversion'

model_name = 'xgb-booster-regression-conversion'
model_file_name = './models/xgb_booster_regression.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=10))
])

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
Namexgb-booster-regression-conversion
Version43acd46a-4d83-4b78-84b0-5400f3f328a7
File Namexgb_booster_regression.pkl
SHA4d1c91595d7c89f0e67227813f818eee50667ce04116841f1c4d8d817f610b7b
Statusready
Image PathNone
Architecturex86
Accelerationnone
Updated At2024-11-Apr 20:32:18

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.162',
   'name': 'engine-66f67fdf48-nz44s',
   'status': 'Running',
   'reason': None,
   'details': [],
   'pipeline_statuses': {'pipelines': [{'id': 'xgb-booster-regression-conversion',
      'status': 'Running'}]},
   'model_statuses': {'models': [{'name': 'xgb-booster-regression-conversion',
      'sha': '4d1c91595d7c89f0e67227813f818eee50667ce04116841f1c4d8d817f610b7b',
      'status': 'Running',
      'version': '43acd46a-4d83-4b78-84b0-5400f3f328a7'}]}}],
 'engine_lbs': [{'ip': '10.28.1.121',
   'name': 'engine-lb-d7cc8fc9c-dbdjr',
   '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_diabetes()

# assuming the model is trained on the following DMatrix
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-04-11 20:32:33.372[0.0453409851, -0.0446416363, -0.0062059541, -...121.6729000
12024-04-11 20:32:33.372[0.0925639868, -0.0446416363, 0.0369065292, 0....190.4848300
22024-04-11 20:32:33.372[0.0635036752, 0.0506801195, -0.0040503298, -0...111.7823800
32024-04-11 20:32:33.372[0.0961965248, -0.0446416363, 0.0519958995, 0....109.6128800
42024-04-11 20:32:33.372[0.0126481373, 0.0506801195, -0.0202175118, -0...116.3457500
...............
842024-04-11 20:32:33.372[0.0017505219, -0.0446416363, -0.065485619, -0...71.9961240
852024-04-11 20:32:33.372[0.0126481373, -0.0446416363, -0.0256065708, -...46.8906170
862024-04-11 20:32:33.372[-0.0273097865, -0.0446416363, -0.0633299947, ...63.5779800
872024-04-11 20:32:33.372[-0.0236772466, -0.0446416363, -0.0697968677, ...55.5954600
882024-04-11 20:32:33.372[-0.0636351705, -0.0446416363, 0.0358287171, -...178.0565300

89 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-regression-conversion
created2024-04-11 20:32:08.774703+00:00
last_updated2024-04-11 20:32:21.135142+00:00
deployedFalse
archx86
accelnone
tags
versionse90daeac-7496-4bce-b427-29173e4d6fe6, 7ebdfb2d-1442-41e9-a00a-833d118a6241
stepsxgb-booster-regression-conversion
publishedFalse