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.7 KiB83 linesraw
1//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
2//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
3//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type
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]
12float test_if_only(float y)
13{
14    float o = y + 1.0f;
15    if (y > 0.5)
16    {
17        o = y * 2.0f;
18    }
19
20    return o;
21}
22
23[BackwardDifferentiable]
24float test_nested_ifelse(float y)
25{
26    float o = 0.f;
27    if (y > 0.5)
28    {
29        o = y * 2.0f;
30    }
31    else
32    {
33        if (y > 0.25)
34        {
35            o = y * 3.0f + 2.0f;
36        }
37        else
38        {
39            o = y * 4.0f;
40        }
41    }
42
43    return o;
44}
45
46[numthreads(1, 1, 1)]
47void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
48{
49    {
50        dpfloat dpa = dpfloat(1.0, 0.0);
51
52        __bwd_diff(test_nested_ifelse)(dpa, 1.0f);
53        outputBuffer[0] = dpa.d; // Expect: 2.0
54    }
55
56    {
57        dpfloat dpa = dpfloat(0.4, 0.0);
58        
59        __bwd_diff(test_nested_ifelse)(dpa, 1.0f);
60        outputBuffer[1] = dpa.d; // Expect: 3.0
61    }
62
63    {
64        dpfloat dpa = dpfloat(0.2, 0.0);
65        
66        __bwd_diff(test_nested_ifelse)(dpa, 1.0f);
67        outputBuffer[2] = dpa.d; // Expect: 4.0
68    }
69
70    {
71        dpfloat dpa = dpfloat(1.0, 0.0);
72
73        __bwd_diff(test_if_only)(dpa, 1.0f);
74        outputBuffer[3] = dpa.d; // Expect: 2.0
75    }
76
77    {
78        dpfloat dpa = dpfloat(0.4, 0.0);
79        
80        __bwd_diff(test_if_only)(dpa, 1.0f);
81        outputBuffer[4] = dpa.d; // Expect: 1.0
82    }
83}