From d599d1132b3e6e80454ab9dcec4cabf148669bf3 Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Tue, 19 Jan 2021 07:36:45 +0000 Subject: [PATCH] Moving benchmarks implementations to common --- .../kscience.kmath.torch/BenchmarkMatMult.kt | 24 ++++ .../BenchmarkRandomGenerators.kt | 132 ++++++++++++++++++ .../kmath/torch/BenchmarkMatMultGPU.kt | 20 --- .../torch/BenchmarkRandomGeneratorsGPU.kt | 64 --------- .../kscience/kmath/torch/BenchmarkMatMul.kt | 27 ++++ .../kscience/kmath/torch/BenchmarkMatMult.kt | 66 --------- .../kmath/torch/BenchmarkRandomGenerators.kt | 103 ++------------ 7 files changed, 195 insertions(+), 241 deletions(-) create mode 100644 kmath-torch/src/commonTest/kotlin/kscience.kmath.torch/BenchmarkMatMult.kt create mode 100644 kmath-torch/src/commonTest/kotlin/kscience.kmath.torch/BenchmarkRandomGenerators.kt delete mode 100644 kmath-torch/src/nativeGPUTest/kotlin/kscience/kmath/torch/BenchmarkMatMultGPU.kt delete mode 100644 kmath-torch/src/nativeGPUTest/kotlin/kscience/kmath/torch/BenchmarkRandomGeneratorsGPU.kt create mode 100644 kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkMatMul.kt delete mode 100644 kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkMatMult.kt diff --git a/kmath-torch/src/commonTest/kotlin/kscience.kmath.torch/BenchmarkMatMult.kt b/kmath-torch/src/commonTest/kotlin/kscience.kmath.torch/BenchmarkMatMult.kt new file mode 100644 index 000000000..8224a98d5 --- /dev/null +++ b/kmath-torch/src/commonTest/kotlin/kscience.kmath.torch/BenchmarkMatMult.kt @@ -0,0 +1,24 @@ +@file:Suppress("NOTHING_TO_INLINE") + +package kscience.kmath.torch + +import kotlin.time.measureTime + +internal inline fun , + TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra> + TorchTensorAlgebraType.benchmarkMatMul( + scale: Int, + numWarmUp: Int, + numIter: Int, + fieldName: String, + device: Device = Device.CPU +): Unit { + println("Benchmarking $scale x $scale $fieldName matrices on $device: ") + setSeed(SEED) + val lhs = randNormal(shape = intArrayOf(scale, scale), device = device) + val rhs = randNormal(shape = intArrayOf(scale, scale), device = device) + repeat(numWarmUp) { lhs dotAssign rhs } + val measuredTime = measureTime { repeat(numIter) { lhs dotAssign rhs } } + println(" ${measuredTime / numIter} p.o. with $numIter iterations") +} + diff --git a/kmath-torch/src/commonTest/kotlin/kscience.kmath.torch/BenchmarkRandomGenerators.kt b/kmath-torch/src/commonTest/kotlin/kscience.kmath.torch/BenchmarkRandomGenerators.kt new file mode 100644 index 000000000..b01afb878 --- /dev/null +++ b/kmath-torch/src/commonTest/kotlin/kscience.kmath.torch/BenchmarkRandomGenerators.kt @@ -0,0 +1,132 @@ +@file:Suppress("NOTHING_TO_INLINE") + +package kscience.kmath.torch + +import kotlin.time.measureTime + +internal inline fun , + TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra> + TorchTensorAlgebraType.benchmarkRand( + samples: Int, + numWarmUp: Int, + numIter: Int, + device: Device, + distName: String, + initBock: TorchTensorAlgebraType.(IntArray, Device) -> TorchTensorType, + runBlock: TorchTensorAlgebraType.(TorchTensorType) -> Unit +): Unit{ + println("Benchmarking generation of $samples $distName samples on $device: ") + setSeed(SEED) + val shape = intArrayOf(samples) + val tensor = this.initBock(shape,device) + repeat(numWarmUp) { this.runBlock(tensor) } + val measuredTime = measureTime { repeat(numIter) { this.runBlock(tensor) } } + println(" ${measuredTime / numIter} p.o. with $numIter iterations") +} + +internal inline fun , + TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra> + TorchTensorAlgebraType.benchmarkRandNormal( + samples: Int, + numWarmUp: Int, + numIter: Int, + device: Device = Device.CPU): Unit{ + benchmarkRand( + samples, + numWarmUp, + numIter, + device, + "Normal", + {sh, dc -> randNormal(shape = sh, device = dc)}, + {ten -> ten.randNormalAssign() } + ) +} + +internal inline fun , + TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra> + TorchTensorAlgebraType.benchmarkRandUniform( + samples: Int, + numWarmUp: Int, + numIter: Int, + device: Device = Device.CPU): Unit{ + benchmarkRand( + samples, + numWarmUp, + numIter, + device, + "Uniform", + {sh, dc -> randUniform(shape = sh, device = dc)}, + {ten -> ten.randUniformAssign() } + ) +} + +internal inline fun , + TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra> + TorchTensorAlgebraType.benchmarkRandIntegral( + samples: Int, + numWarmUp: Int, + numIter: Int, + device: Device = Device.CPU): Unit{ + benchmarkRand( + samples, + numWarmUp, + numIter, + device, + "integer [0,100]", + {sh, dc -> randIntegral(0f, 100f, shape = sh, device = dc)}, + {ten -> ten.randIntegralAssign(0f, 100f) } + ) +} + +internal inline fun , + TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra> + TorchTensorAlgebraType.benchmarkingRand1(): Unit { + benchmarkRandNormal(10, 10, 100000) + benchmarkRandUniform(10, 10, 100000) + benchmarkRandIntegral(10, 10, 100000) + if(cudaAvailable()) { + benchmarkRandNormal(10, 10, 100000, device = Device.CUDA(0)) + benchmarkRandUniform(10, 10, 100000, device = Device.CUDA(0)) + benchmarkRandIntegral(10, 10, 100000, device = Device.CUDA(0)) + } + +} + +internal inline fun , + TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra> + TorchTensorAlgebraType.benchmarkingRand3(): Unit { + benchmarkRandNormal(1000, 10, 10000) + benchmarkRandUniform(1000, 10, 10000) + benchmarkRandIntegral(1000, 10, 10000) + if(cudaAvailable()) { + benchmarkRandNormal(1000, 10, 100000, device = Device.CUDA(0)) + benchmarkRandUniform(1000, 10, 100000, device = Device.CUDA(0)) + benchmarkRandIntegral(1000, 10, 100000, device = Device.CUDA(0)) + } +} + +internal inline fun , + TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra> + TorchTensorAlgebraType.benchmarkingRand5(): Unit { + benchmarkRandNormal(100000, 5, 100) + benchmarkRandUniform(100000, 5, 100) + benchmarkRandIntegral(100000, 5, 100) + if(cudaAvailable()){ + benchmarkRandNormal(100000, 10, 100000, device = Device.CUDA(0)) + benchmarkRandUniform(100000, 10, 100000, device = Device.CUDA(0)) + benchmarkRandIntegral(100000, 10, 100000, device = Device.CUDA(0)) + } +} + +internal inline fun , + TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra> + TorchTensorAlgebraType.benchmarkingRand7(): Unit { + benchmarkRandNormal(10000000, 3, 20) + benchmarkRandUniform(10000000, 3, 20) + benchmarkRandIntegral(10000000, 3, 20) + if(cudaAvailable()){ + benchmarkRandNormal(10000000, 10, 10000, device = Device.CUDA(0)) + benchmarkRandUniform(10000000, 10, 10000, device = Device.CUDA(0)) + benchmarkRandIntegral(10000000, 10, 10000, device = Device.CUDA(0)) + } +} \ No newline at end of file diff --git a/kmath-torch/src/nativeGPUTest/kotlin/kscience/kmath/torch/BenchmarkMatMultGPU.kt b/kmath-torch/src/nativeGPUTest/kotlin/kscience/kmath/torch/BenchmarkMatMultGPU.kt deleted file mode 100644 index 62424a59d..000000000 --- a/kmath-torch/src/nativeGPUTest/kotlin/kscience/kmath/torch/BenchmarkMatMultGPU.kt +++ /dev/null @@ -1,20 +0,0 @@ -package kscience.kmath.torch - -import kotlin.test.Test - -class BenchmarkMatMultGPU { - @Test - fun benchmarkMatMultFloat20() = - benchmarkingMatMultFloat(20, 10, 100000, - device = Device.CUDA(0)) - - @Test - fun benchmarkMatMultFloat200() = - benchmarkingMatMultFloat(200, 10, 10000, - device = Device.CUDA(0)) - - @Test - fun benchmarkMatMultFloat2000() = - benchmarkingMatMultFloat(2000, 10, 1000, - device = Device.CUDA(0)) -} \ No newline at end of file diff --git a/kmath-torch/src/nativeGPUTest/kotlin/kscience/kmath/torch/BenchmarkRandomGeneratorsGPU.kt b/kmath-torch/src/nativeGPUTest/kotlin/kscience/kmath/torch/BenchmarkRandomGeneratorsGPU.kt deleted file mode 100644 index c9593a825..000000000 --- a/kmath-torch/src/nativeGPUTest/kotlin/kscience/kmath/torch/BenchmarkRandomGeneratorsGPU.kt +++ /dev/null @@ -1,64 +0,0 @@ -package kscience.kmath.torch - -import kotlin.test.Test - -class BenchmarkRandomGeneratorsGPU { - @Test - fun benchmarkRandNormal1() = - benchmarkingRandNormal(10, 10, 100000, - device = Device.CUDA(0)) - @Test - fun benchmarkRandUniform1() = - benchmarkingRandUniform(10, 10, 100000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandIntegral1() = - benchmarkingRandIntegral(10, 10, 100000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandNormal3() = - benchmarkingRandNormal(1000, 10, 100000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandUniform3() = - benchmarkingRandUniform(1000, 10, 100000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandIntegral3() = - benchmarkingRandIntegral(1000, 10, 100000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandNormal5() = - benchmarkingRandNormal(100000, 10, 100000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandUniform5() = - benchmarkingRandUniform(100000, 10, 100000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandIntegral5() = - benchmarkingRandIntegral(100000, 10, 100000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandNormal7() = - benchmarkingRandNormal(10000000, 10, 10000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandUniform7() = - benchmarkingRandUniform(10000000, 10, 10000, - device = Device.CUDA(0)) - - @Test - fun benchmarkRandIntegral7() = - benchmarkingRandIntegral(10000000, 10, 10000, - device = Device.CUDA(0)) -} \ No newline at end of file diff --git a/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkMatMul.kt b/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkMatMul.kt new file mode 100644 index 000000000..f990dbc1a --- /dev/null +++ b/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkMatMul.kt @@ -0,0 +1,27 @@ +package kscience.kmath.torch + +import kotlin.test.Test + +internal class BenchmarkMatMul { + + @Test + fun benchmarkMatMulDouble() = TorchTensorRealAlgebra { + benchmarkMatMul(20, 10, 100000, "Real") + benchmarkMatMul(200, 10, 10000, "Real") + benchmarkMatMul(2000, 3, 20, "Real") + } + + @Test + fun benchmarkMatMulFloat() = TorchTensorFloatAlgebra { + benchmarkMatMul(20, 10, 100000, "Float") + benchmarkMatMul(200, 10, 10000, "Float") + benchmarkMatMul(2000, 3, 20, "Float") + if (cudaAvailable()) { + benchmarkMatMul(20, 10, 100000, "Float", Device.CUDA(0)) + benchmarkMatMul(200, 10, 10000, "Float", Device.CUDA(0)) + benchmarkMatMul(2000, 10, 1000, "Float", Device.CUDA(0)) + } + } + + +} \ No newline at end of file diff --git a/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkMatMult.kt b/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkMatMult.kt deleted file mode 100644 index d0a5ba968..000000000 --- a/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkMatMult.kt +++ /dev/null @@ -1,66 +0,0 @@ -package kscience.kmath.torch - -import kotlin.test.Test -import kotlin.time.measureTime - -internal fun benchmarkingMatMultDouble( - scale: Int, - numWarmUp: Int, - numIter: Int, - device: Device = Device.CPU -): Unit { - TorchTensorRealAlgebra { - println("Benchmarking $scale x $scale matrices over Double's on $device: ") - setSeed(SEED) - val lhs = randNormal(shape = intArrayOf(scale, scale), device = device) - val rhs = randNormal(shape = intArrayOf(scale, scale), device = device) - repeat(numWarmUp) { lhs dotAssign rhs } - val measuredTime = measureTime { repeat(numIter) { lhs dotAssign rhs } } - println(" ${measuredTime / numIter} p.o. with $numIter iterations") - } -} - -internal fun benchmarkingMatMultFloat( - scale: Int, - numWarmUp: Int, - numIter: Int, - device: Device = Device.CPU -): Unit { - TorchTensorFloatAlgebra { - println("Benchmarking $scale x $scale matrices over Float's on $device: ") - setSeed(SEED) - val lhs = randNormal(shape = intArrayOf(scale, scale), device = device) - val rhs = randNormal(shape = intArrayOf(scale, scale), device = device) - repeat(numWarmUp) { lhs dotAssign rhs } - val measuredTime = measureTime { repeat(numIter) { lhs dotAssign rhs } } - println(" ${measuredTime / numIter} p.o. with $numIter iterations") - } -} - -internal class BenchmarkMatMult { - - @Test - fun benchmarkMatMultDouble20() = - benchmarkingMatMultDouble(20, 10, 100000) - - @Test - fun benchmarkMatMultFloat20() = - benchmarkingMatMultFloat(20, 10, 100000) - - @Test - fun benchmarkMatMultDouble200() = - benchmarkingMatMultDouble(200, 10, 10000) - - @Test - fun benchmarkMatMultFloat200() = - benchmarkingMatMultFloat(200, 10, 10000) - - @Test - fun benchmarkMatMultDouble2000() = - benchmarkingMatMultDouble(2000, 3, 20) - - @Test - fun benchmarkMatMultFloat2000() = - benchmarkingMatMultFloat(2000, 3, 20) - -} \ No newline at end of file diff --git a/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkRandomGenerators.kt b/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkRandomGenerators.kt index bc4d1ad12..f60277554 100644 --- a/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkRandomGenerators.kt +++ b/kmath-torch/src/nativeTest/kotlin/kscience/kmath/torch/BenchmarkRandomGenerators.kt @@ -1,107 +1,28 @@ package kscience.kmath.torch import kotlin.test.Test -import kotlin.time.measureTime - -internal fun benchmarkingRandNormal( - samples: Int, - numWarmUp: Int, - numIter: Int, - device: Device = Device.CPU): Unit -{ - TorchTensorFloatAlgebra{ - println("Benchmarking generation of $samples Normal samples on $device: ") - setSeed(SEED) - val shape = intArrayOf(samples) - val tensor = randNormal(shape = shape, device = device) - repeat(numWarmUp) { tensor.randNormalAssign() } - val measuredTime = measureTime { repeat(numIter) { tensor.randNormalAssign() } } - println(" ${measuredTime / numIter} p.o. with $numIter iterations") - } -} -internal fun benchmarkingRandUniform( - samples: Int, - numWarmUp: Int, - numIter: Int, - device: Device = Device.CPU): Unit -{ - TorchTensorFloatAlgebra{ - println("Benchmarking generation of $samples Uniform samples on $device: ") - setSeed(SEED) - val shape = intArrayOf(samples) - val tensor = randUniform(shape = shape, device = device) - repeat(numWarmUp) { tensor.randUniformAssign() } - val measuredTime = measureTime { repeat(numIter) { tensor.randUniformAssign() } } - println(" ${measuredTime / numIter} p.o. with $numIter iterations") - } -} - -internal fun benchmarkingRandIntegral( - samples: Int, - numWarmUp: Int, - numIter: Int, - device: Device = Device.CPU): Unit -{ - TorchTensorIntAlgebra { - println("Benchmarking generation of $samples integer [0,100] samples on $device: ") - setSeed(SEED) - val shape = intArrayOf(samples) - val tensor = randIntegral(0,100, shape = shape, device = device) - repeat(numWarmUp) { tensor.randIntegralAssign(0,100) } - val measuredTime = measureTime { repeat(numIter) { tensor.randIntegralAssign(0,100) } } - println(" ${measuredTime / numIter} p.o. with $numIter iterations") - } -} internal class BenchmarkRandomGenerators { @Test - fun benchmarkRandNormal1() = - benchmarkingRandNormal(10, 10, 100000) + fun benchmarkRand1() = TorchTensorFloatAlgebra{ + benchmarkingRand1() + } @Test - fun benchmarkRandUniform1() = - benchmarkingRandUniform(10, 10, 100000) + fun benchmarkRand3() = TorchTensorFloatAlgebra{ + benchmarkingRand3() + } @Test - fun benchmarkRandIntegral1() = - benchmarkingRandIntegral(10, 10, 100000) + fun benchmarkRand5() = TorchTensorFloatAlgebra{ + benchmarkingRand5() + } @Test - fun benchmarkRandNormal3() = - benchmarkingRandNormal(1000, 10, 10000) - - @Test - fun benchmarkRandUniform3() = - benchmarkingRandUniform(1000, 10, 10000) - - @Test - fun benchmarkRandIntegral3() = - benchmarkingRandIntegral(1000, 10, 10000) - - @Test - fun benchmarkRandNormal5() = - benchmarkingRandNormal(100000, 5, 100) - - @Test - fun benchmarkRandUniform5() = - benchmarkingRandUniform(100000, 5, 100) - - @Test - fun benchmarkRandIntegral5() = - benchmarkingRandIntegral(100000, 5, 100) - - @Test - fun benchmarkRandNormal7() = - benchmarkingRandNormal(10000000, 3, 20) - - @Test - fun benchmarkRandUniform7() = - benchmarkingRandUniform(10000000, 3, 20) - - @Test - fun benchmarkRandIntegral7() = - benchmarkingRandIntegral(10000000, 3, 20) + fun benchmarkRand7() = TorchTensorFloatAlgebra{ + benchmarkingRand7() + } } \ No newline at end of file