Wallaroo SDK Upload and Deploy Tutorial: XGBoost Booster Regression
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 Type | Wallaroo 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 thesklearn.datasets.load_diabetes
dataset.
Prerequisites
- A deployed Wallaroo instance with Edge Registry Services and Edge Observability enabled.
- The following Python libraries installed:
- 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()
. 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'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.
Parameter | Type | Description |
---|---|---|
name | string (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. |
path | string (Required) | The path to the model file being uploaded. |
framework | string (Required) | Set as Framework.XGBOOST . |
input_schema | pyarrow.lib.Schema (Required) | The input schema in Apache Arrow schema format. |
output_schema | pyarrow.lib.Schema (Required) | The output schema in Apache Arrow schema format. |
convert_wait | bool (Optional) (Default: True) |
|
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.
Field | Type | Description |
---|---|---|
name | string | The name of the model. |
version | string | The model version as a unique UUID. |
file_name | string | The file name of the model as stored in Wallaroo. |
image_path | string | The image used to deploy the model in the Wallaroo engine. |
last_update_time | DateTime | When 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
Name | xgb-booster-regression-conversion |
Version | 6046fc4c-137f-4f8c-a499-8370588553d5 |
File Name | xgb_booster_regression.pkl |
SHA | 4d1c91595d7c89f0e67227813f818eee50667ce04116841f1c4d8d817f610b7b |
Status | ready |
Image Path | None |
Architecture | x86 |
Acceleration | none |
Updated At | 2024-19-Jul 16:37:43 |
Workspace id | 44 |
Workspace name | xgb-booster-regression-conversion |
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.5.13',
'name': 'engine-658489b859-kv98j',
'status': 'Running',
'reason': None,
'details': [],
'pipeline_statuses': {'pipelines': [{'id': 'xgb-booster-regression-conversion',
'status': 'Running',
'version': '414dc6f2-ebb6-47d5-a000-bf1a327799d3'}]},
'model_statuses': {'models': [{'name': 'xgb-booster-regression-conversion',
'sha': '4d1c91595d7c89f0e67227813f818eee50667ce04116841f1c4d8d817f610b7b',
'status': 'Running',
'version': '6046fc4c-137f-4f8c-a499-8370588553d5'}]}}],
'engine_lbs': [{'ip': '10.28.5.12',
'name': 'engine-lb-6b59985857-btnr8',
'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
time | in.inputs | out.predictions | anomaly.count | |
---|---|---|---|---|
0 | 2024-07-19 16:38:00.252 | [0.0453409851, -0.0446416363, -0.0062059541, -... | 121.672900 | 0 |
1 | 2024-07-19 16:38:00.252 | [0.0925639868, -0.0446416363, 0.0369065292, 0.... | 190.484830 | 0 |
2 | 2024-07-19 16:38:00.252 | [0.0635036752, 0.0506801195, -0.0040503298, -0... | 111.782380 | 0 |
3 | 2024-07-19 16:38:00.252 | [0.0961965248, -0.0446416363, 0.0519958995, 0.... | 109.612880 | 0 |
4 | 2024-07-19 16:38:00.252 | [0.0126481373, 0.0506801195, -0.0202175118, -0... | 116.345750 | 0 |
... | ... | ... | ... | ... |
84 | 2024-07-19 16:38:00.252 | [0.0017505219, -0.0446416363, -0.065485619, -0... | 71.996124 | 0 |
85 | 2024-07-19 16:38:00.252 | [0.0126481373, -0.0446416363, -0.0256065708, -... | 46.890617 | 0 |
86 | 2024-07-19 16:38:00.252 | [-0.0273097865, -0.0446416363, -0.0633299947, ... | 63.577980 | 0 |
87 | 2024-07-19 16:38:00.252 | [-0.0236772466, -0.0446416363, -0.0697968677, ... | 55.595460 | 0 |
88 | 2024-07-19 16:38:00.252 | [-0.0636351705, -0.0446416363, 0.0358287171, -... | 178.056530 | 0 |
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()
name | xgb-booster-regression-conversion |
---|---|
created | 2024-07-19 16:37:33.927027+00:00 |
last_updated | 2024-07-19 16:37:47.017489+00:00 |
deployed | False |
workspace_id | 44 |
workspace_name | xgb-booster-regression-conversion |
arch | x86 |
accel | none |
tags | |
versions | 414dc6f2-ebb6-47d5-a000-bf1a327799d3, 64bc467b-0d96-4780-b58a-8f748cc40bea |
steps | xgb-booster-regression-conversion |
published | False |