XGBoost

Users can now use XGBoost with BentoML with the following API: load, save, and load_runner as follow:

import bentoml
import xgboost as xgb

def xgboost_model() -> "xgb.Booster":
   from sklearn.datasets import load_breast_cancer

   # read in data
   cancer = load_breast_cancer()

   X = cancer.data
   y = cancer.target

   dt = xgb.DMatrix(X, label=y)

   # specify parameters via map
   param = {"max_depth": 3, "eta": 0.3, "objective": "multi:softprob", "num_class": 2}
   bst = xgb.train(param, dt)

   return bst

model = xgboost_model()

# `save` a given model and retrieve coresponding tag:
tag = bentoml.xgboost.save("booster_tree", model, booster_params={"disable_default_eval_metric": 1, "nthread": 2, "tree_method": "hist"})

# retrieve metadata with `bentoml.models.get`:
metadata = bentoml.models.get(tag)

# `load` the model back in memory:
loaded = bentoml.xgboost.load("booster_tree")

Note

You can find more examples for XGBoost in our gallery repo.

bentoml.xgboost.save(name, model, *, booster_params=None, labels=None, custom_objects=None, metadata=None)

Save a model instance to BentoML modelstore.

Parameters
  • name (str) – Name for given model instance. This should pass Python identifier check.

  • model (xgboost.core.Booster) – Instance of model to be saved

  • booster_params (Dict[str, Union[str, int]], optional, default to None) – Params for booster initialization

  • labels (Dict[str, str], optional, default to None) – user-defined labels for managing models, e.g. team=nlp, stage=dev

  • custom_objects (Dict[str, Any]], optional, default to None) – user-defined additional python objects to be saved alongside the model, e.g. a tokenizer instance, preprocessor function, model configuration json

  • metadata (Dict[str, Any], optional, default to None) – Custom metadata for given model.

  • model_store (ModelStore, default to BentoMLContainer.model_store) – BentoML modelstore, provided by DI Container.

Returns

A tag with a format name:version where name is the user-defined model’s name, and a generated version by BentoML.

Return type

Tag

Examples:

import xgboost as xgb
import bentoml

# read in data
dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('demo/data/agaricus.txt.test')
# specify parameters via map
param = dict(max_depth=2, eta=1, objective='binary:logistic')
num_round = 2
bst = xgb.train(param, dtrain, num_round)
...

# `save` the booster to BentoML modelstore:
tag = bentoml.xgboost.save("my_xgboost_model", bst, booster_params=param)
bentoml.xgboost.load(tag, booster_params=None, model_store=<simple_di.providers.SingletonFactory object>)

Load a model from BentoML local modelstore with given name.

Parameters
  • tag (Union[str, Tag]) – Tag of a saved model in BentoML local modelstore.

  • booster_params (t.Dict[str, t.Union[str, int]]) – Params for xgb.core.Booster initialization

  • model_store (ModelStore, default to BentoMLContainer.model_store) – BentoML modelstore, provided by DI Container.

Returns

an instance of xgboost.core.Booster from BentoML modelstore.

Return type

xgboost.core.Booster

Examples:

import bentoml

# `load` the booster back in memory:
booster = bentoml.xgboost.load('booster_tree', booster_params=dict(gpu_id=0))
bentoml.xgboost.load_runner(tag, predict_fn_name='predict', *, booster_params=None, name=None)

Runner represents a unit of serving logic that can be scaled horizontally to maximize throughput. bentoml.xgboost.load_runner implements a Runner class that wrap around a Xgboost booster model, which optimize it for the BentoML runtime.

Parameters
  • tag (Union[str, Tag]) – Tag of a saved model in BentoML local modelstore.

  • predict_fn_name (str, default to predict) – Options for inference functions. If you want to use run or run_batch in a thread context then use inplace_predict. Otherwise, predict are the de facto functions.

  • booster_params (t.Dict[str, t.Union[str, int]], default to None) – Parameters for boosters. Refers to Parameters docs for more information.

Returns

Runner instances for bentoml.xgboost model

Return type

Runner

Examples:

import xgboost as xgb
import bentoml.xgboost
import pandas as pd

input_data = pd.from_csv("/path/to/csv")
runner = bentoml.xgboost.load_runner("my_model:20201012_DE43A2")
runner.run(xgb.DMatrix(input_data))