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 KiB75 linesraw
1//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
2//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type
3//TEST:SIMPLE(filecheck=CHECK): -target hlsl -profile cs_5_0 -entry computeMain -line-directive-mode none
4
5//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8typedef DifferentialPair<float> dpfloat;
9typedef float.Differential dfloat;
10
11// Test that compute does not have a context.
12// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}_compute_{{[a-zA-Z0-9_]*}}
13
14[BackwardDifferentiable]
15[PreferRecompute]
16float compute(float x, float y, out float k)
17{
18    k = y * 2;
19    return x * y;
20}
21
22// Test that computeLoop compiles to just return 0.
23// CHECK: float computeLoop{{[_0-9]*}}(float y{{[_0-9]*}})
24// CHECK-NOT: for{{.*}}
25// CHECK: return 0
26
27[BackwardDifferentiable]
28[PreferRecompute]
29float computeLoop(float y)
30{
31    float w = 0;
32    int i = 0;
33    [MaxIters(8)]
34    do
35    {
36        float k = float(0.f);
37        w += compute(i, y, k);
38        w += k * k;
39        i++;
40    }
41    while (i < 8);
42
43    return w - detach(w);
44}
45
46// Since computeLoop is recomputed, test_simple_loop should have nothing to store
47// therefore we check that there is no intermediate context type generated for test_simple_loop.
48
49// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}test_simple_loop{{[a-zA-Z0-9_]*}}
50[BackwardDifferentiable]
51float test_simple_loop(float y)
52{  
53    float x = computeLoop(y);
54    return y + x;
55}
56
57[numthreads(1, 1, 1)]
58void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
59{
60    {
61        dpfloat dpa = dpfloat(1.0, 0.0);
62
63        __bwd_diff(test_simple_loop)(dpa, 1.0f);
64        outputBuffer[0] = dpa.d; // Expect: 29.0
65    }
66
67    {
68        dpfloat dpa = dpfloat(0.4, 0.0);
69
70        __bwd_diff(test_simple_loop)(dpa, 0.5f);
71        outputBuffer[1] = dpa.d; // Expect: 14.5
72    }
73
74    outputBuffer[2] = computeLoop(1.0);
75}