From 2d88d17008a74e72c1b6196859bcd65b4a475626 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Thu, 27 Jun 2024 11:30:51 +0800 Subject: [PATCH 1/2] Remove deprecated `DeviceQuantileDMatrix`. (#10491) --- python-package/xgboost/__init__.py | 11 +---------- python-package/xgboost/core.py | 14 -------------- python-package/xgboost/dask/__init__.py | 14 -------------- 3 files changed, 1 insertion(+), 38 deletions(-) diff --git a/python-package/xgboost/__init__.py b/python-package/xgboost/__init__.py index 220093b47c4c..f6060973e1dc 100644 --- a/python-package/xgboost/__init__.py +++ b/python-package/xgboost/__init__.py @@ -5,15 +5,7 @@ from . import tracker # noqa from . import collective, dask -from .core import ( - Booster, - DataIter, - DeviceQuantileDMatrix, - DMatrix, - QuantileDMatrix, - _py_version, - build_info, -) +from .core import Booster, DataIter, DMatrix, QuantileDMatrix, _py_version, build_info from .tracker import RabitTracker # noqa from .training import cv, train @@ -38,7 +30,6 @@ __all__ = [ # core "DMatrix", - "DeviceQuantileDMatrix", "QuantileDMatrix", "Booster", "DataIter", diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index e8bc735e6ff1..215bdd63957f 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -1635,20 +1635,6 @@ def _init( self.handle = handle -class DeviceQuantileDMatrix(QuantileDMatrix): - """Use `QuantileDMatrix` instead. - - .. deprecated:: 1.7.0 - - .. versionadded:: 1.1.0 - - """ - - def __init__(self, *args: Any, **kwargs: Any) -> None: - warnings.warn("Please use `QuantileDMatrix` instead.", FutureWarning) - super().__init__(*args, **kwargs) - - Objective = Callable[[np.ndarray, DMatrix], Tuple[np.ndarray, np.ndarray]] Metric = Callable[[np.ndarray, DMatrix], Tuple[str, float]] diff --git a/python-package/xgboost/dask/__init__.py b/python-package/xgboost/dask/__init__.py index c5140a1fe552..bd23df21b341 100644 --- a/python-package/xgboost/dask/__init__.py +++ b/python-package/xgboost/dask/__init__.py @@ -711,20 +711,6 @@ def _create_fn_args(self, worker_addr: str) -> Dict[str, Any]: return args -class DaskDeviceQuantileDMatrix(DaskQuantileDMatrix): - """Use `DaskQuantileDMatrix` instead. - - .. deprecated:: 1.7.0 - - .. versionadded:: 1.2.0 - - """ - - def __init__(self, *args: Any, **kwargs: Any) -> None: - warnings.warn("Please use `DaskQuantileDMatrix` instead.", FutureWarning) - super().__init__(*args, **kwargs) - - def _create_quantile_dmatrix( feature_names: Optional[FeatureNames], feature_types: Optional[Union[Any, List[Any]]], From 824fba783e974038b448a503fe41ac1c3ff40146 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Thu, 27 Jun 2024 11:31:53 +0800 Subject: [PATCH 2/2] Remove support for deprecated format in Python. (#10490) --- python-package/xgboost/core.py | 2 +- tests/python/test_model_io.py | 86 ++-------------------------------- 2 files changed, 6 insertions(+), 82 deletions(-) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index 215bdd63957f..40433c28ea29 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -2636,7 +2636,7 @@ def save_raw(self, raw_format: str = "ubj") -> bytearray: Parameters ---------- raw_format : - Format of output buffer. Can be `json`, `ubj` or `deprecated`. + Format of output buffer. Can be `json` or `ubj`. Returns ------- diff --git a/tests/python/test_model_io.py b/tests/python/test_model_io.py index df0fff22e712..37b3aac35bfc 100644 --- a/tests/python/test_model_io.py +++ b/tests/python/test_model_io.py @@ -75,26 +75,10 @@ def run_model_json_io(self, parameters: dict, ext: str) -> None: from_ubjraw = xgb.Booster() from_ubjraw.load_model(ubj_raw) - if parameters.get("multi_strategy", None) != "multi_output_tree": - # Old binary model is not supported for vector leaf. - with pytest.warns(Warning, match="Model format is default to UBJSON"): - old_from_json = from_jraw.save_raw(raw_format="deprecated") - old_from_ubj = from_ubjraw.save_raw(raw_format="deprecated") - - assert old_from_json == old_from_ubj - raw_json = bst.save_raw(raw_format="json") pretty = json.dumps(json.loads(raw_json), indent=2) + "\n\n" bst.load_model(bytearray(pretty, encoding="ascii")) - if parameters.get("multi_strategy", None) != "multi_output_tree": - # old binary model is not supported. - with pytest.warns(Warning, match="Model format is default to UBJSON"): - old_from_json = from_jraw.save_raw(raw_format="deprecated") - old_from_ubj = from_ubjraw.save_raw(raw_format="deprecated") - - assert old_from_json == old_from_ubj - rng = np.random.default_rng() X = rng.random(size=from_jraw.num_features() * 10).reshape( (10, from_jraw.num_features()) @@ -126,11 +110,6 @@ def test_categorical_model_io(self) -> None: predt_0 = booster.predict(Xy) with tempfile.TemporaryDirectory() as tempdir: - path = os.path.join(tempdir, "model.deprecated") - with pytest.raises(ValueError, match=r".*JSON/UBJSON.*"): - with pytest.warns(Warning, match="Model format is default to UBJSON"): - booster.save_model(path) - path = os.path.join(tempdir, "model.json") booster.save_model(path) booster = xgb.Booster(model_file=path) @@ -181,34 +160,6 @@ def test_json_io_schema(self) -> None: objectives_from_schema.add(j_obj["properties"]["name"]["const"]) assert set(objectives) == objectives_from_schema - def test_model_binary_io(self) -> None: - model_path = "test_model_binary_io.deprecated" - parameters = { - "tree_method": "hist", - "booster": "gbtree", - "scale_pos_weight": "0.5", - } - X = np.random.random((10, 3)) - y = np.random.random((10,)) - dtrain = xgb.DMatrix(X, y) - bst = xgb.train(parameters, dtrain, num_boost_round=2) - with pytest.warns(Warning, match="Model format is default to UBJSON"): - bst.save_model(model_path) - bst = xgb.Booster(model_file=model_path) - os.remove(model_path) - config = json.loads(bst.save_config()) - assert ( - float(config["learner"]["objective"]["reg_loss_param"]["scale_pos_weight"]) - == 0.5 - ) - - buf = bst.save_raw() - from_raw = xgb.Booster() - from_raw.load_model(buf) - - buf_from_raw = from_raw.save_raw() - assert buf == buf_from_raw - def test_with_pathlib(self) -> None: """Saving and loading model files from paths.""" save_path = Path("model.ubj") @@ -269,41 +220,19 @@ def rename(src: str, dst: str) -> None: os.rename(src, dst) with tempfile.TemporaryDirectory() as tmpdir: - path_dep = os.path.join(tmpdir, "model.deprecated") - # save into deprecated format - with pytest.warns(UserWarning, match="UBJSON"): - booster.save_model(path_dep) - path_ubj = os.path.join(tmpdir, "model.ubj") - rename(path_dep, path_ubj) - - with pytest.raises(ValueError, match="{"): - xgb.Booster(model_file=path_ubj) - path_json = os.path.join(tmpdir, "model.json") + + booster.save_model(path_ubj) rename(path_ubj, path_json) with pytest.raises(ValueError, match="{"): xgb.Booster(model_file=path_json) - # save into ubj format - booster.save_model(path_ubj) - rename(path_ubj, path_dep) - # deprecated is not a recognized format internally, XGBoost can guess the - # right format - xgb.Booster(model_file=path_dep) - rename(path_dep, path_json) - with pytest.raises(ValueError, match="Expecting"): - xgb.Booster(model_file=path_json) - - # save into JSON format booster.save_model(path_json) - rename(path_json, path_dep) - # deprecated is not a recognized format internally, XGBoost can guess the - # right format - xgb.Booster(model_file=path_dep) - rename(path_dep, path_ubj) - with pytest.raises(ValueError, match="Expecting"): + rename(path_json, path_ubj) + + with pytest.raises(ValueError, match="{"): xgb.Booster(model_file=path_ubj) # save model without file extension @@ -382,11 +311,6 @@ def test_sklearn_model() -> None: from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split - with tempfile.TemporaryDirectory() as tempdir: - model_path = os.path.join(tempdir, "digits.deprecated") - with pytest.warns(Warning, match="Model format is default to UBJSON"): - save_load_model(model_path) - with tempfile.TemporaryDirectory() as tempdir: model_path = os.path.join(tempdir, "digits.model.json") save_load_model(model_path)