From 259a015feb9d4ab65e8fbba32f6c777e92780cc7 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 22 Mar 2023 21:16:35 -0700 Subject: Type legalization and autodiff bug fixes. (#2722) * Bug fixes. * Fix. * Only perform autodiff for functions whose derivative is actually used. * Fix loop optimize bug. * Fix high order diff. * Fix trivial diff func generation. * Fixes. * Cleanup. --------- Co-authored-by: Yong He --- tests/autodiff-dstdlib/determinant.slang | 24 +++++++++++++++++++ .../determinant.slang.expected.txt | 5 ++++ tests/bugs/loop-optimize.slang | 28 ++++++++++++++++++++++ tests/bugs/loop-optimize.slang.expected.txt | 2 ++ 4 files changed, 59 insertions(+) create mode 100644 tests/autodiff-dstdlib/determinant.slang create mode 100644 tests/autodiff-dstdlib/determinant.slang.expected.txt create mode 100644 tests/bugs/loop-optimize.slang create mode 100644 tests/bugs/loop-optimize.slang.expected.txt (limited to 'tests') diff --git a/tests/autodiff-dstdlib/determinant.slang b/tests/autodiff-dstdlib/determinant.slang new file mode 100644 index 000000000..d2e699551 --- /dev/null +++ b/tests/autodiff-dstdlib/determinant.slang @@ -0,0 +1,24 @@ +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +[BackwardDifferentiable] +float diffDeterminant(float2x2 x) +{ + return determinant(x); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + { + var dpx = diffPair(float2x2(1.0, 2.0, 3.0, 4.0)); + __bwd_diff(diffDeterminant)(dpx, 1.0); + outputBuffer[0] = dpx.d[0][0]; + outputBuffer[1] = dpx.d[0][1]; + outputBuffer[2] = dpx.d[1][0]; + outputBuffer[3] = dpx.d[1][1]; + } +} diff --git a/tests/autodiff-dstdlib/determinant.slang.expected.txt b/tests/autodiff-dstdlib/determinant.slang.expected.txt new file mode 100644 index 000000000..b2c2f7e17 --- /dev/null +++ b/tests/autodiff-dstdlib/determinant.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +4.000000 +-3.000000 +-2.000000 +1.000000 diff --git a/tests/bugs/loop-optimize.slang b/tests/bugs/loop-optimize.slang new file mode 100644 index 000000000..85231dbea --- /dev/null +++ b/tests/bugs/loop-optimize.slang @@ -0,0 +1,28 @@ +//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, vulkan):COMPARE_COMPUTE_EX:-cpu -compute -shaderobj -output-using-type +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0], stride=4):out,name outputBuffer +RWStructuredBuffer outputBuffer; + +void test(inout float v, int i) +{ + while (true) + { + i--; + if (i < 2) + { + v += 1.0; + return; + } + } +} + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + float v = 2.0; + test(v, 3); + outputBuffer[0] = v; // Expect 3.0 +} \ No newline at end of file diff --git a/tests/bugs/loop-optimize.slang.expected.txt b/tests/bugs/loop-optimize.slang.expected.txt new file mode 100644 index 000000000..f38cc1080 --- /dev/null +++ b/tests/bugs/loop-optimize.slang.expected.txt @@ -0,0 +1,2 @@ +type: float +3.0 -- cgit v1.2.3