diff --git a/mllib/src/main/scala/org/apache/spark/mllib/feature/PCA.scala b/mllib/src/main/scala/org/apache/spark/mllib/feature/PCA.scala index aaecfa8d45dc0869fb5b4b78894ca352ae92ca10..a01503f4b80a626a26bf5147df97e1162e230015 100644 --- a/mllib/src/main/scala/org/apache/spark/mllib/feature/PCA.scala +++ b/mllib/src/main/scala/org/apache/spark/mllib/feature/PCA.scala @@ -44,6 +44,11 @@ class PCA @Since("1.4.0") (@Since("1.4.0") val k: Int) { require(k <= numFeatures, s"source vector size $numFeatures must be no less than k=$k") + require(PCAUtil.memoryCost(k, numFeatures) < Int.MaxValue, + "The param k and numFeatures is too large for SVD computation. " + + "Try reducing the parameter k for PCA, or reduce the input feature " + + "vector dimension to make this tractable.") + val mat = new RowMatrix(sources) val (pc, explainedVariance) = mat.computePrincipalComponentsAndExplainedVariance(k) val densePC = pc match { @@ -110,3 +115,17 @@ class PCAModel private[spark] ( } } } + +private[feature] object PCAUtil { + + // This memory cost formula is from breeze code: + // https://github.com/scalanlp/breeze/blob/ + // 6e541be066d547a097f5089165cd7c38c3ca276d/math/src/main/scala/breeze/linalg/ + // functions/svd.scala#L87 + def memoryCost(k: Int, numFeatures: Int): Long = { + 3L * math.min(k, numFeatures) * math.min(k, numFeatures) + + math.max(math.max(k, numFeatures), 4L * math.min(k, numFeatures) + * math.min(k, numFeatures) + 4L * math.min(k, numFeatures)) + } + +} diff --git a/mllib/src/test/scala/org/apache/spark/mllib/feature/PCASuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/feature/PCASuite.scala index 2f90afdcee55ec3fb482212c1b5d5364bf641c02..8eab12416a698e308836b7051eb878cac83867e7 100644 --- a/mllib/src/test/scala/org/apache/spark/mllib/feature/PCASuite.scala +++ b/mllib/src/test/scala/org/apache/spark/mllib/feature/PCASuite.scala @@ -48,4 +48,10 @@ class PCASuite extends SparkFunSuite with MLlibTestSparkContext { } assert(pca.explainedVariance ~== explainedVariance relTol 1e-8) } + + test("memory cost computation") { + assert(PCAUtil.memoryCost(10, 100) < Int.MaxValue) + // check overflowing + assert(PCAUtil.memoryCost(40000, 60000) > Int.MaxValue) + } }