diff options
| author | Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> | 2023-01-30 11:46:36 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-30 08:46:36 -0800 |
| commit | 134dd7eb26fc7988ae13559d276cbf337b4b9d27 (patch) | |
| tree | 35bd06e6bebb4518bca805e14e85f8f9ef4341c6 /tests | |
| parent | 4a66e9729175a89833e5db784bb64e6a7f60cdf2 (diff) | |
Overhauled reverse-mode control flow handling (#2608)
* Added switch-case support; fixed non-diff parameter transposition
* Made region propagation much more robust. Partial loop unzip implementation
* WIP: Added most loop handling code, and a test. Still untested
* Added CFG Normalization pass + CFG Reversal Pass + Loop Unzipping + most loop transcription
* Add single-iter-loop test.
* proj files
* removed comments
* Update reverse-loop.slang
* Removed out-of-date code
* Disabled IR validation during constructSSA phase of normalizeCFG. constructSSA now reuses sharedBuilder
* Moved normalizeCFG() call to prepareFuncForBackwardDiff()
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/autodiff/reverse-loop.slang | 40 | ||||
| -rw-r--r-- | tests/autodiff/reverse-single-iter-loop.slang | 99 | ||||
| -rw-r--r-- | tests/autodiff/reverse-single-iter-loop.slang.expected.txt | 7 |
3 files changed, 146 insertions, 0 deletions
diff --git a/tests/autodiff/reverse-loop.slang b/tests/autodiff/reverse-loop.slang new file mode 100644 index 000000000..6ae207080 --- /dev/null +++ b/tests/autodiff/reverse-loop.slang @@ -0,0 +1,40 @@ +//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_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; +typedef float.Differential dfloat; + +[BackwardDifferentiable] +float test_simple_loop(float y) +{ + float t = y; + + for (int i = 0; i < 3; i++) + { + t = t * t; + } + + return t; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + dpfloat dpa = dpfloat(1.0, 0.0); + + __bwd_diff(test_simple_loop)(dpa, 1.0f); + outputBuffer[0] = dpa.d; // Expect: 2.0 + } + + { + dpfloat dpa = dpfloat(0.4, 0.0); + + __bwd_diff(test_simple_loop)(dpa, 1.0f); + outputBuffer[1] = dpa.d; // Expect: 1.0 + } +} diff --git a/tests/autodiff/reverse-single-iter-loop.slang b/tests/autodiff/reverse-single-iter-loop.slang new file mode 100644 index 000000000..47232147a --- /dev/null +++ b/tests/autodiff/reverse-single-iter-loop.slang @@ -0,0 +1,99 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; +typedef float.Differential dfloat; + +[BackwardDifferentiable] +float test_simple_single_iter_loop(float y) +{ + float x = 0; + + for (int i = 0; i < 1; i++) + { + if (y > 0.6) + { + x = y * 10.0f; + break; + } + else + { + x = y * 6.0f; + break; + } + } + + return x; +} + +[BackwardDifferentiable] +float test_nested_if_else_single_iter_loop(float y) +{ + float x = 0; + + for (int i = 0; i < 1; i++) + { + if (y > 0.6) + { + if (y > 0.8) + { + x = y * 10.0f; + break; + } + else + { + x = y * 4.0f; + break; + } + } + else + { + x = y * 6.0f; + break; + } + } + + return x; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + dpfloat dpa = dpfloat(1.0, 0.0); + + __bwd_diff(test_simple_single_iter_loop)(dpa, 1.0f); + outputBuffer[0] = dpa.d; // Expect: 10.0 + } + + { + dpfloat dpa = dpfloat(0.4, 0.0); + + __bwd_diff(test_simple_single_iter_loop)(dpa, 1.0f); + outputBuffer[1] = dpa.d; // Expect: 6.0 + } + + { + dpfloat dpa = dpfloat(1.0, 0.0); + + __bwd_diff(test_nested_if_else_single_iter_loop)(dpa, 1.0f); + outputBuffer[2] = dpa.d; // Expect: 10.0 + } + + { + dpfloat dpa = dpfloat(0.7, 0.0); + + __bwd_diff(test_nested_if_else_single_iter_loop)(dpa, 1.0f); + outputBuffer[3] = dpa.d; // Expect: 4.0 + } + + { + dpfloat dpa = dpfloat(0.4, 0.0); + + __bwd_diff(test_nested_if_else_single_iter_loop)(dpa, 1.0f); + outputBuffer[4] = dpa.d; // Expect: 6.0 + } +} diff --git a/tests/autodiff/reverse-single-iter-loop.slang.expected.txt b/tests/autodiff/reverse-single-iter-loop.slang.expected.txt new file mode 100644 index 000000000..229f05e65 --- /dev/null +++ b/tests/autodiff/reverse-single-iter-loop.slang.expected.txt @@ -0,0 +1,7 @@ +type: float +10.000000 +6.000000 +10.000000 +4.000000 +6.000000 +0.000000
\ No newline at end of file |
