-
Notifications
You must be signed in to change notification settings - Fork 29k
[WIP][SPARK-1485][MLLIB] Implement Butterfly AllReduce #506
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.spark.mllib.rdd | ||
|
|
||
| import scala.reflect.ClassTag | ||
|
|
||
| import org.apache.spark.{TaskContext, Partition} | ||
| import org.apache.spark.rdd.RDD | ||
|
|
||
| /** A partition in a butterfly-reduced RDD. */ | ||
| private case class ButterflyReducedRDDPartition( | ||
| override val index: Int, | ||
| source: Partition, | ||
| target: Partition) extends Partition | ||
|
|
||
| /** | ||
| * Butterfly-reduced RDD. | ||
| */ | ||
| private[mllib] class ButterflyReducedRDD[T: ClassTag]( | ||
| @transient rdd: RDD[T], | ||
| reducer: (T, T) => T, | ||
| @transient offset: Int) extends RDD[T](rdd) { | ||
|
|
||
| /** Computes the target partition. */ | ||
| private def targetPartition(i: Int): Partition = { | ||
| val j = (i + offset) % rdd.partitions.size | ||
| rdd.partitions(j) | ||
| } | ||
|
|
||
| override def getPartitions: Array[Partition] = { | ||
| rdd.partitions.zipWithIndex.map { case (part, i) => | ||
| ButterflyReducedRDDPartition(i, part, targetPartition(i)) | ||
| } | ||
| } | ||
|
|
||
| override def compute(s: Partition, context: TaskContext): Iterator[T] = { | ||
| val pair = s.asInstanceOf[ButterflyReducedRDDPartition] | ||
| Iterator((firstParent[T].iterator(pair.source, context) ++ | ||
| firstParent[T].iterator(pair.target, context)).reduce(reducer)) | ||
| } | ||
|
|
||
| override def getPreferredLocations(s: Partition): Seq[String] = { | ||
| rdd.preferredLocations(s.asInstanceOf[ButterflyReducedRDDPartition].source) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.spark.mllib.rdd | ||
|
|
||
| import scala.reflect.ClassTag | ||
|
|
||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.{TaskContext, Partition} | ||
|
|
||
| /** | ||
| * Represents an RDD obtained from partition slicing of its parent RDD. | ||
| */ | ||
| private[mllib] class PartitionSlicingRDD[T: ClassTag]( | ||
| @transient rdd: RDD[T], | ||
| @transient slice: Seq[Int]) extends RDD[T](rdd) { | ||
|
|
||
| override def getPartitions: Array[Partition] = { | ||
| slice.map(i => rdd.partitions(i)).toArray | ||
| } | ||
|
|
||
| override def compute(s: Partition, context: TaskContext): Iterator[T] = { | ||
| firstParent[T].iterator(s, context) | ||
| } | ||
|
|
||
| override def getPreferredLocations(s: Partition): Seq[String] = { | ||
| rdd.preferredLocations(s) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,54 @@ class RDDFunctions[T: ClassTag](self: RDD[T]) { | |
| new SlidingRDD[T](self, windowSize) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns an RDD with the specified slice of partitions. | ||
| */ | ||
| def slicePartitions(slice: Seq[Int]): RDD[T] = { | ||
| new PartitionSlicingRDD(self, slice) | ||
| } | ||
|
|
||
| /** | ||
| * Computes the all-reduced RDD of the parent RDD, which has the same number of partitions and | ||
| * locality information as its parent RDD. Each partition contains only one record, which is the | ||
| * same as calling `RDD#reduce` on its parent RDD. | ||
| * | ||
| * @param f reducer | ||
| * @return all-reduced RDD | ||
| */ | ||
| def allReduce(f: (T, T) => T): RDD[T] = { | ||
| val numPartitions = self.partitions.size | ||
| require(numPartitions > 0, "Parent RDD does not have any partitions.") | ||
| val nextPowerOfTwo = { | ||
| var i = 0 | ||
| while ((numPartitions >> i) > 0) { | ||
| i += 1 | ||
| } | ||
| 1 << i | ||
| } | ||
| var butterfly = self.mapPartitions( (iter) => | ||
| Iterator(iter.reduce(f)), | ||
| preservesPartitioning = true | ||
| ).cache() | ||
|
|
||
| if (nextPowerOfTwo > numPartitions) { | ||
| val padding = self.context.parallelize(Seq.empty[T], nextPowerOfTwo - numPartitions) | ||
| butterfly = butterfly.union(padding) | ||
| } | ||
|
|
||
| var offset = nextPowerOfTwo >> 1 | ||
| while (offset > 0) { | ||
| butterfly = new ButterflyReducedRDD[T](butterfly, f, offset).cache() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO its a little risky to cache all the iterations of this loop in terms of memory usage. The right thing to do is to probably hold references to them and unpersist at the end ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each partition will be visited twice in a butterfly step. If the previous stage is not cached or falls out cache, the cost is huge. I'm looking at the Btw, I don't quite understand what do you mean by
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we create a new RDD at each step we store the RDD references in say a ArrayBuffer. After the loop exits, we call unpersist on all the older RDDs. This doesn't work very well with lazy transformations, though allReduce doesn't need to be lazy ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I thought about doing that. I prefer lazy transformations, given the fact that old cached RDDs will be cleared from memory for new ones. But I am not sure whether cleaning is reliable.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the default clean up policy is still LRU as far as I know. In that case you could see weird things like RDDs cached before the ButterflyRDD getting evicted first. What we need is an interface to say unpersist some RDDs after they have been computed upon, but I don't think we have that yet. |
||
| offset >>= 1 | ||
| } | ||
|
|
||
| if (nextPowerOfTwo > numPartitions) { | ||
| new PartitionSlicingRDD(butterfly, 0 until numPartitions) | ||
| } else { | ||
| butterfly | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private[mllib] | ||
|
|
||
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.
Isn't this the same as PartitionPruningRDD ?
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.
Yes, I didn't know there is one.