Example of training with Dask on GPU
import
dask
as
dxgb
from
xgboost.dask
import
DaskDMatrix
def
using_dask_matrix
(
client
:
Client
,
X
:
,
y
:
)
->
:
dtrain
=
DaskDMatrix
(
client
,
X
,
y
)
output
=
dxgb
.
train
(
client
,
{
"tree_method"
:
"hist"
,
"device"
:
"cuda"
},
dtrain
,
num_boost_round
=
4
,
evals
=
[(
dtrain
,
"train"
)],
)
bst
=
output
[
"booster"
]
history
=
output
[
"history"
]
prediction
=
dxgb
.
predict
(
client
,
bst
,
dtrain
)
print
(
"Evaluation history:"
,
history
)
return
prediction
def
using_quantile_device_dmatrix
(
client
:
Client
,
X
:
,
y
:
)
->
:
"""`DaskQuantileDMatrix` is a data type specialized for `hist` tree methods for
reducing memory usage.
.. versionadded:: 1.2.0
"""
dtrain
=
dxgb
.
DaskQuantileDMatrix
(
client
,
X
,
y
)
output
=
dxgb
.
train
(
client
,
{
"tree_method"
:
"hist"
,
"device"
:
"cuda"
},
dtrain
,
num_boost_round
=
4
,
evals
=
[(
dtrain
,
"train"
)],
)
prediction
=
dxgb
.
predict
(
client
,
output
,
X
)
return
prediction
if
__name__
==
"__main__"
:
with
LocalCUDACluster
(
n_workers
=
2
,
threads_per_worker
=
4
)
as
cluster
:
with
Client
(
cluster
)
as
client
,
({
"array.backend"
:
"cupy"
}):
rng
=
(
1
)
m
=
2
**
18
n
=
100
X
=
rng
.
uniform
(
size
=
(
m
,
n
),
chunks
=
(
128
**
2
,
-
1
))
y
=
X
.
sum
(
axis
=
1
)
X
=
(
X
)
y
=
(
y
)
assert
isinstance
(
X
,
dask_cudf
.
DataFrame
)
assert
isinstance
(
y
,
dask_cudf
.
Series
)
print
(
"Using DaskQuantileDMatrix"
)
from_ddqdm
=
using_quantile_device_dmatrix
(
client
,
X
,
y
)
.
compute
()
print
(
"Using DMatrix"
)
from_dmatrix
=
using_dask_matrix
(
client
,
X
,
y
)
.
compute
()