-
Notifications
You must be signed in to change notification settings - Fork 156
feat: add CatBoost model format inference example #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kittywaresz
wants to merge
3
commits into
kserve:main
Choose a base branch
from
kittywaresz:mlserver-catboost-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
docs/model-serving/predictive-inference/frameworks/catboost/catboost-input.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "inputs": [ | ||
| { | ||
| "name": "predict-prob", | ||
| "shape": [ | ||
| 1, | ||
| 10 | ||
| ], | ||
| "datatype": "FP32", | ||
| "data": [ | ||
| [ | ||
| 61, | ||
| 56, | ||
| 27, | ||
| 59, | ||
| 15, | ||
| 50, | ||
| 60, | ||
| 77, | ||
| 62, | ||
| 98 | ||
| ] | ||
| ] | ||
| } | ||
| ] | ||
| } |
180 changes: 180 additions & 0 deletions
180
docs/model-serving/predictive-inference/frameworks/catboost/catboost.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| --- | ||
| title: CatBoost | ||
| description: Deploy CatBoost models with KServe | ||
| --- | ||
|
|
||
| # Deploying CatBoost Models with KServe | ||
|
|
||
| This guide demonstrates how to deploy MLflow models using KServe's `InferenceService` and how to send inference requests using the [Open Inference Protocol](https://github.com/kserve/open-inference-protocol). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before you begin, make sure you have: | ||
|
|
||
| - A Kubernetes cluster with [KServe installed](../../../../getting-started/quickstart-guide.md). | ||
| - `kubectl` CLI configured to communicate with your cluster. | ||
| - Basic knowledge of Kubernetes concepts and MLflow models. | ||
| - Access to cloud storage (like Google Cloud Storage) to store your model artifacts. | ||
|
|
||
| ## Training a Sample CatBoost Model | ||
|
|
||
| The first step is to train a sample CatBoost model and serialize it in an appropriate format using [save_model](https://catboost.ai/docs/en/concepts/python-reference_catboost_save_model) API. | ||
|
|
||
| ```python | ||
| import numpy as np | ||
| from catboost import CatBoostClassifier | ||
|
|
||
| train_data = np.random.randint(0, 100, size=(100, 10)) | ||
| train_labels = np.random.randint(0, 2, size=(100)) | ||
|
|
||
| model = CatBoostClassifier( | ||
| iterations=2, | ||
| depth=2, | ||
| learning_rate=1, | ||
| loss_function="Logloss", | ||
| verbose=True, | ||
| ) | ||
| model.fit(train_data, train_labels) | ||
| model.save_model("model.cbm") | ||
| ``` | ||
|
|
||
| ## Testing the Model Locally | ||
|
|
||
| Once you have your model serialized as `model.cbm`, you can use [MLServer](https://github.com/SeldonIO/MLServer) to create a local model server. For more details, check the [CatBoost example documentation](https://mlserver.readthedocs.io/en/stable/examples/catboost/README.html). | ||
|
|
||
| :::tip | ||
| This local testing step is optional. You can skip to the deployment section if you prefer. | ||
| ::: | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| To use MLServer locally, install the `mlserver` package and the CatBoost runtime: | ||
|
|
||
| ```bash | ||
| pip install mlserver mlserver-catboost | ||
| ``` | ||
|
|
||
| ### Model Settings | ||
|
|
||
| Next, provide model settings so that MLServer knows: | ||
|
|
||
| - The inference runtime to serve your model (i.e. `mlserver_catboost.CatboostModel`) | ||
| - The model's parameters | ||
|
|
||
| These can be specified through environment variables or by creating a local `model-settings.json` file: | ||
|
|
||
| ```json | ||
| { | ||
| "implementation": "mlserver_catboost.CatboostModel", | ||
| "name": "catboost-classifier", | ||
| "parameters": { | ||
| "uri": "./model.cbm", | ||
| "version": "v0.1.0" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Starting the Model Server Locally | ||
|
|
||
| With the `mlserver` package installed and a local `model-settings.json` file, start your server with: | ||
|
|
||
| ```bash | ||
| cat << EOF > model-settings.json | ||
| { | ||
| "implementation": "mlserver_catboost.CatboostModel", | ||
| "name": "catboost-classifier", | ||
| "parameters": { | ||
| "uri": "./model.cbm", | ||
| "version": "v0.1.0" | ||
| } | ||
| } | ||
| EOF | ||
|
|
||
| mlserver start . | ||
| ``` | ||
|
|
||
| If everything is fine, you will see such messages in the mlserver process output: | ||
|
|
||
| ```txt | ||
| 2025-09-18 01:15:05,483 [mlserver.parallel] DEBUG - Starting response processing loop... | ||
| 2025-09-18 01:15:05,484 [mlserver.rest] INFO - HTTP server running on http://0.0.0.0:8080 | ||
| ... | ||
| 2025-09-18 01:15:06,528 [mlserver][catboost-classifier:v0.1.0] INFO - Loaded model 'catboost-classifier' successfully. | ||
| 2025-09-18 01:15:06,529 [mlserver][catboost-classifier:v0.1.0] INFO - Loaded model 'catboost-classifier' successfully. | ||
| ``` | ||
|
|
||
| ## Deploying the Model with InferenceService | ||
|
|
||
| When deploying the model with InferenceService, KServe injects sensible defaults that work out-of-the-box without additional configuration. However, you can override these defaults by providing a `model-settings.json` file similar to your local one. You can even provide [multiple `model-settings.json` files to load multiple models](https://github.com/SeldonIO/MLServer/tree/master/docs/examples/mms). | ||
|
|
||
| To use the Open Inference Protocol (v2) for inference with the deployed model, set the `protocolVersion` field to `v2`. In this example, your model artifacts have already been uploaded to a Google Cloud Storage bucket and can be accessed at `gs://kfserving-examples/models/catboost/classifier`. | ||
|
|
||
| Apply the YAML manifest: | ||
|
|
||
| ```bash | ||
| kubectl apply -f - << EOF | ||
| apiVersion: "serving.kserve.io/v1beta1" | ||
| kind: "InferenceService" | ||
| metadata: | ||
| name: "catboost-example" | ||
| spec: | ||
| predictor: | ||
| model: | ||
| runtime: kserve-mlserver | ||
| modelFormat: | ||
| name: catboost | ||
| protocolVersion: v2 | ||
| storageUri: "gs://kfserving-examples/models/catboost/classifier" | ||
| resources: | ||
| requests: | ||
| cpu: "1" | ||
| memory: "1Gi" | ||
| limits: | ||
| cpu: "1" | ||
| memory: "1Gi" | ||
| EOF | ||
| ``` | ||
|
|
||
| ## Testing the Deployed Model | ||
|
|
||
| You can test your deployed model by sending a sample request following the [Open Inference Protocol](https://github.com/kserve/open-inference-protocol). | ||
|
|
||
| Use our sample input file [catboost-input.json](./catboost-input.json) to test the model: | ||
|
|
||
| [Determine the ingress IP and ports](../../../../getting-started/predictive-first-isvc.md#4-determine-the-ingress-ip-and-ports), then set the `INGRESS_HOST` and `INGRESS_PORT` environment variables. | ||
|
|
||
| ```bash | ||
| SERVICE_HOSTNAME=$(kubectl get inferenceservice catboost-example -o jsonpath='{.status.url}' | cut -d "/" -f 3) | ||
|
|
||
| curl -v \ | ||
| -H "Host: ${SERVICE_HOSTNAME}" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d @./catboost-input.json \ | ||
| http://${INGRESS_HOST}:${INGRESS_PORT}/v2/models/catboost-example/infer | ||
| ``` | ||
|
|
||
| :::tip[Expected Output] | ||
| ```json | ||
| { | ||
| "model_name": "catboost-example", | ||
| "id": "70062817-f7de-4105-93ef-e0e2ea3d214e", | ||
| "parameters": {}, | ||
| "outputs": [ | ||
| { | ||
| "name": "predict", | ||
| "shape": [ | ||
| 1, | ||
| 1 | ||
| ], | ||
| "datatype": "INT64", | ||
| "parameters": { | ||
| "content_type": "np" | ||
| }, | ||
| "data": [ | ||
| 0 | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
| ::: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spolti, would it be possible to upload this file to the official KServe bucket?
Is's just a single
.cbmfileThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question, @yuzisun wdyt?