From 2c437498d3a09b58de17a8865242814d9ea92fde Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Sun, 15 Jan 2023 15:00:20 -0500 Subject: Switched to a much simpler method to transpose control flow, nested control flow works now (#2595) --- tests/autodiff/reverse-nested-control-flow.slang | 82 ++++++++++++++++++++++ .../reverse-nested-control-flow.slang.expected.txt | 6 ++ 2 files changed, 88 insertions(+) create mode 100644 tests/autodiff/reverse-nested-control-flow.slang create mode 100644 tests/autodiff/reverse-nested-control-flow.slang.expected.txt (limited to 'tests') diff --git a/tests/autodiff/reverse-nested-control-flow.slang b/tests/autodiff/reverse-nested-control-flow.slang new file mode 100644 index 000000000..f6020d539 --- /dev/null +++ b/tests/autodiff/reverse-nested-control-flow.slang @@ -0,0 +1,82 @@ +//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 outputBuffer; + +typedef DifferentialPair dpfloat; +typedef float.Differential dfloat; + +[BackwardDifferentiable] +float test_if_only(float y) +{ + float o = y + 1.0f; + if (y > 0.5) + { + o = y * 2.0f; + } + + return o; +} + +[BackwardDifferentiable] +float test_nested_ifelse(float y) +{ + float o = 0.f; + if (y > 0.5) + { + o = y * 2.0f; + } + else + { + if (y > 0.25) + { + o = y * 3.0f + 2.0f; + } + else + { + o = y * 4.0f; + } + } + + return o; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + dpfloat dpa = dpfloat(1.0, 0.0); + + __bwd_diff(test_nested_ifelse)(dpa, 1.0f); + outputBuffer[0] = dpa.d; // Expect: 2.0 + } + + { + dpfloat dpa = dpfloat(0.4, 0.0); + + __bwd_diff(test_nested_ifelse)(dpa, 1.0f); + outputBuffer[1] = dpa.d; // Expect: 3.0 + } + + { + dpfloat dpa = dpfloat(0.2, 0.0); + + __bwd_diff(test_nested_ifelse)(dpa, 1.0f); + outputBuffer[2] = dpa.d; // Expect: 4.0 + } + + { + dpfloat dpa = dpfloat(1.0, 0.0); + + __bwd_diff(test_if_only)(dpa, 1.0f); + outputBuffer[3] = dpa.d; // Expect: 2.0 + } + + { + dpfloat dpa = dpfloat(0.4, 0.0); + + __bwd_diff(test_if_only)(dpa, 1.0f); + outputBuffer[4] = dpa.d; // Expect: 1.0 + } +} diff --git a/tests/autodiff/reverse-nested-control-flow.slang.expected.txt b/tests/autodiff/reverse-nested-control-flow.slang.expected.txt new file mode 100644 index 000000000..d12f4774e --- /dev/null +++ b/tests/autodiff/reverse-nested-control-flow.slang.expected.txt @@ -0,0 +1,6 @@ +type: float +2.000000 +3.000000 +4.000000 +2.000000 +1.000000 -- cgit v1.2.3