Skip to content
This repository was archived by the owner on Jan 12, 2026. It is now read-only.

Commit df38184

Browse files
authored
Make xgboost an extra dependency (#151)
1 parent 6abf2e3 commit df38184

File tree

13 files changed

+63
-30
lines changed

13 files changed

+63
-30
lines changed

.github/workflows/test.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
if [ -f requirements-test.txt ]; then python -m pip install -r requirements-test.txt; fi
5656
- name: Install package
5757
run: |
58-
python -m pip install -e .
58+
python -m pip install -e ".[default]"
5959
- name: Print environment info
6060
run: |
6161
./xgboost_ray/tests/env_info.sh
@@ -86,7 +86,7 @@ jobs:
8686
if [ -f requirements-test.txt ]; then python -m pip install -r requirements-test.txt; fi
8787
- name: Install package
8888
run: |
89-
python -m pip install -e .
89+
python -m pip install -e ".[default]"
9090
- name: Print environment info
9191
run: |
9292
./xgboost_ray/tests/env_info.sh
@@ -124,7 +124,7 @@ jobs:
124124
python -m pip uninstall -y tabulate
125125
- name: Install package
126126
run: |
127-
python -m pip install -e .
127+
python -m pip install -e ".[default]"
128128
- name: Print environment info
129129
run: |
130130
./xgboost_ray/tests/env_info.sh
@@ -166,7 +166,7 @@ jobs:
166166
sudo apt-get install -y --no-install-recommends ninja-build
167167
- name: Install package
168168
run: |
169-
python -m pip install -e .
169+
python -m pip install -e ".[default]"
170170
- name: Clone XGBoost repo
171171
uses: actions/checkout@v2
172172
with:
@@ -212,7 +212,7 @@ jobs:
212212
if [ -f requirements-test.txt ]; then python -m pip install -r requirements-test.txt; fi
213213
- name: Install package
214214
run: |
215-
python -m pip install -e .
215+
python -m pip install -e ".[default]"
216216
- name: Install legacy XGBoost
217217
run: |
218218
python -m pip install xgboost==0.90

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ Installation
2222
You can install the latest XGBoost-Ray release from PIP:
2323

2424
```bash
25-
pip install xgboost_ray
25+
pip install "xgboost_ray[default]"
2626
```
2727

2828
If you'd like to install the latest master, use this command instead:
2929

3030
```bash
31-
pip install git+https://github.com/ray-project/xgboost_ray.git#xgboost_ray
31+
pip install "git+https://github.com/ray-project/xgboost_ray.git#xgboost_ray[default]"
3232
```
3333

34+
Omitting `[default]` will cause `xgboost` to not be installed as a dependency.
3435

3536
Usage
3637
-----

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"distributed computing framework Ray.",
1111
url="https://github.com/ray-project/xgboost_ray",
1212
install_requires=[
13-
"xgboost>=0.90", "ray", "numpy>=1.16,<1.20", "pandas", "pyarrow<5.0.0",
14-
"wrapt>=1.12.1"
15-
])
13+
"ray", "numpy>=1.16,<1.20", "pandas", "pyarrow<5.0.0", "wrapt>=1.12.1"
14+
],
15+
extras_require={"default": ["xgboost>=0.90"]})
1616
# pyarrow<5.0.0 pinned until petastorm is updated

xgboost_ray/compat/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import xgboost as xgb
1+
from typing import TYPE_CHECKING
2+
3+
if TYPE_CHECKING:
4+
from xgboost_ray.xgb import xgboost as xgb
25

36
try:
47
from xgboost.callback import TrainingCallback
@@ -15,7 +18,7 @@ def __init__(self):
1518
self._before_iteration = getattr(self, "before_iteration")
1619
self.__dict__["before_iteration"] = True
1720

18-
def __call__(self, callback_env: xgb.core.CallbackEnv):
21+
def __call__(self, callback_env: "xgb.core.CallbackEnv"):
1922
if hasattr(self, "_before_iteration"):
2023
self._before_iteration(
2124
model=callback_env.model,

xgboost_ray/data_sources/data_source.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from typing import Any, Optional, Sequence, Tuple, Dict, List
1+
from typing import Any, Optional, Sequence, Tuple, Dict, List, TYPE_CHECKING
22

33
from enum import Enum
44

55
import pandas as pd
6-
import xgboost as xgb
76

87
from ray.actor import ActorHandle
98

9+
if TYPE_CHECKING:
10+
from xgboost_ray.xgb import xgboost as xgb
11+
1012

1113
class RayFileType(Enum):
1214
"""Enum for different file types (used for overrides)."""
@@ -86,7 +88,7 @@ def load_data(data: Any,
8688
raise NotImplementedError
8789

8890
@staticmethod
89-
def update_feature_names(matrix: xgb.DMatrix,
91+
def update_feature_names(matrix: "xgb.DMatrix",
9092
feature_names: Optional[List[str]]):
9193
"""Optionally update feature names before training/prediction
9294

xgboost_ray/data_sources/numpy.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from typing import Any, Optional, Sequence, List
1+
from typing import Any, Optional, Sequence, List, TYPE_CHECKING
22

33
import numpy as np
44
import pandas as pd
5-
import xgboost as xgb
65

76
from xgboost_ray.data_sources.data_source import DataSource, RayFileType
87
from xgboost_ray.data_sources.pandas import Pandas
98

9+
if TYPE_CHECKING:
10+
from xgboost_ray.xgb import xgboost as xgb
11+
1012

1113
class Numpy(DataSource):
1214
"""Read from numpy arrays."""
@@ -17,7 +19,7 @@ def is_data_type(data: Any,
1719
return isinstance(data, np.ndarray)
1820

1921
@staticmethod
20-
def update_feature_names(matrix: xgb.DMatrix,
22+
def update_feature_names(matrix: "xgb.DMatrix",
2123
feature_names: Optional[List[str]]):
2224
# Potentially unset feature names
2325
matrix.feature_names = feature_names

xgboost_ray/main.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import numpy as np
1515
import pandas as pd
1616

17-
import xgboost as xgb
17+
from xgboost_ray.xgb import xgboost as xgb
1818
from xgboost.core import XGBoostError, EarlyStopException
1919

2020
from xgboost_ray.callback import DistributedCallback, \
@@ -76,16 +76,18 @@
7676
ELASTIC_RESTART_GRACE_PERIOD_S = int(
7777
os.getenv("RXGB_ELASTIC_RESTART_GRACE_PERIOD_S", 10))
7878

79+
xgboost_version = xgb.__version__ if xgb else "0.0.0"
80+
7981
LEGACY_WARNING = (
8082
f"You are using `xgboost_ray` with a legacy XGBoost version "
81-
f"(version {xgb.__version__}). While we try to support "
83+
f"(version {xgboost_version}). While we try to support "
8284
f"older XGBoost versions, please note that this library is only "
8385
f"fully tested and supported for XGBoost >= 1.4. Please consider "
8486
f"upgrading your XGBoost version (`pip install -U xgboost`).")
8587

8688
# XGBoost version as an int tuple for comparisions
8789
XGBOOST_VERSION_TUPLE = tuple(
88-
int(x) for x in re.sub(r"[^\.0-9]", "", xgb.__version__).split("."))
90+
int(x) for x in re.sub(r"[^\.0-9]", "", xgboost_version).split("."))
8991

9092

9193
class RayXGBoostTrainingError(RuntimeError):
@@ -1144,6 +1146,11 @@ def train(
11441146
"""
11451147
os.environ.setdefault("RAY_IGNORE_UNHANDLED_ERRORS", "1")
11461148

1149+
if xgb is None:
1150+
raise ImportError(
1151+
"xgboost package is not installed. XGBoost-Ray WILL NOT WORK. "
1152+
"FIX THIS by running `pip install \"xgboost-ray[default]\"`.")
1153+
11471154
if _remote is None:
11481155
_remote = _is_client_connected() and \
11491156
not is_session_enabled()
@@ -1528,6 +1535,11 @@ def predict(model: xgb.Booster,
15281535
"""
15291536
os.environ.setdefault("RAY_IGNORE_UNHANDLED_ERRORS", "1")
15301537

1538+
if xgb is None:
1539+
raise ImportError(
1540+
"xgboost package is not installed. XGBoost-Ray WILL NOT WORK. "
1541+
"FIX THIS by running `pip install \"xgboost-ray[default]\"`.")
1542+
15311543
if _remote is None:
15321544
_remote = _is_client_connected() and \
15331545
not is_session_enabled()

xgboost_ray/matrix.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import uuid
33
from enum import Enum
44
from typing import Union, Optional, Tuple, Iterable, List, Dict, Sequence, \
5-
Callable, Type
5+
Callable, Type, TYPE_CHECKING
66

77
from ray.actor import ActorHandle
88

@@ -13,7 +13,6 @@
1313

1414
import numpy as np
1515
import pandas as pd
16-
import xgboost as xgb
1716

1817
import os
1918

@@ -43,6 +42,9 @@ class RayDataset:
4342
DataIter = object
4443
LEGACY_MATRIX = True
4544

45+
if TYPE_CHECKING:
46+
from xgboost_ray.xgb import xgboost as xgb
47+
4648
Data = Union[str, List[str], np.ndarray, pd.DataFrame, pd.Series, MLDataset]
4749

4850

@@ -194,7 +196,7 @@ def assert_enough_shards_for_actors(self, num_actors: int):
194196
# Pass per default
195197
pass
196198

197-
def update_matrix_properties(self, matrix: xgb.DMatrix):
199+
def update_matrix_properties(self, matrix: "xgb.DMatrix"):
198200
data_source = self.get_data_source()
199201
data_source.update_feature_names(matrix, self.feature_names)
200202

@@ -780,7 +782,7 @@ def unload_data(self):
780782
del self.refs[rank][name]
781783
self.loaded = False
782784

783-
def update_matrix_properties(self, matrix: xgb.DMatrix):
785+
def update_matrix_properties(self, matrix: "xgb.DMatrix"):
784786
self.loader.update_matrix_properties(matrix)
785787

786788
def __hash__(self):

xgboost_ray/tests/release/cluster_cpu.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ file_mounts_sync_continuously: false
3737
setup_commands:
3838
- pip install -U {{env["RAY_WHEEL"] | default("ray")}}
3939
- pip install dask pytest
40-
- pip install -U {{env["XGBOOST_RAY_PACKAGE"] | default("xgboost_ray")}}
40+
- pip install -U {{env["XGBOOST_RAY_PACKAGE"] | default("xgboost_ray")}}[default]

xgboost_ray/tests/release/setup_xgboost.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pip install pytest
55
pip uninstall -y xgboost_ray || true
66

77
# Install xgboost package
8-
pip install -U ${XGBOOST_RAY_PACKAGE:-xgboost_ray}
8+
pip install -U ${XGBOOST_RAY_PACKAGE:-xgboost_ray}[default]
99

1010
# Create test dataset
1111
sudo mkdir -p /data || true

0 commit comments

Comments
 (0)