-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
71 changes: 71 additions & 0 deletions
71
mllib/src/main/scala/org/apache/spark/mllib/rdd/BinaryTreeReducedRDD.scala
This file contains hidden or 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,71 @@ | ||
| package org.apache.spark.mllib.rdd | ||
|
|
||
| import org.apache.spark.{TaskContext, Partition, NarrowDependency} | ||
|
|
||
| import scala.reflect.ClassTag | ||
| import org.apache.spark.rdd.RDD | ||
|
|
||
| /** | ||
| * Represents a binary tree dependency, where partition `i` depends on partitions `2 * i` and | ||
| * `2 * i + 1` (if it exists) of the parent RDD. | ||
| * @param rdd parent RDD | ||
| * @tparam T value type | ||
| */ | ||
| private class BinaryTreeDependency[T](@transient rdd: RDD[T]) extends NarrowDependency(rdd) { | ||
|
|
||
| val n = rdd.partitions.size | ||
|
|
||
| override def getParents(partitionId: Int): Seq[Int] = { | ||
| val i1 = 2 * partitionId | ||
| val i2 = i1 + 1 | ||
| if (i2 < n) { | ||
| Seq(i1, i2) | ||
| } else { | ||
| Seq(i1) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private class BinaryTreeNodePartition( | ||
| override val index: Int, | ||
| val left: Partition, | ||
| val right: Option[Partition]) extends Partition { | ||
| } | ||
|
|
||
| private object BinaryTreeNodePartition { | ||
| def apply(rdd: RDD[_], i: Int): Partition = { | ||
| val n = rdd.partitions.size | ||
| val i1 = 2 * i | ||
| val i2 = i1 + 1 | ||
| if (i2 < n) { | ||
| new BinaryTreeNodePartition(i, rdd.partitions(i1), Some(rdd.partitions(i2))) | ||
| } else { | ||
| new BinaryTreeNodePartition(i, rdd.partitions(i1), None) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private[mllib] class BinaryTreeReducedRDD[T: ClassTag](rdd: RDD[T], f: (T, T) => T) | ||
| extends RDD[T](rdd.context, List(new BinaryTreeDependency(rdd))) { | ||
|
|
||
| override protected def getPartitions: Array[Partition] = { | ||
| Array.tabulate((rdd.partitions.size + 1) / 2)(i => BinaryTreeNodePartition(rdd, i)) | ||
| } | ||
|
|
||
| override def compute(split: Partition, context: TaskContext): Iterator[T] = { | ||
| val p = split.asInstanceOf[BinaryTreeNodePartition] | ||
| val iterLeft = rdd.compute(p.left, context) | ||
| val iterRight = if (p.right.isDefined) rdd.compute(p.right.get, context) else Iterator.empty | ||
| val iter = iterLeft ++ iterRight | ||
| if (iter.isEmpty) { | ||
| Iterator.empty | ||
| } else { | ||
| Iterator(iter.reduce(f)) | ||
| } | ||
| } | ||
|
|
||
| override protected def getPreferredLocations(split: Partition): Seq[String] = { | ||
| val p = split.asInstanceOf[BinaryTreeNodePartition] | ||
| rdd.preferredLocations(p.left) | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
mllib/src/main/scala/org/apache/spark/mllib/rdd/ButterflyReducedRDD.scala
This file contains hidden or 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,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) | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
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.
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 ?
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.
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
RangeDependencynow. Maybe it can help.Btw, I don't quite understand what do you mean by
hold references to them. Could you elaborate?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.
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 ?
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.
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.
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.
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.