summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/autodiff/reverse-loop.slang40
-rw-r--r--tests/autodiff/reverse-single-iter-loop.slang99
-rw-r--r--tests/autodiff/reverse-single-iter-loop.slang.expected.txt7
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