-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-6685][MLLIB]Use DSYRK to compute AtA in ALS #13891
Closed
Closed
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8fb4a82
add dsyrk to ALS
hqzizania 7e3d238
implicitprefs ut fails fix
hqzizania 3607bdc
use "while" loop instead of "for"
hqzizania 56194eb
add unit test for dostack ALS
hqzizania dc4f4ba
reset threshold values for doStack and remove UT
hqzizania d29fd67
add threshold param to ALS
hqzizania 294164d
nit fix
hqzizania 513e791
nit
hqzizania f56b586
Merge remote-tracking branch 'origin/master' into ALSdsyrk
hqzizania 1081e64
mima fix
hqzizania a6b5a16
oops
hqzizania 2077457
solve mima failure
hqzizania 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -625,6 +625,7 @@ object ALS extends DefaultParamsReadable[ALS] with Logging { | |
val atb = new Array[Double](k) | ||
|
||
private val da = new Array[Double](k) | ||
private val ata2 = new Array[Double](k * k) | ||
private val upper = "U" | ||
|
||
private def copyToDouble(a: Array[Float]): Unit = { | ||
|
@@ -635,6 +636,15 @@ object ALS extends DefaultParamsReadable[ALS] with Logging { | |
} | ||
} | ||
|
||
private def copyToTri(): Unit = { | ||
var ii = 0 | ||
for(i <- 0 until k) | ||
for(j <- 0 to i) { | ||
ata(ii) += ata2(i * k + j) | ||
ii += 1 | ||
} | ||
} | ||
|
||
/** Adds an observation. */ | ||
def add(a: Array[Float], b: Double, c: Double = 1.0): this.type = { | ||
require(c >= 0.0) | ||
|
@@ -647,6 +657,15 @@ object ALS extends DefaultParamsReadable[ALS] with Logging { | |
this | ||
} | ||
|
||
/** Adds a stack of observations. */ | ||
def addStack(a: Array[Double], b: Array[Double], n: Int): this.type = { | ||
require(a.length == n * k) | ||
blas.dsyrk(upper, "N", k, n, 1.0, a, k, 1.0, ata2, k) | ||
copyToTri() | ||
blas.dgemv("N", k, n, 1.0, a, k, b, 1, 1.0, atb, 1) | ||
this | ||
} | ||
|
||
/** Merges another normal equation object. */ | ||
def merge(other: NormalEquation): this.type = { | ||
require(other.k == k) | ||
|
@@ -658,6 +677,7 @@ object ALS extends DefaultParamsReadable[ALS] with Logging { | |
/** Resets everything to zero, which should be called after each solve. */ | ||
def reset(): Unit = { | ||
ju.Arrays.fill(ata, 0.0) | ||
ju.Arrays.fill(ata2, 0.0) | ||
ju.Arrays.fill(atb, 0.0) | ||
} | ||
} | ||
|
@@ -1296,6 +1316,9 @@ object ALS extends DefaultParamsReadable[ALS] with Logging { | |
} | ||
var i = srcPtrs(j) | ||
var numExplicits = 0 | ||
val doStack = if (srcPtrs(j + 1) - srcPtrs(j) > 10) true else false | ||
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.
|
||
val srcFactorBuffer = mutable.ArrayBuilder.make[Double] | ||
val bBuffer = mutable.ArrayBuilder.make[Double] | ||
while (i < srcPtrs(j + 1)) { | ||
val encoded = srcEncodedIndices(i) | ||
val blockId = srcEncoder.blockId(encoded) | ||
|
@@ -1313,11 +1336,23 @@ object ALS extends DefaultParamsReadable[ALS] with Logging { | |
ls.add(srcFactor, (c1 + 1.0) / c1, c1) | ||
} | ||
} else { | ||
ls.add(srcFactor, rating) | ||
numExplicits += 1 | ||
if (doStack) { | ||
bBuffer += rating | ||
var ii = 0 | ||
while(ii < srcFactor.length) { | ||
srcFactorBuffer += srcFactor(ii) | ||
ii += 1 | ||
} | ||
} else { | ||
ls.add(srcFactor, rating) | ||
} | ||
} | ||
i += 1 | ||
} | ||
if (!implicitPrefs && doStack) { | ||
ls.addStack(srcFactorBuffer.result(), bBuffer.result(), numExplicits) | ||
} | ||
// Weight lambda by the number of explicit ratings based on the ALS-WR paper. | ||
dstFactors(j) = solver.solve(ls, numExplicits * regParam) | ||
j += 1 | ||
|
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.
I think this might fail the style check for missing space before the paren.
Also, I think the received wisdom is that for loops are slow in Scala? if this is performance-critical, you may convert to while loops. Also you can cache i*k from the outer loop below in the inner loop