|
1 | 1 | @testset "debug_mode" begin |
| 2 | + @testset "reverse debug mode" begin |
| 3 | + # Unless we explicitly check that the arguments are of the type as expected by the rule, |
| 4 | + # this will segfault. |
| 5 | + @testset "argument checking" begin |
| 6 | + f = x -> 5x |
| 7 | + rule = build_rrule(f, 5.0; debug_mode=true) |
| 8 | + @test_throws ErrorException rule(zero_fcodual(f), CoDual(0.0f0, 1.0f0)) |
| 9 | + end |
2 | 10 |
|
3 | | - # Unless we explicitly check that the arguments are of the type as expected by the rule, |
4 | | - # this will segfault. |
5 | | - @testset "argument checking" begin |
6 | | - f = x -> 5x |
7 | | - rule = build_rrule(f, 5.0; debug_mode=true) |
8 | | - @test_throws ErrorException rule(zero_fcodual(f), CoDual(0.0f0, 1.0f0)) |
| 11 | + # Forwards-pass tests. |
| 12 | + x = (CoDual(sin, NoTangent()), CoDual(5.0, NoFData())) |
| 13 | + @test_throws(ErrorException, Mooncake.DebugRRule(rrule!!)(x...)) |
| 14 | + x = (CoDual(sin, NoFData()), CoDual(5.0, NoFData())) |
| 15 | + @test_throws( |
| 16 | + ErrorException, |
| 17 | + Mooncake.DebugRRule((x...,) -> (CoDual(1.0, 0.0), nothing))(x...) |
| 18 | + ) |
| 19 | + |
| 20 | + # Basic type checking. |
| 21 | + x = (CoDual(size, NoFData()), CoDual(randn(10), randn(Float16, 11))) |
| 22 | + @test_throws ErrorException Mooncake.DebugRRule(rrule!!)(x...) |
| 23 | + |
| 24 | + # Element type checking. Abstractly typed-elements prevent determining incorrectness |
| 25 | + # just by looking at the array. |
| 26 | + x = ( |
| 27 | + CoDual(size, NoFData()), |
| 28 | + CoDual(Any[rand() for _ in 1:10], Any[rand(Float16) for _ in 1:10]), |
| 29 | + ) |
| 30 | + @test_throws ErrorException Mooncake.DebugRRule(rrule!!)(x...) |
| 31 | + |
| 32 | + # Test that bad rdata is caught as a pre-condition. |
| 33 | + y, pb!! = Mooncake.DebugRRule(rrule!!)(zero_fcodual(sin), zero_fcodual(5.0)) |
| 34 | + @test_throws(InvalidRDataException, pb!!(5)) |
| 35 | + |
| 36 | + # Test that bad rdata is caught as a post-condition. |
| 37 | + rule_with_bad_pb(x::CoDual{Float64}) = x, dy -> (5,) # returns the wrong type |
| 38 | + y, pb!! = Mooncake.DebugRRule(rule_with_bad_pb)(zero_fcodual(5.0)) |
| 39 | + @test_throws InvalidRDataException pb!!(1.0) |
| 40 | + |
| 41 | + # Test that bad rdata is caught as a post-condition. |
| 42 | + rule_with_bad_pb_length(x::CoDual{Float64}) = x, dy -> (5, 5.0) # returns the wrong type |
| 43 | + y, pb!! = Mooncake.DebugRRule(rule_with_bad_pb_length)(zero_fcodual(5.0)) |
| 44 | + @test_throws ErrorException pb!!(1.0) |
9 | 45 | end |
10 | 46 |
|
11 | | - # Forwards-pass tests. |
12 | | - x = (CoDual(sin, NoTangent()), CoDual(5.0, NoFData())) |
13 | | - @test_throws(ErrorException, Mooncake.DebugRRule(rrule!!)(x...)) |
14 | | - x = (CoDual(sin, NoFData()), CoDual(5.0, NoFData())) |
15 | | - @test_throws( |
16 | | - ErrorException, Mooncake.DebugRRule((x...,) -> (CoDual(1.0, 0.0), nothing))(x...) |
17 | | - ) |
18 | | - |
19 | | - # Basic type checking. |
20 | | - x = (CoDual(size, NoFData()), CoDual(randn(10), randn(Float16, 11))) |
21 | | - @test_throws ErrorException Mooncake.DebugRRule(rrule!!)(x...) |
22 | | - |
23 | | - # Element type checking. Abstractly typed-elements prevent determining incorrectness |
24 | | - # just by looking at the array. |
25 | | - x = ( |
26 | | - CoDual(size, NoFData()), |
27 | | - CoDual(Any[rand() for _ in 1:10], Any[rand(Float16) for _ in 1:10]), |
28 | | - ) |
29 | | - @test_throws ErrorException Mooncake.DebugRRule(rrule!!)(x...) |
30 | | - |
31 | | - # Test that bad rdata is caught as a pre-condition. |
32 | | - y, pb!! = Mooncake.DebugRRule(rrule!!)(zero_fcodual(sin), zero_fcodual(5.0)) |
33 | | - @test_throws(InvalidRDataException, pb!!(5)) |
34 | | - |
35 | | - # Test that bad rdata is caught as a post-condition. |
36 | | - rule_with_bad_pb(x::CoDual{Float64}) = x, dy -> (5,) # returns the wrong type |
37 | | - y, pb!! = Mooncake.DebugRRule(rule_with_bad_pb)(zero_fcodual(5.0)) |
38 | | - @test_throws InvalidRDataException pb!!(1.0) |
39 | | - |
40 | | - # Test that bad rdata is caught as a post-condition. |
41 | | - rule_with_bad_pb_length(x::CoDual{Float64}) = x, dy -> (5, 5.0) # returns the wrong type |
42 | | - y, pb!! = Mooncake.DebugRRule(rule_with_bad_pb_length)(zero_fcodual(5.0)) |
43 | | - @test_throws ErrorException pb!!(1.0) |
| 47 | + @testset "forward debug mode" begin |
| 48 | + @testset "argument checking" begin |
| 49 | + f = x -> 5x |
| 50 | + rule = Mooncake.build_frule(zero_dual(f), 5.0; debug_mode=true) |
| 51 | + @test_throws ErrorException rule( |
| 52 | + zero_dual(f), Mooncake.Dual(Float32(5.0), Float32(1.0)) |
| 53 | + ) |
| 54 | + end |
| 55 | + |
| 56 | + @testset "valid inputs pass" begin |
| 57 | + # Single argument - use Float64, not π which has NoTangent |
| 58 | + rule = Mooncake.build_frule(zero_dual(sin), 0.0; debug_mode=true) |
| 59 | + @test rule(zero_dual(sin), Mooncake.Dual(3.14, 1.0)) isa Mooncake.Dual |
| 60 | + |
| 61 | + # Multiple arguments |
| 62 | + f_mul(x, y) = x * y |
| 63 | + rule = Mooncake.build_frule(zero_dual(f_mul), 2.0, 3.0; debug_mode=true) |
| 64 | + @test rule( |
| 65 | + zero_dual(f_mul), Mooncake.Dual(2.0, 1.0), Mooncake.Dual(3.0, 0.5) |
| 66 | + ) isa Mooncake.Dual |
| 67 | + |
| 68 | + # Arrays |
| 69 | + h(x) = sum(x) |
| 70 | + rule = Mooncake.build_frule(zero_dual(h), randn(5); debug_mode=true) |
| 71 | + @test rule(zero_dual(h), Mooncake.Dual(randn(5), randn(5))) isa Mooncake.Dual |
| 72 | + |
| 73 | + # NoTangent (non-differentiable) |
| 74 | + rule = Mooncake.build_frule(zero_dual(identity), 5; debug_mode=true) |
| 75 | + @test rule(zero_dual(identity), Mooncake.Dual(5, NoTangent())) isa Mooncake.Dual |
| 76 | + end |
| 77 | + |
| 78 | + @testset "size mismatch detected" begin |
| 79 | + rule = Mooncake.build_frule(zero_dual(size), randn(10); debug_mode=true) |
| 80 | + @test_throws ErrorException rule( |
| 81 | + zero_dual(size), Mooncake.Dual(randn(11), randn(10)) |
| 82 | + ) |
| 83 | + end |
| 84 | + |
| 85 | + @testset "element type mismatch detected" begin |
| 86 | + rule = Mooncake.build_frule(zero_dual(identity), Any[1.0]; debug_mode=true) |
| 87 | + @test_throws ErrorException rule( |
| 88 | + zero_dual(identity), Mooncake.Dual(Any[1.0], Any[Float16(1.0)]) |
| 89 | + ) |
| 90 | + end |
| 91 | + |
| 92 | + @testset "scalar type mismatch detected" begin |
| 93 | + rule = Mooncake.build_frule(zero_dual(identity), 1.0; debug_mode=true) |
| 94 | + @test_throws ErrorException rule( |
| 95 | + zero_dual(identity), Mooncake.Dual(1.0, Float32(1.0)) |
| 96 | + ) |
| 97 | + end |
| 98 | + |
| 99 | + @testset "container type mismatch detected" begin |
| 100 | + rule = Mooncake.build_frule(zero_dual(identity), (1.0, 2.0); debug_mode=true) |
| 101 | + @test_throws ErrorException rule( |
| 102 | + zero_dual(identity), Mooncake.Dual((1.0, 2.0), [1.0, 2.0]) |
| 103 | + ) |
| 104 | + end |
| 105 | + |
| 106 | + @testset "output tangent type mismatch detected" begin |
| 107 | + # Rule that returns wrong tangent type in output |
| 108 | + bad_rule = Mooncake.DebugFRule((x...,) -> Mooncake.Dual(1.0, Float32(0.0))) |
| 109 | + @test_throws ErrorException bad_rule(Mooncake.Dual(5.0, 1.0)) |
| 110 | + end |
| 111 | + |
| 112 | + @testset "error messages include type info" begin |
| 113 | + rule = Mooncake.build_frule(zero_dual(identity), [1.0]; debug_mode=true) |
| 114 | + |
| 115 | + try |
| 116 | + rule(zero_dual(identity), Mooncake.Dual([1.0], [Float32(1.0)])) |
| 117 | + @test false # Expected ErrorException but none was thrown |
| 118 | + catch e |
| 119 | + msg = sprint(showerror, e) |
| 120 | + @test occursin("input types", msg) |
| 121 | + @test occursin("Float", msg) # Type info present |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + @testset "integration with test_rule" begin |
| 126 | + # Test basic case - test_rule expects primal functions, not Duals |
| 127 | + Mooncake.TestUtils.test_rule( |
| 128 | + sr(123456), sin, 1.0; mode=ForwardMode, debug_mode=true, perf_flag=:none |
| 129 | + ) |
| 130 | + |
| 131 | + # Test with array |
| 132 | + Mooncake.TestUtils.test_rule( |
| 133 | + sr(123456), |
| 134 | + sum, |
| 135 | + randn(5); |
| 136 | + mode=ForwardMode, |
| 137 | + debug_mode=true, |
| 138 | + perf_flag=:none, |
| 139 | + ) |
| 140 | + end |
| 141 | + end |
44 | 142 | end |
0 commit comments