@@ -9,6 +9,7 @@ import com.thoughtworks.raii.covariant._
9
9
import com .thoughtworks .tryt .covariant ._
10
10
import com .typesafe .scalalogging .StrictLogging
11
11
import org .lwjgl .opencl .CLCapabilities
12
+ import org .lwjgl .system .Configuration
12
13
import org .nd4j .linalg .api .ndarray .INDArray
13
14
import org .nd4j .linalg .convolution .Convolution
14
15
import org .nd4j .linalg .factory .Nd4j
@@ -24,24 +25,29 @@ object benchmarks {
24
25
25
26
@ Threads (value = Threads .MAX )
26
27
@ State (Scope .Benchmark )
27
- class Nd4jSigmoid extends SigmoidState {
28
+ class Nd4jTanh extends TanhState {
28
29
29
30
@ transient
30
31
private lazy val input = Nd4j .randn(Array .fill(numberOfDimensions)(size))
31
- private def sigmoid (x : INDArray ): INDArray = {
32
+ private def tanh (x : INDArray ): INDArray = {
32
33
val expX = Transforms .exp(x)
33
34
expX.div(expX.add(1.0 ))
34
35
}
35
36
@ Benchmark
36
- final def nd4jSigmoidBenchmark (): Array [Float ] = {
37
- sigmoid(input).data().asFloat()
37
+ final def nd4jTanhBenchmark (): Array [Float ] = {
38
+ (0 until numberOfIterations)
39
+ .foldLeft(input) { (input, _) =>
40
+ Transforms .tanh(input)
41
+ }
42
+ .data()
43
+ .asFloat()
38
44
}
39
45
40
46
}
41
47
42
48
@ Threads (value = Threads .MAX )
43
49
@ State (Scope .Benchmark )
44
- class TensorSigmoid extends SigmoidState {
50
+ class TensorTanh extends TanhState {
45
51
trait Benchmarks
46
52
extends StrictLogging
47
53
with Tensors .UnsafeMathOptimizations
@@ -56,18 +62,18 @@ object benchmarks {
56
62
57
63
protected val numberOfCommandQueuesPerDevice : Int = 2
58
64
59
- private def sigmoid (x : Tensor ): Tensor = {
60
- val expX = Tensor .exp(x)
61
- expX / (expX + Tensor .fill(1.0f , expX.shape))
62
- }
63
-
64
65
def doBenchmark (): Do [() => Array [Float ]] = {
65
66
val input = Tensor .randomNormal(Array .fill(numberOfDimensions)(size))
66
67
67
68
input.doBuffer.map { _ =>
68
69
{ () =>
69
- sigmoid(input).flatArray.run.blockingAwait
70
-
70
+ (0 until numberOfIterations)
71
+ .foldLeft(input) { (input, _) =>
72
+ Tensor .tanh(input)
73
+ }
74
+ .flatArray
75
+ .run
76
+ .blockingAwait
71
77
}
72
78
}
73
79
}
@@ -77,6 +83,7 @@ object benchmarks {
77
83
78
84
@ Setup
79
85
final def setup (): Unit = {
86
+ // Configuration.OPENCL_LIBRARY_NAME.set("/opt/pocl-1.1/lib/libOpenCL.dylib")
80
87
assert(benchmarkResouce == null )
81
88
val Do (TryT (ResourceT (resourceContinuation))) =
82
89
Do .monadicCloseable(Factory [Benchmarks ].newInstance()).flatMap(_.doBenchmark())
@@ -91,14 +98,17 @@ object benchmarks {
91
98
}
92
99
93
100
@ Benchmark
94
- final def tensorSigmoidBenchmark (): Array [Float ] = {
101
+ final def tensorTanhBenchmark (): Array [Float ] = {
95
102
benchmarkResouce.value.get.apply()
96
103
}
97
104
98
105
}
99
106
100
- trait SigmoidState {
101
- @ Param (Array (" 3" , " 2" , " 1" ))
107
+ trait TanhState {
108
+ @ Param (Array (" 100" , " 10" , " 1" ))
109
+ protected var numberOfIterations : Int = _
110
+
111
+ @ Param (Array (" 2" , " 3" , " 1" ))
102
112
protected var numberOfDimensions : Int = _
103
113
104
114
@ Param (Array (" 128" , " 64" , " 32" , " 16" ))
0 commit comments