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
138 lines (132 loc) · 3.5 KB
/
twoarg.jl
File metadata and controls
138 lines (132 loc) · 3.5 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
struct MooncakeTwoArgPullbackPrep{SIG, Tcache, F, N} <: DI.PullbackPrep{SIG}
_sig::Val{SIG}
cache::Tcache
target_function::F
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)
target_function = function (f!, y, x, contexts...)
f!(y, x, contexts...)
return y
end
config = get_config(backend)
cache = prepare_pullback_cache(
target_function,
f!,
y,
x,
map(DI.unwrap, contexts)...;
config,
)
contexts_tup_false = map(_ -> false, contexts)
args_to_zero = (
false, # target_function
false, # f!
false, # y
true, # x
contexts_tup_false...,
)
prep = MooncakeTwoArgPullbackPrep(_sig, cache, target_function, 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)
# Run the reverse-pass and return the results.
y_after, (_, _, _, dx) = value_and_pullback!!(
prep.cache,
dy,
prep.target_function,
f!,
y,
x,
map(DI.unwrap, contexts)...;
prep.args_to_zero
)
copyto!(y, y_after)
return y, (_copy_output(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_righttype_after = copyto!(prep.dy_righttype, dy)
y_after, (_, _, _, dx) = value_and_pullback!!(
prep.cache,
dy_righttype_after,
prep.target_function,
f!,
y,
x,
map(DI.unwrap, contexts)...;
prep.args_to_zero
)
copyto!(y, y_after)
_copy_output(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(copyto!, 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