Skip to content

Commit

Permalink
Test.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Feb 25, 2025
1 parent 822ced0 commit fe76daf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
27 changes: 27 additions & 0 deletions python-package/xgboost/testing/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,30 @@ def run_normalization(device: str) -> None:
ltr.fit(X, y, qid=qid, eval_set=[(X, y)], eval_qid=[qid])
e1 = ltr.evals_result()
assert e1["validation_0"]["ndcg@32"][-1] > e0["validation_0"]["ndcg@32"][-1]


def run_score_normalization(device: str, objective: str) -> None:
print()
if objective == "rank:map":
# Binary relevance
X, y, qid, _ = tm.make_ltr(4096, 4, 64, max_rel=1)
else:
X, y, qid, _ = tm.make_ltr(4096, 4, 64, 3)
ltr = xgb.XGBRanker(objective=objective, n_estimators=4, device=device)
ltr.fit(X, y, qid=qid, eval_set=[(X, y)], eval_qid=[qid])
e0 = ltr.evals_result()

ltr = xgb.XGBRanker(
objective="rank:pairwise",
n_estimators=4,
device=device,
lambdarank_score_normalization=False,
)
ltr.fit(X, y, qid=qid, eval_set=[(X, y)], eval_qid=[qid])
e1 = ltr.evals_result()

m0, m1 = (
list(e0["validation_0"].values())[-1][-1],
list(e1["validation_0"].values())[-1][-1],
)
assert m0 != m1
7 changes: 6 additions & 1 deletion tests/python-gpu/test_gpu_ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import xgboost
from xgboost import testing as tm
from xgboost.testing.ranking import run_normalization
from xgboost.testing.ranking import run_normalization, run_score_normalization

pytestmark = tm.timeout(30)

Expand Down Expand Up @@ -131,3 +131,8 @@ def test_with_mq2008(objective, metric) -> None:

def test_normalization() -> None:
run_normalization("cuda")


@pytest.mark.parametrize("objective", ["rank:pairwise", "rank:ndcg", "rank:map"])
def test_score_normalization(objective: str) -> None:
run_score_normalization("cuda", objective)
7 changes: 6 additions & 1 deletion tests/python/test_ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from xgboost import testing as tm
from xgboost.testing.data import RelDataCV, simulate_clicks, sort_ltr_samples
from xgboost.testing.params import lambdarank_parameter_strategy
from xgboost.testing.ranking import run_normalization
from xgboost.testing.ranking import run_normalization, run_score_normalization


def test_ndcg_custom_gain():
Expand Down Expand Up @@ -213,6 +213,11 @@ def test_normalization() -> None:
run_normalization("cpu")


@pytest.mark.parametrize("objective", ["rank:pairwise", "rank:ndcg", "rank:map"])
def test_score_normalization(objective: str) -> None:
run_score_normalization("cpu", objective)


class TestRanking:
@classmethod
def setup_class(cls):
Expand Down

0 comments on commit fe76daf

Please sign in to comment.