-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathEnzymeCore07Ext.jl
More file actions
347 lines (315 loc) · 9.89 KB
/
EnzymeCore07Ext.jl
File metadata and controls
347 lines (315 loc) · 9.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# https://github.com/EnzymeAD/Enzyme.jl/issues/1516
# On the CPU `autodiff_deferred` can deadlock.
# Hence a specialized CPU version
function cpu_fwd(ctx, f, args...)
EnzymeCore.autodiff(Forward, Const(f), Const{Nothing}, Const(ctx), args...)
return nothing
end
function gpu_fwd(ctx, f, args...)
EnzymeCore.autodiff_deferred(Forward, Const(f), Const{Nothing}, Const(ctx), args...)
return nothing
end
function EnzymeRules.forward(
func::Const{<:Kernel{CPU}},
::Type{Const{Nothing}},
args...;
ndrange = nothing,
workgroupsize = nothing,
)
kernel = func.val
f = kernel.f
fwd_kernel = similar(kernel, cpu_fwd)
return fwd_kernel(f, args...; ndrange, workgroupsize)
end
function EnzymeRules.forward(
func::Const{<:Kernel{<:GPU}},
::Type{Const{Nothing}},
args...;
ndrange = nothing,
workgroupsize = nothing,
)
kernel = func.val
f = kernel.f
fwd_kernel = similar(kernel, gpu_fwd)
return fwd_kernel(f, args...; ndrange, workgroupsize)
end
_enzyme_mkcontext(kernel::Kernel{CPU}, ndrange, iterspace, dynamic) =
mkcontext(kernel, first(blocks(iterspace)), ndrange, iterspace, dynamic)
_enzyme_mkcontext(kernel::Kernel{<:GPU}, ndrange, iterspace, dynamic) =
mkcontext(kernel, ndrange, iterspace)
_augmented_return(::Kernel{CPU}, subtape, arg_refs, tape_type) =
AugmentedReturn{Nothing, Nothing, Tuple{Array, typeof(arg_refs), typeof(tape_type)}}(
nothing,
nothing,
(subtape, arg_refs, tape_type),
)
_augmented_return(::Kernel{<:GPU}, subtape, arg_refs, tape_type) =
AugmentedReturn{Nothing, Nothing, Any}(nothing, nothing, (subtape, arg_refs, tape_type))
function _create_tape_kernel(
kernel::Kernel{CPU},
ModifiedBetween,
FT,
ctxTy,
ndrange,
iterspace,
args2...,
)
TapeType = EnzymeCore.tape_type(
ReverseSplitModified(ReverseSplitWithPrimal, ModifiedBetween),
FT,
Const{Nothing},
Const{ctxTy},
map(Core.Typeof, args2)...,
)
subtape = Array{TapeType}(undef, size(blocks(iterspace)))
aug_kernel = similar(kernel, cpu_aug_fwd)
return TapeType, subtape, aug_kernel
end
function _create_tape_kernel(
kernel::Kernel{<:GPU},
ModifiedBetween,
FT,
ctxTy,
ndrange,
iterspace,
args2...,
)
# For peeking at the TapeType we need to first construct a correct compilation job
# this requires the use of the device side representation of arguments.
# So we convert the arguments here, this is a bit wasteful since the `aug_kernel` call
# will later do the same.
dev_args2 = ((argconvert(kernel, a) for a in args2)...,)
dev_TT = map(Core.Typeof, dev_args2)
job =
EnzymeCore.compiler_job_from_backend(backend(kernel), typeof(() -> return), Tuple{})
TapeType = EnzymeCore.tape_type(
job,
ReverseSplitModified(ReverseSplitWithPrimal, ModifiedBetween),
FT,
Const{Nothing},
Const{ctxTy},
dev_TT...,
)
# Allocate per thread
subtape = allocate(backend(kernel), TapeType, prod(ndrange))
aug_kernel = similar(kernel, gpu_aug_fwd)
return TapeType, subtape, aug_kernel
end
_create_rev_kernel(kernel::Kernel{CPU}) = similar(kernel, cpu_rev)
_create_rev_kernel(kernel::Kernel{<:GPU}) = similar(kernel, gpu_rev)
function cpu_aug_fwd(
ctx,
f::FT,
::Val{ModifiedBetween},
subtape,
::Val{TapeType},
args...,
) where {ModifiedBetween, FT, TapeType}
# A2 = Const{Nothing} -- since f->Nothing
forward, _ = EnzymeCore.autodiff_thunk(
ReverseSplitModified(ReverseSplitWithPrimal, Val(ModifiedBetween)),
Const{Core.Typeof(f)},
Const{Nothing},
Const{Core.Typeof(ctx)},
map(Core.Typeof, args)...,
)
# On the CPU: F is a per block function
# On the CPU: subtape::Vector{Vector}
I = __index_Group_Cartesian(ctx, CartesianIndex(1, 1)) #=fake=#
subtape[I] = forward(Const(f), Const(ctx), args...)[1]
return nothing
end
function cpu_rev(
ctx,
f::FT,
::Val{ModifiedBetween},
subtape,
::Val{TapeType},
args...,
) where {ModifiedBetween, FT, TapeType}
_, reverse = EnzymeCore.autodiff_thunk(
ReverseSplitModified(ReverseSplitWithPrimal, Val(ModifiedBetween)),
Const{Core.Typeof(f)},
Const{Nothing},
Const{Core.Typeof(ctx)},
map(Core.Typeof, args)...,
)
I = __index_Group_Cartesian(ctx, CartesianIndex(1, 1)) #=fake=#
tp = subtape[I]
reverse(Const(f), Const(ctx), args..., tp)
return nothing
end
# GPU support
function gpu_aug_fwd(
ctx,
f::FT,
::Val{ModifiedBetween},
subtape,
::Val{TapeType},
args...,
) where {ModifiedBetween, FT, TapeType}
# A2 = Const{Nothing} -- since f->Nothing
forward, _ = EnzymeCore.autodiff_deferred_thunk(
ReverseSplitModified(ReverseSplitWithPrimal, Val(ModifiedBetween)),
TapeType,
Const{Core.Typeof(f)},
Const{Nothing},
Const{Core.Typeof(ctx)},
map(Core.Typeof, args)...,
)
# On the GPU: F is a per thread function
# On the GPU: subtape::Vector
if __validindex(ctx)
I = __index_Global_Linear(ctx)
subtape[I] = forward(Const(f), Const(ctx), args...)[1]
end
return nothing
end
function gpu_rev(
ctx,
f::FT,
::Val{ModifiedBetween},
subtape,
::Val{TapeType},
args...,
) where {ModifiedBetween, FT, TapeType}
# XXX: TapeType and A2 as args to autodiff_deferred_thunk
_, reverse = EnzymeCore.autodiff_deferred_thunk(
ReverseSplitModified(ReverseSplitWithPrimal, Val(ModifiedBetween)),
TapeType,
Const{Core.Typeof(f)},
Const{Nothing},
Const{Core.Typeof(ctx)},
map(Core.Typeof, args)...,
)
if __validindex(ctx)
I = __index_Global_Linear(ctx)
tp = subtape[I]
reverse(Const(f), Const(ctx), args..., tp)
end
return nothing
end
function EnzymeRules.augmented_primal(
config::Config,
func::Const{<:Kernel},
::Type{Const{Nothing}},
args::Vararg{Any, N};
ndrange = nothing,
workgroupsize = nothing,
) where {N}
kernel = func.val
f = kernel.f
ndrange, workgroupsize, iterspace, dynamic =
launch_config(kernel, ndrange, workgroupsize)
ctx = _enzyme_mkcontext(kernel, ndrange, iterspace, dynamic)
ctxTy = Core.Typeof(ctx) # CompilerMetadata{ndrange(kernel), Core.Typeof(dynamic)}
# TODO autodiff_deferred on the func.val
ModifiedBetween = Val((overwritten(config)[1], false, overwritten(config)[2:end]...))
FT = Const{Core.Typeof(f)}
arg_refs = ntuple(Val(N)) do i
Base.@_inline_meta
if args[i] isa Active
if func.val isa Kernel{<:GPU}
error("""
Active kernel arguments not supported on GPU, got
\n$(typeof(args[i]))
\nFor argument $i of kernel
\n$(func.val).
""")
else
Ref(EnzymeCore.make_zero(args[i].val))
end
else
nothing
end
end
args2 = ntuple(Val(N)) do i
Base.@_inline_meta
if args[i] isa Active
MixedDuplicated(args[i].val, arg_refs[i])
else
args[i]
end
end
TapeType, subtape, aug_kernel = _create_tape_kernel(
kernel,
ModifiedBetween,
FT,
ctxTy,
ndrange,
iterspace,
args2...,
)
aug_kernel(f, ModifiedBetween, subtape, Val(TapeType), args2...; ndrange, workgroupsize)
# TODO the fact that ctxTy is type unstable means this is all type unstable.
# Since custom rules require a fixed return type, explicitly cast to Any, rather
# than returning a AugmentedReturn{Nothing, Nothing, T} where T.
return _augmented_return(kernel, subtape, arg_refs, TapeType)
end
function EnzymeRules.reverse(
config::Config,
func::Const{<:Kernel},
::Type{<:EnzymeCore.Annotation},
tape,
args::Vararg{Any, N};
ndrange = nothing,
workgroupsize = nothing,
) where {N}
subtape, arg_refs, tape_type = tape
args2 = ntuple(Val(N)) do i
Base.@_inline_meta
if args[i] isa Active
MixedDuplicated(args[i].val, arg_refs[i])
else
args[i]
end
end
kernel = func.val
f = kernel.f
ModifiedBetween = Val((overwritten(config)[1], false, overwritten(config)[2:end]...))
rev_kernel = _create_rev_kernel(kernel)
rev_kernel(
f,
ModifiedBetween,
subtape,
Val(tape_type),
args2...;
ndrange,
workgroupsize,
)
res = ntuple(Val(N)) do i
Base.@_inline_meta
if args[i] isa Active
arg_refs[i][]
else
nothing
end
end
# Reverse synchronization right after the kernel launch
synchronize(backend(kernel))
return res
end
# Synchronize rules
# TODO: Right now we do the synchronization as part of the kernel launch in the augmented primal
# and reverse rules. This is not ideal, as we would want to launch the kernel in the reverse
# synchronize rule and then synchronize where the launch was. However, with the current
# kernel semantics this ensures correctness for now.
function EnzymeRules.augmented_primal(
config::Config,
func::Const{typeof(synchronize)},
::Type{Const{Nothing}},
backend::T,
) where {T <: EnzymeCore.Annotation}
synchronize(backend.val)
return AugmentedReturn(nothing, nothing, nothing)
end
function EnzymeRules.reverse(
config::Config,
func::Const{typeof(synchronize)},
::Type{Const{Nothing}},
tape,
backend,
)
# noop for now
return (nothing,)
end