diff options
| author | Yong He <yonghe@outlook.com> | 2023-02-03 16:44:33 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-03 16:44:33 -0800 |
| commit | 228e71dab7dfa18ece979f4099ec0c7d1e37e5ff (patch) | |
| tree | ff357f4aaed2dab25ae9e3665a97a7f3e6be32ef /tests/autodiff/reverse-inout-param-2.slang | |
| parent | ee49a62083d28353812185fd0f0c04fb50ca6be0 (diff) | |
Overhaul `transposeParameterBlock` to support `inout` params. (#2621)
* Overhaul `transposeParameterBlock` to support `inout` params.
* Small bug fixes.
* Bug fix on differentiable intrinsic specialization.
* Fixes.
* Run autodiff tests on CPU.
* Clean up.
* More bug fixes.,
* Add test coverage on inout param.
* Fix language server hinting for transcribed mutable params.
---------
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'tests/autodiff/reverse-inout-param-2.slang')
| -rw-r--r-- | tests/autodiff/reverse-inout-param-2.slang | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/autodiff/reverse-inout-param-2.slang b/tests/autodiff/reverse-inout-param-2.slang new file mode 100644 index 000000000..1fa64751b --- /dev/null +++ b/tests/autodiff/reverse-inout-param-2.slang @@ -0,0 +1,72 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -shaderobj + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +struct D : IDifferentiable +{ + float m; + float n; +} + +struct ND +{ + float nd; +} + +[BackwardDifferentiable] +void g( + inout no_diff D p, + out no_diff D po, + out ND v1, + inout ND v2, + float x, + out float y) +{ + v1 = v2; + v2.nd = v2.nd + 1.0; + p.n = v1.nd + 1.0; + p.m = v2.nd + 1.0 + x; // == v2.nd + 2 + x == 1 + 2 + x == 3+x + po = p; + po.m += 1.0; // == 4+x + y = p.m * x; // == (3+x)*x +} + +[BackwardDifferentiable] +void f(inout no_diff D p, out no_diff D p0, out ND v1, inout ND v2, float x, out float y) +{ + // v2.nd is 3. + g(p, p0, v1, v2, x, y); + // v2.nd is now 4, now g is equivalent to (4+x)*x. + g(p, p0, v1, v2, x, y); +} + +[ForwardDifferentiable] +float f_ref(float x) +{ + return (3 + 3 * x + x * x) * (3 * x + x * x); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + D p; + p.m = 1.0; + p.n = 2.0; + + ND v2 = { 1.0 }; + + var x = diffPair(5.0); + float yDiffOut = 1.0; + + __bwd_diff(f)(p, v2, x, yDiffOut); + + // (3+((3+x)*x))*((3+x)*x) = (3+3x+x^2)*(3x+x^2) + outputBuffer[0] = x.p; // should be 5, since bwd_diff does not write back new primal val. + outputBuffer[1] = x.d; // 14 + outputBuffer[2] = p.m; // 1.0 + outputBuffer[3] = p.n; // 2.0 + outputBuffer[4] = v2.nd; // 1.0 +}
\ No newline at end of file |
