summaryrefslogtreecommitdiffstats
path: root/tests/autodiff/generic-impl-jvp.slang
blob: 1567b3c680e3f30642303168e0f35c5d41ce6408 (plain)
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
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type

//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<float> outputBuffer;

typedef float Real;

typealias IDFloat = __BuiltinRealType & IDifferentiable;

__generic<T : IDifferentiable, let N : int>
struct dvector : IDifferentiable
{
    typedef dvector<T.Differential, N> Differential;
    [DerivativeMember(Differential.values)]
    T values[N];
};

__generic<T : IDFloat, let N : int>
struct myvector : IDifferentiable
{
    typedef dvector<T.Differential, N> Differential;

    [DerivativeMember(Differential.values)]
    T values[N];
    
    __init(T c)
    {
        [ForceUnroll]
        for (int i = 0; i < N; i++)
        {
            values[i] = c;
        }
    }

    static Differential dadd(Differential a, Differential b)
    {
        Differential output;

        for (int i = 0; i < N; i++)
        {
            output.values[i] = T.dadd(a.values[i], b.values[i]);
        }

        return output;
    }

    
    static Differential dmul<U: __BuiltinRealType>(U a, Differential b)
    {
        Differential output;

        for (int i = 0; i < N; i++)
        {
            output.values[i] = T.dmul<U>(a, b.values[i]);
        }

        return output;
    }

    static Differential dzero()
    {
        Differential output;

        for (int i = 0; i < N; i++)
        {
            output.values[i] = T.dzero();
        }

        return output;
    }
};

[ForwardDifferentiable]
__generic<T : IDFloat, let N : int>
myvector<T, N> operator +(myvector<T, N> a, myvector<T, N> b)
{
    myvector<T, N> output;
    [ForceUnroll]
    for (int i = 0; i < N; i++)
    {
        output.values[i] = a.values[i] + b.values[i];
    }
    return output;
}

[ForwardDifferentiable]
__generic<T : IDFloat, let N : int>
myvector<T, N> operator *(myvector<T, N> a, myvector<T, N> b)
{
    myvector<T, N> output;
    [ForceUnroll]
    for (int i = 0; i < N; i++)
    {
        output.values[i] = a.values[i] * b.values[i];
    }
    return output;
}

[ForwardDifferentiable]
__generic<T : IDFloat, let N : int>
myvector<T, N> operator *(T a, myvector<T, N> b)
{
    myvector<T, N> output;
    [ForceUnroll]
    for (int i = 0; i < N; i++)
    {
        output.values[i] = a * b.values[i];
    }
    return output;
}

__generic<T : IDFloat, let N : int>
[ForwardDerivative(dot_jvp)]
T dot(myvector<T, N> a, myvector<T, N> b)
{
    T curr = __realCast<T, float>(0.f);
    [ForceUnroll]
    for (int i = 0; i < N; i++)
    {
        curr = curr + (a.values[i] * b.values[i]);
    }

    return curr;
}

__generic<T : IDFloat, let N : int>
typedef DifferentialPair<myvector<T, N>> dpvector;

__generic<T : IDFloat, let N : int>
DifferentialPair<T> dot_jvp(dpvector<T, N> a, dpvector<T, N> b)
{
    T.Differential curr_d = (T.dzero());
    T curr_p = __realCast<T, float>(0.f);
    [ForceUnroll]
    for (int i = 0; i < N; i++)
    {
        curr_p = curr_p + (a.p.values[i] * b.p.values[i]);
        curr_d = T.dadd(
                    curr_d, 
                    T.dadd(
                        T.dmul<T>(a.p.values[i], b.d.values[i]),
                        T.dmul<T>(b.p.values[i], a.d.values[i])));
    }

    return DifferentialPair<T>(curr_p, curr_d);
}

__generic<let N : int>
struct lineardvector : IDifferentiable
{
    typedef lineardvector<N> Differential;

    myvector<Real, N>.Differential val;

    __init(vector<Real.Differential, N> a)
    {
        [ForceUnroll]
        for (int i = 0; i < N; i++)
        {
            val.values[i] = a[i];
        }
    }

    // Add a new constructor for dadd() function.
    __init(Real a[N])
    {
        [ForceUnroll]
        for (int i = 0; i < N; i++)
        {
            val.values[i] = a[i];
        }
    }
};

__generic<let N : int>
struct linearvector : MyLinearArithmeticType, IDifferentiable
{
    typedef lineardvector<N> Differential;

    [DerivativeMember(Differential.val)]
    myvector<Real, N> val;

    [ForwardDifferentiable]
    static linearvector<N> ladd(linearvector<N> a, linearvector<N> b)
    {
        return linearvector<N>(a.val + b.val);
    }

    [ForwardDifferentiable]
    static linearvector<N> lmul(linearvector<N> a, linearvector<N> b)
    {
        return linearvector<N>(a.val * b.val);
    }

    [ForwardDifferentiable]
    static linearvector<N> lscale(float a, linearvector<N> b)
    {
        return linearvector<N>(a * b.val);
    }

    [ForwardDifferentiable]
    static float ldot(linearvector<N> a, linearvector<N> b)
    {
        return dot(a.val, b.val);
    }

    static Differential dzero()
    {
        lineardvector<N> dout;
        dout.val = myvector<Real, N>.dzero();
        return dout;
    }

    static Differential dadd(Differential a, Differential b)
    {
        // return { myvector<Real, N>.dadd(a.val, b.val) };
        //
        // Above code will not work because
        // myvector<Real, N>.dadd will return dvector<T.Differential, N> type
        // while Differential == lineardvector<N> type
        // and the constructor of lineardvector<N> requires a vector<Real.Differential, N> type
        // and dvector<T.Differential, N> != vector<Real.Differential, N>, though they have the
        // same members.
        //
        // In our new design, generic will not be C-Style struct anymore.
        dvector<Real.Differential, N> d = myvector<Real, N>.dadd(a.val, b.val);
        return {d.values};
    }

    static Differential dmul<T: __BuiltinRealType>(T a, Differential b)
    {
        dvector<Real.Differential, N> d = myvector<Real, N>.dmul<T>(a, b.val);
        return {d.values};
    }

    [ForwardDifferentiable]
    __init(vector<Real, N> a)
    {
        [ForceUnroll]
        for (int i = 0; i < N; i++)
        {
            val.values[i] = a[i];
        }
    }

    [ForwardDifferentiable]
    __init(myvector<Real, N> a)
    {
        val = a;
    }
};

typedef linearvector<3> myfloat3;
typedef linearvector<4> myfloat4;

typedef lineardvector<3> mydfloat3;
typedef lineardvector<4> mydfloat4;

typedef DifferentialPair<Real> dpfloat;

[TreatAsDifferentiable]
interface MyLinearArithmeticType
{
    static This ladd(This a, This b);
    static This lmul(This a, This b);
    static This lscale(Real a, This b);
    static Real ldot(This a, This b);
};

typedef DifferentialPair<myfloat4> dpfloat4;
typedef DifferentialPair<myfloat3> dpfloat3;

extension float : MyLinearArithmeticType
{
    [ForwardDifferentiable]
    static float ladd(float a, float b)
    {
        return a + b;
    }

    [ForwardDifferentiable]
    static float lmul(float a, float b)
    {
        return a * b;
    }

    [ForwardDifferentiable]
    static float lscale(float a, float b)
    {
        return a * b;
    }

    [ForwardDifferentiable]
    static float ldot(float a, float b)
    {
        return a * b;
    }
};

typealias MyLinearArithmeticDifferentiableType = IDifferentiable & MyLinearArithmeticType;

__generic<T : MyLinearArithmeticDifferentiableType>
[ForwardDifferentiable]
T operator +(T a, T b)
{
    return T.ladd(a, b);
}

__generic<T : MyLinearArithmeticDifferentiableType>
[ForwardDifferentiable]
T operator *(T a, T b)
{
    return T.lmul(a, b);
}

__generic<G : MyLinearArithmeticDifferentiableType>
[ForwardDifferentiable]
G f(G x)
{
    G a = x + x;
    G b = x * x;

    return a * a + G.lscale((Real)3.0, x); 
}


[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    {
        dpfloat dpa = dpfloat(2.0, 1.0);
        dpfloat4 dpf4 = dpfloat4(myfloat4(float4(1.5, 2.0, 0.5, 1.0)), mydfloat4(float4(0.5, 0.8, 1.6, 2.5)));
        dpfloat3 dpf3 = dpfloat3(myfloat3(float3(1.0, 3.0, 5.0)), mydfloat3(float3(0.5, 1.5, 2.5)));

        outputBuffer[0] = f(dpa.p);                               // Expect: 22.0
        outputBuffer[1] = __fwd_diff(f)(dpfloat(2.0, 0.5)).d;          // Expect: 9.5
        outputBuffer[2] = __fwd_diff(f)(dpf4).d.val.values[3];         // Expect: 27.5
        outputBuffer[3] = __fwd_diff(f)(dpf3).d.val.values[1];         // Expect: 40.5
    }
}