|
| 1 | +function test_noallocs(skip::Bool, func, args...) |
| 2 | + possible_allocations = check_allocs(func, typeof.(args); ignore_throw=true) |
| 3 | + return @test isempty(possible_allocations) skip = skip |
| 4 | +end |
| 5 | + |
| 6 | +for op in ALL_OPS |
| 7 | + op! = Symbol(op, "!") |
| 8 | + val_prefix = if op == :second_derivative |
| 9 | + "value_derivative_and_" |
| 10 | + elseif op in [:hessian, :hvp] |
| 11 | + "value_gradient_and_" |
| 12 | + else |
| 13 | + "value_and_" |
| 14 | + end |
| 15 | + val_and_op = Symbol(val_prefix, op) |
| 16 | + val_and_op! = Symbol(val_prefix, op!) |
| 17 | + prep_op = Symbol("prepare_", op) |
| 18 | + |
| 19 | + S1out = Scenario{op,:out,:out} |
| 20 | + S1in = Scenario{op,:in,:out} |
| 21 | + S2out = Scenario{op,:out,:in} |
| 22 | + S2in = Scenario{op,:in,:in} |
| 23 | + |
| 24 | + if op in [:derivative, :gradient, :jacobian] |
| 25 | + @eval function test_alloccheck( |
| 26 | + ba::AbstractADType, scen::$S1out; subset::Symbol, skip::Bool |
| 27 | + ) |
| 28 | + (; f, x, contexts) = deepcopy(scen) |
| 29 | + prep = $prep_op(f, ba, x, contexts...) |
| 30 | + (subset == :full) && test_noallocs(skip, $prep_op, f, ba, x, contexts...) |
| 31 | + (subset == :full) && test_noallocs(skip, $op, f, ba, x, contexts...) |
| 32 | + (subset == :full) && test_noallocs(skip, $val_and_op, f, ba, x, contexts...) |
| 33 | + (subset != :none) && test_noallocs(skip, $op, f, prep, ba, x, contexts...) |
| 34 | + (subset != :none) && |
| 35 | + test_noallocs(skip, $val_and_op, f, prep, ba, x, contexts...) |
| 36 | + return nothing |
| 37 | + end |
| 38 | + |
| 39 | + @eval function test_alloccheck( |
| 40 | + ba::AbstractADType, scen::$S1in; subset::Symbol, skip::Bool |
| 41 | + ) |
| 42 | + (; f, x, res1, contexts) = deepcopy(scen) |
| 43 | + res1_sim = mysimilar(res1) |
| 44 | + prep = $prep_op(f, ba, x, contexts...) |
| 45 | + (subset == :full) && test_noallocs(skip, $prep_op, f, ba, x, contexts...) |
| 46 | + (subset == :full) && |
| 47 | + test_noallocs(skip, $op!, f, res1_sim, prep, ba, x, contexts...) |
| 48 | + (subset == :full) && |
| 49 | + test_noallocs(skip, $val_and_op!, f, res1_sim, prep, ba, x, contexts...) |
| 50 | + (subset != :none) && |
| 51 | + test_noallocs(skip, $op!, f, res1_sim, prep, ba, x, contexts...) |
| 52 | + (subset != :none) && |
| 53 | + test_noallocs(skip, $val_and_op!, f, res1_sim, prep, ba, x, contexts...) |
| 54 | + return nothing |
| 55 | + end |
| 56 | + |
| 57 | + op == :gradient && continue |
| 58 | + |
| 59 | + @eval function test_alloccheck( |
| 60 | + ba::AbstractADType, scen::$S2out; subset::Symbol, skip::Bool |
| 61 | + ) |
| 62 | + (; f, x, y, contexts) = deepcopy(scen) |
| 63 | + prep = $prep_op(f, y, ba, x, contexts...) |
| 64 | + (subset == :full) && test_noallocs(skip, $prep_op, f, y, ba, x, contexts...) |
| 65 | + (subset == :full) && test_noallocs(skip, $op, f, y, ba, x, contexts...) |
| 66 | + (subset == :full) && test_noallocs(skip, $val_and_op, f, y, ba, x, contexts...) |
| 67 | + (subset != :none) && test_noallocs(skip, $op, f, y, prep, ba, x, contexts...) |
| 68 | + (subset != :none) && |
| 69 | + test_noallocs(skip, $val_and_op, f, y, prep, ba, x, contexts...) |
| 70 | + return nothing |
| 71 | + end |
| 72 | + |
| 73 | + @eval function test_alloccheck( |
| 74 | + ba::AbstractADType, scen::$S2in; subset::Symbol, skip::Bool |
| 75 | + ) |
| 76 | + (; f, x, y, res1, contexts) = deepcopy(scen) |
| 77 | + res1_sim = mysimilar(res1) |
| 78 | + prep = $prep_op(f, y, ba, x, contexts...) |
| 79 | + (subset == :full) && test_noallocs(skip, $prep_op, f, y, ba, x, contexts...) |
| 80 | + (subset == :full) && |
| 81 | + test_noallocs(skip, $op!, f, y, res1_sim, ba, x, contexts...) |
| 82 | + (subset == :full) && |
| 83 | + test_noallocs(skip, $val_and_op!, f, y, res1_sim, ba, x, contexts...) |
| 84 | + (subset != :none) && |
| 85 | + test_noallocs(skip, $op!, f, y, res1_sim, prep, ba, x, contexts...) |
| 86 | + (subset != :none) && |
| 87 | + test_noallocs(skip, $val_and_op!, f, y, res1_sim, prep, ba, x, contexts...) |
| 88 | + return nothing |
| 89 | + end |
| 90 | + |
| 91 | + elseif op in [:second_derivative, :hessian] |
| 92 | + @eval function test_alloccheck( |
| 93 | + ba::AbstractADType, scen::$S1out; subset::Symbol, skip::Bool |
| 94 | + ) |
| 95 | + (; f, x, contexts) = deepcopy(scen) |
| 96 | + prep = $prep_op(f, ba, x, contexts...) |
| 97 | + (subset == :full) && test_noallocs(skip, $prep_op, f, ba, x, contexts...) |
| 98 | + (subset == :full) && test_noallocs(skip, $op, f, ba, x, contexts...) |
| 99 | + (subset == :full) && test_noallocs(skip, $val_and_op, f, ba, x, contexts...) |
| 100 | + (subset != :none) && test_noallocs(skip, $op, f, prep, ba, x, contexts...) |
| 101 | + (subset != :none) && |
| 102 | + test_noallocs(skip, $val_and_op, f, prep, ba, x, contexts...) |
| 103 | + return nothing |
| 104 | + end |
| 105 | + |
| 106 | + @eval function test_alloccheck( |
| 107 | + ba::AbstractADType, scen::$S1in; subset::Symbol, skip::Bool |
| 108 | + ) |
| 109 | + (; f, x, res1, res2, contexts) = deepcopy(scen) |
| 110 | + res1_sim, res2_sim = mysimilar(res1), mysimilar(res2) |
| 111 | + prep = $prep_op(f, ba, x, contexts...) |
| 112 | + (subset == :full) && test_noallocs(skip, $prep_op, f, ba, x, contexts...) |
| 113 | + (subset == :full) && test_noallocs(skip, $op!, f, res2_sim, ba, x, contexts...) |
| 114 | + (subset == :full) && |
| 115 | + test_noallocs(skip, $val_and_op!, f, res1_sim, res2_sim, ba, x, contexts...) |
| 116 | + (subset != :none) && |
| 117 | + test_noallocs(skip, $op!, f, res2_sim, prep, ba, x, contexts...) |
| 118 | + (subset != :none) && test_noallocs( |
| 119 | + skip, $val_and_op!, f, res1_sim, res2_sim, prep, ba, x, contexts... |
| 120 | + ) |
| 121 | + return nothing |
| 122 | + end |
| 123 | + |
| 124 | + elseif op in [:pushforward, :pullback] |
| 125 | + @eval function test_alloccheck( |
| 126 | + ba::AbstractADType, scen::$S1out; subset::Symbol, skip::Bool |
| 127 | + ) |
| 128 | + (; f, x, tang, contexts) = deepcopy(scen) |
| 129 | + prep = $prep_op(f, ba, x, tang, contexts...) |
| 130 | + (subset == :full) && test_noallocs(skip, $prep_op, f, ba, x, tang, contexts...) |
| 131 | + (subset == :full) && test_noallocs(skip, $op, f, ba, x, tang, contexts...) |
| 132 | + (subset == :full) && |
| 133 | + test_noallocs(skip, $val_and_op, f, ba, x, tang, contexts...) |
| 134 | + (subset != :none) && test_noallocs(skip, $op, f, prep, ba, x, tang, contexts...) |
| 135 | + (subset != :none) && |
| 136 | + test_noallocs(skip, $val_and_op, f, prep, ba, x, tang, contexts...) |
| 137 | + return nothing |
| 138 | + end |
| 139 | + |
| 140 | + @eval function test_alloccheck( |
| 141 | + ba::AbstractADType, scen::$S1in; subset::Symbol, skip::Bool |
| 142 | + ) |
| 143 | + (; f, x, tang, res1, contexts) = deepcopy(scen) |
| 144 | + res1_sim = mysimilar(res1) |
| 145 | + prep = $prep_op(f, ba, x, tang, contexts...) |
| 146 | + (subset == :full) && test_noallocs(skip, $prep_op, f, ba, x, tang, contexts...) |
| 147 | + (subset == :full) && |
| 148 | + test_noallocs(skip, $op!, f, res1_sim, ba, x, tang, contexts...) |
| 149 | + (subset == :full) && |
| 150 | + test_noallocs(skip, $val_and_op!, f, res1_sim, ba, x, tang, contexts...) |
| 151 | + (subset != :none) && |
| 152 | + test_noallocs(skip, $op!, f, res1_sim, prep, ba, x, tang, contexts...) |
| 153 | + (subset != :none) && test_noallocs( |
| 154 | + skip, $val_and_op!, f, res1_sim, prep, ba, x, tang, contexts... |
| 155 | + ) |
| 156 | + return nothing |
| 157 | + end |
| 158 | + |
| 159 | + @eval function test_alloccheck( |
| 160 | + ba::AbstractADType, scen::$S2out; subset::Symbol, skip::Bool |
| 161 | + ) |
| 162 | + (; f, x, y, tang, contexts) = deepcopy(scen) |
| 163 | + prep = $prep_op(f, y, ba, x, tang, contexts...) |
| 164 | + (subset == :full) && |
| 165 | + test_noallocs(skip, $prep_op, f, y, ba, x, tang, contexts...) |
| 166 | + (subset == :full) && test_noallocs(skip, $op, f, y, ba, x, tang, contexts...) |
| 167 | + (subset == :full) && |
| 168 | + test_noallocs(skip, $val_and_op, f, y, ba, x, tang, contexts...) |
| 169 | + (subset != :none) && |
| 170 | + test_noallocs(skip, $op, f, y, prep, ba, x, tang, contexts...) |
| 171 | + (subset != :none) && |
| 172 | + test_noallocs(skip, $val_and_op, f, y, prep, ba, x, tang, contexts...) |
| 173 | + return nothing |
| 174 | + end |
| 175 | + |
| 176 | + @eval function test_alloccheck( |
| 177 | + ba::AbstractADType, scen::$S2in; subset::Symbol, skip::Bool |
| 178 | + ) |
| 179 | + (; f, x, y, tang, res1, contexts) = deepcopy(scen) |
| 180 | + res1_sim = mysimilar(res1) |
| 181 | + prep = $prep_op(f, y, ba, x, tang, contexts...) |
| 182 | + (subset == :full) && |
| 183 | + test_noallocs(skip, $prep_op, f, y, ba, x, tang, contexts...) |
| 184 | + (subset == :full) && |
| 185 | + test_noallocs(skip, $op!, f, y, res1_sim, ba, x, tang, contexts...) |
| 186 | + (subset == :full) && |
| 187 | + test_noallocs(skip, $val_and_op!, f, y, res1_sim, ba, x, tang, contexts...) |
| 188 | + (subset != :none) && |
| 189 | + test_noallocs(skip, $op!, f, y, res1_sim, prep, ba, x, tang, contexts...) |
| 190 | + (subset != :none) && test_noallocs( |
| 191 | + skip, $val_and_op!, f, y, res1_sim, prep, ba, x, tang, contexts... |
| 192 | + ) |
| 193 | + return nothing |
| 194 | + end |
| 195 | + |
| 196 | + elseif op in [:hvp] |
| 197 | + @eval function test_alloccheck( |
| 198 | + ba::AbstractADType, scen::$S1out; subset::Symbol, skip::Bool |
| 199 | + ) |
| 200 | + (; f, x, tang, contexts) = deepcopy(scen) |
| 201 | + prep = $prep_op(f, ba, x, tang, contexts...) |
| 202 | + (subset == :full) && test_noallocs(skip, $prep_op, f, ba, x, tang, contexts...) |
| 203 | + (subset == :full) && test_noallocs(skip, $op, f, ba, x, tang, contexts...) |
| 204 | + (subset != :none) && test_noallocs(skip, $op, f, prep, ba, x, tang, contexts...) |
| 205 | + return nothing |
| 206 | + end |
| 207 | + |
| 208 | + @eval function test_alloccheck( |
| 209 | + ba::AbstractADType, scen::$S1in; subset::Symbol, skip::Bool |
| 210 | + ) |
| 211 | + (; f, x, tang, res1, res2, contexts) = deepcopy(scen) |
| 212 | + res1_sim, res2_sim = mysimilar(res1), mysimilar(res2) |
| 213 | + prep = $prep_op(f, ba, x, tang, contexts...) |
| 214 | + (subset == :full) && test_noallocs(skip, $prep_op, f, ba, x, tang, contexts...) |
| 215 | + (subset == :full) && |
| 216 | + test_noallocs(skip, $op!, f, res2_sim, ba, x, tang, contexts...) |
| 217 | + (subset != :none) && |
| 218 | + test_noallocs(skip, $op!, f, res2_sim, prep, ba, x, tang, contexts...) |
| 219 | + return nothing |
| 220 | + end |
| 221 | + end |
| 222 | +end |
0 commit comments