Learning to rank with the Dask Interface
argparse
import
os
from
contextlib
import
from
typing
import
import
dask
import
numpy
as
np
from
dask
import
dataframe
as
dd
from
distributed
import
,
,
from
sklearn.datasets
import
from
xgboost
import
dask
as
dxgb
def
load_mslr_10k
(
device
:
str
,
data_path
:
str
,
cache_path
:
str
)
->
tuple
[
,
,
]:
"""Load the MSLR10k dataset from data_path and save parquet files in the cache_path."""
root_path
=
(
args
.
data
)
cache_path
=
(
args
.
cache
)
fold
=
1
if
not
(
cache_path
):
(
cache_path
)
fold_path
=
(
root_path
,
f
"Fold
{
fold
}
"
)
train_path
=
(
fold_path
,
"train.txt"
)
valid_path
=
(
fold_path
,
"vali.txt"
)
test_path
=
(
fold_path
,
"test.txt"
)
X_train
,
y_train
,
qid_train
=
(
train_path
,
query_id
=
True
,
dtype
=
)
columns
=
[
f
"f
{
i
}
"
for
i
in
range
(
X_train
.
shape
[
1
])]
X_train
=
(
X_train
.
toarray
(),
columns
=
columns
)
y_train
=
y_train
.
astype
(
)
qid_train
=
qid_train
.
astype
(
)
X_train
[
"y"
]
=
(
y_train
)
X_train
[
"qid"
]
=
(
qid_train
)
X_train
.
to_parquet
(
(
cache_path
,
"train"
),
engine
=
"pyarrow"
)
X_valid
,
y_valid
,
qid_valid
=
(
valid_path
,
query_id
=
True
,
dtype
=
)
X_valid
=
(
X_valid
.
toarray
(),
columns
=
columns
)
y_valid
=
y_valid
.
astype
(
)
qid_valid
=
qid_valid
.
astype
(
)
X_valid
[
"y"
]
=
(
y_valid
)
X_valid
[
"qid"
]
=
(
qid_valid
)
X_valid
.
to_parquet
(
(
cache_path
,
"valid"
),
engine
=
"pyarrow"
)
X_test
,
y_test
,
qid_test
=
(
test_path
,
query_id
=
True
,
dtype
=
)
X_test
=
(
X_test
.
toarray
(),
columns
=
columns
)
y_test
=
y_test
.
astype
(
)
qid_test
=
qid_test
.
astype
(
)
X_test
[
"y"
]
=
(
y_test
)
X_test
[
"qid"
]
=
(
qid_test
)
X_test
.
to_parquet
(
(
cache_path
,
"test"
),
engine
=
"pyarrow"
)
df_train
=
(
(
cache_path
,
"train"
),
calculate_divisions
=
True
)
df_valid
=
(
(
cache_path
,
"valid"
),
calculate_divisions
=
True
)
df_test
=
(
(
cache_path
,
"test"
),
calculate_divisions
=
True
)
return
df_train
,
df_valid
,
df_test
def
ranking_demo
(
client
:
,
args
:
)
->
None
:
"""Learning to rank with data sorted locally."""
df_tr
,
df_va
,
_
=
load_mslr_10k
(
args
.
device
,
args
.
data
,
args
.
cache
)
X_train
:
=
df_tr
[
df_tr
.
columns
.
difference
([
"y"
,
"qid"
])]
y_train
=
df_tr
[[
"y"
,
"qid"
]]
Xy_train
=
dxgb
.
DaskQuantileDMatrix
(
client
,
X_train
,
y_train
.
y
,
qid
=
y_train
.
qid
)
X_valid
:
=
df_va
[
df_va
.
columns
.
difference
([
"y"
,
"qid"
])]
y_valid
=
df_va
[[
"y"
,
"qid"
]]
Xy_valid
=
dxgb
.
DaskQuantileDMatrix
(
client
,
X_valid
,
y_valid
.
y
,
qid
=
y_valid
.
qid
,
ref
=
Xy_train
)
dxgb
.
train
(
client
,
{
"objective"
:
"rank:ndcg"
,
"device"
:
args
.
device
},
Xy_train
,
evals
=
[(
Xy_train
,
"Train"
),
(
Xy_valid
,
"Valid"
)],
num_boost_round
=
100
,
)
def
ranking_wo_split_demo
(
client
:
,
args
:
)
->
None
:
"""Learning to rank with data partitioned according to query groups."""
df_tr
,
df_va
,
df_te
=
load_mslr_10k
(
args
.
device
,
args
.
data
,
args
.
cache
)
X_tr
=
df_tr
[
df_tr
.
columns
.
difference
([
"y"
,
"qid"
])]
X_va
=
df_va
[
df_va
.
columns
.
difference
([
"y"
,
"qid"
])]
ltr
=
dxgb
.
DaskXGBRanker
(
allow_group_split
=
False
,
device
=
args
.
device
)
ltr
.
client
=
client
ltr
=
ltr
.
fit
(
X_tr
,
df_tr
.
y
,
qid
=
df_tr
.
qid
,
eval_set
=
[(
X_tr
,
df_tr
.
y
),
(
X_va
,
df_va
.
y
)],
eval_qid
=
[
df_tr
.
qid
,
df_va
.
qid
],
verbose
=
True
,
)
df_te
=
df_te
.
persist
()
([
df_te
])
X_te
=
df_te
[
df_te
.
columns
.
difference
([
"y"
,
"qid"
])]
predt
=
ltr
.
predict
(
X_te
)
y
=
client
.
compute
(
df_te
.
y
)
([
predt
,
y
])
@contextmanager
def
gen_client
(
device
:
str
)
->
[
,
None
,
None
]:
match
device
:
case
"cuda"
:
from
dask_cuda
import
LocalCUDACluster
with
LocalCUDACluster
()
as
cluster
:
with
(
cluster
)
as
client
:
with
(
{
"array.backend"
:
"cupy"
,
"dataframe.backend"
:
"cudf"
,
}
):
yield
client
case
"cpu"
:
with
()
as
cluster
:
with
(
cluster
)
as
client
:
yield
client
if
__name__
==
"__main__"
:
parser
=
(
description
=
"Demonstration of learning to rank using XGBoost."
)
parser
.
add_argument
(
"--data"
,
type
=
str
,
help
=
"Root directory of the MSLR-WEB10K data."
,
required
=
True
,
)
parser
.
add_argument
(
"--cache"
,
type
=
str
,
help
=
"Directory for caching processed data."
,
required
=
True
,
)
parser
.
add_argument
(
"--device"
,
choices
=
[
"cpu"
,
"cuda"
],
default
=
"cpu"
)
parser
.
add_argument
(
"--no-split"
,
action
=
"store_true"
,
help
=
"Flag to indicate query groups should not be split."
,
)
args
=
parser
.
parse_args
()
with
gen_client
(
args
.
device
)
as
client
:
if
args
.
no_split
:
ranking_wo_split_demo
(
client
,
args
)
else
:
ranking_demo
(
client
,
args
)