-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVectorInterfaceEnzymeExt.jl
More file actions
297 lines (278 loc) · 8.94 KB
/
VectorInterfaceEnzymeExt.jl
File metadata and controls
297 lines (278 loc) · 8.94 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
module VectorInterfaceEnzymeExt
# COV_EXCL_START
# Enzyme rules aren't reachable by coverage
using VectorInterface
using Enzyme
using Enzyme.EnzymeCore
using Enzyme.EnzymeCore: EnzymeRules
"""
project_scalar(x::Number, dx::Number)
Project a computed tangent `dx` onto the correct tangent type for `x`.
For example, we might compute a complex `dx` but only require the real part.
"""
project_scalar(x::Number, dx::Number) = oftype(x, dx)
project_scalar(x::Real, dx::Complex) = project_scalar(x, real(dx))
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(scale!)},
::Type{RT},
C::Annotation,
α::Annotation{<:Number},
) where {RT}
dret = !isa(C, Const) ? C.dval : nothing
cacheα = EnzymeRules.overwritten(config)[3] ? copy(α.val) : α.val
cache = (cacheα, copy(C.val)) # is this better than just unscaling?
ret = scale!(C.val, α.val)
shadow = EnzymeRules.needs_shadow(config) ? dret : nothing
primal = EnzymeRules.needs_primal(config) ? ret : nothing
return EnzymeRules.AugmentedReturn(primal, shadow, cache)
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(scale!)},
::Type{RT},
cache,
C::Annotation,
α::Annotation{<:Number},
) where {RT}
αval, Cval = cache
Δα = if !isa(α, Const) && !isa(C, Const)
project_scalar(α.val, inner(Cval, C.dval))
elseif !isa(α, Const)
zero(α.val)
else
nothing
end
scale!(C.dval, conj(αval))
return (nothing, Δα)
end
function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
func::Const{typeof(scale!)},
::Type{RT},
C::Annotation,
α::Annotation{<:Number},
) where {RT}
if !isa(α, Const) && !isa(C, Const)
add!(C.dval, C.val, α.dval, α.val)
elseif !isa(C, Const)
scale!(C.dval, α.val)
end
scale!(C.val, α.val)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return C
elseif EnzymeRules.needs_primal(config)
return C.val
elseif EnzymeRules.needs_shadow(config)
return C.dval
else
return nothing
end
end
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(scale!)},
::Type{RT},
C::Annotation,
A::Annotation,
α::Annotation{<:Number},
) where {RT}
cacheA = EnzymeRules.overwritten(config)[3] ? copy(A.val) : A.val
cacheα = EnzymeRules.overwritten(config)[4] ? copy(α.val) : α.val
cache = (cacheA, cacheα)
ret = scale!(C.val, A.val, α.val)
dret = !isa(C, Const) ? C.dval : nothing
shadow = EnzymeRules.needs_shadow(config) ? dret : nothing
primal = EnzymeRules.needs_primal(config) ? ret : nothing
return EnzymeRules.AugmentedReturn(primal, shadow, cache)
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(scale!)},
::Type{RT},
cache,
C::Annotation,
A::Annotation,
α::Annotation{<:Number},
) where {RT}
Aval, αval = cache
!isa(A, Const) && !isa(C, Const) && add!(A.dval, C.dval, conj(αval))
Δα = if !isa(α, Const) && !isa(C, Const)
project_scalar(α.val, inner(Aval, C.dval))
elseif !isa(α, Const)
zero(α.val)
else
nothing
end
zerovector!(C.dval)
return (nothing, nothing, Δα)
end
function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
func::Const{typeof(scale!)},
::Type{RT},
C::Annotation,
A::Annotation,
α::Annotation{<:Number},
) where {RT}
scale!(C.val, A.val, α.val)
!isa(C, Const) && !isa(A, Const) && scale!(C.dval, A.dval, α.val)
!isa(α, Const) && !isa(C, Const) && add!(C.dval, A.val, α.dval, One())
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return C
elseif EnzymeRules.needs_primal(config)
return C.val
elseif EnzymeRules.needs_shadow(config)
return C.dval
else
return nothing
end
end
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(add!)},
::Type{RT},
C::Annotation,
A::Annotation,
α::Annotation{<:Number},
β::Annotation{<:Number},
) where {RT}
dret = !isa(C, Const) ? C.dval : nothing
# only need copy of A if α is not constant
cacheA = !isa(α, Const) && EnzymeRules.overwritten(config)[3] ? copy(A.val) : A.val
cacheα = EnzymeRules.overwritten(config)[4] ? copy(α.val) : α.val
cacheβ = EnzymeRules.overwritten(config)[5] ? copy(β.val) : β.val
# only need copy of C if β is not constant
cacheC = !isa(β, Const) ? copy(C.val) : C.val
cache = (cacheA, cacheα, cacheβ, cacheC)
ret = add!(C.val, A.val, α.val, β.val)
shadow = EnzymeRules.needs_shadow(config) ? dret : nothing
primal = EnzymeRules.needs_primal(config) ? ret : nothing
return EnzymeRules.AugmentedReturn(primal, shadow, cache)
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(add!)},
::Type{RT},
cache,
C::Annotation,
A::Annotation,
α::Annotation{<:Number},
β::Annotation{<:Number},
) where {RT}
Aval, αval, βval, Cval = cache
Δα = if !isa(α, Const) && !isa(C, Const)
project_scalar(α.val, inner(Aval, C.dval))
elseif !isa(α, Const)
zero(α.val)
else
nothing
end
Δβ = if !isa(β, Const) && !isa(C, Const)
project_scalar(β.val, inner(Cval, C.dval))
elseif !isa(β, Const)
zero(β.val)
else
nothing
end
!isa(A, Const) && !isa(C, Const) && add!(A.dval, C.dval, conj(αval))
!isa(C, Const) && scale!(C.dval, conj(βval))
return (nothing, nothing, Δα, Δβ)
end
function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
func::Const{typeof(add!)},
::Type{RT},
C::Annotation,
A::Annotation,
α::Annotation{<:Number},
β::Annotation{<:Number},
) where {RT}
!isa(C, Const) && !isa(A, Const) && add!(C.dval, A.dval, α.val, β.val)
!isa(C, Const) && !isa(α, Const) && add!(C.dval, A.val, α.dval, One())
!isa(C, Const) && !isa(β, Const) && add!(C.dval, C.val, β.dval, One())
add!(C.val, A.val, α.val, β.val)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return C
elseif EnzymeRules.needs_primal(config)
return C.val
elseif EnzymeRules.needs_shadow(config)
return C.dval
else
return nothing
end
end
function project_add!(C, A, α)
TC = Base.promote_op(+, scalartype(A), scalartype(α))
return if !(TC <: Real) && scalartype(C) <: Real
add!(C, real(add!(zerovector(C, TC), A, α)))
else
add!(C, A, α)
end
end
function EnzymeRules.augmented_primal(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(inner)},
::Type{RT},
A::Annotation,
B::Annotation,
) where {RT}
cacheA = EnzymeRules.overwritten(config)[2] ? copy(A.val) : A.val
cacheB = EnzymeRules.overwritten(config)[3] ? copy(B.val) : B.val
cache = (cacheA, cacheB)
ret = inner(A.val, B.val)
shadow = EnzymeRules.needs_shadow(config) ? zero(ret) : nothing
primal = EnzymeRules.needs_primal(config) ? ret : nothing
return EnzymeRules.AugmentedReturn(primal, shadow, cache)
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(inner)},
dret::Active,
cache,
A::Annotation,
B::Annotation,
)
ΔS = dret.val
Aval, Bval = cache
!isa(A, Const) && project_add!(A.dval, Bval, conj(ΔS))
!isa(B, Const) && project_add!(B.dval, Aval, ΔS)
return (nothing, nothing)
end
function EnzymeRules.reverse(
config::EnzymeRules.RevConfigWidth{1},
func::Const{typeof(inner)},
RT::Type{<:Const},
cache,
A::Annotation,
B::Annotation,
)
return (nothing, nothing)
end
function EnzymeRules.forward(
config::EnzymeRules.FwdConfigWidth{1},
func::Const{typeof(inner)},
::Type{RT},
A::Annotation,
B::Annotation,
) where {RT}
ret = inner(A.val, B.val)
if EnzymeRules.needs_shadow(config) # only compute this if actually needed
dret = zero(ret)
!isa(A, Const) && (dret += inner(A.dval, B.val))
!isa(B, Const) && (dret += inner(A.val, B.dval))
else
dret = nothing
end
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
return Duplicated(ret, dret)
elseif EnzymeRules.needs_primal(config)
return ret
elseif EnzymeRules.needs_shadow(config)
return dret
else
return nothing
end
end
# COV_EXCL_STOP
end