|
1 | 1 | @testset "accumulation.jl" begin
|
2 |
| - @testset "scalar" begin |
3 |
| - @test 16 == add!!(12, 4) |
4 |
| - end |
| 2 | + @testset "is_inplaceable_destination" begin |
| 3 | + is_inplaceable_destination = ChainRulesCore.is_inplaceable_destination |
| 4 | + |
| 5 | + @test is_inplaceable_destination([1, 2, 3, 4]) |
| 6 | + @test !is_inplaceable_destination(1:4) |
| 7 | + |
| 8 | + @test is_inplaceable_destination(Diagonal([1, 2, 3, 4])) |
| 9 | + @test !is_inplaceable_destination(Diagonal(1:4)) |
5 | 10 |
|
6 |
| - @testset "Differentials" begin |
7 |
| - @test 16 == add!!(12, @thunk(2*2)) |
8 |
| - @test 16 == add!!(16, Zero()) |
| 11 | + @test is_inplaceable_destination(view([1, 2, 3, 4], :, :)) |
| 12 | + @test !is_inplaceable_destination(view(1:4, :, :)) |
9 | 13 |
|
10 |
| - @test 16 == add!!(16, DoesNotExist()) # Should this be an error? |
| 14 | + @test is_inplaceable_destination(falses(4)) |
| 15 | + @test is_inplaceable_destination(spzeros(4)) |
| 16 | + @test is_inplaceable_destination(spzeros(2, 2)) |
| 17 | + |
| 18 | + @test !is_inplaceable_destination(1.3) |
| 19 | + @test !is_inplaceable_destination(@SVector [1, 2, 3]) |
| 20 | + @test !is_inplaceable_destination(Hermitian([1 2; 2 4])) |
| 21 | + @test !is_inplaceable_destination(Symmetric([1 2; 2 4])) |
11 | 22 | end
|
12 | 23 |
|
13 |
| - @testset "Array" begin |
14 |
| - @testset "Happy Path" begin |
15 |
| - @testset "RHS Array" begin |
16 |
| - A = [1.0 2.0; 3.0 4.0] |
17 |
| - result = -1.0*ones(2,2) |
18 |
| - ret = add!!(result, A) |
19 |
| - @test ret === result # must be same object |
20 |
| - @test result == [0.0 1.0; 2.0 3.0] |
| 24 | + @testset "add!!" begin |
| 25 | + @testset "scalar" begin |
| 26 | + @test 16 == add!!(12, 4) |
| 27 | + end |
| 28 | + |
| 29 | + @testset "misc AbstractDifferential subtypes" begin |
| 30 | + @test 16 == add!!(12, @thunk(2*2)) |
| 31 | + @test 16 == add!!(16, Zero()) |
| 32 | + |
| 33 | + @test 16 == add!!(16, DoesNotExist()) # Should this be an error? |
| 34 | + end |
| 35 | + |
| 36 | + @testset "add!!(::AbstractArray, ::AbstractArray)" begin |
| 37 | + @testset "LHS Array (inplace)" begin |
| 38 | + @testset "RHS Array" begin |
| 39 | + A = [1.0 2.0; 3.0 4.0] |
| 40 | + accumuland = -1.0*ones(2,2) |
| 41 | + ret = add!!(accumuland, A) |
| 42 | + @test ret === accumuland # must be same object |
| 43 | + @test accumuland == [0.0 1.0; 2.0 3.0] |
| 44 | + end |
| 45 | + |
| 46 | + @testset "RHS StaticArray" begin |
| 47 | + A = @SMatrix[1.0 2.0; 3.0 4.0] |
| 48 | + accumuland = -1.0*ones(2,2) |
| 49 | + ret = add!!(accumuland, A) |
| 50 | + @test ret === accumuland # must be same object |
| 51 | + @test accumuland == [0.0 1.0; 2.0 3.0] |
| 52 | + end |
| 53 | + |
| 54 | + @testset "RHS Diagonal" begin |
| 55 | + A = Diagonal([1.0, 2.0]) |
| 56 | + accumuland = -1.0*ones(2,2) |
| 57 | + ret = add!!(accumuland, A) |
| 58 | + @test ret === accumuland # must be same object |
| 59 | + @test accumuland == [0.0 -1.0; -1.0 1.0] |
| 60 | + end |
21 | 61 | end
|
22 | 62 |
|
23 |
| - @testset "RHS StaticArray" begin |
24 |
| - A = @SMatrix[1.0 2.0; 3.0 4.0] |
25 |
| - result = -1.0*ones(2,2) |
26 |
| - ret = add!!(result, A) |
27 |
| - @test ret === result # must be same object |
28 |
| - @test result == [0.0 1.0; 2.0 3.0] |
| 63 | + @testset "add!!(::StaticArray, ::Array) (out of place)" begin |
| 64 | + A = [1.0 2.0; 3.0 4.0] |
| 65 | + accumuland = @SMatrix [-1.0 -1.0; -1.0 -1.0] |
| 66 | + ret = add!!(accumuland, A) |
| 67 | + @test ret == [0.0 1.0; 2.0 3.0] # must return right answer |
| 68 | + @test ret !== accumuland # must not be same object |
| 69 | + @test accumuland == [-1.0 -1.0; -1.0 -1.0] # must not have changed |
29 | 70 | end
|
30 | 71 |
|
31 |
| - @testset "RHS Diagonal" begin |
| 72 | + @testset "add!!(::Diagonal{<:Vector}, ::Diagonal{<:Vector}) (inplace)" begin |
32 | 73 | A = Diagonal([1.0, 2.0])
|
33 |
| - result = -1.0*ones(2,2) |
34 |
| - ret = add!!(result, A) |
35 |
| - @test ret === result # must be same object |
36 |
| - @test result == [0.0 -1.0; -1.0 1.0] |
| 74 | + accumuland = Diagonal([-2.0, -2.0]) |
| 75 | + ret = add!!(accumuland, A) |
| 76 | + @test ret === accumuland # must be same object |
| 77 | + @test accumuland == Diagonal([-1.0, 0.0]) |
| 78 | + end |
| 79 | + |
| 80 | + @testset "Unhappy Path" begin |
| 81 | + # wrong length |
| 82 | + @test_throws DimensionMismatch add!!(ones(4,4), ones(2,2)) |
| 83 | + # wrong shape |
| 84 | + @test_throws DimensionMismatch add!!(ones(4,4), ones(16)) |
| 85 | + # wrong type (adding scalar to array) |
| 86 | + @test_throws MethodError add!!(ones(4), 21.0) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + @testset "InplaceableThunk" begin |
| 91 | + ithunk = InplaceableThunk( |
| 92 | + @thunk(-1.0*ones(2, 2)), |
| 93 | + x -> x .-= ones(2, 2) |
| 94 | + ) |
| 95 | + |
| 96 | + @testset "in place" begin |
| 97 | + accumuland = [1.0 2.0; 3.0 4.0] |
| 98 | + ret = add!!(accumuland, ithunk) |
| 99 | + @test ret == [0.0 1.0; 2.0 3.0] # must return right answer |
| 100 | + @test ret === accumuland # must be same object |
| 101 | + end |
| 102 | + |
| 103 | + @testset "out of place" begin |
| 104 | + accumuland = @SMatrix [1.0 2.0; 3.0 4.0] |
| 105 | + |
| 106 | + ret = add!!(accumuland, ithunk) |
| 107 | + @test ret == [0.0 1.0; 2.0 3.0] # must return right answer |
| 108 | + @test ret !== accumuland # must not be same object |
| 109 | + @test accumuland == [1.0 2.0; 3.0 4.0] # must not have mutated |
37 | 110 | end
|
38 | 111 | end
|
39 | 112 |
|
40 |
| - @testset "Unhappy Path" begin |
41 |
| - # wrong length |
42 |
| - @test_throws DimensionMismatch add!!(ones(4,4), ones(2,2)) |
43 |
| - # wrong shape |
44 |
| - @test_throws DimensionMismatch add!!(ones(4,4), ones(16)) |
45 |
| - # wrong type (adding scalar to array) |
46 |
| - @test_throws MethodError add!!(ones(4), 21.0) |
| 113 | + @testset "not actually inplace but said it was" begin |
| 114 | + ithunk = InplaceableThunk( |
| 115 | + @thunk(@assert false), # this should never be used in this test |
| 116 | + x -> 77*ones(2, 2) # not actually inplace (also wrong) |
| 117 | + ) |
| 118 | + accumuland = ones(2, 2) |
| 119 | + @assert ChainRulesCore.debug_mode() == false |
| 120 | + # without debug being enabled should return the result, not error |
| 121 | + @test 77*ones(2, 2) == add!!(accumuland, ithunk) |
| 122 | + |
| 123 | + ChainRulesCore.debug_mode() = true # enable debug mode |
| 124 | + # with debug being enabled should error |
| 125 | + @test_throws ChainRulesCore.BadInplaceException add!!(accumuland, ithunk) |
| 126 | + ChainRulesCore.debug_mode() = false # disable it again |
47 | 127 | end
|
48 | 128 | end
|
49 | 129 |
|
50 |
| - @testset "InplaceableThunk" begin |
51 |
| - A=[1.0 2.0; 3.0 4.0] |
52 |
| - ithunk = InplaceableThunk( |
53 |
| - @thunk(A*B), |
54 |
| - x -> x.+=A |
55 |
| - ) |
56 |
| - |
57 |
| - result = -1.0*ones(2,2) |
58 |
| - ret = add!!(result, ithunk) |
59 |
| - @test ret === result # must be same object |
60 |
| - @test result == [0.0 1.0; 2.0 3.0] |
| 130 | + @testset "showerror BadInplaceException" begin |
| 131 | + BadInplaceException = ChainRulesCore.BadInplaceException |
| 132 | + ithunk = InplaceableThunk(@thunk(@assert false), x̄->nothing) |
| 133 | + msg = sprint(showerror, BadInplaceException(ithunk, [22], [23])) |
| 134 | + @test occursin("22", msg) |
| 135 | + |
| 136 | + msg_equal = sprint(showerror, BadInplaceException(ithunk, [22], [22])) |
| 137 | + @test occursin("equal", msg_equal) |
61 | 138 | end
|
62 | 139 | end
|
0 commit comments