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 KiB47 linesraw
1//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -shaderobj -output-using-type -g0
2//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-cuda -compute -shaderobj -output-using-type
3
4//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBufferPrimal
5RWStructuredBuffer<float> outputBufferPrimal;
6
7//TEST_INPUT:ubuffer(data=[1 2 3 4], stride=4):name=gradBuffer
8RWStructuredBuffer<float> gradBuffer;
9
10struct BufferWithGrad
11{
12    RWStructuredBuffer<float> primal;
13    RWStructuredBuffer<float> grad;
14
15    [Differentiable]
16    void add(float value) { primal[0] = primal[0] + detach(value); }
17
18    [PrimalSubstituteOf(add), Differentiable]
19    void add_subst(float value)
20    {
21    }
22
23    [BackwardDerivativeOf(add)]
24    void add_bwd(inout DifferentialPair<float> d)
25    {
26        d = diffPair(d.p, grad[0]);
27    }
28}
29
30[Differentiable]
31void diffCall(BufferWithGrad result)
32{
33    result.add(1.0f);
34}
35
36[shader("compute")]
37[numthreads(1, 1, 1)]
38void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
39{
40    BufferWithGrad bg = {outputBufferPrimal, gradBuffer};
41    diffCall(bg); 
42    bwd_diff(diffCall)(bg);
43
44    // CHECK: type: float
45    // CHECK-NEXT: 1.0
46    // CHECK-NEXT: 0.0
47}