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 KiB73 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[BackwardDifferentiable]
12[PreferRecompute]
13float compute(float x, float y)
14{
15    return x * y * y;
16}
17
18[BackwardDifferentiable]
19[ForceInline]
20float infinitesimal(float x)
21{
22    return x - detach(x);
23}
24
25// Test that computeLoop compiles to just return 0.
26// CHECK: float computeLoop{{[_0-9]*}}(float y{{[_0-9]*}})
27// CHECK-NOT: for{{.*}}
28// CHECK: return 0
29
30[BackwardDifferentiable]
31[PreferRecompute]
32float computeLoop(float y)
33{
34    float w = 0;
35
36    for (int i = 0; i < 8; i++)
37    {
38        w += compute(i, y);
39    }
40
41    return w - detach(w);
42}
43
44// Since computeLoop is recomputed, test_simple_loop should have nothing to store
45// therefore we check that there is no intermediate context type generated for test_simple_loop.
46
47// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}test_simple_loop{{[a-zA-Z0-9_]*}}
48[BackwardDifferentiable]
49float test_simple_loop(float y)
50{  
51    float x = __fwd_diff(computeLoop)(diffPair(y, 1.0)).d;
52    return y + x;
53}
54
55[numthreads(1, 1, 1)]
56void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
57{
58    {
59        dpfloat dpa = dpfloat(1.0, 0.0);
60
61        __bwd_diff(test_simple_loop)(dpa, 1.0f);
62        outputBuffer[0] = dpa.d; // Expect: 57.0
63    }
64
65    {
66        dpfloat dpa = dpfloat(0.4, 0.0);
67
68        __bwd_diff(test_simple_loop)(dpa, 0.5f);
69        outputBuffer[1] = dpa.d; // Expect: 28.5
70    }
71
72    outputBuffer[2] = computeLoop(1.0);
73}