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

Dualsum #50

Merged
merged 42 commits into from
Nov 30, 2023
Merged
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
dc81d44
Try to isolate Breeze
kwalcock Nov 8, 2023
c777301
WIP
kwalcock Nov 8, 2023
6018fdf
Rename to Math, transpose
kwalcock Nov 8, 2023
eab249a
Complete Breeze isolation
kwalcock Nov 9, 2023
97e3d2b
Fix it again
kwalcock Nov 9, 2023
2ab8e60
Try Emjl math
kwalcock Nov 2, 2023
2f91702
Test BreezeMath
kwalcock Nov 9, 2023
f038c40
Unit test Emjl math
kwalcock Nov 9, 2023
92c269e
Organize math better
kwalcock Nov 9, 2023
42b2679
Clean up types
kwalcock Nov 9, 2023
c3851f4
Fix up test app
kwalcock Nov 9, 2023
e50c05d
Test better
kwalcock Nov 9, 2023
b21c0a6
Make column matrix directly
kwalcock Nov 10, 2023
0d8a1ed
Make column matrix directly
kwalcock Nov 10, 2023
8148de7
Remove OnnxMath
kwalcock Nov 10, 2023
fd29280
Rename things
kwalcock Nov 10, 2023
b7c2326
Move the BlasInstanceApp to apps subproject
kwalcock Nov 10, 2023
fb0d70d
Only include necessary ejml parts
kwalcock Nov 10, 2023
cddb890
Add CommonsMath
kwalcock Nov 10, 2023
79084ca
Add some mkRowVector to tests
kwalcock Nov 10, 2023
49ac525
Add some logging
kwalcock Nov 11, 2023
f16b818
use sum instead of concat in dual mode
MihaiSurdeanu Nov 13, 2023
a75b72c
Add CluMath
kwalcock Nov 14, 2023
72d210f
Remove .t from main interface
kwalcock Nov 14, 2023
ff7c5d7
Write some comment on CluMath
kwalcock Nov 14, 2023
f3b5362
Hide math files that aren't used
kwalcock Nov 14, 2023
2721d6e
sum not concat in scala
MihaiSurdeanu Nov 15, 2023
21c0f34
use sum instead of concat everywhere.
MihaiSurdeanu Nov 16, 2023
a41971e
Merge branch 'dualsum' into kwalcock/mathWithDualSum
kwalcock Nov 16, 2023
aa39eb9
Do the sum
kwalcock Nov 16, 2023
c095106
Simplify
kwalcock Nov 16, 2023
21cf682
Remove log
kwalcock Nov 16, 2023
b3d901d
Format
kwalcock Nov 16, 2023
32e07af
deberta full model
MihaiSurdeanu Nov 22, 2023
8bc4471
Merge branch 'dualsum' into kwalcock/mathWithDualSum
kwalcock Nov 29, 2023
f207514
Clean up the apps
kwalcock Nov 29, 2023
635bfa7
Remove breeze and go with ejml
kwalcock Nov 29, 2023
9c7463f
Add documentation
kwalcock Nov 29, 2023
cbc99d7
Shade EJML
kwalcock Nov 29, 2023
e7e417e
Shade the other class as well
kwalcock Nov 30, 2023
70f44f2
Update CHANGES
kwalcock Nov 30, 2023
09218f7
Merge pull request #51 from clulab/kwalcock/mathWithDualSum
kwalcock Nov 30, 2023
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
Prev Previous commit
Next Next commit
Make column matrix directly
kwalcock committed Nov 10, 2023
commit b21c0a678e3864be4b511e17b067587517ab040f
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ object TokenClassifierTimerApp extends App {
}
}

val log = true
val verbose = false
val fileName = args.lift(0).getOrElse("../corpora/sentences/sentences.txt")
val untimedTokenClassifier = TokenClassifier.fromFiles("../roberta-base-mtl/avg_export")
val tokenClassifier = new TimedTokenClassifier(untimedTokenClassifier)
@@ -110,7 +110,7 @@ object TokenClassifierTimerApp extends App {
val words = line.split(" ").toSeq
val allLabelSeqs = tokenClassifier.predictWithScores(words)

if (log) {
if (verbose) {
println(s"Words: ${words.mkString(", ")}")
for (layer <- allLabelSeqs) {
val words = layer.map(_.head) // Collapse the next layer by just taking the head.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.clulab.scala_transformers.encoder

import breeze.linalg._
import BreezeUtils._
import breeze.linalg._
import org.clulab.scala_transformers.encoder.math.BreezeMath

object BreezeExamples extends App {
val m = mkRowMatrix[Float](Array(Array(1f, 2f), Array(3f, 4f)))
val m = BreezeMath.mkRowMatrix(Array(Array(1f, 2f), Array(3f, 4f)))
println(m)

println("Row 0: " + m(0, ::))

This file was deleted.

Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ class LinearLayer(
val headHiddenState = Math.row(sentenceHiddenStates, headAbsolutePosition)

// vector concatenation in Breeze operates over vertical vectors, hence the transposing here
val concatState = Math.cat(modHiddenState, headHiddenState)
val concatState = Math.horcat(modHiddenState, headHiddenState)
// row i in the concatenated matrix contains the embedding of modifier i and its head
Math.inplaceMatrixAddition(concatMatrix, i, concatState)
}
@@ -121,7 +121,7 @@ class LinearLayer(

// concatenation of the modifier and head embeddings
// vector concatenation in Breeze operates over vertical vectors, hence the transposing here
val concatState = Math.cat(modHiddenState, headHiddenState)
val concatState = Math.horcat(modHiddenState, headHiddenState)
Math.inplaceMatrixAddition(concatMatrix, 0, concatState)
concatMatrix
}
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ abstract class LinearLayerFactory(val linearLayerLayout: LinearLayerLayout) exte
if (exists(place)) {
val values = sourceFloatVector(newSource(place))
// the bias is a column vector
val biases = Math.mkVector(values)
val biases = Math.mkColVector(values)

logger.info(s"Found biases with dimension ${Math.length(biases)}")
Some(biases)
Original file line number Diff line number Diff line change
@@ -2,33 +2,30 @@ package org.clulab.scala_transformers.encoder.math

import ai.onnxruntime.OrtSession.Result
import breeze.linalg.{DenseMatrix, DenseVector, Transpose, `*`, argmax => BreezeArgmax}
import org.clulab.scala_transformers.encoder.BreezeUtils

object BreezeMath extends Math {
type MathValue = Float
type MathColMatrix = DenseMatrix[MathValue]
type MathRowMatrix = DenseMatrix[MathValue]
type MathColVector = DenseVector[MathValue]
type MathRowVector = Transpose[DenseVector[MathValue]]

def fromResult(result: Result): Array[MathRowMatrix] = {
val array = result.get(0).getValue.asInstanceOf[Array[Array[Array[Float]]]]
val outputs = array.map(BreezeUtils.mkRowMatrix(_))
val outputs = array.map(mkRowMatrix(_))

outputs
}

def argmax(rowVector: MathRowVector): Int = {
val bestIndex = BreezeArgmax(rowVector.t)

bestIndex
BreezeArgmax(rowVector)
}

def inplaceMatrixAddition(matrix: MathRowMatrix, colVector: MathColVector): Unit = {
matrix(*, ::) :+= colVector
}

def inplaceMatrixAddition(matrix: MathRowMatrix, rowIndex: Int, rowVector: MathRowVector): Unit = {

matrix(rowIndex, ::) :+= rowVector
}

@@ -64,7 +61,7 @@ object BreezeMath extends Math {
matrix(index, ::)
}

def cat(leftRowVector: MathRowVector, rightRowVector: MathRowVector): MathRowVector = {
def horcat(leftRowVector: MathRowVector, rightRowVector: MathRowVector): MathRowVector = {
DenseVector.vertcat(leftRowVector.t, rightRowVector.t).t
}

@@ -77,10 +74,28 @@ object BreezeMath extends Math {
}

def mkRowMatrix(values: Array[Array[MathValue]]): MathRowMatrix = {
BreezeUtils.mkRowMatrix(values)
val rows = values.length
val cols = values.head.length
val denseMatrix = new DenseMatrix[Float](rows, cols)

for (row <- 0 until rows)
for (col <- 0 until cols)
denseMatrix(row, col) = values(row)(col)
denseMatrix
}

def mkColMatrix(values: Array[Array[MathValue]]): MathColMatrix = {
val rows = values.length
val cols = values.head.length
val denseMatrix = new DenseMatrix[Float](cols, rows)

for (row <- 0 until rows)
for (col <- 0 until cols)
denseMatrix(col, row) = values(row)(col)
denseMatrix
}

def mkVector(values: Array[MathValue]): MathColVector = {
def mkColVector(values: Array[MathValue]): MathColVector = {
DenseVector(values)
}
}
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import org.ejml.simple.SimpleMatrix

object EjmlMath extends Math {
type MathValue = Float
type MathColMatrix = FMatrixRMaj
type MathRowMatrix = FMatrixRMaj
type MathColVector = FMatrixRMaj
type MathRowVector = FMatrixRMaj
@@ -111,7 +112,7 @@ object EjmlMath extends Math {
result
}

def cat(leftRowVector: MathRowVector, rightRowVector: MathRowVector): MathRowVector = {
def horcat(leftRowVector: MathRowVector, rightRowVector: MathRowVector): MathRowVector = {
assert(isRowVector(leftRowVector))
assert(isRowVector(rightRowVector))
val leftSimple = SimpleMatrix.wrap(leftRowVector)
@@ -138,7 +139,18 @@ object EjmlMath extends Math {
new FMatrixRMaj(values)
}

def mkVector(values: Array[MathValue]): MathColVector = {
def mkColMatrix(values: Array[Array[MathValue]]): MathColMatrix = {
val rows = values.length
val cols = values.head.length
val matrix = new FMatrixRMaj(cols, rows)

for (row <- 0 until rows)
for (col <- 0 until cols)
matrix.set(col, row, values(row)(col))
matrix
}

def mkColVector(values: Array[MathValue]): MathColVector = {
val result = new FMatrixRMaj(values)

assert(isColVector(result))
Original file line number Diff line number Diff line change
@@ -20,9 +20,13 @@ trait Math {
def vertcat(leftColVector: MathColVector, rightColVector: MathColVector): MathColVector
def zeros(rows: Int, cols: Int): MathRowMatrix
def row(matrix: MathRowMatrix, index: Int): MathRowVector
def cat(leftRowVector: MathRowVector, rightRowVector: MathRowVector): MathRowVector
def horcat(leftRowVector: MathRowVector, rightRowVector: MathRowVector): MathRowVector
def toArray(rowVector: MathRowVector): Array[MathValue]
def get(rowVector: MathRowVector, index: Int): MathValue
def mkRowMatrix(values: Array[Array[MathValue]]): MathRowMatrix
def mkVector(values: Array[MathValue]): MathColVector

// For this, the array is specified in column-major order,
// but it should be converted to the normal representation.
def mkColMatrix(values: Array[Array[MathValue]]): MathRowMatrix
def mkColVector(values: Array[MathValue]): MathColVector
}
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@ package org.clulab.scala_transformers.encoder.math

object Mathematics {
// Pick one of these.
// val Math = BreezeMath // This takes care of the companion object.
val Math = EjmlMath
val Math = BreezeMath // This takes care of the companion object.
// val Math = EjmlMath
// val Math = OnnxMath

type MathMatrix = Math.MathRowMatrix
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ object OnnxMath extends Math {
???
}

def cat(left: OnnxTensor, right: OnnxTensor): OnnxTensor = {
def horcat(left: OnnxTensor, right: OnnxTensor): OnnxTensor = {
???
}

@@ -82,7 +82,11 @@ object OnnxMath extends Math {
OnnxTensor.createTensor(ortEnvironment, values)
}

def mkVector(values: Array[Float]): OnnxTensor = {
def mkColMatrix(values: Array[Array[Float]]): OnnxTensor = {
???
}

def mkColVector(values: Array[Float]): OnnxTensor = {
OnnxTensor.createTensor(ortEnvironment, values)
}
}
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ class BreezeMathTest extends Test {

it should "argmax" in {
val values = Array(1f, 3f, 2f)
val vector = BreezeMath.mkVector(values).t
val vector = BreezeMath.mkColVector(values).t
val expectedResult = 1
val actualResult = BreezeMath.argmax(vector)

@@ -22,7 +22,7 @@ class BreezeMathTest extends Test {
)
val matrix = BreezeMath.mkRowMatrix(matrixValues)
val vectorValues = Array(1f, 2f, 3f)
val vector = BreezeMath.mkVector(vectorValues)
val vector = BreezeMath.mkColVector(vectorValues)
val expectedResult = Array(
Array(2f, 4f, 6f),
Array(3f, 6f, 9f)
@@ -44,7 +44,7 @@ class BreezeMathTest extends Test {
)
val matrix = BreezeMath.mkRowMatrix(matrixValues)
val vectorValues = Array(1f, 2f, 3f)
val vector = BreezeMath.mkVector(vectorValues).t
val vector = BreezeMath.mkColVector(vectorValues).t
val expectedResult = Array(
Array(1f, 2f, 3f),
Array(3f, 6f, 9f)
@@ -110,7 +110,7 @@ class BreezeMathTest extends Test {

it should "length" in {
val values = Array(1f, 2f, 3f)
val vector = BreezeMath.mkVector(values)
val vector = BreezeMath.mkColVector(values)
val expectedResult = 3
val actualResult = BreezeMath.length(vector)

@@ -140,8 +140,8 @@ class BreezeMathTest extends Test {
it should "vertcat" in {
val leftVectorValues = Array(1f, 2f, 3f)
val rightVectorValues = Array(2f, 4f, 6f)
val leftVector = BreezeMath.mkVector(leftVectorValues)
val rightVector = BreezeMath.mkVector(rightVectorValues)
val leftVector = BreezeMath.mkColVector(leftVectorValues)
val rightVector = BreezeMath.mkColVector(rightVectorValues)
val expectedResult = Array(1f, 2f, 3f, 2f, 4f, 6f)
val actualResult = BreezeMath.vertcat(leftVector, rightVector)

@@ -182,10 +182,10 @@ class BreezeMathTest extends Test {
it should "cat" in {
val leftVectorValues = Array(1f, 2f, 3f)
val rightVectorValues = Array(2f, 4f, 6f)
val leftVector = BreezeMath.mkVector(leftVectorValues).t
val rightVector = BreezeMath.mkVector(rightVectorValues).t
val leftVector = BreezeMath.mkColVector(leftVectorValues).t
val rightVector = BreezeMath.mkColVector(rightVectorValues).t
val expectedResult = Array(1f, 2f, 3f, 2f, 4f, 6f)
val actualResult = BreezeMath.cat(leftVector, rightVector)
val actualResult = BreezeMath.horcat(leftVector, rightVector)

expectedResult.zipWithIndex.foreach { case (expectedValue, index) =>
actualResult(index) should be(expectedValue)
@@ -194,7 +194,7 @@ class BreezeMathTest extends Test {

it should "toArray" in {
val values = Array(1f, 2f, 3f)
val vector = BreezeMath.mkVector(values).t
val vector = BreezeMath.mkColVector(values).t
val expectedResult = values
val actualResult = BreezeMath.toArray(vector)

@@ -205,33 +205,50 @@ class BreezeMathTest extends Test {

it should "get" in {
val values = Array(1f, 2f, 3f)
val vector = BreezeMath.mkVector(values).t
val vector = BreezeMath.mkColVector(values).t
val expectedResult = 2f
val actualResult = BreezeMath.get(vector, 1)

actualResult should be (expectedResult)
}

it should "mkRowMatrix" in {
val values = Array(
val matrix = Array(
Array(1f, 2f, 3f),
Array(2f, 4f, 6f)
)
val matrix = BreezeMath.mkRowMatrix(values)
val expectedResult = "1.0 2.0 3.0 \n2.0 4.0 6.0 "
val actualResult = matrix.toString
val expectedResult = matrix
val actualResult = BreezeMath.mkRowMatrix(matrix)

actualResult should be (expectedResult)
values.zipWithIndex.foreach { case (values, rowIndex) =>
values.zipWithIndex.foreach { case (value, colIndex) =>
matrix(rowIndex, colIndex) should be (value)
expectedResult.zipWithIndex.foreach { case (expectedValues, rowIndex) =>
expectedValues.zipWithIndex.foreach { case (expectedValue, colIndex) =>
actualResult(rowIndex, colIndex) should be (expectedValue)
}
}
}

it should "mkColMatrix" in {
val matrix = Array(
Array(1f, 2f, 3f),
Array(2f, 4f, 6f)
)
val expectedResult = Array(
Array(1f, 2f),
Array(2f, 4f),
Array(3f, 6f)
)
val actualResult = BreezeMath.mkColMatrix(matrix)

expectedResult.zipWithIndex.foreach { case (expectedValues, rowIndex) =>
expectedValues.zipWithIndex.foreach { case (expectedValue, colIndex) =>
actualResult(rowIndex, colIndex) should be (expectedValue)
}
}
}

it should "mkVector" in {
val values = Array(1f, 2f, 3f)
val vector = BreezeMath.mkVector(values)
val vector = BreezeMath.mkColVector(values)
val expectedResult = "DenseVector(1.0, 2.0, 3.0)"
val actualResult = vector.toString

Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ class EjmlMathTest extends Test {
behavior of "Math"

it should "argmax" in {
val values = Array(1f, 3f, 2f)
val vector = EjmlMath.t(EjmlMath.mkVector(values))
val vectorValues = Array(1f, 3f, 2f)
val vector = EjmlMath.t(EjmlMath.mkColVector(vectorValues))
val expectedResult = 1
val actualResult = EjmlMath.argmax(vector)

@@ -22,7 +22,7 @@ class EjmlMathTest extends Test {
)
val matrix = EjmlMath.mkRowMatrix(matrixValues)
val vectorValues = Array(1f, 2f, 3f)
val vector = EjmlMath.mkVector(vectorValues)
val vector = EjmlMath.mkColVector(vectorValues)
val expectedResult = Array(
Array(2f, 4f, 6f),
Array(3f, 6f, 9f)
@@ -44,7 +44,7 @@ class EjmlMathTest extends Test {
)
val matrix = EjmlMath.mkRowMatrix(matrixValues)
val vectorValues = Array(1f, 2f, 3f)
val vector = EjmlMath.t(EjmlMath.mkVector(vectorValues))
val vector = EjmlMath.t(EjmlMath.mkColVector(vectorValues))
val expectedResult = Array(
Array(1f, 2f, 3f),
Array(3f, 6f, 9f)
@@ -109,8 +109,8 @@ class EjmlMathTest extends Test {
}

it should "length" in {
val values = Array(1f, 2f, 3f)
val vector = EjmlMath.mkVector(values)
val vectorValues = Array(1f, 2f, 3f)
val vector = EjmlMath.mkColVector(vectorValues)
val expectedResult = 3
val actualResult = EjmlMath.length(vector)

@@ -140,8 +140,8 @@ class EjmlMathTest extends Test {
it should "vertcat" in {
val leftVectorValues = Array(1f, 2f, 3f)
val rightVectorValues = Array(2f, 4f, 6f)
val leftVector = EjmlMath.mkVector(leftVectorValues)
val rightVector = EjmlMath.mkVector(rightVectorValues)
val leftVector = EjmlMath.mkColVector(leftVectorValues)
val rightVector = EjmlMath.mkColVector(rightVectorValues)
val expectedResult = Array(1f, 2f, 3f, 2f, 4f, 6f)
val actualResult = EjmlMath.vertcat(leftVector, rightVector)

@@ -182,20 +182,20 @@ class EjmlMathTest extends Test {
it should "cat" in {
val leftVectorValues = Array(1f, 2f, 3f)
val rightVectorValues = Array(2f, 4f, 6f)
val leftVector = EjmlMath.t(EjmlMath.mkVector(leftVectorValues))
val rightVector = EjmlMath.t(EjmlMath.mkVector(rightVectorValues))
val leftVector = EjmlMath.t(EjmlMath.mkColVector(leftVectorValues))
val rightVector = EjmlMath.t(EjmlMath.mkColVector(rightVectorValues))
val expectedResult = Array(1f, 2f, 3f, 2f, 4f, 6f)
val actualResult = EjmlMath.cat(leftVector, rightVector)
val actualResult = EjmlMath.horcat(leftVector, rightVector)

expectedResult.zipWithIndex.foreach { case (expectedValue, index) =>
actualResult.get(index) should be(expectedValue)
}
}

it should "toArray" in {
val values = Array(1f, 2f, 3f)
val vector = EjmlMath.t(EjmlMath.mkVector(values))
val expectedResult = values
val vectorValues = Array(1f, 2f, 3f)
val vector = EjmlMath.t(EjmlMath.mkColVector(vectorValues))
val expectedResult = vectorValues
val actualResult = EjmlMath.toArray(vector)

expectedResult.zipWithIndex.foreach { case (expectedValue, index) =>
@@ -204,21 +204,21 @@ class EjmlMathTest extends Test {
}

it should "get" in {
val values = Array(1f, 2f, 3f)
val vector = EjmlMath.t(EjmlMath.mkVector(values))
val vectorValues = Array(1f, 2f, 3f)
val vector = EjmlMath.t(EjmlMath.mkColVector(vectorValues))
val expectedResult = 2f
val actualResult = EjmlMath.get(vector, 1)

actualResult should be (expectedResult)
}

it should "mkRowMatrix" in {
val values = Array(
val matrixValues = Array(
Array(1f, 2f, 3f),
Array(2f, 4f, 6f)
)
val expectedResult = values
val actualResult = EjmlMath.mkRowMatrix(values)
val expectedResult = matrixValues
val actualResult = EjmlMath.mkRowMatrix(matrixValues)

expectedResult.zipWithIndex.foreach { case (expectedValues, rowIndex) =>
expectedValues.zipWithIndex.foreach { case (expectedValue, colIndex) =>
@@ -227,10 +227,29 @@ class EjmlMathTest extends Test {
}
}

it should "mkColMatrix" in {
val matrixValues = Array(
Array(1f, 2f, 3f),
Array(2f, 4f, 6f)
)
val expectedResult = Array(
Array(1f, 2f),
Array(2f, 4f),
Array(3f, 6f)
)
val actualResult = EjmlMath.mkColMatrix(matrixValues)

expectedResult.zipWithIndex.foreach { case (expectedValues, rowIndex) =>
expectedValues.zipWithIndex.foreach { case (expectedValue, colIndex) =>
actualResult.get(rowIndex, colIndex) should be(expectedValue)
}
}
}

it should "mkVector" in {
val values = Array(1f, 2f, 3f)
val expectedResult = values
val actualResult = EjmlMath.mkVector(expectedResult)
val vectorValues = Array(1f, 2f, 3f)
val expectedResult = vectorValues
val actualResult = EjmlMath.mkColVector(expectedResult)

expectedResult.zipWithIndex.foreach { case (expectedValue, index) =>
actualResult.get(index) should be (expectedValue)