Github user Krimit commented on a diff in the pull request:
https://github.com/apache/spark/pull/16607#discussion_r96327575
--- Diff: mllib/src/main/scala/org/apache/spark/ml/feature/Word2Vec.scala
---
@@ -302,16 +303,36 @@ class Word2VecModel private[ml] (
@Since("1.6.0")
object Word2VecModel extends MLReadable[Word2VecModel] {
+ private case class Data(word: String, vector: Seq[Float])
+
private[Word2VecModel]
class Word2VecModelWriter(instance: Word2VecModel) extends MLWriter {
- private case class Data(wordIndex: Map[String, Int], wordVectors:
Seq[Float])
-
override protected def saveImpl(path: String): Unit = {
DefaultParamsWriter.saveMetadata(instance, path, sc)
- val data = Data(instance.wordVectors.wordIndex,
instance.wordVectors.wordVectors.toSeq)
+
+ val wordVectors = instance.wordVectors.getVectors
+ val dataArray = wordVectors.toSeq.map { case (word, vector) =>
Data(word, vector) }
val dataPath = new Path(path, "data").toString
-
sparkSession.createDataFrame(Seq(data)).repartition(1).write.parquet(dataPath)
+ sparkSession.createDataFrame(dataArray)
+ .repartition(calculateNumberOfPartitions)
+ .write
+ .parquet(dataPath)
+ }
+
+ val FloatSize = 4
+ val AverageWordSize = 15
+ def calculateNumberOfPartitions(): Int = {
+ // [SPARK-11994] - We want to partition the model in partitions
smaller than
+ // spark.kryoserializer.buffer.max
+ val bufferSizeInBytes = Utils.byteStringAsBytes(
+ sc.conf.get("spark.kryoserializer.buffer.max", "64m"))
+ // Calculate the approximate size of the model.
+ // Assuming an average word size of 15 bytes, the formula is:
+ // (floatSize * vectorSize + 15) * numWords
+ val numWords = instance.wordVectors.wordIndex.size
+ val approximateSizeInBytes = (FloatSize * instance.getVectorSize +
AverageWordSize) * numWords
+ ((approximateSizeInBytes / bufferSizeInBytes) + 1).toInt
--- End diff --
This is basically copied from here:
https://github.com/apache/spark/blob/master/mllib/src/main/scala/org/apache/spark/mllib/feature/Word2Vec.scala#L661-L671.
Could you please clarify what you mean by rounding it?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]