-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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-19922][ML] small speedups to findSynonyms #17263
Conversation
Currently generating synonyms using a model with 3m words is painfully slow. These efficiencies have sped things up by more than 17%. Address a few issues in the findSynonyms logic: 1) no need to zero out the cosineVec array each time, since default value for float arrays is 0.0f. This should offer some nice speedups 2) use floats throughout. The conversion to Doubles before doing the priorityQueue is totally superflous, since all the computations are done using floats anyway 3) convert the slow for(i <- cosVec.indices), which combines a scala closure with a Range, to an ugly but faster while loop
Test build #74393 has finished for PR 17263 at commit
|
Test build #74394 has finished for PR 17263 at commit
|
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 that's right, given that only BLAS.s*
routines are used (= single-precision = float). A few minor suggestions here.
@@ -570,7 +570,7 @@ class Word2VecModel private[spark] ( | |||
require(num > 0, "Number of similar words should > 0") | |||
|
|||
val fVector = vector.toArray.map(_.toFloat) | |||
val cosineVec = Array.fill[Float](numWords)(0) | |||
val cosineVec = new Array[Float](numWords) // default value is 0.0f |
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.
It's fine, it doesn't need a comment
if (norm == 0.0) { | ||
cosVec(ind) = 0.0 | ||
cosineVec(i) = 0 |
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'd still write 0.0f for clarity but it's no big deal. I guess we can write norm == 0.0f
too
also, remove comment about default value, it's not needed
Test build #74407 has finished for PR 17263 at commit
|
Merged to master |
Currently generating synonyms using a large model (I've tested with 3m words) is very slow. These efficiencies have sped things up for us by ~17%
I wasn't sure if such small changes were worthy of a jira, but the guidelines seemed to suggest that that is the preferred approach
What changes were proposed in this pull request?
Address a few small issues in the findSynonyms logic:
Array.fill
to zero out thecosineVec
array. The default float value in Scala and Java is 0.0f, so explicitly setting the values to zero is not neededpriorityQueue
is totally superfluous, since all the similarity computations are done using Floats anyway. Creating a second large array just serves to put extra strain on the GCfor(i <- cosVec.indices)
to an ugly, but faster,while
loopThese efficiencies are really only apparent when working with a large model
How was this patch tested?
Existing unit tests + some in-house tests to time the difference
cc @jkbradley @MLnick @srowen