forked from JuliaDiff/DifferentiationInterface.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonearg.jl
More file actions
184 lines (170 loc) · 5.28 KB
/
onearg.jl
File metadata and controls
184 lines (170 loc) · 5.28 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
## Pullback
struct MooncakeOneArgPullbackPrep{SIG, Tcache, N} <: DI.PullbackPrep{SIG}
_sig::Val{SIG}
cache::Tcache
args_to_zero::NTuple{N, Bool}
end
function DI.prepare_pullback_nokwarg(
strict::Val, f::F, backend::AutoMooncake, x, ty::NTuple, contexts::Vararg{DI.Context, C}
) where {F, C}
_sig = DI.signature(f, backend, x, ty, contexts...; strict)
config = get_config(backend)
cache = prepare_pullback_cache(f, x, map(DI.unwrap, contexts)...; config)
contexts_tup_false = map(_ -> false, contexts)
args_to_zero = (
false, # f
true, # x
contexts_tup_false...,
)
prep = MooncakeOneArgPullbackPrep(_sig, cache, args_to_zero)
return prep
end
function DI.value_and_pullback(
f::F,
prep::MooncakeOneArgPullbackPrep{Y},
backend::AutoMooncake,
x,
ty::NTuple{1},
contexts::Vararg{DI.Context, C},
) where {F, Y, C}
DI.check_prep(f, prep, backend, x, ty, contexts...)
dy = only(ty)
new_y, (_, new_dx) = value_and_pullback!!(
prep.cache, dy, f, x, map(DI.unwrap, contexts)...; prep.args_to_zero
)
return new_y, (_to_primal_alloc(x, new_dx),)
end
function DI.value_and_pullback(
f::F,
prep::MooncakeOneArgPullbackPrep{Y},
backend::AutoMooncake,
x,
ty::NTuple,
contexts::Vararg{DI.Context, C},
) where {F, Y, C}
DI.check_prep(f, prep, backend, x, ty, contexts...)
ys_and_tx = map(ty) do dy
y, (_, new_dx) = value_and_pullback!!(
prep.cache, dy, f, x, map(DI.unwrap, contexts)...; prep.args_to_zero
)
y, _to_primal_alloc(x, new_dx)
end
y = first(ys_and_tx[1])
tx = map(last, ys_and_tx)
return y, tx
end
function DI.value_and_pullback!(
f::F,
tx::NTuple,
prep::MooncakeOneArgPullbackPrep,
backend::AutoMooncake,
x,
ty::NTuple,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, ty, contexts...)
y, new_tx = DI.value_and_pullback(f, prep, backend, x, ty, contexts...)
foreach(_to_primal!, tx, new_tx)
return y, tx
end
function DI.pullback(
f::F,
prep::MooncakeOneArgPullbackPrep,
backend::AutoMooncake,
x,
ty::NTuple,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, ty, contexts...)
return DI.value_and_pullback(f, prep, backend, x, ty, contexts...)[2]
end
function DI.pullback!(
f::F,
tx::NTuple,
prep::MooncakeOneArgPullbackPrep,
backend::AutoMooncake,
x,
ty::NTuple,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, ty, contexts...)
return DI.value_and_pullback!(f, tx, prep, backend, x, ty, contexts...)[2]
end
## Gradient
struct MooncakeGradientPrep{SIG, Tcache, N} <: DI.GradientPrep{SIG}
_sig::Val{SIG}
cache::Tcache
args_to_zero::NTuple{N, Bool}
end
function DI.prepare_gradient_nokwarg(
strict::Val, f::F, backend::AutoMooncake, x, contexts::Vararg{DI.Context, C}
) where {F, C}
_sig = DI.signature(f, backend, x, contexts...; strict)
config = get_config(backend)
cache = prepare_gradient_cache(f, x, map(DI.unwrap, contexts)...; config)
contexts_tup_false = map(_ -> false, contexts)
args_to_zero = (
false, # f
true, # x
contexts_tup_false...,
)
prep = MooncakeGradientPrep(_sig, cache, args_to_zero)
return prep
end
function DI.value_and_gradient(
f::F,
prep::MooncakeGradientPrep,
backend::AutoMooncake,
x,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, contexts...)
y, (_, new_grad) = value_and_gradient!!(
prep.cache, f, x, map(DI.unwrap, contexts)...;
prep.args_to_zero
)
return y, _to_primal_alloc(x, new_grad)
end
function DI.value_and_gradient!(
f::F,
grad,
prep::MooncakeGradientPrep,
backend::AutoMooncake,
x,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, contexts...)
y, (_, new_grad) = value_and_gradient!!(
prep.cache, f, x, map(DI.unwrap, contexts)...;
prep.args_to_zero
)
grad = _to_primal_into!(grad, x, new_grad)
return y, grad
end
function DI.gradient(
f::F,
prep::MooncakeGradientPrep,
backend::AutoMooncake,
x,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, contexts...)
_, grad = DI.value_and_gradient(f, prep, backend, x, contexts...)
return grad
end
function DI.gradient!(
f::F,
grad,
prep::MooncakeGradientPrep,
backend::AutoMooncake,
x,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f, prep, backend, x, contexts...)
# Note: when `grad` is immutable (e.g. an `SVector`), `value_and_gradient!`
# returns a freshly built primal-shaped value rather than the original
# buffer (no in-place update is possible). Forward that value to the
# caller instead of returning the unchanged `grad`.
_, new_grad = DI.value_and_gradient!(f, grad, prep, backend, x, contexts...)
return new_grad
end