|
| 1 | +import SnoopPrecompile: @precompile_all_calls, @precompile_setup |
| 2 | + |
| 3 | +macro ignore_domain_error(ex) |
| 4 | + return esc( |
| 5 | + quote |
| 6 | + try |
| 7 | + $ex |
| 8 | + catch e |
| 9 | + if !(e isa DomainError) |
| 10 | + rethrow(e) |
| 11 | + end |
| 12 | + end |
| 13 | + end, |
| 14 | + ) |
| 15 | +end |
| 16 | + |
| 17 | +""" |
| 18 | + test_all_combinations(; binary_operators, unary_operators, turbo, types) |
| 19 | +
|
| 20 | +Test all combinations of the given operators and types. Useful for precompilation. |
| 21 | +""" |
| 22 | +function test_all_combinations(; binary_operators, unary_operators, turbo, types) |
| 23 | + for binops in binary_operators, |
| 24 | + unaops in unary_operators, |
| 25 | + use_turbo in turbo, |
| 26 | + T in types |
| 27 | + |
| 28 | + length(binops) == 0 && length(unaops) == 0 && continue |
| 29 | + T == Float16 && use_turbo && continue |
| 30 | + |
| 31 | + X = rand(T, 3, 10) |
| 32 | + operators = OperatorEnum(; |
| 33 | + binary_operators=binops, |
| 34 | + unary_operators=unaops, |
| 35 | + define_helper_functions=false, |
| 36 | + enable_autodiff=true, |
| 37 | + ) |
| 38 | + x = Node(T; feature=1) |
| 39 | + c = Node(T; val=one(T)) |
| 40 | + |
| 41 | + # Trivial: |
| 42 | + for l in (x, c) |
| 43 | + @ignore_domain_error eval_tree_array(l, X, operators; turbo=use_turbo) |
| 44 | + for variable in (true, false) |
| 45 | + @ignore_domain_error eval_grad_tree_array( |
| 46 | + l, X, operators; turbo=use_turbo, variable |
| 47 | + ) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + # Binary operators |
| 52 | + for i in eachindex(binops), l in (x, c), r in (x, c) |
| 53 | + tree = Node(i, l, r) |
| 54 | + tree = convert(Node{T}, tree) |
| 55 | + @ignore_domain_error eval_tree_array(tree, X, operators; turbo=use_turbo) |
| 56 | + for variable in (true, false) |
| 57 | + @ignore_domain_error eval_grad_tree_array( |
| 58 | + l, X, operators; turbo=use_turbo, variable |
| 59 | + ) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + # Unary operators |
| 64 | + for j in eachindex(unaops), k in eachindex(unaops), l in (x, c) |
| 65 | + tree = Node(j, l) |
| 66 | + tree = convert(Node{T}, tree) |
| 67 | + @ignore_domain_error eval_tree_array(tree, X, operators; turbo=use_turbo) |
| 68 | + for variable in (true, false) |
| 69 | + @ignore_domain_error eval_grad_tree_array( |
| 70 | + l, X, operators; turbo=use_turbo, variable |
| 71 | + ) |
| 72 | + end |
| 73 | + |
| 74 | + tree = Node(j, Node(k, l)) |
| 75 | + tree = convert(Node{T}, tree) |
| 76 | + @ignore_domain_error eval_tree_array(tree, X, operators; turbo=use_turbo) |
| 77 | + for variable in (true, false) |
| 78 | + @ignore_domain_error eval_grad_tree_array( |
| 79 | + l, X, operators; turbo=use_turbo, variable |
| 80 | + ) |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + # Both operators |
| 85 | + for i in eachindex(binary_operators), |
| 86 | + j1 in eachindex(unary_operators), |
| 87 | + j2 in eachindex(unary_operators), |
| 88 | + l in (x, c), |
| 89 | + r in (x, c) |
| 90 | + |
| 91 | + tree = Node(i, Node(j1, l), Node(j2, r)) |
| 92 | + tree = convert(Node{T}, tree) |
| 93 | + @ignore_domain_error eval_tree_array(tree, X, operators; turbo=use_turbo) |
| 94 | + for variable in (true, false) |
| 95 | + @ignore_domain_error eval_grad_tree_array( |
| 96 | + l, X, operators; turbo=use_turbo, variable |
| 97 | + ) |
| 98 | + end |
| 99 | + |
| 100 | + tree = Node(j1, Node(i, l, r)) |
| 101 | + tree = convert(Node{T}, tree) |
| 102 | + @ignore_domain_error eval_tree_array(tree, X, operators; turbo=use_turbo) |
| 103 | + for variable in (true, false) |
| 104 | + @ignore_domain_error eval_grad_tree_array( |
| 105 | + l, X, operators; turbo=use_turbo, variable |
| 106 | + ) |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + return nothing |
| 111 | +end |
| 112 | + |
| 113 | +function test_functions_on_trees(::Type{T}, operators) where {T} |
| 114 | + local x, c, tree |
| 115 | + for T1 in [Float16, Float32, Float64] |
| 116 | + x = Node(T1; feature=1) |
| 117 | + c = Node(T1; val=T1(1.0)) |
| 118 | + tree = Node( |
| 119 | + 2, |
| 120 | + Node(1, Node(1, Node(2, x, c), Node(3, c, Node(1, x)))), |
| 121 | + Node(3, Node(1, Node(4, x, x))), |
| 122 | + ) |
| 123 | + end |
| 124 | + tree = convert(Node{T}, tree) |
| 125 | + for preserve_topology in [true, false] |
| 126 | + tree = copy_node(tree; preserve_topology) |
| 127 | + set_node!(tree, copy_node(tree; preserve_topology)) |
| 128 | + end |
| 129 | + |
| 130 | + string_tree(tree, operators) |
| 131 | + count_nodes(tree) |
| 132 | + count_constants(tree) |
| 133 | + count_depth(tree) |
| 134 | + index_constants(tree) |
| 135 | + has_operators(tree) |
| 136 | + has_constants(tree) |
| 137 | + get_constants(tree) |
| 138 | + set_constants(tree, get_constants(tree)) |
| 139 | + combine_operators(tree, operators) |
| 140 | + simplify_tree(tree, operators) |
| 141 | + return nothing |
| 142 | +end |
| 143 | + |
| 144 | +macro maybe_precompile_setup(mode, ex) |
| 145 | + precompile_ex = Expr( |
| 146 | + :macrocall, Symbol("@precompile_setup"), LineNumberNode(@__LINE__), ex |
| 147 | + ) |
| 148 | + return quote |
| 149 | + if $(esc(mode)) == :compile |
| 150 | + $(esc(ex)) |
| 151 | + elseif $(esc(mode)) == :precompile |
| 152 | + $(esc(precompile_ex)) |
| 153 | + else |
| 154 | + error("Invalid value for mode: " * show($(esc(mode)))) |
| 155 | + end |
| 156 | + end |
| 157 | +end |
| 158 | + |
| 159 | +macro maybe_precompile_all_calls(mode, ex) |
| 160 | + precompile_ex = Expr( |
| 161 | + :macrocall, Symbol("@precompile_all_calls"), LineNumberNode(@__LINE__), ex |
| 162 | + ) |
| 163 | + return quote |
| 164 | + if $(esc(mode)) == :compile |
| 165 | + $(esc(ex)) |
| 166 | + elseif $(esc(mode)) == :precompile |
| 167 | + $(esc(precompile_ex)) |
| 168 | + else |
| 169 | + error("Invalid value for mode: " * show($(esc(mode)))) |
| 170 | + end |
| 171 | + end |
| 172 | +end |
| 173 | + |
| 174 | +"""`mode=:precompile` will use `@precompile_*` directives; `mode=:compile` runs.""" |
| 175 | +function do_precompilation(; mode=:precompile) |
| 176 | + @maybe_precompile_setup mode begin |
| 177 | + binary_operators = [[+, -, *, /, ^]] |
| 178 | + unary_operators = [[sin, cos, exp, log, sqrt, abs]] |
| 179 | + turbo = [true, false] |
| 180 | + types = [Float32, Float64] |
| 181 | + @maybe_precompile_all_calls mode begin |
| 182 | + test_all_combinations(; |
| 183 | + binary_operators=binary_operators, |
| 184 | + unary_operators=unary_operators, |
| 185 | + turbo=turbo, |
| 186 | + types=types, |
| 187 | + ) |
| 188 | + end |
| 189 | + operators = OperatorEnum(; |
| 190 | + binary_operators=binary_operators[1], |
| 191 | + unary_operators=unary_operators[1], |
| 192 | + define_helper_functions=false, |
| 193 | + ) |
| 194 | + # Want to precompile all above calls. |
| 195 | + types = [Float16, Float32, Float64] |
| 196 | + for T in types |
| 197 | + @maybe_precompile_all_calls mode begin |
| 198 | + test_functions_on_trees(T, operators) |
| 199 | + end |
| 200 | + end |
| 201 | + end |
| 202 | +end |
0 commit comments