zhengruifeng commented on a change in pull request #26803: [SPARK-30178][ML]
RobustScaler support large numFeatures
URL: https://github.com/apache/spark/pull/26803#discussion_r355831173
##########
File path: mllib/src/main/scala/org/apache/spark/ml/feature/RobustScaler.scala
##########
@@ -147,49 +146,43 @@ class RobustScaler (override val uid: String)
override def fit(dataset: Dataset[_]): RobustScalerModel = {
transformSchema(dataset.schema, logging = true)
- val localRelativeError = $(relativeError)
- val summaries = dataset.select($(inputCol)).rdd.map {
- case Row(vec: Vector) => vec
- }.mapPartitions { iter =>
- var agg: Array[QuantileSummaries] = null
- while (iter.hasNext) {
- val vec = iter.next()
- if (agg == null) {
- agg = Array.fill(vec.size)(
- new QuantileSummaries(QuantileSummaries.defaultCompressThreshold,
localRelativeError))
- }
- require(vec.size == agg.length,
- s"Number of dimensions must be ${agg.length} but got ${vec.size}")
- var i = 0
- while (i < vec.size) {
- agg(i) = agg(i).insert(vec(i))
- i += 1
- }
- }
-
- if (agg == null) {
- Iterator.empty
- } else {
- Iterator.single(agg.map(_.compress))
- }
- }.treeReduce { (agg1, agg2) =>
- require(agg1.length == agg2.length)
- var i = 0
- while (i < agg1.length) {
- agg1(i) = agg1(i).merge(agg2(i))
- i += 1
- }
- agg1
- }
+ val vectors = dataset.select($(inputCol)).rdd.map { case Row(vec: Vector)
=> vec }
+ val numFeatures = MetadataUtils.getNumFeatures(dataset.schema($(inputCol)))
+ .getOrElse(vectors.first().size)
- val (range, median) = summaries.map { s =>
- (s.query($(upper)).get - s.query($(lower)).get,
- s.query(0.5).get)
- }.unzip
+ val localRelativeError = $(relativeError)
+ val localUpper = $(upper)
+ val localLower = $(lower)
+
+ val collected = vectors.flatMap { vec =>
+ require(vec.size == numFeatures,
+ s"Number of dimensions must be $numFeatures but got ${vec.size}")
+ Iterator.range(0, numFeatures).map { i => (i, vec(i)) }
+ }.aggregateByKey(
Review comment:
> It is a bottleneck to collect the whole Array[QuantileSummaries] from
executors
I think I did not make it clear, it is not only a bottleneck for the driver,
but also a bottleneck in the executors, since the `Array[QuantileSummaries]` is
too large.
> If I have relative low-dimensional vectors -- say just 2 dimensions --
then this will require aggregating half of all the data in just two tasks each.
I guess the parallelism is still #partition, since according to the doc of
`aggregateByKey`:
"The former operation (seqOp) is used for merging values within a partition,
and the latter (combOp) is used for merging values between partitions."
So the `insert` op are executed with parallelism=#partition, and the `merge`
op are with parallelism=#feature.
> You can map everything to a (0, summarizer) pair, aggregateByKey, do the
final aggregation in one remote executor, then map out the medians and return
just that. But it just pushes the problem to 1 executor.
Yes, this may still cause OOM when `--executor-memory` is not enough.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]