Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into fix_freebsd
Browse files Browse the repository at this point in the history
  • Loading branch information
hcho3 committed Jun 27, 2024
2 parents 1423606 + 824fba7 commit 4908ccf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 120 deletions.
11 changes: 1 addition & 10 deletions python-package/xgboost/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -38,7 +30,6 @@
__all__ = [
# core
"DMatrix",
"DeviceQuantileDMatrix",
"QuantileDMatrix",
"Booster",
"DataIter",
Expand Down
16 changes: 1 addition & 15 deletions python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down Expand Up @@ -2650,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
-------
Expand Down
14 changes: 0 additions & 14 deletions python-package/xgboost/dask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]],
Expand Down
86 changes: 5 additions & 81 deletions tests/python/test_model_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4908ccf

Please sign in to comment.