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 | |
| 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')
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang | 15 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang.expected.txt | 3 | ||||
| -rw-r--r-- | tests/autodiff/reverse-addr-eliminate.slang | 1 | ||||
| -rw-r--r-- | tests/autodiff/reverse-array-out-param.slang | 16 | ||||
| -rw-r--r-- | tests/autodiff/reverse-inout-param-0.slang | 31 | ||||
| -rw-r--r-- | tests/autodiff/reverse-inout-param-0.slang.expected.txt | 7 | ||||
| -rw-r--r-- | tests/autodiff/reverse-inout-param-2.slang | 72 | ||||
| -rw-r--r-- | tests/autodiff/reverse-inout-param-2.slang.expected.txt | 7 | ||||
| -rw-r--r-- | tests/autodiff/reverse-inout-param.slang | 47 | ||||
| -rw-r--r-- | tests/autodiff/reverse-inout-param.slang.expected.txt | 6 | ||||
| -rw-r--r-- | tests/autodiff/reverse-loop.slang | 1 | ||||
| -rw-r--r-- | tests/autodiff/reverse-multi-return.slang | 1 | ||||
| -rw-r--r-- | tests/autodiff/reverse-nested-calls.slang | 1 | ||||
| -rw-r--r-- | tests/autodiff/reverse-single-iter-loop.slang | 1 | ||||
| -rw-r--r-- | tests/autodiff/reverse-struct-multi-write.slang | 1 |
15 files changed, 194 insertions, 16 deletions
diff --git a/tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang b/tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang index ce282eb77..379e2c3ef 100644 --- a/tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang +++ b/tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang @@ -1,7 +1,8 @@ //TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type //TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -shaderobj -//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name=outputBuffer RWStructuredBuffer<float> outputBuffer; typedef DifferentialPair<float> dpfloat; @@ -14,6 +15,12 @@ float diffExp(float x) return exp(x); } +[BackwardDifferentiable] +float diffSin(float x) +{ + return sin(x); +} + [numthreads(1, 1, 1)] void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) { @@ -30,4 +37,10 @@ void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) outputBuffer[2] = dpx.p; // Expect: 0.000000 outputBuffer[3] = dpx.d; // Expect: 1.000000 } + + { + dpfloat dpx = dpfloat(float.getPi(), 1.0); + __bwd_diff(diffSin)(dpx, 1.0); + outputBuffer[4] = dpx.d; // Expect: -1.000000 + } } diff --git a/tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang.expected.txt index 49e34b0bf..a4b804cb8 100644 --- a/tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang.expected.txt +++ b/tests/autodiff-dstdlib/dstdlib-elementary-bwd.slang.expected.txt @@ -2,4 +2,5 @@ type: float 2.000000 7.389056 0.000000 -1.000000
\ No newline at end of file +1.000000 +-1.00000
\ No newline at end of file diff --git a/tests/autodiff/reverse-addr-eliminate.slang b/tests/autodiff/reverse-addr-eliminate.slang index e23e83e6a..49f34a6e3 100644 --- a/tests/autodiff/reverse-addr-eliminate.slang +++ b/tests/autodiff/reverse-addr-eliminate.slang @@ -1,5 +1,6 @@ //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; diff --git a/tests/autodiff/reverse-array-out-param.slang b/tests/autodiff/reverse-array-out-param.slang index e1c822489..c7965ca5d 100644 --- a/tests/autodiff/reverse-array-out-param.slang +++ b/tests/autodiff/reverse-array-out-param.slang @@ -1,5 +1,6 @@ //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; @@ -39,17 +40,4 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) outputBuffer[0] = dpd.d.m; outputBuffer[1] = dpd.d.n; -} - -[BackwardDifferentiable] -void inner(float x, out float y) -{ - y = 2 * x; -} - -[BackwardDifferentiable] -void outter(float x, out float y) -{ - inner(x, y); - inner(x, y); -} +}
\ No newline at end of file diff --git a/tests/autodiff/reverse-inout-param-0.slang b/tests/autodiff/reverse-inout-param-0.slang new file mode 100644 index 000000000..13bfb6277 --- /dev/null +++ b/tests/autodiff/reverse-inout-param-0.slang @@ -0,0 +1,31 @@ +//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; + +[BackwardDifferentiable] +void g(float x, out float y) +{ + y = (x + 1) * (x + 1); +} + +[BackwardDifferentiable] +void f(float x, out float y) +{ + g(x, y); + g(x, y); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + var x = diffPair(5.0); + float yDiffOut = 1.0; + + __bwd_diff(f)(x, yDiffOut); + + outputBuffer[0] = x.p; // should be 5, since bwd_diff does not write back new primal val. + outputBuffer[1] = x.d; // 12.0 +}
\ No newline at end of file diff --git a/tests/autodiff/reverse-inout-param-0.slang.expected.txt b/tests/autodiff/reverse-inout-param-0.slang.expected.txt new file mode 100644 index 000000000..a11719af3 --- /dev/null +++ b/tests/autodiff/reverse-inout-param-0.slang.expected.txt @@ -0,0 +1,7 @@ +type: float +5.000000 +12.000000 +0.000000 +0.000000 +0.000000 + 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 diff --git a/tests/autodiff/reverse-inout-param-2.slang.expected.txt b/tests/autodiff/reverse-inout-param-2.slang.expected.txt new file mode 100644 index 000000000..65933cc7d --- /dev/null +++ b/tests/autodiff/reverse-inout-param-2.slang.expected.txt @@ -0,0 +1,7 @@ +type: float +5.000000 +14.000000 +1.000000 +2.000000 +1.000000 + diff --git a/tests/autodiff/reverse-inout-param.slang b/tests/autodiff/reverse-inout-param.slang new file mode 100644 index 000000000..7d7f4cb05 --- /dev/null +++ b/tests/autodiff/reverse-inout-param.slang @@ -0,0 +1,47 @@ +//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 n; + float m; +} + +[BackwardDifferentiable] +void g(no_diff float p, inout float x) +{ + x = p * ((x+1)*(x+1)); +} + +[BackwardDifferentiable] +void f(no_diff float p, inout float x) +{ + g(p, x); + g(p, x); +} +[BackwardDifferentiable] +float f_ref(no_diff float p, float x) +{ + float y1 = p * (x+1)*(x+1); + float y2 = p * (y1+1)*(y1+1); + return y2; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + var x = diffPair(2.0, 1.0); + + __bwd_diff(f)(3.0, x); + + outputBuffer[0] = x.p; // should be 2, since bwd_diff does not write back new primal val. + outputBuffer[1] = x.d; // 3024 + + var refVal = __fwd_diff(f_ref)(3.0, diffPair(2.0, 1.0)).d; + outputBuffer[2] = refVal; // 3024 + +}
\ No newline at end of file diff --git a/tests/autodiff/reverse-inout-param.slang.expected.txt b/tests/autodiff/reverse-inout-param.slang.expected.txt new file mode 100644 index 000000000..2df174e2f --- /dev/null +++ b/tests/autodiff/reverse-inout-param.slang.expected.txt @@ -0,0 +1,6 @@ +type: float +2.000000 +3024.000000 +3024.000000 +0.000000 +0.000000 diff --git a/tests/autodiff/reverse-loop.slang b/tests/autodiff/reverse-loop.slang index 6ae207080..46d707548 100644 --- a/tests/autodiff/reverse-loop.slang +++ b/tests/autodiff/reverse-loop.slang @@ -1,6 +1,7 @@ //TEST_IGNORE_FILE: //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; diff --git a/tests/autodiff/reverse-multi-return.slang b/tests/autodiff/reverse-multi-return.slang index ee8bb9a4c..21f72619e 100644 --- a/tests/autodiff/reverse-multi-return.slang +++ b/tests/autodiff/reverse-multi-return.slang @@ -1,5 +1,6 @@ //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 0], stride=4):out,name=outputBuffer RWStructuredBuffer<float> outputBuffer; diff --git a/tests/autodiff/reverse-nested-calls.slang b/tests/autodiff/reverse-nested-calls.slang index 55d22e191..caf2df6f8 100644 --- a/tests/autodiff/reverse-nested-calls.slang +++ b/tests/autodiff/reverse-nested-calls.slang @@ -1,5 +1,6 @@ //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; diff --git a/tests/autodiff/reverse-single-iter-loop.slang b/tests/autodiff/reverse-single-iter-loop.slang index 20c26e000..3597f0ec8 100644 --- a/tests/autodiff/reverse-single-iter-loop.slang +++ b/tests/autodiff/reverse-single-iter-loop.slang @@ -1,5 +1,6 @@ //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 0], stride=4):out,name=outputBuffer RWStructuredBuffer<float> outputBuffer; diff --git a/tests/autodiff/reverse-struct-multi-write.slang b/tests/autodiff/reverse-struct-multi-write.slang index dd12c7d3d..2a63141cb 100644 --- a/tests/autodiff/reverse-struct-multi-write.slang +++ b/tests/autodiff/reverse-struct-multi-write.slang @@ -1,6 +1,7 @@ //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; |
