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.5 KiB41 linesraw
1
2[Differentiable]
3float sumOfSquares(float x, float y, no_diff float4* test)
4{
5    return x * x + y * y * (test->x + test->y + test->z);
6}
7
8//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type -compile-arg -skip-spirv-validation -emit-spirv-directly
9//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type
10
11//TEST_INPUT: set ptr = ubuffer(data=[1.0 2.0 3.0], stride=4)
12uniform float* ptr;
13
14//TEST_INPUT:ubuffer(data=[0.0 0.0 0.0 0.0 0.0], stride=4):out, name outputBuffer
15RWStructuredBuffer<float> outputBuffer;
16
17[shader("compute")]
18[numthreads(1, 1, 1)]
19void computeMain()
20{
21    float4* testPtr = (float4*)ptr;
22
23    let result = sumOfSquares(2.0, 3.0, testPtr);
24
25    // Use forward differentiation to compute the gradient of the output w.r.t. x only.
26    let diffX = fwd_diff(sumOfSquares)(diffPair(2.0, 1.0), diffPair(3.0, 0.0), testPtr);
27
28    // Create a differentiable pair to pass in the primal value and to receive the gradient.
29    var dpX = diffPair(2.0);
30    var dpY = diffPair(3.0);
31
32    // Propagate the gradient of the output (1.0f) to the input parameters.
33    bwd_diff(sumOfSquares)(dpX, dpY, testPtr, 1.0);
34
35    outputBuffer[0] = result;     // 2^2 + 3^2 * (1 + 2 + 3) = 58
36    outputBuffer[1] = diffX.d;    // 2*x * dx + 2*y * dy * (1 + 2 + 3) = 4
37    outputBuffer[2] = diffX.p;    // 2^2 + 3^2 * (1 + 2 + 3) = 58
38    outputBuffer[3] = dpX.d;      // 2*x = 4
39
40    outputBuffer[4] = dpY.d;      // 2*y * (1 + 2 +3) = 36
41}