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.3 KiB60 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], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8struct A : IDifferentiable
9{
10    float x;
11
12    [ForwardDerivative(diff_f)]
13    static float f(float v)
14    {
15        return v * v;
16    }
17
18    static DifferentialPair<float> diff_f(DifferentialPair<float> v)
19    {
20        return diffPair(v.p * v.p, v.p * v.d * 2.0);
21    }
22
23    [BackwardDerivative(diff_g)]
24    static float g(float v)
25    {
26        return v * v;
27    }
28
29    static void diff_g(inout DifferentialPair<float> v, float.Differential dOut)
30    {
31        v = diffPair(v.p, dOut * 2.0);
32    }
33}
34
35[ForwardDifferentiable]
36float test(A obj, float v)
37{
38    return obj.f(v);
39}
40
41[BackwardDifferentiable]
42float test2(A obj, float v)
43{
44    return obj.g(v);
45}
46
47[numthreads(1, 1, 1)]
48void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
49{
50    A a = {0.0};
51    var p = diffPair(3.0, 1.0);
52    let rs = fwd_diff(test)(diffPair(a, {1.0}), p);
53
54    var q = diffPair(3.0);
55    var qa = diffPair(a);
56    bwd_diff(test2)(qa, q, 1.0);
57
58    outputBuffer[0] = rs.d;
59    outputBuffer[1] = q.d;
60}