-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest_differentiation.jl
More file actions
259 lines (227 loc) · 9.73 KB
/
test_differentiation.jl
File metadata and controls
259 lines (227 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
$(TYPEDSIGNATURES)
Apply a list of `backends` on a list of `scenarios`, running a variety of different tests and/or benchmarks.
# Return
This function always creates and runs a `@testset`, though its contents may vary.
- if `benchmark == :none`, it returns `nothing`.
- if `benchmark != :none`, it returns a `DataFrame` of benchmark results, whose columns correspond to the fields of [`DifferentiationBenchmarkDataRow`](@ref).
# Positional arguments
- `backends::Vector{<:AbstractADType}`: the backends to test
- `scenarios::Vector{<:Scenario}`: the scenarios on which to test these backends. Defaults to a standard set of first- and second-order scenarios, whose contents are not part of the public API and may change without notice.
# Keyword arguments
- `testset_name=nothing`: how to display the test set
**Test categories:**
- `correctness=true`: whether to compare the differentiation results with the theoretical values specified in each scenario
- `type_stability=:none`: whether (and how) to check type stability of operators with JET.jl.
- `allocations=:none`: whether (and how) to check allocations inside operators with AllocCheck.jl
- `benchmark=:none`: whether (and how) to benchmark operators with Chairmarks.jl
For `type_stability`, `allocations` and `benchmark`, the possible values are `:none`, `:prepared` or `:full`.
Each setting tests/benchmarks a different subset of calls:
| kwarg | prepared operator | unprepared operator | preparation |
|---|---|---|---|
| `:none` | no | no | no |
| `:prepared` | yes | no | no |
| `:full` | yes | yes | yes |
**Misc options:**
- `excluded::Vector{Symbol}`: list of operators to exclude, such as [`FIRST_ORDER`](@ref) or [`SECOND_ORDER`](@ref)
- `detailed=false`: whether to create a detailed or condensed testset
- `logging=false`: whether to log progress
**Correctness options:**
- `isapprox=isapprox`: function used to compare objects approximately, with the standard signature `isapprox(x, y; atol, rtol)`
- `atol=0`: absolute precision for correctness testing (when comparing to the reference outputs)
- `rtol=1e-3`: relative precision for correctness testing (when comparing to the reference outputs)
- `scenario_intact=true`: whether to check that the scenario remains unchanged after the operators are applied
- `sparsity=false`: whether to check sparsity patterns for Jacobians / Hessians
- `reprepare::Bool=true`: whether to modify preparation before testing when the preparation arguments have the wrong size
**Type stability options:**
- `ignored_modules=nothing`: list of modules that JET.jl should ignore
- `function_filter`: filter for functions that JET.jl should ignore (with a reasonable default)
**Benchmark options:**
- `count_calls=true`: whether to also count function calls during benchmarking
- `benchmark_test=true`: whether to include tests which succeed iff benchmark doesn't error
- `benchmark_seconds=1`: how long to run each benchmark for
- `benchmark_aggregation=minimum`: function used to aggregate sample measurements
**Batch size options**
- `adaptive_batchsize=true`: whether to cap the backend's preset batch size (when it exists) to prevent errors on small inputs
"""
function test_differentiation(
backends::Vector{<:AbstractADType},
scenarios::Vector{<:Scenario}=default_scenarios();
testset_name::Union{String,Nothing}=nothing,
# test categories
correctness::Bool=true,
type_stability::Symbol=:none,
allocations::Symbol=:none,
benchmark::Symbol=:none,
# misc options
excluded::Vector{Symbol}=Symbol[],
detailed::Bool=false,
logging::Bool=false,
# correctness options
isapprox=isapprox,
atol::Real=0,
rtol::Real=1e-3,
scenario_intact::Bool=true,
sparsity::Bool=false,
reprepare::Bool=true,
# type stability options
ignored_modules=nothing,
function_filter=if VERSION >= v"1.11"
@nospecialize(f) -> true
else
@nospecialize(f) -> f != Base.mapreduce_empty # fix for `mapreduce` in jacobian and hessian
end,
# allocs options
skip_allocations::Bool=false, # private, only for code coverage
# benchmark options
count_calls::Bool=true,
benchmark_test::Bool=true,
benchmark_seconds::Real=1,
benchmark_aggregation=minimum,
# batch size
adaptive_batchsize::Bool=true,
)
@assert type_stability in (:none, :prepared, :full)
@assert allocations in (:none, :prepared, :full)
@assert benchmark in (:none, :prepared, :full)
scenarios = filter(s -> !(operator(s) in excluded), scenarios)
scenarios = sort(scenarios; by=s -> (operator(s), string(s.f)))
if isnothing(testset_name)
title_additions =
(correctness ? " + correctness" : "") *
((type_stability != :none) ? " + type stability" : "") *
((benchmark != :none) ? " + benchmarks" : "")
title = "Testing" * title_additions[3:end]
else
title = testset_name
end
benchmark_data = DifferentiationBenchmarkDataRow[]
prog = ProgressUnknown(; desc="$title", spinner=true, enabled=logging)
@testset verbose = true "$title" begin
@testset verbose = detailed "$backend" for (i, backend) in enumerate(backends)
filtered_scenarios = filter(s -> compatible(backend, s), scenarios)
grouped_scenarios = group_by_operator(filtered_scenarios)
@testset verbose = detailed "$op" for (j, (op, op_group)) in
enumerate(pairs(grouped_scenarios))
@testset "$scen" for (k, scen) in enumerate(op_group)
next!(
prog;
showvalues=[
(:backend, "$backend - $i/$(length(backends))"),
(:scenario_type, "$op - $j/$(length(grouped_scenarios))"),
(:scenario, "$k/$(length(op_group))"),
(:operator_place, operator_place(scen)),
(:function_place, function_place(scen)),
(:function, scen.f),
(:input_type, typeof(scen.x)),
(:input_size, mysize(scen.x)),
(:output_type, typeof(scen.y)),
(:output_size, mysize(scen.y)),
(:nb_tangents, scen.t isa NTuple ? length(scen.t) : nothing),
(:nb_contexts, length(scen.contexts)),
],
)
adapted_backend = if adaptive_batchsize
adapt_batchsize(backend, scen)
else
backend
end
correctness && @testset "Correctness" begin
test_correctness(
adapted_backend,
scen;
isapprox,
atol,
rtol,
scenario_intact,
sparsity,
reprepare,
)
end
yield()
(type_stability != :none) && @testset "Type stability" begin
test_jet(
adapted_backend,
scen;
subset=type_stability,
ignored_modules,
function_filter,
)
end
yield()
(allocations != :none) && @testset "Allocations" begin
test_alloccheck(
adapted_backend,
scen;
subset=allocations,
skip=skip_allocations,
)
end
yield()
(benchmark != :none) && @testset "Benchmark" begin
run_benchmark!(
benchmark_data,
adapted_backend,
scen;
logging,
subset=benchmark,
count_calls,
benchmark_test,
benchmark_seconds,
benchmark_aggregation,
)
end
yield()
end
end
end
end
if benchmark != :none
return DataFrame(benchmark_data)
else
return nothing
end
end
"""
$(TYPEDSIGNATURES)
Shortcut for a single backend.
"""
function test_differentiation(backend::AbstractADType, args...; kwargs...)
return test_differentiation([backend], args...; kwargs...)
end
"""
$(TYPEDSIGNATURES)
Shortcut for [`test_differentiation`](@ref) with only benchmarks and no correctness or type stability checks.
Specifying the set of scenarios is mandatory for this function.
"""
function benchmark_differentiation(
backends,
scenarios::Vector{<:Scenario};
testset_name::Union{String,Nothing}=nothing,
benchmark::Symbol=:prepared,
excluded::Vector{Symbol}=Symbol[],
logging::Bool=false,
count_calls::Bool=true,
benchmark_test::Bool=true,
benchmark_seconds::Real=1,
benchmark_aggregation=minimum,
# batch size
adaptive_batchsize::Bool=true,
)
return test_differentiation(
backends,
scenarios;
testset_name,
correctness=false,
type_stability=:none,
allocations=:none,
benchmark,
logging,
excluded,
count_calls,
benchmark_test,
benchmark_seconds,
benchmark_aggregation,
adaptive_batchsize,
)
end