Wallaroo SDK Upload and Deploy Tutorial: XGBoost RF Regressor
This tutorial can be downloaded as part of the Wallaroo Tutorials repository.
Wallaroo Model Upload via the Wallaroo SDK: XGBoost RF Regressor
The following tutorial demonstrates how to upload a XGBoost RF Regressor model to a Wallaroo instance.
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.
Tutorial Goals
Demonstrate the following:
- Upload a XGBoost RF Regressor model to a Wallaroo instance.
- Create a pipeline and add the model as a pipeline step.
- Perform a sample inference.
Prerequisites
- Wallaroo Version 2023.2.1 or above instance.
Resources
This tutorial provides the following:
- Models:
./models/model-auto-conversion_xgboost_xgb_rf_regressor_diabetes.pkl
: The sample XGBoost model.
References
- Wallaroo MLOps API Essentials Guide: Model Upload and Registrations
- Wallaroo API Connection Guide
- DNS Integration Guide
Tutorial Steps
Import Libraries
The first step is to import the libraries we’ll be using. These are included by default in the Wallaroo instance’s JupyterHub service. See ./requirements.txt
for a list of additional libraries used with this tutorial.
import json
import os
import pickle
import wallaroo
from wallaroo.pipeline import Pipeline
from wallaroo.deployment_config import DeploymentConfigBuilder
from wallaroo.object import EntityNotFoundError
from wallaroo.framework import Framework
import pyarrow as pa
import numpy as np
import pandas as pd
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-rf-regressor'
pipeline_name = f'xgboost-rf-regressor'
model_name = 'xgboost-rf-regressor'
model_file_name = './models/model-auto-conversion_xgboost_xgb_rf_regressor_diabetes.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)
Configure Data Schemas
XGBoost models are uploaded to Wallaroo through the Wallaroo 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.
XGBoost Schema Inputs
XGBoost schema follows a different format than other models. To prevent inputs from being out of order, the inputs should be submitted in a single row in the order the model is trained to accept, with all of the data types being the same. If a model is originally trained to accept inputs of different data types, it will need to be retrained to only accept one data type for each column - typically pa.float32()
is a good choice.
For example, the following DataFrame has 4 columns, each column a float
.
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | |
---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 |
1 | 4.9 | 3.0 | 1.4 | 0.2 |
For submission to an XGBoost model, the data input schema will be a single array with 4 float values.
input_schema = pa.schema([
pa.field('inputs', pa.list_(pa.float64(), list_size=4))
])
When submitting as an inference, the DataFrame is converted to rows with the column data expressed as a single array. The data must be in the same order as the model expects, which is why the data is submitted as a single array rather than JSON labeled columns: this insures that the data is submitted in the exact order as the model is trained to accept.
Original DataFrame:
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | |
---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 |
1 | 4.9 | 3.0 | 1.4 | 0.2 |
Converted DataFrame:
inputs | |
---|---|
0 | [5.1, 3.5, 1.4, 0.2] |
1 | [4.9, 3.0, 1.4, 0.2] |
XGBoost Schema Outputs
Outputs for XGBoost are labeled based on the trained model outputs. For this example, the output is simply a single output listed as output
. In the Wallaroo inference result, it is grouped with the metadata out
as out.output
.
output_schema = pa.schema([
pa.field('output', pa.int32())
])
pipeline.infer(dataframe)
time | in.inputs | out.output | check_failures | |
---|---|---|---|---|
0 | 2023-07-05 15:11:29.776 | [5.1, 3.5, 1.4, 0.2] | 0 | 0 |
1 | 2023-07-05 15:11:29.776 | [4.9, 3.0, 1.4, 0.2] | 0 | 0 |
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
The model will be uploaded with the framework set as Framework.XGBOOST
.
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.........incompatible
Model is pending loading to a container runtime..
Model is attempting loading to a container runtime......successful
Ready
Name | xgboost-rf-regressor |
Version | 52c9c8b3-3c7f-491a-bbd1-7efcf626338c |
File Name | model-auto-conversion_xgboost_xgb_rf_regressor_diabetes.pkl |
SHA | 461341d78d54a9bfc8e4faa94be6037aef15217974ba59bad92d31ef48e6bd99 |
Status | ready |
Image Path | proxy.replicated.com/proxy/wallaroo/ghcr.io/wallaroolabs/mac-deploy:v2024.2.0-main-5408 |
Architecture | x86 |
Acceleration | none |
Updated At | 2024-19-Jul 15:56:00 |
Workspace id | 36 |
Workspace name | xgboost-rf-regressor |
Deploy Pipeline
The model is uploaded and ready for use. We’ll add it as a step in our pipeline, then deploy the pipeline. For this example we’re allocated 0.25 cpu and 4 Gi RAM to the pipeline through the pipeline’s deployment configuration.
deployment_config = DeploymentConfigBuilder() \
.cpus(0.25).memory('1Gi') \
.build()
# clear the pipeline if it was used before
pipeline.clear()
pipeline.add_model_step(model)
pipeline.deploy(deployment_config=deployment_config)
pipeline.status()
{'status': 'Running',
'details': [],
'engines': [{'ip': '10.28.1.9',
'name': 'engine-56644c98b-25m9l',
'status': 'Running',
'reason': None,
'details': [],
'pipeline_statuses': {'pipelines': [{'id': 'xgboost-rf-regressor',
'status': 'Running',
'version': 'ffba6b6b-6961-443c-9800-be334245d21f'}]},
'model_statuses': {'models': [{'name': 'xgboost-rf-regressor',
'sha': '461341d78d54a9bfc8e4faa94be6037aef15217974ba59bad92d31ef48e6bd99',
'status': 'Running',
'version': '52c9c8b3-3c7f-491a-bbd1-7efcf626338c'}]}}],
'engine_lbs': [{'ip': '10.28.1.8',
'name': 'engine-lb-6b59985857-px9rr',
'status': 'Running',
'reason': None,
'details': []}],
'sidekicks': [{'ip': '10.28.1.7',
'name': 'engine-sidekick-xgboost-rf-regressor-27-77d6b77786-x47wc',
'status': 'Running',
'reason': None,
'details': [],
'statuses': '\n'}]}
Run Inference
A sample inference will be run. First the pandas DataFrame used for the inference is created, then the inference run through the pipeline’s infer
method.
data = pd.read_json('./data/test_xgb_rf-regressor.json')
display(data)
dataframe = pd.DataFrame({"inputs": data[:2].values.tolist()})
display(dataframe)
pipeline.infer(dataframe)
age | sex | bmi | bp | s1 | s2 | s3 | s4 | s5 | s6 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 0.038076 | 0.050680 | 0.061696 | 0.021872 | -0.044223 | -0.034821 | -0.043401 | -0.002592 | 0.019907 | -0.017646 |
1 | -0.001882 | -0.044642 | -0.051474 | -0.026328 | -0.008449 | -0.019163 | 0.074412 | -0.039493 | -0.068332 | -0.092204 |
inputs | |
---|---|
0 | [0.0380759064, 0.0506801187, 0.0616962065, 0.0... |
1 | [-0.0018820165, -0.0446416365, -0.051474061200... |
time | in.inputs | out.predictions | anomaly.count | |
---|---|---|---|---|
0 | 2024-07-19 15:59:21.118 | [0.038075905, 0.05068012, 0.061696205, 0.02187... | 166.61877 | 0 |
1 | 2024-07-19 15:59:21.118 | [-0.0018820165, -0.044641636, -0.05147406, -0.... | 76.18958 | 0 |
Undeploy Pipelines
With the tutorial complete, the pipeline is undeployed to return the resources back to the cluster.
pipeline.undeploy()
name | xgboost-rf-regressor |
---|---|
created | 2024-07-19 15:52:08.373683+00:00 |
last_updated | 2024-07-19 15:56:24.163075+00:00 |
deployed | False |
workspace_id | 36 |
workspace_name | xgboost-rf-regressor |
arch | x86 |
accel | none |
tags | |
versions | ffba6b6b-6961-443c-9800-be334245d21f, 597a8adf-a244-4726-9e0e-7649a8311e93 |
steps | xgboost-rf-regressor |
published | False |