Skip to content

Commit

Permalink
xgboost added warning for deprecated format and increased python version
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonpzg committed Sep 26, 2024
1 parent 3a7a27e commit 2a03a65
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 113 deletions.
2 changes: 1 addition & 1 deletion servers/xgboostserver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ docker-build:
s2i build \
-E environment \
./xgboostserver \
${DOCKER_REGISTRY}/seldon-core-s2i-python38:${VERSION} \
seldonio/seldon-core-s2i-python3:${VERSION} \
${IMAGE_NAME}:${VERSION}

docker-push:
Expand Down
66 changes: 43 additions & 23 deletions servers/xgboostserver/test/xgboost_iris.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,55 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import xgboost as xgb\n",
"from sklearn.datasets import load_iris"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"model_dir = \".\"\n",
"BOOSTER_FILE = \"model.json\"\n",
"BOOSTER_FILE_DEPRECATED = \"model.bst\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"iris = load_iris()\n",
"y = iris[\"target\"]\n",
"X = iris[\"data\"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"name": "stderr",
"output_type": "stream",
"text": [
"[07:31:00] WARNING: ../src/learner.cc:573: \n",
"Parameters: { \"silent\" } might not be used.\n",
"\n",
" This may not be accurate due to some parameters are only used in language bindings but\n",
" passed down to XGBoost core. Or some parameters are not used but slip through this\n",
" verification. Please open an issue if you find above cases.\n",
"\n",
"/home/ramonpzg/micromamba/envs/mlserver-quick/lib/python3.11/site-packages/xgboost/core.py:158: UserWarning: [18:32:52] WARNING: /workspace/src/learner.cc:740: \n",
"Parameters: { \"silent\" } are not used.\n",
"\n",
"[07:31:00] WARNING: ../src/learner.cc:1095: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'multi:softmax' was changed from 'merror' to 'mlogloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n"
" warnings.warn(smsg, UserWarning)\n"
]
}
],
"source": [
"import os\n",
"\n",
"import xgboost as xgb\n",
"from sklearn.datasets import load_iris\n",
"\n",
"model_dir = \".\"\n",
"BST_FILE = \"model.bst\"\n",
"\n",
"iris = load_iris()\n",
"y = iris[\"target\"]\n",
"X = iris[\"data\"]\n",
"dtrain = xgb.DMatrix(X, label=y)\n",
"param = {\n",
" \"max_depth\": 6,\n",
Expand All @@ -50,8 +68,10 @@
" \"objective\": \"multi:softmax\",\n",
"}\n",
"xgb_model = xgb.train(params=param, dtrain=dtrain)\n",
"model_file = os.path.join((model_dir), BST_FILE)\n",
"xgb_model.save_model(model_file)"
"model_file_json = os.path.join((model_dir), BOOSTER_FILE)\n",
"model_file_bst= os.path.join((model_dir), BOOSTER_FILE_DEPRECATED)\n",
"xgb_model.save_model(model_file_json)\n",
"xgb_model.save_model(model_file_bst)"
]
},
{
Expand Down Expand Up @@ -349,7 +369,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"version": "3.11.9"
},
"varInspector": {
"cols": {
Expand Down
37 changes: 31 additions & 6 deletions servers/xgboostserver/xgboostserver/XGBoostServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import yaml
import logging
import xgboost as xgb
import json
from packaging import version

logger = logging.getLogger(__name__)

BOOSTER_FILE = "model.bst"
BOOSTER_FILE = "model.json"
BOOSTER_FILE_DEPRECATED = "model.bst"


class XGBoostServer(SeldonComponent):
Expand All @@ -22,7 +25,27 @@ def load(self):
model_file = os.path.join(
seldon_core.Storage.download(self.model_uri), BOOSTER_FILE
)
self._booster = xgb.Booster(model_file=model_file)
if not os.path.exists(model_file):
# Fallback to deprecated .bst format
model_file = os.path.join(
seldon_core.Storage.download(self.model_uri), BOOSTER_FILE_DEPRECATED
)
if os.path.exists(model_file):
logger.warning(
"Using deprecated .bst format for XGBoost model. "
"Please update to the .json format in the future."
)
else:
raise FileNotFoundError(f"Model file not found: {BOOSTER_FILE} or {BOOSTER_FILE_DEPRECATED}")

if version.parse(xgb.__version__) < version.parse("1.7.0"):
# Load model using deprecated method for older XGBoost versions
self._booster = xgb.Booster(model_file=model_file)
else:
# Load model using the new .json format for XGBoost >= 1.7.0
self._booster = xgb.Booster()
self._booster.load_model(model_file)

self.ready = True

def predict(
Expand All @@ -39,12 +62,14 @@ def init_metadata(self):

try:
with open(file_path, "r") as f:
return yaml.safe_load(f.read())
metadata = yaml.safe_load(f.read())
# Validate and sanitize the loaded metadata if needed
return metadata
except FileNotFoundError:
logger.debug(f"metadata file {file_path} does not exist")
logger.debug(f"Metadata file {file_path} does not exist")
return {}
except yaml.YAMLError:
logger.error(
f"metadata file {file_path} present but does not contain valid yaml"
f"Metadata file {file_path} present but does not contain valid YAML"
)
return {}
return {}
75 changes: 0 additions & 75 deletions servers/xgboostserver/xgboostserver/XGBoostServer2.py

This file was deleted.

1 change: 1 addition & 0 deletions servers/xgboostserver/xgboostserver/model.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions servers/xgboostserver/xgboostserver/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
scikit-learn == 1.0.2
scikit-learn >= 1.0.2, <=1.5.0
numpy >= 1.8.2
xgboost == 1.4.2
xgboost >= 1.7.0, <=2.2.0
6 changes: 3 additions & 3 deletions wrappers/s2i/python/build_scripts/build_all.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
make -C ../ docker-build-conda-base

# Build standard wrapper images
make -C ../ docker-build PYTHON_VERSION=3.8.10
make -C ../ docker-build PYTHON_VERSION=3.10.15

# Push default tag image
make -C ../ docker-tag-base-python PYTHON_VERSION=3.8.10
make -C ../ docker-tag-base-python PYTHON_VERSION=3.10.15

# Build GPU images
make -C ../ docker-build-gpu PYTHON_VERSION=3.8.10
make -C ../ docker-build-gpu PYTHON_VERSION=3.10.15
6 changes: 3 additions & 3 deletions wrappers/s2i/python/build_scripts/push_all.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
make -C ../ docker-push-conda-base

# Push standard wrapper images
make -C ../ push_to_dockerhub PYTHON_VERSION=3.8.10
make -C ../ push_to_dockerhub PYTHON_VERSION=3.10.15

# Push default tag image
make -C ../ docker-push-base-python PYTHON_VERSION=3.8.10
make -C ../ docker-push-base-python PYTHON_VERSION=3.10.15

# Push GPU images
make -C ../ docker-push-gpu PYTHON_VERSION=3.8.10
make -C ../ docker-push-gpu PYTHON_VERSION=3.10.15

0 comments on commit 2a03a65

Please sign in to comment.