Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pyspark] sort qid for SparkRanker #8497

Merged
merged 2 commits into from
Dec 2, 2022
Merged

[pyspark] sort qid for SparkRanker #8497

merged 2 commits into from
Dec 2, 2022

Conversation

wbo4958
Copy link
Contributor

@wbo4958 wbo4958 commented Nov 30, 2022

To fix #8491

@wbo4958
Copy link
Contributor Author

wbo4958 commented Nov 30, 2022

@WeichenXu123 @trivialfis please help to review it.

@wbo4958 wbo4958 mentioned this pull request Nov 30, 2022
9 tasks
@hcho3 hcho3 changed the title [pyspark] sort qid for SparkRandker [pyspark] sort qid for SparkRanker Nov 30, 2022
@@ -729,6 +729,10 @@ def _fit(self, dataset):
else:
dataset = dataset.repartition(num_workers)

if self.isDefined(self.qid_col) and self.getOrDefault(self.qid_col):
# XGBoost requires qid to be sorted for each partition
dataset = dataset.sortWithinPartitions(alias.qid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add ascending=True explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

(Vectors.sparse(3, {1: 8.0, 2: 9.5}), 2, 1),
(Vectors.dense(1.0, 2.0, 3.0), 0, 0),
(Vectors.dense(4.0, 5.0, 6.0), 1, 0),
(Vectors.dense(9.0, 4.0, 8.0), 2, 0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do we need hardcode so long data list ?
we can hardcode 4 rows and use [ ... ] * 100 instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

ranker = SparkXGBRanker(qid_col="qid", num_workers=2)
assert ranker.getOrDefault(ranker.objective) == "rank:pairwise"
model = ranker.fit(self.ranker_df_train_1)
model.transform(self.ranker_df_test).collect()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the purpose of this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to test if the SparkRanker will throw exception

)
self.ranker_df_train_1 = self.session.createDataFrame(
[
(Vectors.sparse(3, {1: 1.0, 2: 5.5}), 0, 9),
Copy link
Member

@trivialfis trivialfis Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you produce this data and the expected result? Please try not to use hardcoded results.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the qid is the descending order. without the fix, it will throw exception ../src/data/data.cc:486: Check failed: non_dec: qid must be sorted in non-decreasing order along with data.

@wbo4958
Copy link
Contributor Author

wbo4958 commented Dec 1, 2022

@hcho3 please help to merge it. thx

@hcho3 hcho3 merged commit 8e41ad2 into dmlc:master Dec 2, 2022
@wbo4958 wbo4958 deleted the ranker branch December 2, 2022 01:09
@trivialfis
Copy link
Member

@wbo4958 Could you please change the tests to NOT use hardcoded results?

@trivialfis
Copy link
Member

#8497 (comment)

@wbo4958
Copy link
Contributor Author

wbo4958 commented Dec 2, 2022

#8497 (comment)

Hi @trivialfis, For this case, the test I added is to check if the pyspark application will be crashed, so it's ok, I think, to hardcode the data. Since I think the data is so straightforward to show the scenario which can crash the process.

pred_result = model.transform(self.ranker_df_test).collect()

for row in pred_result:
assert np.isclose(row.prediction, row.expected_prediction, rtol=1e-3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wbo4958 This is not only checking exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ... That's a headache, I'm blocked by these tests and don't know how to recreate them...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we can have the following PR to refactor these tests by not hardcoding them

trivialfis pushed a commit to trivialfis/xgboost that referenced this pull request Dec 6, 2022
* [pyspark] sort qid for SparkRandker

* resolve comments
trivialfis added a commit that referenced this pull request Dec 6, 2022
* [pyspark] sort qid for SparkRandker

* resolve comments

Co-authored-by: Bobby Wang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pyspark] SparkXGBRanker does not work on dataframe with multiple partitions
4 participants