|
1 | | -using Test |
2 | | -using ForwardDiff, ReverseDiff, Tracker, Mooncake |
3 | | -using Enzyme, EnzymeTestUtils |
4 | | - |
5 | | -const GROUP = get(ENV, "GROUP", "All") |
6 | | - |
7 | | -if GROUP == "QA" |
8 | | - using Pkg |
9 | | - Pkg.activate(joinpath(@__DIR__, "qa")) |
10 | | - Pkg.instantiate() |
11 | | - include(joinpath(@__DIR__, "qa", "qa.jl")) |
12 | | -end |
13 | | - |
14 | | -if GROUP == "All" || GROUP == "Core" || GROUP == "Enzyme" |
15 | | - using FastPower |
16 | | - using FastPower: fastlog2, fastpower |
17 | | -end |
18 | | - |
19 | | -if GROUP == "All" || GROUP == "Core" |
20 | | - @testset "Fast log2" begin |
21 | | - for x in 0.001:0.001:1.2 # (0, 1+something] is the domain which a controller uses |
22 | | - @test log2(x) ≈ fastlog2(Float32(x)) atol = 1.0e-3 |
23 | | - end |
24 | | - end |
25 | | - |
26 | | - @testset "Fast pow" begin |
27 | | - @test fastpower(1, 1) isa Float64 |
28 | | - @test fastpower(1.0, 1.0) isa Float64 |
29 | | - errors = [abs(^(x, y) - fastpower(x, y)) for x in 0.001:0.001:1, y in 0.08:0.001:0.5] |
30 | | - @test maximum(errors) < 1.0e-4 |
31 | | - |
32 | | - errors = [abs(^(x, y) - fastpower(x, y)) for x in 0.001:0.001:1, y in 0.08:0.001:1000.0] |
33 | | - @test maximum(errors) < 1.0e-3 |
34 | | - |
35 | | - errors = [abs(^(x, y) - fastpower(x, y)) for x in 0.001:0.001:100, y in 0.08:0.001:1.0] |
36 | | - @test maximum(errors) < 1.0e-2 |
37 | | - end |
38 | | - |
39 | | - function mooncake_derivative(f, x) |
40 | | - return Mooncake.value_and_gradient!!(Mooncake.build_rrule(f, x), f, x)[2][2] |
41 | | - end |
42 | | - @testset "Fast pow - Other AD Engines" begin |
43 | | - x = 1.5123233245141 |
44 | | - y = 0.22352354326 |
45 | | - @test ForwardDiff.derivative(x -> fastpower(x, x + y), x) ≈ |
46 | | - ForwardDiff.derivative(x -> ^(x, x + y), x) |
47 | | - @test Tracker.gradient(x -> fastpower(x, x + y), x)[1] ≈ |
48 | | - Tracker.gradient(x -> ^(x, x + y), x)[1] |
49 | | - @test ReverseDiff.gradient(x -> fastpower(x[1], x[1] + y), [x])[1] ≈ |
50 | | - ReverseDiff.gradient(x -> ^(x[1], x[1] + y), [x])[1] |
51 | | - @test mooncake_derivative(x -> fastpower(x, x + y), x) ≈ |
52 | | - mooncake_derivative(x -> ^(x, x + y), x) |
53 | | - end |
54 | | -end |
55 | | - |
56 | | -if GROUP == "All" || GROUP == "Enzyme" |
57 | | - @testset "Fast pow - Enzyme forward rule" begin |
58 | | - @testset for RT in (Duplicated, DuplicatedNoNeed), |
59 | | - Tx in (Const, Duplicated), |
60 | | - Ty in (Const, Duplicated) |
61 | | - |
62 | | - x = 1.0 |
63 | | - y = 0.5 |
64 | | - test_forward(fastpower, RT, (x, Tx), (y, Ty), atol = 1.0e-4, rtol = 1.0e-3) |
65 | | - end |
66 | | - end |
67 | | - |
68 | | - @testset "Fast pow - Enzyme reverse rule" begin |
69 | | - @testset for RT in (Active,), Tx in (Active, Const), Ty in (Active, Const) |
70 | | - x = 1.0 |
71 | | - y = 0.5 |
72 | | - test_reverse(fastpower, RT, (x, Tx), (y, Ty), atol = 1.0e-4, rtol = 1.0e-3) |
73 | | - end |
74 | | - end |
75 | | -end |
| 1 | +using SciMLTesting |
| 2 | +run_tests() |
0 commit comments