|
| 1 | +"""Module for testing the test utils themselves.""" |
| 2 | +module TestUtilsTests |
| 3 | + |
| 4 | +using ..ADUtils: ADTypeCheckContext, AbstractWrongADBackendError |
| 5 | +using ForwardDiff: ForwardDiff |
| 6 | +using ReverseDiff: ReverseDiff |
| 7 | +using Test: @test, @testset, @test_throws |
| 8 | +using Turing: Turing |
| 9 | +using Turing: DynamicPPL |
| 10 | +using Zygote: Zygote |
| 11 | + |
| 12 | +# Check that the ADTypeCheckContext works as expected. |
| 13 | +@testset "ADTypeCheckContext" begin |
| 14 | + Turing.@model test_model() = x ~ Turing.Normal(0, 1) |
| 15 | + tm = test_model() |
| 16 | + adtypes = ( |
| 17 | + Turing.AutoForwardDiff(), |
| 18 | + Turing.AutoReverseDiff(), |
| 19 | + Turing.AutoZygote(), |
| 20 | + # TODO: Mooncake |
| 21 | + # Turing.AutoMooncake(config=nothing), |
| 22 | + ) |
| 23 | + for actual_adtype in adtypes |
| 24 | + sampler = Turing.HMC(0.1, 5; adtype=actual_adtype) |
| 25 | + for expected_adtype in adtypes |
| 26 | + if ( |
| 27 | + actual_adtype == Turing.AutoForwardDiff() && |
| 28 | + expected_adtype == Turing.AutoZygote() |
| 29 | + ) |
| 30 | + # TODO(mhauru) We are currently unable to check this case. |
| 31 | + continue |
| 32 | + end |
| 33 | + contextualised_tm = DynamicPPL.contextualize( |
| 34 | + tm, ADTypeCheckContext(expected_adtype, tm.context) |
| 35 | + ) |
| 36 | + @testset "Expected: $expected_adtype, Actual: $actual_adtype" begin |
| 37 | + if actual_adtype == expected_adtype |
| 38 | + # Check that this does not throw an error. |
| 39 | + Turing.sample(contextualised_tm, sampler, 2) |
| 40 | + else |
| 41 | + @test_throws AbstractWrongADBackendError Turing.sample( |
| 42 | + contextualised_tm, sampler, 2 |
| 43 | + ) |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +end |
0 commit comments