forked from JuliaDiff/DifferentiationInterface.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoarg.jl
More file actions
139 lines (133 loc) · 3.53 KB
/
twoarg.jl
File metadata and controls
139 lines (133 loc) · 3.53 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
struct MooncakeTwoArgPullbackPrep{SIG, Tcache, DY, N} <: DI.PullbackPrep{SIG}
_sig::Val{SIG}
cache::Tcache
dy_backup::DY
args_to_zero::NTuple{N, Bool}
end
function DI.prepare_pullback_nokwarg(
strict::Val,
f!::F,
y,
backend::AutoMooncake,
x,
ty::NTuple,
contexts::Vararg{DI.Context, C}
) where {F, C}
_sig = DI.signature(f!, y, backend, x, ty, contexts...; strict)
config = get_config(backend)
cache = prepare_pullback_cache(
call_and_return,
f!,
y,
x,
map(DI.unwrap, contexts)...;
config,
)
dy_backup = zero_tangent_or_primal(y, backend)
contexts_tup_false = map(_ -> false, contexts)
args_to_zero = (
false, # call_and_return
false, # f!
false, # y
true, # x
contexts_tup_false...,
)
prep = MooncakeTwoArgPullbackPrep(
_sig, cache, dy_backup, args_to_zero
)
return prep
end
function DI.value_and_pullback(
f!::F,
y,
prep::MooncakeTwoArgPullbackPrep,
backend::AutoMooncake,
x,
ty::NTuple{1},
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
dy = only(ty)
# Prepare cotangent to add after the forward pass.
dy_backup = copyto!(prep.dy_backup, dy)
# Run the reverse-pass and return the results.
y_after, (_, _, _, dx) = value_and_pullback!!(
prep.cache,
dy_backup,
call_and_return,
f!,
y,
x,
map(DI.unwrap, contexts)...;
prep.args_to_zero
)
copyto!(y, y_after)
return y, (_to_primal_alloc(x, dx),)
end
function DI.value_and_pullback(
f!::F,
y,
prep::MooncakeTwoArgPullbackPrep,
backend::AutoMooncake,
x,
ty::NTuple,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
tx = map(ty) do dy
dy_backup = copyto!(prep.dy_backup, dy)
y_after, (_, _, _, dx) = value_and_pullback!!(
prep.cache,
dy_backup,
call_and_return,
f!,
y,
x,
map(DI.unwrap, contexts)...;
prep.args_to_zero
)
copyto!(y, y_after)
_to_primal_alloc(x, dx)
end
return y, tx
end
function DI.value_and_pullback!(
f!::F,
y,
tx::NTuple,
prep::MooncakeTwoArgPullbackPrep,
backend::AutoMooncake,
x,
ty::NTuple,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
_, new_tx = DI.value_and_pullback(f!, y, prep, backend, x, ty, contexts...)
foreach(_to_primal!, tx, new_tx)
return y, tx
end
function DI.pullback(
f!::F,
y,
prep::MooncakeTwoArgPullbackPrep,
backend::AutoMooncake,
x,
ty::NTuple,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
return DI.value_and_pullback(f!, y, prep, backend, x, ty, contexts...)[2]
end
function DI.pullback!(
f!::F,
y,
tx::NTuple,
prep::MooncakeTwoArgPullbackPrep,
backend::AutoMooncake,
x,
ty::NTuple,
contexts::Vararg{DI.Context, C},
) where {F, C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
return DI.value_and_pullback!(f!, y, tx, prep, backend, x, ty, contexts...)[2]
end