yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeDiagnose on storing differentiable value into non-differentiable location. (#2681)f23e36243

master
1.6 KiB71 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:-cpu -compute -output-using-type -shaderobj
4
5//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8struct D : IDifferentiable
9{
10    float m;
11    float n;
12}
13
14struct ND
15{
16    float nd;
17}
18
19[BackwardDifferentiable]
20void g(
21    inout no_diff D p,
22    out no_diff D po,
23    out ND v1,
24    inout ND v2,
25    float x,
26    out float y)
27{
28    v1 = v2;
29    v2.nd = v2.nd + 1.0;
30    p.n = v1.nd + 1.0;
31    p.m = detach(v2.nd + 1.0 + x); // == v2.nd + 2 + x == 1 + 2 + x == 3+x
32    po = p;
33    po.m += 1.0; // == 4+x
34    y = p.m * x; // == (3+x)*x
35}
36
37[BackwardDifferentiable]
38void f(inout no_diff D p, out no_diff D p0, out ND v1, inout ND v2, float x, out float y)
39{
40    // v2.nd is 3.
41    g(p, p0, v1, v2, x, y);
42    // v2.nd is now 4, now g is equivalent to detach(4+x)*x, so g' = 9.
43    g(p, p0, v1, v2, x, y);
44}
45
46[ForwardDifferentiable]
47float f_ref(float x)
48{
49    return (3 + 3 * x + x * x) * (3 * x + x * x);
50}
51
52[numthreads(1, 1, 1)]
53void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
54{
55    D p;
56    p.m = 1.0;
57    p.n = 2.0;
58
59    let v2 : ND = { 1.0 };
60
61    var x = diffPair(5.0);
62    float yDiffOut = 1.0;
63
64    __bwd_diff(f)(p, v2, x, yDiffOut);
65
66    outputBuffer[0] = x.p; // should be 5, since bwd_diff does not write back new primal val.
67    outputBuffer[1] = x.d; // 9
68    outputBuffer[2] = p.m; // 1.0
69    outputBuffer[3] = p.n; // 2.0
70    outputBuffer[4] = v2.nd; // 1.0
71}