Moving benchmarks implementations to common

This commit is contained in:
Roland Grinis 2021-01-19 07:36:45 +00:00
parent 274d1a3105
commit d599d1132b
7 changed files with 195 additions and 241 deletions

View File

@ -0,0 +1,24 @@
@file:Suppress("NOTHING_TO_INLINE")
package kscience.kmath.torch
import kotlin.time.measureTime
internal inline fun <T, PrimitiveArrayType, TorchTensorType : TorchTensorOverField<T>,
TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra<T, PrimitiveArrayType, TorchTensorType>>
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")
}

View File

@ -0,0 +1,132 @@
@file:Suppress("NOTHING_TO_INLINE")
package kscience.kmath.torch
import kotlin.time.measureTime
internal inline fun <TorchTensorType : TorchTensorOverField<Float>,
TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra<Float, FloatArray, TorchTensorType>>
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 <TorchTensorType : TorchTensorOverField<Float>,
TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra<Float, FloatArray, TorchTensorType>>
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 <TorchTensorType : TorchTensorOverField<Float>,
TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra<Float, FloatArray, TorchTensorType>>
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 <TorchTensorType : TorchTensorOverField<Float>,
TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra<Float, FloatArray, TorchTensorType>>
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 <TorchTensorType : TorchTensorOverField<Float>,
TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra<Float, FloatArray, TorchTensorType>>
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 <TorchTensorType : TorchTensorOverField<Float>,
TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra<Float, FloatArray, TorchTensorType>>
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 <TorchTensorType : TorchTensorOverField<Float>,
TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra<Float, FloatArray, TorchTensorType>>
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 <TorchTensorType : TorchTensorOverField<Float>,
TorchTensorAlgebraType : TorchTensorPartialDivisionAlgebra<Float, FloatArray, TorchTensorType>>
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))
}
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))
}
}
}

View File

@ -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)
}

View File

@ -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()
}
}