Skip to content
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

bug fix for DenseTensorBlas.gemm #35

Merged
merged 3 commits into from
Oct 20, 2016
Merged
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 @@ -35,12 +35,16 @@ object DenseTensorBLAS {

var time = 0L

def dgemm[@specialized(Float, Double) T](transa: String, transb: String, m: Int, n: Int,
k: Int, alpha: T, a: Array[T], aOffset: Int,
lda: Int, b: Array[T], bOffset: Int, ldb: Int, beta: T, c: Array[T], cOffset: Int,
ldc: Int)(implicit ev: TensorNumeric[T]): Unit = {
def gemm[@specialized(Float, Double) T](transa: String, transb: String,
m: Int, n: Int, k: Int,
alpha: T,
a: Array[T], aOffset: Int, lda: Int,
b: Array[T], bOffset: Int, ldb: Int,
beta: T,
c: Array[T], cOffset: Int, ldc: Int)(implicit ev: TensorNumeric[T]): Unit = {

val _transa = (transa == "t" || transa == "T")
val _transb = (transa == "t" || transa == "T")
val _transb = (transb == "t" || transb == "T")

var _ldc = ldc
if (n == 1) {
Expand Down Expand Up @@ -75,8 +79,9 @@ object DenseTensorBLAS {
time += (System.nanoTime() - start)
}

def dgemv[@specialized(Float, Double) T](alpha: T, matrix: Tensor[T], vector: Tensor[T],
def gemv[@specialized(Float, Double) T](alpha: T, matrix: Tensor[T], vector: Tensor[T],
beta: T, r: Tensor[T])(implicit ev: TensorNumeric[T]): Unit = {

require(matrix.size(2) == vector.size(1), "matrix vector size doesn't match")
require(matrix.size(1) == r.size(1), "matrix result size doesn't match")
if (matrix.stride(1) == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ object DenseTensorMath {
new DenseTensor(new ArrayStorage(Array(result)))
} else if (self.nDimension() == 2 && t.nDimension() == 1) {
val result = new DenseTensor[T](self.size(1))
DenseTensorBLAS.dgemv[T](ev.fromType[Int](1), self, t, ev.fromType[Int](0), result)
DenseTensorBLAS.gemv[T](ev.fromType[Int](1), self, t, ev.fromType[Int](0), result)
result
} else if (self.nDimension() == 2 && t.nDimension() == 2) {
val result = new DenseTensor[T](t.size(2), self.size(1)).t()
Expand Down Expand Up @@ -367,7 +367,7 @@ object DenseTensorMath {
__m2 = _m2.contiguous()
}

DenseTensorBLAS.dgemm[T](transpose_m1, transpose_m2, _r.size(index1), _r.size(index2),
DenseTensorBLAS.gemm[T](transpose_m1, transpose_m2, _r.size(index1), _r.size(index2),
__m1.size(index2), alpha, __m1.storage().array(), __m1.storageOffset() - 1,
if (transpose_m1 == "n") __m1.stride(index2) else __m1.stride(index1),
__m2.storage().array(), __m2.storageOffset() - 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,4 +595,116 @@ class DenseTensorMathSpec extends FlatSpec with Matchers {
r should be(Tensor[Float](Storage[Float](
Array(1.0986123f, 1.3862944f, 1.609438f)), 1, Array(1, 3)))
}

"gemm(N, N)" should "return correct value" in {
val matrixA = Tensor[Float](2, 3)
val matrixB = Tensor[Float](3, 2)

var i = 0
matrixA.apply1(_ => {
i = i + 1;
i
})
matrixB.copy(matrixA)

val matrixC = Tensor[Float](2, 2)

DenseTensorBLAS.gemm[Float](
"N", "N",
2, 2, 3,
1,
matrixA.storage().array(), matrixA.storageOffset() - 1, 2,
matrixB.storage().array(), matrixB.storageOffset() - 1, 3,
0,
matrixC.storage().array(), matrixC.storageOffset() - 1, 2
)

val result = Tensor[Float](Storage(Array[Float](22, 28, 49, 64)), 1, Array(2, 2))

matrixC should be (result)
}

"gemm(N, T)" should "return correct value" in {
val matrixA = Tensor[Float](2, 3)
val matrixB = Tensor[Float](2, 3)

var i = 0
matrixA.apply1(_ => {
i = i + 1;
i
})
matrixB.copy(matrixA)

val matrixC = Tensor[Float](2, 2)

DenseTensorBLAS.gemm[Float](
"N", "T",
2, 2, 3,
1,
matrixA.storage().array(), matrixA.storageOffset() - 1, 2,
matrixB.storage().array(), matrixB.storageOffset() - 1, 2,
0,
matrixC.storage().array(), matrixC.storageOffset() - 1, 2
)

val result = Tensor[Float](Storage(Array[Float](35, 44, 44, 56)), 1, Array(2, 2))

matrixC should be (result)
}

"gemm(T, N)" should "return correct value" in {
val matrixA = Tensor[Float](3, 2)
val matrixB = Tensor[Float](3, 2)

var i = 0
matrixA.apply1(_ => {
i = i + 1;
i
})
matrixB.copy(matrixA)

val matrixC = Tensor[Float](2, 2)

DenseTensorBLAS.gemm[Float](
"T", "N",
2, 2, 3,
1,
matrixA.storage().array(), matrixA.storageOffset() - 1, 3,
matrixB.storage().array(), matrixB.storageOffset() - 1, 3,
0,
matrixC.storage().array(), matrixC.storageOffset() - 1, 2
)

val result = Tensor[Float](Storage(Array[Float](14, 32, 32, 77)), 1, Array(2, 2))

matrixC should be (result)
}

"gemm(T, T)" should "return correct value" in {
val matrixA = Tensor[Float](3, 2)
val matrixB = Tensor[Float](2, 3)

var i = 0
matrixA.apply1(_ => {
i = i + 1;
i
})
matrixB.copy(matrixA)

val matrixC = Tensor[Float](2, 2)

DenseTensorBLAS.gemm[Float](
"T", "T",
2, 2, 3,
1,
matrixA.storage().array(), matrixA.storageOffset() - 1, 3,
matrixB.storage().array(), matrixB.storageOffset() - 1, 2,
0,
matrixC.storage().array(), matrixC.storageOffset() - 1, 2
)

val result = Tensor[Float](Storage(Array[Float](22, 49, 28, 64)), 1, Array(2, 2))

matrixC should be (result)
}
}