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.7 KiB81 linesraw
1// Test calling backward 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
12{
13    [BackwardDifferentiable]
14    float calc(IInterface2 i2, float x);
15}
16
17interface IInterface2
18{ 
19    float innerCalc(float x);
20}
21
22struct C : IInterface2
23{
24    float innerCalc(float x) { return 2 * x; }
25}
26
27struct A : IInterface
28{
29    float a;
30
31    [BackwardDifferentiable]
32    [NoDiffThis]
33    float calc(IInterface2 i2, float x)
34    {
35        float b = no_diff(i2.innerCalc(x));
36        return a*b*x; 
37    }
38};
39
40struct B : IInterface
41{
42    float a;
43
44    [BackwardDifferentiable]
45    [NoDiffThis]
46    float calc(IInterface2 i2, float x)
47    {
48        float b = no_diff(i2.innerCalc(x));
49        return a*b*x*x;
50    }
51};
52
53[BackwardDifferentiable]
54float run(int id, float x, no_diff float y)
55{
56    IInterface obj = createDynamicObject<IInterface>(id, y);
57    C c = {};
58    return obj.calc(c, x);
59}
60
61//TEST_INPUT: type_conformance A:IInterface = 0
62//TEST_INPUT: type_conformance B:IInterface = 1
63//TEST_INPUT: type_conformance C:IInterface2 = 0
64
65[numthreads(1, 1, 1)]
66void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
67{
68    {
69        var p = diffPair(3.0);
70
71        __bwd_diff(run)(0, p, 0.5, 1.0f);
72        outputBuffer[0] = p.d; // A.calc, expect 3
73    }
74
75    {
76        var p = diffPair(3.0);
77
78        __bwd_diff(run)(1, p, 1.5, 1.0f);
79        outputBuffer[1] = p.d; // B.calc, expect 40.5
80    }
81}