|
| 1 | +using DynamicExpressions, BenchmarkTools, Random |
| 2 | + |
| 3 | +const v_PACKAGE_VERSION = try |
| 4 | + VersionNumber(PACKAGE_VERSION) |
| 5 | +catch |
| 6 | + VersionNumber("v0.0.0") |
| 7 | +end |
| 8 | + |
| 9 | +const SUITE = BenchmarkGroup() |
| 10 | + |
| 11 | +SUITE["OperatorEnum"] = BenchmarkGroup() |
| 12 | + |
| 13 | +operators = OperatorEnum(; |
| 14 | + binary_operators=[+, -, /, *], unary_operators=[cos, exp], enable_autodiff=true |
| 15 | +) |
| 16 | +simple_tree = Node( |
| 17 | + 2, |
| 18 | + Node( |
| 19 | + 1, |
| 20 | + Node(3, Node(1, Node(; val=1.0f0), Node(; feature=2)), Node(2, Node(; val=-1.0f0))), |
| 21 | + Node(1, Node(; feature=3), Node(; feature=4)), |
| 22 | + ), |
| 23 | + Node( |
| 24 | + 4, |
| 25 | + Node(3, Node(1, Node(; val=1.0f0), Node(; feature=2)), Node(2, Node(; val=-1.0f0))), |
| 26 | + Node(1, Node(; feature=3), Node(; feature=4)), |
| 27 | + ), |
| 28 | +) |
| 29 | +for T in (ComplexF32, ComplexF64, Float32, Float64) |
| 30 | + !(T <: Real) && v_PACKAGE_VERSION < v"0.5.0" && continue |
| 31 | + evals = 10 |
| 32 | + samples = 1_000 |
| 33 | + n = 1_000 |
| 34 | + |
| 35 | + #! format: off |
| 36 | + if !haskey(SUITE["OperatorEnum"], T) |
| 37 | + SUITE["OperatorEnum"][T] = BenchmarkGroup() |
| 38 | + end |
| 39 | + |
| 40 | + for turbo in (false, true) |
| 41 | + turbo && !(T in (Float32, Float64)) && continue |
| 42 | + extra_key = turbo ? "_turbo" : "" |
| 43 | + SUITE["OperatorEnum"][T]["evaluation$(extra_key)"] = @benchmarkable( |
| 44 | + eval_tree_array(tree, X, $operators), |
| 45 | + evals=evals, |
| 46 | + samples=samples, |
| 47 | + seconds=5.0, |
| 48 | + setup=( |
| 49 | + X=randn(MersenneTwister(0), $T, 5, $n); |
| 50 | + tree=convert(Node{$T}, copy_node($simple_tree)) |
| 51 | + ) |
| 52 | + ) |
| 53 | + if T <: Real |
| 54 | + SUITE["OperatorEnum"][T]["derivative$(extra_key)"] = @benchmarkable( |
| 55 | + eval_grad_tree_array(tree, X, $operators; variable=true), |
| 56 | + evals=evals, |
| 57 | + samples=samples, |
| 58 | + seconds=5.0, |
| 59 | + setup=( |
| 60 | + X=randn(MersenneTwister(0), $T, 5, $n); |
| 61 | + tree=convert(Node{$T}, copy_node($simple_tree)) |
| 62 | + ) |
| 63 | + ) |
| 64 | + end |
| 65 | + end |
| 66 | + #! format: on |
| 67 | +end |
0 commit comments