-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathscenario.jl
More file actions
232 lines (206 loc) · 6.89 KB
/
scenario.jl
File metadata and controls
232 lines (206 loc) · 6.89 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
"""
Scenario{op,pl_op,pl_fun}
Store a testing scenario composed of a function and its input + output for a given operator.
This generic type should never be used directly: use the specific constructor corresponding to the operator you want to test, or a predefined list of scenarios.
# Type parameters
- `op`: one of `:pushforward`, `:pullback`, `:derivative`, `:gradient`, `:jacobian`,`:second_derivative`, `:hvp`, `:hessian`
- `pl_op`: either `:in` (for `op!(f, result, backend, x)`) or `:out` (for `result = op(f, backend, x)`)
- `pl_fun`: either `:in` (for `f!(y, x)`) or `:out` (for `y = f(x)`)
# Constructors
Scenario{op,pl_op}(
f, x, [t], contexts...;
prep_args, res1, res2, name
)
Scenario{op,pl_op}(
f!, y, x, [t,] contexts...;
prep_args, res1, res2, name
)
Default values:
- `prep_args = ` the result of `zero` applied to each execution argument
- `res1 = res2 = nothing`
- `name = nothing`
# Fields
$(TYPEDFIELDS)
"""
struct Scenario{op,pl_op,pl_fun,F,X,Y,T<:Union{Nothing,NTuple},C<:Tuple,R1,R2,P<:NamedTuple}
"function `f` (if `pl_fun==:out`) or `f!` (if `pl_fun==:in`) to apply"
f::F
"primal output"
y::Y
"primal input"
x::X
"tangents (if applicable)"
t::T
"contexts (if applicable)"
contexts::C
"first-order result of the operator (if applicable)"
res1::R1
"second-order result of the operator (if applicable)"
res2::R2
"named tuple of arguments passed to preparation, without the function - the required keys are a subset of `(; y, x, t, contexts)` depending on the operator"
prep_args::P
"name of the scenario for display in test sets and dataframes"
name::Union{String,Nothing}
function Scenario{op,pl_op,pl_fun}(;
f::F,
y::Y,
x::X,
t::T,
contexts::C,
res1::R1,
res2::R2,
prep_args::P,
name::Union{String,Nothing},
) where {op,pl_op,pl_fun,F,X,Y,T,C,R1,R2,P}
@assert op in ALL_OPS
@assert pl_op in (:in, :out)
@assert pl_fun in (:in, :out)
return new{op,pl_op,pl_fun,F,X,Y,T,C,R1,R2,P}(
f, y, x, t, contexts, res1, res2, prep_args, name
)
end
end
function zero_contexts(contexts...)
rewrap = Rewrap(contexts...)
return rewrap(map(zero ∘ unwrap, contexts)...)
end
function Scenario{op,pl_op}(
f,
x,
contexts::Vararg{Context};
res1=nothing,
res2=nothing,
prep_args=(; x=zero(x), contexts=zero_contexts(contexts...)),
name=nothing,
) where {op,pl_op}
y = f(x, map(unwrap, contexts)...)
return Scenario{op,pl_op,:out}(;
f, y, x, t=nothing, contexts, res1, res2, prep_args, name
)
end
function Scenario{op,pl_op}(
f,
y,
x,
contexts::Vararg{Context};
res1=nothing,
res2=nothing,
prep_args=(; y=zero(y), x=zero(x), contexts=zero_contexts(contexts...)),
name=nothing,
) where {op,pl_op}
f(y, x, map(unwrap, contexts)...)
return Scenario{op,pl_op,:in}(;
f, y, x, t=nothing, contexts, res1, res2, prep_args, name
)
end
function Scenario{op,pl_op}(
f,
x,
t::NTuple,
contexts::Vararg{Context};
res1=nothing,
res2=nothing,
prep_args=(; x=zero(x), t=map(zero, t), contexts=zero_contexts(contexts...)),
name=nothing,
) where {op,pl_op}
y = f(x, map(unwrap, contexts)...)
return Scenario{op,pl_op,:out}(; f, y, x, t, contexts, res1, res2, prep_args, name)
end
function Scenario{op,pl_op}(
f,
y,
x,
t::NTuple,
contexts::Vararg{Context};
res1=nothing,
res2=nothing,
prep_args=(; y=zero(y), x=zero(x), t=map(zero, t), contexts=zero_contexts(contexts...)),
name=nothing,
) where {op,pl_op}
f(y, x, map(unwrap, contexts)...)
return Scenario{op,pl_op,:in}(; f, y, x, t, contexts, res1, res2, prep_args, name)
end
Base.:(==)(scen1::Scenario, scen2::Scenario) = false
function Base.:(==)(
scen1::Scenario{op,pl_op,pl_fun}, scen2::Scenario{op,pl_op,pl_fun}
) where {op,pl_op,pl_fun}
eq_f = scen1.f == scen2.f
eq_x = scen1.x == scen2.x
eq_y = scen1.y == scen2.y
eq_t = scen1.t == scen2.t
eq_contexts = all(
map(scen1.contexts, scen2.contexts) do c1, c2
if c1 isa Union{Cache,ConstantOrCache} || c2 isa Union{Cache,ConstantOrCache}
return true
else
return c1 == c2
end
end,
)
eq_res1 = scen1.res1 == scen2.res1
eq_res2 = scen1.res2 == scen2.res2
eq_name = scen1.name == scen2.name
return (eq_x && eq_y && eq_t && eq_contexts && eq_res1 && eq_res2 && eq_name)
end
operator(::Scenario{op}) where {op} = op
operator_place(::Scenario{op,pl_op}) where {op,pl_op} = pl_op
function_place(::Scenario{op,pl_op,pl_fun}) where {op,pl_op,pl_fun} = pl_fun
function order(scen::Scenario)
if operator(scen) in [:pushforward, :pullback, :derivative, :gradient, :jacobian]
return 1
elseif operator(scen) in [:hvp, :hessian, :second_derivative]
return 2
end
end
function compatible(backend::AbstractADType, scen::Scenario)
place_compatible = function_place(scen) == :out || Bool(inplace_support(backend))
sparse_compatible = operator(scen) in (:jacobian, :hessian) || !isa(backend, AutoSparse)
secondorder_compatible =
order(scen) == 2 || !isa(backend, Union{SecondOrder,AutoSparse{<:SecondOrder}})
mixedmode_compatible =
operator(scen) == :jacobian || !isa(backend, AutoSparse{<:MixedMode})
return place_compatible &&
secondorder_compatible &&
sparse_compatible &&
mixedmode_compatible
end
function group_by_operator(scenarios::AbstractVector{<:Scenario})
return Dict(
op => filter(s -> operator(s) == op, scenarios) for
op in unique(operator.(scenarios))
)
end
function Base.show(
io::IO, scen::Scenario{op,pl_op,pl_fun,F,X,Y,T}
) where {op,pl_op,pl_fun,F,X,Y,T}
if isnothing(scen.name)
print(io, "Scenario{$(repr(op)),$(repr(pl_op))} $(string(scen.f)) : $X -> $Y")
if op in (:pushforward, :pullback, :hvp)
print(io, " ($(length(scen.t)) tangents)")
end
if length(scen.contexts) > 0
print(io, " ($(length(scen.contexts)) contexts)")
end
else
print(io, scen.name)
end
return nothing
end
function adapt_batchsize(backend::AbstractADType, scen::Scenario)
(; x, y, prep_args) = scen
xprep = prep_args.x
yprep = hasproperty(prep_args, :y) ? prep_args.y : y
Bmax = if x isa AbstractArray && y isa AbstractArray
min(length(x), length(y), length(xprep), length(yprep))
elseif x isa AbstractArray
min(length(x), length(xprep))
elseif y isa AbstractArray
min(length(y), length(yprep))
else
typemax(Int)
end
return DI.threshold_batchsize(backend, Bmax)
end
function no_matrices(scens::AbstractVector{<:Scenario})
return filter(s -> !isa(s.x, AbstractMatrix) && !isa(s.y, AbstractMatrix), scens)
end