Skip to content

[SPARK-17721][MLlib][ML] Fix for multiplying transposed SparseMatrix with SparseVector #15296

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,16 @@ private[spark] object BLAS extends Serializable {
val indEnd = Arows(rowCounter + 1)
var sum = 0.0
var k = 0
while (k < xNnz && i < indEnd) {
while (i < indEnd && k < xNnz) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: an unnecessary change?

if (xIndices(k) == Acols(i)) {
sum += Avals(i) * xValues(k)
k += 1
i += 1
} else if (xIndices(k) < Acols(i)) {
k += 1
} else {
i += 1
}
k += 1
}
yValues(rowCounter) = sum * alpha + beta * yValues(rowCounter)
rowCounter += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,23 @@ class BLASSuite extends SparkMLFunSuite {
}
}

val y17 = new DenseVector(Array(0.0, 0.0))
val y18 = y17.copy

val sA3 = new SparseMatrix(3, 2, Array(0, 2, 4), Array(1, 2, 0, 1), Array(2.0, 1.0, 1.0, 2.0))
.transpose
val sA4 =
new SparseMatrix(2, 3, Array(0, 1, 3, 4), Array(1, 0, 1, 0), Array(1.0, 2.0, 2.0, 1.0))
val sx3 = new SparseVector(3, Array(1, 2), Array(2.0, 1.0))

val expected4 = new DenseVector(Array(5.0, 4.0))

gemv(1.0, sA3, sx3, 0.0, y17)
gemv(1.0, sA4, sx3, 0.0, y18)

assert(y17 ~== expected4 absTol 1e-15)
assert(y18 ~== expected4 absTol 1e-15)

val dAT =
new DenseMatrix(3, 4, Array(0.0, 2.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 3.0))
val sAT =
Expand Down
8 changes: 6 additions & 2 deletions mllib/src/main/scala/org/apache/spark/mllib/linalg/BLAS.scala
Original file line number Diff line number Diff line change
Expand Up @@ -637,12 +637,16 @@ private[spark] object BLAS extends Serializable with Logging {
val indEnd = Arows(rowCounter + 1)
var sum = 0.0
var k = 0
while (k < xNnz && i < indEnd) {
while (i < indEnd && k < xNnz) {
if (xIndices(k) == Acols(i)) {
sum += Avals(i) * xValues(k)
k += 1
i += 1
} else if (xIndices(k) < Acols(i)) {
k += 1
} else {
i += 1
}
k += 1
}
yValues(rowCounter) = sum * alpha + beta * yValues(rowCounter)
rowCounter += 1
Expand Down
17 changes: 17 additions & 0 deletions mllib/src/test/scala/org/apache/spark/mllib/linalg/BLASSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,23 @@ class BLASSuite extends SparkFunSuite {
}
}

val y17 = new DenseVector(Array(0.0, 0.0))
val y18 = y17.copy

val sA3 = new SparseMatrix(3, 2, Array(0, 2, 4), Array(1, 2, 0, 1), Array(2.0, 1.0, 1.0, 2.0))
.transpose
val sA4 =
new SparseMatrix(2, 3, Array(0, 1, 3, 4), Array(1, 0, 1, 0), Array(1.0, 2.0, 2.0, 1.0))
val sx3 = new SparseVector(3, Array(1, 2), Array(2.0, 1.0))

val expected4 = new DenseVector(Array(5.0, 4.0))

gemv(1.0, sA3, sx3, 0.0, y17)
gemv(1.0, sA4, sx3, 0.0, y18)

assert(y17 ~== expected4 absTol 1e-15)
assert(y18 ~== expected4 absTol 1e-15)

val dAT =
new DenseMatrix(3, 4, Array(0.0, 2.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 3.0))
val sAT =
Expand Down