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.2 KiB50 linesraw
1//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -shaderobj -output-using-type
2//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-vk -compute -shaderobj -output-using-type
3//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-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
8interface IFoo : IDifferentiable
9{
10    [Differentiable]
11    __init(Differential v);
12}
13
14struct Impl : IFoo
15{
16    float x;
17
18    [Differentiable]
19    __init(Differential v)
20    {
21        x = v.x;
22    }
23
24    // We have to add this __init so that the following code can still work:
25    // Impl.Differential v0 = { (float)x };
26    // because when there is a explicit constructor defined, we will not fall back
27    // to legacy constructor. So this construction will fail.
28    [Differentiable]
29    __init(float v)
30    {
31        x = v;
32    }
33}
34
35[Differentiable]
36float test(float x)
37{
38    Impl.Differential v0 = { x };
39    var v1 = Impl(v0);
40    return v1.x * v1.x;
41}
42
43[numthreads(1,1,1)]
44void computeMain(uint tid : SV_DispatchThreadID)
45{
46    var p = diffPair(3.0, 0.0);
47    bwd_diff(test)(p, 1.0);
48    outputBuffer[tid] = p.d;
49    // CHECK: 6.0
50}