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 KiB79 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 0 0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8typedef DifferentialPair<float> dpfloat;
9typedef float.Differential dfloat;
10
11[BackwardDifferentiable]
12float f(float x)
13{
14    return x;
15}
16
17[BackwardDifferentiable]
18float h(float x, float y)
19{
20    float m = x + y;
21    float n = x - y;
22    return m * n + 2 * x * y;
23}
24
25[BackwardDifferentiable]
26float j(float x, float y)
27{
28    float m = x / y;
29    return m * y;
30}
31
32[BackwardDifferentiable]
33float k(float x, float y)
34{
35    float m = -x;
36    return m * y;
37}
38
39[numthreads(1, 1, 1)]
40void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
41{
42    {
43        dpfloat dpa = dpfloat(2.0, 1.0);
44
45        __bwd_diff(f)(dpa, 1.0);
46
47        outputBuffer[0] = dpa.d; // Expect: 1
48    }
49
50    {
51        dpfloat dpa = dpfloat(2.0, 1.0);
52        dpfloat dpb = dpfloat(1.5, 1.0);
53
54        __bwd_diff(h)(dpa, dpb, 1.0);
55
56        outputBuffer[1] = dpa.d; // Expect: (2 * 2.0) + (2 * 1.5) = 7.0
57        outputBuffer[2] = dpb.d; // Expect: -(2 * 1.5) + (2 * 2.0) = 1.0
58    }
59
60    {
61        dpfloat dpa = dpfloat(2.0, 1.0);
62        dpfloat dpb = dpfloat(1.5, 1.0);
63
64        __bwd_diff(j)(dpa, dpb, 1.0);
65
66        outputBuffer[3] = dpa.d; // Expect: 1
67        outputBuffer[4] = dpb.d; // Expect: 0
68    }
69
70    {
71        dpfloat dpa = dpfloat(2.0, 1.0);
72        dpfloat dpb = dpfloat(1.5, 1.0);
73
74        __bwd_diff(k)(dpa, dpb, 1.0);
75
76        outputBuffer[5] = dpa.d; // Expect: -1.5
77        outputBuffer[6] = dpb.d; // Expect: -2.0
78    }
79}