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.6 KiB84 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 0 0 0 0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8typedef DifferentialPair<float3> dpfloat3;
9typedef float3.Differential dfloat3;
10
11typedef DifferentialPair<float2> dpfloat2;
12typedef float2.Differential dfloat2;
13
14[BackwardDifferentiable]
15float3 test_simple(float3 x, float3 y)
16{
17    return x + y;
18}
19
20[BackwardDifferentiable]
21float test_swizzles(float3 x, float3 y)
22{
23    return x.y + y.x;
24}
25
26[BackwardDifferentiable]
27float3 test_constructor(float3 x, float3 y)
28{
29    return float3(x.y + y.x, y.z, x.z);
30}
31
32[BackwardDifferentiable]
33float3 test_complex_arith(float3 x, float2 y)
34{
35    float2 t = float2(x.x, y.x) + 2.0 * float2(x.z, y.y);
36    return float3(
37        (2.0f * x.z + y.y * 4.0f - t.y), 
38        (3.0f * t + y).x,
39        t.x);
40}
41
42[numthreads(1, 1, 1)]
43void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
44{
45    {
46        dpfloat3 dpx = dpfloat3(float3(2.0, 3.0, 4.0), float3(0.0, 0.0, 0.0));
47        dpfloat3 dpy = dpfloat3(float3(1.5, 2.5, 3.5), float3(0.0, 0.0, 0.0));
48
49        __bwd_diff(test_simple)(dpx, dpy, dfloat3(1.0, 2.0, 3.0));
50        outputBuffer[0] = dpx.d.y; // Expect: 2
51        outputBuffer[1] = dpy.d.y; // Expect: 2
52    }
53
54    {
55        dpfloat3 dpx = dpfloat3(float3(2.0, 3.0, 4.0), float3(0.0, 0.0, 0.0));
56        dpfloat3 dpy = dpfloat3(float3(1.5, 2.5, 3.5), float3(0.0, 0.0, 0.0));
57
58        __bwd_diff(test_swizzles)(dpx, dpy, 2.3);
59        outputBuffer[2] = dpx.d.y; // Expect: 2.3
60        outputBuffer[3] = dpy.d.x; // Expect: 2.3
61        outputBuffer[4] = dpy.d.y; // Expect: 0.0
62    }
63
64    {
65        dpfloat3 dpx = dpfloat3(float3(2.0, 3.0, 4.0), float3(0.0, 0.0, 0.0));
66        dpfloat3 dpy = dpfloat3(float3(1.5, 2.5, 3.5), float3(0.0, 0.0, 0.0));
67
68        __bwd_diff(test_constructor)(dpx, dpy, float3(1.0, 1.5, 2.0));
69        outputBuffer[5] = dpx.d.y; // Expect: 1.0
70        outputBuffer[6] = dpy.d.x; // Expect: 1.0
71        outputBuffer[7] = dpy.d.z; // Expect: 1.5
72    }
73
74    {
75        dpfloat3 dpx = dpfloat3(float3(2.0, 3.0, 4.0), float3(0.0, 0.0, 0.0));
76        dpfloat2 dpy = dpfloat2(float2(1.5, 2.5), float2(0.0, 0.0));
77
78        __bwd_diff(test_complex_arith)(dpx, dpy, float3(1.0, 1.5, 2.0));
79        outputBuffer[8] = dpx.d.y; // Expect: 0.0
80        outputBuffer[9] = dpy.d.x; // Expect: -1.0 + 1.5 = 0.5
81        outputBuffer[10] = dpx.d.z; // Expect: 2.0 + 9.0 + 4.0 = 15.0
82    }
83
84}