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 KiB62 linesraw
1// Test calling backward differentiable function through dynamic dispatch, where the interface
2// being dispatched inherits from IDifferentiable, so that `this` is differentiable.
3
4//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
5//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type
6//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
7
8//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
9RWStructuredBuffer<float> outputBuffer;
10
11[anyValueSize(16)]
12interface IInterface : IDifferentiable
13{
14    [BackwardDifferentiable]
15    float calc(float x);
16}
17
18struct C : IInterface
19{
20    [BackwardDifferentiable]
21    float calc(float x) { return 2 * x; }
22}
23
24struct A : IInterface
25{
26    float a;
27    [BackwardDifferentiable]
28    float calc(float x)
29    {
30        return a * x * x;
31    }
32};
33
34
35[BackwardDifferentiable]
36float run(int id, float x, no_diff float y)
37{
38    IInterface obj = createDynamicObject<IInterface>(id, y);
39    C c = {};
40    return obj.calc(x);
41}
42
43//TEST_INPUT: type_conformance A:IInterface = 0
44//TEST_INPUT: type_conformance C:IInterface = 1
45
46[numthreads(1, 1, 1)]
47void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
48{
49    {
50        var p = diffPair(3.0);
51
52        __bwd_diff(run)(0, p, 0.5, 1.0f);
53        outputBuffer[0] = p.d; // A.calc, expect 3
54    }
55
56    {
57        var p = diffPair(3.0);
58
59        __bwd_diff(run)(1, p, 1.5, 1.0f);
60        outputBuffer[1] = p.d; // c.calc, expect 2
61    }
62}