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.8 KiB76 linesraw
1//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
2//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
3//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type
4
5//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8struct B : IDifferentiable
9{
10    typedef B Differential;
11    float3 z;
12    float.Differential k[10];
13};
14
15struct A : IDifferentiable
16{
17    typedef B Differential;
18
19    [DerivativeMember(B.z)]
20    float3 x;
21    [DerivativeMember(B.k)]
22    float y[10];
23
24    [__unsafeForceInlineEarly]
25    static Differential dzero()
26    {
27        B b = {0.0};
28        return b;
29    }
30
31    [__unsafeForceInlineEarly]
32    static Differential dadd(Differential a, Differential b)
33    {
34        B o = {a.z + b.z};
35        return o;
36    }
37
38    [__unsafeForceInlineEarly]
39    static Differential dmul<T: __BuiltinRealType>(T a, Differential b)
40    {
41        B o = {__realCast<float, T>(a) * b.z};
42        return o;
43    }
44};
45
46typedef DifferentialPair<A> dpA;
47
48[ForwardDifferentiable]
49A f(A a)
50{
51    A aout;
52
53    aout.y[5] = (2 * a.x).y;
54    aout.y[2] = (3 * a.y[4]);
55    aout.x = float3(5 * a.x.z, 3 * a.x.y, 0.5 * a.x.x);
56
57    return aout;
58}
59
60[numthreads(1, 1, 1)]
61void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
62{
63    {
64        float arr[10] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
65        A a = {float3(1.0, 2.0, 3.0), arr};
66
67        float d_arr[10] = { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 };
68        B b = {float3(1.0, 0.5, 0.3), d_arr};
69
70        dpA dpa = dpA(a, b);
71
72        outputBuffer[0] = __fwd_diff(f)(dpa).d.z.z;  // Expect: 0.5
73        outputBuffer[1] = __fwd_diff(f)(dpa).d.k[5]; // Expect: 1
74        outputBuffer[2] = __fwd_diff(f)(dpa).d.k[2]; // Expect: 1.5
75    }
76}