|
| 1 | +module InternalRules |
| 2 | + |
| 3 | +using Enzyme |
| 4 | +using Enzyme.EnzymeRules |
| 5 | +using Test |
| 6 | + |
| 7 | +@testset "Internal rules" begin |
| 8 | + function f1(x) |
| 9 | + a = [1.0, 3.0, x] |
| 10 | + sort!(a) |
| 11 | + return a[2] |
| 12 | + end |
| 13 | + |
| 14 | + @test autodiff(Forward, f1, Duplicated(2.0, 1.0))[1] == 1 |
| 15 | + @test autodiff(Forward, f1, BatchDuplicated(2.0, (1.0, 2.0)))[1] == (var"1"=1.0, var"2"=2.0) |
| 16 | + @test autodiff(Reverse, f1, Active, Active(2.0))[1][1] == 1 |
| 17 | + @test autodiff(Forward, f1, Duplicated(4.0, 1.0))[1] == 0 |
| 18 | + @test autodiff(Forward, f1, BatchDuplicated(4.0, (1.0, 2.0)))[1] == (var"1"=0.0, var"2"=0.0) |
| 19 | + @test autodiff(Reverse, f1, Active, Active(4.0))[1][1] == 0 |
| 20 | + |
| 21 | + function f2(x) |
| 22 | + a = [1.0, -3.0, -x, -2x, x] |
| 23 | + sort!(a; rev=true, lt=(x, y) -> abs(x) < abs(y) || (abs(x) == abs(y) && x < y)) |
| 24 | + return sum(a .* [1, 2, 3, 4, 5]) |
| 25 | + end |
| 26 | + |
| 27 | + @test autodiff(Forward, f2, Duplicated(2.0, 1.0))[1] == -3 |
| 28 | + @test autodiff(Forward, f2, BatchDuplicated(2.0, (1.0, 2.0)))[1] == (var"1"=-3.0, var"2"=-6.0) |
| 29 | + @test autodiff(Reverse, f2, Active, Active(2.0))[1][1] == -3 |
| 30 | +end |
| 31 | + |
| 32 | +@testset "Linear Solve" begin |
| 33 | + A = Float64[2 3; 5 7] |
| 34 | + dA = zero(A) |
| 35 | + b = Float64[11, 13] |
| 36 | + db = zero(b) |
| 37 | + |
| 38 | + forward, pullback = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(\)}, Duplicated, Duplicated{typeof(A)}, Duplicated{typeof(b)}) |
| 39 | + |
| 40 | + tape, primal, shadow = forward(Const(\), Duplicated(A, dA), Duplicated(b, db)) |
| 41 | + |
| 42 | + dy = Float64[17, 19] |
| 43 | + copyto!(shadow, dy) |
| 44 | + |
| 45 | + pullback(Const(\), Duplicated(A, dA), Duplicated(b, db), tape) |
| 46 | + |
| 47 | + z = transpose(A) \ dy |
| 48 | + |
| 49 | + y = A \ b |
| 50 | + @test dA ≈ (-z * transpose(y)) |
| 51 | + @test db ≈ z |
| 52 | + |
| 53 | + db = zero(b) |
| 54 | + |
| 55 | + forward, pullback = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(\)}, Duplicated, Const{typeof(A)}, Duplicated{typeof(b)}) |
| 56 | + |
| 57 | + tape, primal, shadow = forward(Const(\), Const(A), Duplicated(b, db)) |
| 58 | + |
| 59 | + dy = Float64[17, 19] |
| 60 | + copyto!(shadow, dy) |
| 61 | + |
| 62 | + pullback(Const(\), Const(A), Duplicated(b, db), tape) |
| 63 | + |
| 64 | + z = transpose(A) \ dy |
| 65 | + |
| 66 | + y = A \ b |
| 67 | + @test db ≈ z |
| 68 | + |
| 69 | + dA = zero(A) |
| 70 | + |
| 71 | + forward, pullback = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(\)}, Duplicated, Duplicated{typeof(A)}, Const{typeof(b)}) |
| 72 | + |
| 73 | + tape, primal, shadow = forward(Const(\), Duplicated(A, dA), Const(b)) |
| 74 | + |
| 75 | + dy = Float64[17, 19] |
| 76 | + copyto!(shadow, dy) |
| 77 | + |
| 78 | + pullback(Const(\), Duplicated(A, dA), Const(b), tape) |
| 79 | + |
| 80 | + z = transpose(A) \ dy |
| 81 | + |
| 82 | + y = A \ b |
| 83 | + @test dA ≈ (-z * transpose(y)) |
| 84 | +end |
| 85 | + |
| 86 | +end # InternalRules |
0 commit comments