-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
[jvm-packages] Support Ranker #10823
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ece0b9b
Support ranker
wbo4958 eeca573
test the group col which should be sorted in each partition
wbo4958 1c8c3b9
sort partition for gpu
wbo4958 da969b7
Merge remote-tracking branch 'upstream/master' into ranker
wbo4958 b62bb8c
Merge branch 'master' into ranker
trivialfis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...packages/xgboost4j-spark/src/main/scala/ml/dmlc/xgboost4j/scala/spark/XGBoostRanker.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
Copyright (c) 2024 by Contributors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package ml.dmlc.xgboost4j.scala.spark | ||
|
||
import org.apache.spark.ml.{PredictionModel, Predictor} | ||
import org.apache.spark.ml.linalg.Vector | ||
import org.apache.spark.ml.param.ParamMap | ||
import org.apache.spark.ml.util.{DefaultParamsReadable, Identifiable, MLReadable, MLReader} | ||
import org.apache.spark.ml.xgboost.SparkUtils | ||
import org.apache.spark.sql.Dataset | ||
import ml.dmlc.xgboost4j.scala.Booster | ||
import ml.dmlc.xgboost4j.scala.spark.XGBoostRanker._uid | ||
import ml.dmlc.xgboost4j.scala.spark.params.HasGroupCol | ||
import ml.dmlc.xgboost4j.scala.spark.params.LearningTaskParams.RANKER_OBJS | ||
import org.apache.spark.sql.types.{DataType, DoubleType, StructType} | ||
|
||
class XGBoostRanker(override val uid: String, | ||
private val xgboostParams: Map[String, Any]) | ||
extends Predictor[Vector, XGBoostRanker, XGBoostRankerModel] | ||
with XGBoostEstimator[XGBoostRanker, XGBoostRankerModel] with HasGroupCol { | ||
|
||
def this() = this(_uid, Map[String, Any]()) | ||
|
||
def this(uid: String) = this(uid, Map[String, Any]()) | ||
|
||
def this(xgboostParams: Map[String, Any]) = this(_uid, xgboostParams) | ||
|
||
def setGroupCol(value: String): XGBoostRanker = set(groupCol, value) | ||
|
||
xgboost2SparkParams(xgboostParams) | ||
|
||
/** | ||
* Validate the parameters before training, throw exception if possible | ||
*/ | ||
override protected[spark] def validate(dataset: Dataset[_]): Unit = { | ||
super.validate(dataset) | ||
|
||
require(isDefinedNonEmpty(groupCol), "groupCol needs to be set") | ||
|
||
// If the objective is set explicitly, it must be in RANKER_OBJS | ||
if (isSet(objective)) { | ||
val tmpObj = getObjective | ||
require(RANKER_OBJS.contains(tmpObj), | ||
s"Wrong objective for XGBoostRanker, supported objs: ${RANKER_OBJS.mkString(",")}") | ||
} else { | ||
setObjective("rank:ndcg") | ||
} | ||
} | ||
|
||
/** | ||
* Preprocess the dataset to meet the xgboost input requirement | ||
* | ||
* @param dataset | ||
* @return | ||
*/ | ||
override private[spark] def preprocess(dataset: Dataset[_]): (Dataset[_], ColumnIndices) = { | ||
val (output, columnIndices) = super.preprocess(dataset) | ||
(output.sortWithinPartitions(getGroupCol), columnIndices) | ||
} | ||
|
||
override protected def createModel( | ||
booster: Booster, | ||
summary: XGBoostTrainingSummary): XGBoostRankerModel = { | ||
new XGBoostRankerModel(uid, booster, Option(summary)) | ||
} | ||
|
||
override protected def validateAndTransformSchema( | ||
schema: StructType, | ||
fitting: Boolean, | ||
featuresDataType: DataType): StructType = | ||
SparkUtils.appendColumn(schema, $(predictionCol), DoubleType) | ||
} | ||
|
||
object XGBoostRanker extends DefaultParamsReadable[XGBoostRanker] { | ||
private val _uid = Identifiable.randomUID("xgbranker") | ||
} | ||
|
||
class XGBoostRankerModel private[ml](val uid: String, | ||
val nativeBooster: Booster, | ||
val summary: Option[XGBoostTrainingSummary] = None) | ||
extends PredictionModel[Vector, XGBoostRankerModel] | ||
with RankerRegressorBaseModel[XGBoostRankerModel] with HasGroupCol { | ||
|
||
def this(uid: String) = this(uid, null) | ||
|
||
def setGroupCol(value: String): XGBoostRankerModel = set(groupCol, value) | ||
|
||
override def copy(extra: ParamMap): XGBoostRankerModel = { | ||
val newModel = copyValues(new XGBoostRankerModel(uid, nativeBooster, summary), extra) | ||
newModel.setParent(parent) | ||
} | ||
|
||
override def predict(features: Vector): Double = { | ||
val values = predictSingleInstance(features) | ||
values(0) | ||
} | ||
} | ||
|
||
object XGBoostRankerModel extends MLReadable[XGBoostRankerModel] { | ||
override def read: MLReader[XGBoostRankerModel] = new ModelReader | ||
|
||
private class ModelReader extends XGBoostModelReader[XGBoostRankerModel] { | ||
override def load(path: String): XGBoostRankerModel = { | ||
val xgbModel = loadBooster(path) | ||
val meta = SparkUtils.loadMetadata(path, sc) | ||
val model = new XGBoostRankerModel(meta.uid, xgbModel, None) | ||
meta.getAndSetParams(model) | ||
model | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this operation interact with spark-rapids plugin if enabled? Any implications on GPU memory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this preprocess even get called if plugin is enabled? If not, partition might not be sorted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. Fixed this issue. Please help review it again. Thx very much.