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
2.5 KiB93 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[BackwardDifferentiable]
23[ForceInline]
24float infinitesimal(float x)
25{
26    return x - detach(x);
27}
28
29// Test that computeLoop compiles to just return 0.
30// CHECK: float computeLoop{{[_0-9]*}}(float y{{[_0-9]*}})
31// CHECK-NOT: for{{.*}}
32// CHECK: return 0
33
34// Test that computeLoop's intermediates have no float sitting
35// around (must not cache the outvar from 'compute()')
36// 
37// Further, if loop exit value inference is working correctly, 
38// then there should be no context type at all.
39// 
40// CHECK-NOT: struct s_bwd_prop_computeLoop_Intermediates
41//
42// Check that the signature of the s_bwd_prop_computeLoop function only
43// contains an inout DiffPair_float_0 and a float.
44//
45// CHECK: void s_bwd_prop_computeLoop{{[_0-9]*}}(inout DiffPair_float{{[_0-9]*}} dpy{{[_0-9]*}}, float {{[_a-zA-Z0-9]*}})
46//
47
48[BackwardDifferentiable]
49[PreferRecompute]
50float computeLoop(float y)
51{
52    float w = 0;
53
54    for (int i = 0; i < 8; i++)
55    {
56        float k = float(0.f);
57        w += compute(i, y, k);
58        w += k * k;
59    }
60
61    return w - detach(w);
62}
63
64// Since computeLoop is recomputed, test_simple_loop should have nothing to store
65// therefore we check that there is no intermediate context type generated for test_simple_loop.
66
67// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}test_simple_loop{{[a-zA-Z0-9_]*}}
68[BackwardDifferentiable]
69float test_simple_loop(float y)
70{  
71    float x = computeLoop(y);
72    return y + x;
73}
74
75[numthreads(1, 1, 1)]
76void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
77{
78    {
79        dpfloat dpa = dpfloat(1.0, 0.0);
80
81        __bwd_diff(test_simple_loop)(dpa, 1.0f);
82        outputBuffer[0] = dpa.d; // Expect: 29.0
83    }
84
85    {
86        dpfloat dpa = dpfloat(0.4, 0.0);
87
88        __bwd_diff(test_simple_loop)(dpa, 0.5f);
89        outputBuffer[1] = dpa.d; // Expect: 14.5
90    }
91
92    outputBuffer[2] = computeLoop(1.0);
93}