diff --git a/benchmarks/build.gradle.kts b/benchmarks/build.gradle.kts index 90ec5dfbe..e46f4a9b4 100644 --- a/benchmarks/build.gradle.kts +++ b/benchmarks/build.gradle.kts @@ -52,6 +52,8 @@ kotlin { implementation(project(":kmath-viktor")) implementation(project(":kmath-jafama")) implementation(project(":kmath-multik")) + implementation(projects.kmath.kmathTensorflow) + implementation("org.tensorflow:tensorflow-core-platform:0.4.0") implementation("org.nd4j:nd4j-native:1.0.0-M1") // uncomment if your system supports AVX2 // val os = System.getProperty("os.name") diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt index 53d7ee9b6..4a5cd4aa2 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt @@ -17,7 +17,9 @@ import space.kscience.kmath.multik.multikAlgebra import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.tensorflow.produceWithTF import space.kscience.kmath.tensors.core.DoubleTensorAlgebra +import space.kscience.kmath.tensors.core.tensorAlgebra import kotlin.random.Random @State(Scope.Benchmark) @@ -44,6 +46,16 @@ internal class DotBenchmark { val ejmlMatrix2 = EjmlLinearSpaceDDRM { matrix2.toEjml() } } + + @Benchmark + fun tfDot(blackhole: Blackhole){ + blackhole.consume( + DoubleField.produceWithTF { + tensor1 dot tensor2 + } + ) + } + @Benchmark fun cmDotWithConversion(blackhole: Blackhole) = CMLinearSpace { blackhole.consume(matrix1 dot matrix2) @@ -64,13 +76,13 @@ internal class DotBenchmark { blackhole.consume(matrix1 dot matrix2) } -// @Benchmark -// fun tensorDot(blackhole: Blackhole) = with(Double.tensorAlgebra) { -// blackhole.consume(matrix1 dot matrix2) -// } + @Benchmark + fun tensorDot(blackhole: Blackhole) = with(DoubleField.tensorAlgebra) { + blackhole.consume(matrix1 dot matrix2) + } @Benchmark - fun multikDot(blackhole: Blackhole) = with(Double.multikAlgebra) { + fun multikDot(blackhole: Blackhole) = with(DoubleField.multikAlgebra) { blackhole.consume(matrix1 dot matrix2) } diff --git a/build.gradle.kts b/build.gradle.kts index a07bcd2c5..3b48c7328 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ allprojects { } group = "space.kscience" - version = "0.3.0-dev-18" + version = "0.3.0-dev-19" } subprojects { diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt similarity index 100% rename from kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt rename to kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt diff --git a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt index e9e75543d..b40739ee0 100644 --- a/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt +++ b/kmath-tensorflow/src/main/kotlin/space/kscience/kmath/tensorflow/TensorFlowAlgebra.kt @@ -199,8 +199,9 @@ public abstract class TensorFlowAlgebra> internal c override fun StructureND.dot(other: StructureND): TensorFlowOutput = operate(other) { l, r -> ops.linalg.matMul( - if (l.asTensor().shape().numDimensions() == 1) ops.expandDims(l, ops.constant(0)) else l, - if (r.asTensor().shape().numDimensions() == 1) ops.expandDims(r, ops.constant(-1)) else r) + if (l.shape().numDimensions() == 1) ops.expandDims(l, ops.constant(0)) else l, + if (r.shape().numDimensions() == 1) ops.expandDims(r, ops.constant(-1)) else r + ) } override fun diagonalEmbedding( diff --git a/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt b/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt index 805ad7c66..3d118d980 100644 --- a/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt +++ b/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt @@ -4,6 +4,9 @@ import org.junit.jupiter.api.Test import space.kscience.kmath.nd.get import space.kscience.kmath.nd.structureND import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.tensors.core.DoubleTensorAlgebra +import space.kscience.kmath.tensors.core.DoubleTensorAlgebra.Companion.sum +import kotlin.random.Random import kotlin.test.assertEquals class DoubleTensorFlowOps { @@ -18,6 +21,19 @@ class DoubleTensorFlowOps { assertEquals(3.0, res[0, 0]) } + @Test + fun dot(){ + val random = Random(12224) + val dim = 1000 + + val tensor1 = DoubleTensorAlgebra.randomNormal(shape = intArrayOf(dim, dim), 12224) + val tensor2 = DoubleTensorAlgebra.randomNormal(shape = intArrayOf(dim, dim), 12225) + + DoubleField.produceWithTF { + tensor1 dot tensor2 + }.sum() + } + @Test fun extensionOps(){ val res = DoubleField.produceWithTF { diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt index a75e5a8e3..ebe7a10b6 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt @@ -997,5 +997,6 @@ public open class DoubleTensorAlgebra : } public val Double.Companion.tensorAlgebra: DoubleTensorAlgebra.Companion get() = DoubleTensorAlgebra +public val DoubleField.tensorAlgebra: DoubleTensorAlgebra.Companion get() = DoubleTensorAlgebra