-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sort! rules * Sort in augmented primal * sort! rule tests * Batched sort! rule * Move A \ B rule test * Fix after rebase * Add missing end --------- Co-authored-by: William Moses <[email protected]>
- Loading branch information
1 parent
10d380b
commit 770b064
Showing
3 changed files
with
162 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module InternalRules | ||
|
||
using Enzyme | ||
using Enzyme.EnzymeRules | ||
using Test | ||
|
||
@testset "Internal rules" begin | ||
function f1(x) | ||
a = [1.0, 3.0, x] | ||
sort!(a) | ||
return a[2] | ||
end | ||
|
||
@test autodiff(Forward, f1, Duplicated(2.0, 1.0))[1] == 1 | ||
@test autodiff(Forward, f1, BatchDuplicated(2.0, (1.0, 2.0)))[1] == (var"1"=1.0, var"2"=2.0) | ||
@test autodiff(Reverse, f1, Active, Active(2.0))[1][1] == 1 | ||
@test autodiff(Forward, f1, Duplicated(4.0, 1.0))[1] == 0 | ||
@test autodiff(Forward, f1, BatchDuplicated(4.0, (1.0, 2.0)))[1] == (var"1"=0.0, var"2"=0.0) | ||
@test autodiff(Reverse, f1, Active, Active(4.0))[1][1] == 0 | ||
|
||
function f2(x) | ||
a = [1.0, -3.0, -x, -2x, x] | ||
sort!(a; rev=true, lt=(x, y) -> abs(x) < abs(y) || (abs(x) == abs(y) && x < y)) | ||
return sum(a .* [1, 2, 3, 4, 5]) | ||
end | ||
|
||
@test autodiff(Forward, f2, Duplicated(2.0, 1.0))[1] == -3 | ||
@test autodiff(Forward, f2, BatchDuplicated(2.0, (1.0, 2.0)))[1] == (var"1"=-3.0, var"2"=-6.0) | ||
@test autodiff(Reverse, f2, Active, Active(2.0))[1][1] == -3 | ||
end | ||
|
||
@testset "Linear Solve" begin | ||
A = Float64[2 3; 5 7] | ||
dA = zero(A) | ||
b = Float64[11, 13] | ||
db = zero(b) | ||
|
||
forward, pullback = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(\)}, Duplicated, Duplicated{typeof(A)}, Duplicated{typeof(b)}) | ||
|
||
tape, primal, shadow = forward(Const(\), Duplicated(A, dA), Duplicated(b, db)) | ||
|
||
dy = Float64[17, 19] | ||
copyto!(shadow, dy) | ||
|
||
pullback(Const(\), Duplicated(A, dA), Duplicated(b, db), tape) | ||
|
||
z = transpose(A) \ dy | ||
|
||
y = A \ b | ||
@test dA ≈ (-z * transpose(y)) | ||
@test db ≈ z | ||
|
||
db = zero(b) | ||
|
||
forward, pullback = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(\)}, Duplicated, Const{typeof(A)}, Duplicated{typeof(b)}) | ||
|
||
tape, primal, shadow = forward(Const(\), Const(A), Duplicated(b, db)) | ||
|
||
dy = Float64[17, 19] | ||
copyto!(shadow, dy) | ||
|
||
pullback(Const(\), Const(A), Duplicated(b, db), tape) | ||
|
||
z = transpose(A) \ dy | ||
|
||
y = A \ b | ||
@test db ≈ z | ||
|
||
dA = zero(A) | ||
|
||
forward, pullback = Enzyme.autodiff_thunk(ReverseSplitNoPrimal, Const{typeof(\)}, Duplicated, Duplicated{typeof(A)}, Const{typeof(b)}) | ||
|
||
tape, primal, shadow = forward(Const(\), Duplicated(A, dA), Const(b)) | ||
|
||
dy = Float64[17, 19] | ||
copyto!(shadow, dy) | ||
|
||
pullback(Const(\), Duplicated(A, dA), Const(b), tape) | ||
|
||
z = transpose(A) \ dy | ||
|
||
y = A \ b | ||
@test dA ≈ (-z * transpose(y)) | ||
end | ||
|
||
end # InternalRules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters