yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Harsh Aggarwal (NVIDIA)Fix 7723 - Add autodiff tests (#7919)d10732742

master
1.9 KiB77 linesraw
1// Test calling differentiable function through dynamic dispatch.
2
3//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
4//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
5//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type
6
7//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
8RWStructuredBuffer<float> outputBuffer;
9
10[anyValueSize(16)]
11interface IInterface : IDifferentiable
12{
13    [Differentiable]
14    float calc(float x);
15}
16
17struct A : IInterface
18{
19    int data1;
20
21    __init(int data1) { this.data1 = data1; }
22
23    [Differentiable]
24    float calc(float x) { return x * x * x * data1; }
25};
26
27struct B : IInterface
28{
29    int data1;
30    int data2;
31
32    __init(int data1, int data2) { this.data1 = data1; this.data2 = data2; }
33
34    [Differentiable]
35    float calc(float x) { return x * x * data1 * data2; }
36};
37
38[Differentiable]
39float doThing(IInterface obj, float x)
40{
41    return obj.calc(x);
42}
43
44[Differentiable]
45float f(uint id, float x)
46{
47    IInterface obj;
48
49    if (id == 0)
50        obj = no_diff(A(2));
51    else
52        obj = no_diff(B(2, 3));
53
54    return doThing(obj, x);
55}
56
57//TEST_INPUT: type_conformance A:IInterface = 0
58//TEST_INPUT: type_conformance B:IInterface = 1
59
60[numthreads(1, 1, 1)]
61void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
62{
63    outputBuffer[0] = fwd_diff(f)(dispatchThreadID.x, DifferentialPair<float>(1.0, 2.0)).d; // A.calc, expect 12
64    outputBuffer[1] = fwd_diff(f)(dispatchThreadID.x + 1, DifferentialPair<float>(1.5, 1.0)).d; // B.calc, expect 18
65
66    {
67        var dpx = diffPair(1.0);
68        bwd_diff(f)(dispatchThreadID.x, dpx, 2.0); // A.calc, expect 12
69        outputBuffer[2] = dpx.d;
70    }
71
72    {
73        var dpx = diffPair(1.5);
74        bwd_diff(f)(dispatchThreadID.x + 1, dpx, 1.0); // B.calc, expect 18
75        outputBuffer[3] = dpx.d;
76    }
77}