@@ -10,13 +10,7 @@ using Random: Random, Xoshiro
10
10
using Statistics: median
11
11
using Test: @test
12
12
13
- export ADResult, run_ad
14
-
15
- # This function needed to work around the fact that different backends can
16
- # return different AbstractArrays for the gradient. See
17
- # https://github.com/JuliaDiff/DifferentiationInterface.jl/issues/754 for more
18
- # context.
19
- _to_vec_f64 (x:: AbstractArray ) = x isa Vector{Float64} ? x : collect (Float64, x)
13
+ export ADResult, run_ad, ADIncorrectException
20
14
21
15
"""
22
16
REFERENCE_ADTYPE
@@ -27,33 +21,50 @@ it's the default AD backend used in Turing.jl.
27
21
const REFERENCE_ADTYPE = AutoForwardDiff ()
28
22
29
23
"""
30
- ADResult
24
+ ADIncorrectException{T<:Real}
25
+
26
+ Exception thrown when an AD backend returns an incorrect value or gradient.
27
+
28
+ The type parameter `T` is the numeric type of the value and gradient.
29
+ """
30
+ struct ADIncorrectException{T<: Real } <: Exception
31
+ value_expected:: T
32
+ value_actual:: T
33
+ grad_expected:: Vector{T}
34
+ grad_actual:: Vector{T}
35
+ end
36
+
37
+ """
38
+ ADResult{Tparams<:Real,Tresult<:Real}
31
39
32
40
Data structure to store the results of the AD correctness test.
41
+
42
+ The type parameter `Tparams` is the numeric type of the parameters passed in;
43
+ `Tresult` is the type of the value and the gradient.
33
44
"""
34
- struct ADResult
45
+ struct ADResult{Tparams <: Real ,Tresult <: Real }
35
46
" The DynamicPPL model that was tested"
36
47
model:: Model
37
48
" The VarInfo that was used"
38
49
varinfo:: AbstractVarInfo
39
50
" The values at which the model was evaluated"
40
- params:: Vector{<:Real }
51
+ params:: Vector{Tparams }
41
52
" The AD backend that was tested"
42
53
adtype:: AbstractADType
43
54
" The absolute tolerance for the value of logp"
44
- value_atol:: Real
55
+ value_atol:: Tresult
45
56
" The absolute tolerance for the gradient of logp"
46
- grad_atol:: Real
57
+ grad_atol:: Tresult
47
58
" The expected value of logp"
48
- value_expected:: Union{Nothing,Float64 }
59
+ value_expected:: Union{Nothing,Tresult }
49
60
" The expected gradient of logp"
50
- grad_expected:: Union{Nothing,Vector{Float64 }}
61
+ grad_expected:: Union{Nothing,Vector{Tresult }}
51
62
" The value of logp (calculated using `adtype`)"
52
- value_actual:: Union{Nothing,Real }
63
+ value_actual:: Union{Nothing,Tresult }
53
64
" The gradient of logp (calculated using `adtype`)"
54
- grad_actual:: Union{Nothing,Vector{Float64 }}
65
+ grad_actual:: Union{Nothing,Vector{Tresult }}
55
66
" If benchmarking was requested, the time taken by the AD backend to calculate the gradient of logp, divided by the time taken to evaluate logp itself"
56
- time_vs_primal:: Union{Nothing,Float64 }
67
+ time_vs_primal:: Union{Nothing,Tresult }
57
68
end
58
69
59
70
"""
72
83
verbose=true,
73
84
)::ADResult
74
85
86
+ ### Description
87
+
75
88
Test the correctness and/or benchmark the AD backend `adtype` for the model
76
89
`model`.
77
90
78
91
Whether to test and benchmark is controlled by the `test` and `benchmark`
79
92
keyword arguments. By default, `test` is `true` and `benchmark` is `false`.
80
93
81
- Returns an [`ADResult`](@ref) object, which contains the results of the
82
- test and/or benchmark.
83
-
84
94
Note that to run AD successfully you will need to import the AD backend itself.
85
95
For example, to test with `AutoReverseDiff()` you will need to run `import
86
96
ReverseDiff`.
87
97
98
+ ### Arguments
99
+
88
100
There are two positional arguments, which absolutely must be provided:
89
101
90
102
1. `model` - The model being tested.
@@ -146,14 +158,23 @@ Everything else is optional, and can be categorised into several groups:
146
158
147
159
By default, this function prints messages when it runs. To silence it, set
148
160
`verbose=false`.
161
+
162
+ ### Returns / Throws
163
+
164
+ Returns an [`ADResult`](@ref) object, which contains the results of the
165
+ test and/or benchmark.
166
+
167
+ If `test` is `true` and the AD backend returns an incorrect value or gradient, an
168
+ `ADIncorrectException` is thrown. If a different error occurs, it will be
169
+ thrown as-is.
149
170
"""
150
171
function run_ad (
151
172
model:: Model ,
152
173
adtype:: AbstractADType ;
153
- test= true ,
154
- benchmark= false ,
155
- value_atol= 1e-6 ,
156
- grad_atol= 1e-6 ,
174
+ test:: Bool = true ,
175
+ benchmark:: Bool = false ,
176
+ value_atol:: Real = 1e-6 ,
177
+ grad_atol:: Real = 1e-6 ,
157
178
linked:: Bool = true ,
158
179
varinfo:: AbstractVarInfo = VarInfo (model),
159
180
params:: Union{Nothing,Vector{<:Real}} = nothing ,
@@ -167,14 +188,14 @@ function run_ad(
167
188
if isnothing (params)
168
189
params = varinfo[:]
169
190
end
170
- params = map (identity, params)
191
+ params = map (identity, params) # Concretise
171
192
172
193
verbose && @info " Running AD on $(model. f) with $(adtype) \n "
173
194
verbose && println (" params : $(params) " )
174
195
ldf = LogDensityFunction (model, varinfo; adtype= adtype)
175
196
176
197
value, grad = logdensity_and_gradient (ldf, params)
177
- grad = _to_vec_f64 (grad)
198
+ grad = collect (grad)
178
199
verbose && println (" actual : $((value, grad)) " )
179
200
180
201
if test
@@ -186,10 +207,11 @@ function run_ad(
186
207
expected_value_and_grad
187
208
end
188
209
verbose && println (" expected : $((value_true, grad_true)) " )
189
- grad_true = _to_vec_f64 (grad_true)
190
- # Then compare
191
- @test isapprox (value, value_true; atol= value_atol)
192
- @test isapprox (grad, grad_true; atol= grad_atol)
210
+ grad_true = collect (grad_true)
211
+
212
+ exc () = throw (ADIncorrectException (value, value_true, grad, grad_true))
213
+ isapprox (value, value_true; atol= value_atol) || exc ()
214
+ isapprox (grad, grad_true; atol= grad_atol) || exc ()
193
215
else
194
216
value_true = nothing
195
217
grad_true = nothing
0 commit comments