yum-mirror/slang

Making it easier to work with shaders

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

Yong HeMake tuple types work in autodiff. (#4923)638e5fb00

master
1.5 KiB49 linesraw
1
2//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -shaderobj -output-using-type
3//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-vk -compute -shaderobj -output-using-type
4//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-cpu -compute -output-using-type -shaderobj
5
6// This is a test modified from autodiff/reverse-struct-multi-write.slang to test that
7// tuple types can be autodiff'ed the same way as struct types.
8
9//TEST_INPUT:ubuffer(data=[1 2], stride=4):out,name=outputBuffer
10RWStructuredBuffer<float> outputBuffer;
11
12typealias A = Tuple<float, Tuple<float, float>>;
13
14[Differentiable]
15A f(A a)
16{
17    // Read/writes to local struct variables won't be SSA'd out by default.
18    // The backward diff preparation pass will kick in to create temp vars for them.
19    A aout;
20    aout._1._1 = 2 * a._1._0;
21    aout._1._1 = aout._1._1 + 2 * a._1._0;
22    aout._1._0 = aout._1._1 + 5 * a._1._0;
23
24    // The result should be equivalent to:
25    /*
26    A aout;
27    var tmp = 2 * a.x;
28    tmp = tmp + 2 * a.x;
29    aout.y = tmp;
30    aout.x = tmp + 5 * a.x;
31    */
32    return aout;
33}
34
35[numthreads(1, 1, 1)]
36void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
37{
38    A a = makeTuple(1.0, makeTuple(1.0, 2.0));
39
40    var dpa = diffPair(a);
41
42    A.Differential dout = makeTuple(1.0, makeTuple(1.0, 1.0));
43    
44    bwd_diff(f)(dpa, dout);
45    // CHECK: 13
46    outputBuffer[0] = dpa.d._1._0; // Expect: 13
47    // CHECK: 0
48    outputBuffer[1] = dpa.d._1._1; // Expect: 0
49}