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.1 KiB51 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], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8struct MyStruct : IDifferentiable
9{
10    float x;
11    uint i;
12
13    [ForwardDifferentiable]
14    __init(float x, uint i)
15    {
16        this.x = x;
17        this.i = i;
18    }
19};
20
21[ForwardDifferentiable]
22float mySqr(MyStruct s)
23{
24    if (s.i >= 1)
25        return s.x * s.x;
26    else
27        return 0.f;
28}
29
30[ForwardDifferentiable]
31float f(float x)
32{
33    MyStruct ms = MyStruct(x * x, 1);
34    return mySqr(ms);
35}
36
37[ForwardDifferentiable]
38float df(float x)
39{
40    return __fwd_diff(f)(DifferentialPair<float>(x, 1.0)).d;
41}
42
43[numthreads(1, 1, 1)]
44void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
45{
46    // Given f(x) = x^4,
47    // f''(x) = 12 * x^2
48    // Expect f''(4) = 192
49    outputBuffer[0] = __fwd_diff(df)(DifferentialPair<float>(4.0, 1.0)).d;
50}
51