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
|
/// Modifer to mark a function for forward-mode differentiation.
/// i.e. the compiler will automatically generate a new function
/// that computes the jacobian-vector product of the original.
__attributeTarget(FunctionDeclBase)
attribute_syntax [ForwardDifferentiable] : ForwardDifferentiableAttribute;
// Custom Forward Derivative Function reference
__attributeTarget(FunctionDeclBase)
attribute_syntax [ForwardDerivative(function)] : ForwardDerivativeAttribute;
/// Interface to denote types as differentiable.
/// Allows for user-specified differential types as
/// well as automatic generation, for when the associated type
/// hasn't been declared explicitly.
/// Note that the requirements must currently be defined in this exact order
/// since the auto-diff pass relies on the order to grab the struct keys.
///
__magic_type(DifferentiableType)
interface IDifferentiable
{
// Note: the compiler implementation requires the `Differential` associated type to be defined
// before anything else.
[__BuiltinRequirement(_BuiltinRequirementKind.DifferentialType)]
associatedtype Differential;
[__BuiltinRequirement(_BuiltinRequirementKind.DZeroFunc)]
static Differential dzero();
[__BuiltinRequirement(_BuiltinRequirementKind.DAddFunc)]
static Differential dadd(Differential, Differential);
[__BuiltinRequirement(_BuiltinRequirementKind.DMulFunc)]
static Differential dmul(This, Differential);
};
// Add extensions for the standard types
extension float : IDifferentiable
{
typedef float Differential;
[__unsafeForceInlineEarly]
static Differential dzero()
{
return float(0.f);
}
[__unsafeForceInlineEarly]
static Differential dadd(Differential a, Differential b)
{
return a + b;
}
[__unsafeForceInlineEarly]
static Differential dmul(This a, Differential b)
{
return a * b;
}
}
extension vector<float, 3> : IDifferentiable
{
typedef vector<float, 3> Differential;
[__unsafeForceInlineEarly]
static Differential dzero()
{
return vector<float, 3>(0.f);
}
[__unsafeForceInlineEarly]
static Differential dadd(Differential a, Differential b)
{
return a + b;
}
[__unsafeForceInlineEarly]
static Differential dmul(This a, Differential b)
{
return a * b;
}
}
extension vector<float, 2> : IDifferentiable
{
typedef vector<float, 2> Differential;
[__unsafeForceInlineEarly]
static Differential dzero()
{
return vector<float, 2>(0.f);
}
[__unsafeForceInlineEarly]
static Differential dadd(Differential a, Differential b)
{
return a + b;
}
[__unsafeForceInlineEarly]
static Differential dmul(This a, Differential b)
{
return a * b;
}
}
extension vector<float, 4> : IDifferentiable
{
typedef vector<float, 4> Differential;
[__unsafeForceInlineEarly]
static Differential dzero()
{
return vector<float, 4>(0.f);
}
[__unsafeForceInlineEarly]
static Differential dadd(Differential a, Differential b)
{
return a + b;
}
[__unsafeForceInlineEarly]
static Differential dmul(This a, Differential b)
{
return a * b;
}
}
__magic_type(DifferentialBottomType)
__intrinsic_type($(kIROp_DifferentialBottomType))
struct __DifferentialBottom : IDifferentiable
{
typedef __DifferentialBottom Differential;
__intrinsic_op($(kIROp_DifferentialBottomValue))
static __DifferentialBottom dzero();
[__unsafeForceInlineEarly]
static __DifferentialBottom dadd(Differential a, Differential b)
{
return dzero();
}
[__unsafeForceInlineEarly]
static __DifferentialBottom dmul(This a, Differential b)
{
return dzero();
}
}
/// Pair type that serves to wrap the primal and
/// differential types of an arbitrary type T.
__generic<T : IDifferentiable>
__magic_type(DifferentialPairType)
__intrinsic_type($(kIROp_DifferentialPairType))
struct DifferentialPair : IDifferentiable
{
typedef DifferentialPair<T.Differential> Differential;
typedef T.Differential DifferentialElementType;
__intrinsic_op($(kIROp_MakeDifferentialPair))
__init(T _primal, T.Differential _differential);
__intrinsic_op($(kIROp_DifferentialPairGetDifferential))
T.Differential d();
T.Differential getDifferential()
{
return d();
}
__intrinsic_op($(kIROp_DifferentialPairGetPrimal))
T p();
T getPrimal()
{
return p();
}
[__unsafeForceInlineEarly]
static Differential dzero()
{
return Differential(T.dzero(), Differential.DifferentialElementType.dzero());
}
[__unsafeForceInlineEarly]
static Differential dadd(Differential a, Differential b)
{
return Differential(
T.dadd(
a.p(),
b.p()
),
Differential.DifferentialElementType.dzero());
}
[__unsafeForceInlineEarly]
static Differential dmul(This a, Differential b)
{
return Differential(
T.dmul(a.p(), b.p()),
Differential.DifferentialElementType.dzero());
}
};
typealias IDFloat = IFloat & IDifferentiable;
namespace dstd
{
// Natural Exponent
__generic<T : IDFloat>
__target_intrinsic(hlsl)
__target_intrinsic(glsl)
__target_intrinsic(cuda, "$P_exp($0)")
__target_intrinsic(cpp, "$P_exp($0)")
__target_intrinsic(spirv_direct, "12 resultType resultId glsl450 27 _0")
[ForwardDerivative(d_exp<T>)]
T exp(T x);
__generic<T : IDFloat>
DifferentialPair<T> d_exp(DifferentialPair<T> dpx)
{
return DifferentialPair<T>(
exp(dpx.p()),
T.dmul(exp(dpx.p()), dpx.d()));
}
// Sine
__generic<T : IDFloat>
__target_intrinsic(hlsl)
__target_intrinsic(glsl)
__target_intrinsic(cuda, "$P_sin($0)")
__target_intrinsic(cpp, "$P_sin($0)")
__target_intrinsic(spirv_direct, "12 resultType resultId glsl450 13 _0")
[ForwardDerivative(d_sin<T>)]
T sin(T x);
__generic<T : IDFloat>
DifferentialPair<T> d_sin(DifferentialPair<T> dpx)
{
return DifferentialPair<T>(
sin(dpx.p()),
T.dmul(cos(dpx.p()), dpx.d()));
}
// Cosine
__generic<T : IDFloat>
__target_intrinsic(hlsl)
__target_intrinsic(glsl)
__target_intrinsic(cuda, "$P_cos($0)")
__target_intrinsic(cpp, "$P_cos($0)")
__target_intrinsic(spirv_direct, "12 resultType resultId glsl450 14 _0")
[ForwardDerivative(d_cos<T>)]
T cos(T x);
__generic<T : IDFloat>
DifferentialPair<T> d_cos(DifferentialPair<T> dpx)
{
return DifferentialPair<T>(
cos(dpx.p()),
T.dmul(-sin(dpx.p()), dpx.d()));
}
};
|