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.1 KiB49 linesraw
1// Tests automatic synthesis of Differential type and method requirements.
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
10struct B : IDifferentiable
11{
12    float x;
13}
14
15struct A : IDifferentiable
16{
17    B b;
18    float y;
19};
20
21typedef DifferentialPair<A> dpA;
22
23A nonDiff(A a)
24{
25    return a;
26}
27
28[ForwardDifferentiable]
29A f(A a)
30{
31    A aout;
32    aout.y = 2 * a.b.x;
33    aout.b.x = 5 * a.b.x;
34
35    return no_diff(nonDiff(aout));
36}
37
38[numthreads(1, 1, 1)]
39void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
40{
41    {
42        A a = {1.0, 2.0};
43        A.Differential b = {0.2};
44        dpA dpa = dpA(a, b);
45        outputBuffer[0] = __fwd_diff(f)(dpa).d.b.x;         // Expect: 0
46        outputBuffer[1] = A.dadd(b, b).b.x;                 // Expect: 0.4
47        outputBuffer[2] = A.dmul<float>(2.0, b).b.x;        // Expect: 0.4
48    }
49}