summaryrefslogtreecommitdiffstats
path: root/tests/autodiff/auto-differential-type-generic.slang
blob: f230d2a2f6d0bebe06c9bdf6594ec8a3e1284164 (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
// Tests automatic synthesis of Differential type requirement for generic types.
//
// This specifically tests a synthesis path that occurs when the lookup of the Differential type happens before the conformance-check.
// If this path doesn't construct the generic differential type correctly, it will throw an error when constructing the array
// in this line: Feature<3>.Differential b = {0.2, 0.3, 0.4};
// 

//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type

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

__generic<let C : int> 
struct Feature: IDifferentiable
{
    float vals[C];
}


struct Linear<let C : int>
{
    typedef Feature<C> Input;
    typedef Feature<C> Output;

    [BackwardDerivative(eval_bwd)]
    Output eval(Input in_feature)
    {
        Output out_feature;
        for (int i = 0; i < C; i++)
        {
            out_feature.vals[i] = in_feature.vals[i] * 2.0;
        }
        return out_feature;
    }

    void eval_bwd(inout DifferentialPair<Input> in_feature_pair, Feature<C>.Differential d_output)
    {
        /* empty.. doesn't really matter */
    }
}

[Differentiable]
Feature<3> f(Feature<3> a, Linear<3> layer)
{
    return layer.eval(a);
}

[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
{
    Feature<3> a = {1.0, 2.0, 3.0};
    Feature<3>.Differential b = {0.2, 0.3, 0.4};

    Linear<3> layer;

    var dpA = diffPair(a, b);

    var result = fwd_diff(f)(dpA, layer).d;

    outputBuffer[0] = result.vals[0];
    outputBuffer[1] = result.vals[1];
    outputBuffer[2] = result.vals[2];
}