summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-02-20 14:42:50 -0800
committerGitHub <noreply@github.com>2023-02-20 14:42:50 -0800
commit47715e625337d489f3c0131bbc2b849378b48a5a (patch)
treebc737c8f03ef537b2ac39860bbb922c7600edc43 /tests
parent8b05df4187117d61491f2fdbeb7d744146ad73f7 (diff)
Miscellaneous backward autodiff fixes. (#2665)
* Fix differentiable type registration * Fix use of non-differentiable return value in a differentiable func. * Fix use of primal inst that does not dominate the diff block. * Fix primal inst hoisting, and add missing type legalization logic. * Make `detach` defined on all differentiable T. --------- Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/autodiff/bool-return-control-flow.slang31
-rw-r--r--tests/autodiff/bool-return-control-flow.slang.expected.txt5
-rw-r--r--tests/autodiff/reverse-control-flow-2.slang75
-rw-r--r--tests/autodiff/reverse-control-flow-2.slang.expected.txt6
-rw-r--r--tests/diagnostics/autodiff-data-flow-2.slang6
-rw-r--r--tests/diagnostics/autodiff-data-flow-2.slang.expected5
-rw-r--r--tests/diagnostics/autodiff.slang5
-rw-r--r--tests/diagnostics/autodiff.slang.expected3
8 files changed, 127 insertions, 9 deletions
diff --git a/tests/autodiff/bool-return-control-flow.slang b/tests/autodiff/bool-return-control-flow.slang
new file mode 100644
index 000000000..9dd398a89
--- /dev/null
+++ b/tests/autodiff/bool-return-control-flow.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_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+[BackwardDifferentiable]
+bool conditionFunc(no_diff float a, inout float x)
+{
+ x = x * a;
+ return x > 100.f;
+}
+
+[BackwardDifferentiable]
+float outerFunc(no_diff float a, float x)
+{
+ if (conditionFunc(a, x))
+ return x;
+ else
+ return -x;
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
+{
+ float a = 10.0;
+ DifferentialPair<float> dpx = DifferentialPair<float>(4.f, 1.f);
+ __bwd_diff(outerFunc)(a, dpx, 1.0);
+
+ outputBuffer[0] = dpx.d;
+} \ No newline at end of file
diff --git a/tests/autodiff/bool-return-control-flow.slang.expected.txt b/tests/autodiff/bool-return-control-flow.slang.expected.txt
new file mode 100644
index 000000000..e2789a12c
--- /dev/null
+++ b/tests/autodiff/bool-return-control-flow.slang.expected.txt
@@ -0,0 +1,5 @@
+type: float
+-10.000000
+0.000000
+0.000000
+0.000000 \ No newline at end of file
diff --git a/tests/autodiff/reverse-control-flow-2.slang b/tests/autodiff/reverse-control-flow-2.slang
new file mode 100644
index 000000000..cde707b4d
--- /dev/null
+++ b/tests/autodiff/reverse-control-flow-2.slang
@@ -0,0 +1,75 @@
+//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;
+
+[BackwardDifferentiable]
+bool doWork(float x, out float y)
+{
+ bool retVal = false;
+ y = 0;
+ for (;;)
+ {
+ if (x == 0.0)
+ break;
+
+ bool exited = (x == 1.0);
+
+ y += x;
+
+ if (!exited)
+ {
+ if (x < 1.0)
+ {
+ float b = x * 2.0f;
+ y += b;
+ exited = true;
+ }
+ }
+ retVal = true;
+ break;
+ }
+ return retVal;
+}
+
+[BackwardDifferentiable]
+bool doWork2(float x, out float y)
+{
+ y = 0;
+
+ if (x == 0.0) return false;
+
+ [ForceUnroll]
+ for (int i = 0; i < 2; ++i)
+ {
+ if (x > 0.0)
+ {
+ y += x;
+
+ if (x == 1.0) break;
+
+ y += x;
+ }
+ else
+ {
+ y += x;
+ }
+ }
+ return true;
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
+{
+ {
+ var dpx = diffPair(0.5f, 1.0f);
+ __bwd_diff(doWork)(dpx, 1.0f);
+ outputBuffer[0] = dpx.d;
+ }
+ {
+ var dpx = diffPair(0.5f, 0.0f);
+ __bwd_diff(doWork2)(dpx, 1.0);
+ outputBuffer[1] = dpx.d;
+ }
+}
diff --git a/tests/autodiff/reverse-control-flow-2.slang.expected.txt b/tests/autodiff/reverse-control-flow-2.slang.expected.txt
new file mode 100644
index 000000000..31023ed32
--- /dev/null
+++ b/tests/autodiff/reverse-control-flow-2.slang.expected.txt
@@ -0,0 +1,6 @@
+type: float
+3.000000
+4.000000
+0.000000
+0.000000
+0.000000
diff --git a/tests/diagnostics/autodiff-data-flow-2.slang b/tests/diagnostics/autodiff-data-flow-2.slang
index aa923c5d6..3148c6a41 100644
--- a/tests/diagnostics/autodiff-data-flow-2.slang
+++ b/tests/diagnostics/autodiff-data-flow-2.slang
@@ -24,5 +24,11 @@ float h(float x)
float val = 0;
// no diagnostic by clarifying intention.
val = no_diff(f(x + 1));
+
+ // error: dynamic loop without [MaxIters] or [ForceUnroll]
+ for (int i = 0; i < (int)x; i++)
+ {
+ }
+
return val;
}
diff --git a/tests/diagnostics/autodiff-data-flow-2.slang.expected b/tests/diagnostics/autodiff-data-flow-2.slang.expected
index 9026c0748..725a27c9c 100644
--- a/tests/diagnostics/autodiff-data-flow-2.slang.expected
+++ b/tests/diagnostics/autodiff-data-flow-2.slang.expected
@@ -1,8 +1,11 @@
result code = -1
standard error = {
-tests/diagnostics/autodiff-data-flow-2.slang(18): error 41020: derivative cannot be propagated through call to non-backward-differentiable function `f`, use 'no_diff' to clarify intention.
+tests/diagnostics/autodiff-data-flow-2.slang(17): error 41020: derivative cannot be propagated through call to non-backward-differentiable function `f`, use 'no_diff' to clarify intention.
float val = f(x + 1); // Error: f must also be backward-differentiable
^
+tests/diagnostics/autodiff-data-flow-2.slang(29): error 30510: loops inside a differentiable function need to provide either '[MaxIters(n)]' or '[ForceUnroll]' attribute.
+ for (int i = 0; i < (int)x; i++)
+ ^~~
}
standard output = {
}
diff --git a/tests/diagnostics/autodiff.slang b/tests/diagnostics/autodiff.slang
index 935ef07cb..7905b48b6 100644
--- a/tests/diagnostics/autodiff.slang
+++ b/tests/diagnostics/autodiff.slang
@@ -12,11 +12,6 @@ float f(float x)
if (x > 5)
val = x + 1;
- // warning: dynamic loop without [MaxIters] or [ForceUnroll]
- for (int i = 0; i < (int)x; i++)
- {
- }
-
[MaxIters(2)]
for (int i = 0; i < (int)x; i++) // OK
{
diff --git a/tests/diagnostics/autodiff.slang.expected b/tests/diagnostics/autodiff.slang.expected
index 952503d1c..d075b9406 100644
--- a/tests/diagnostics/autodiff.slang.expected
+++ b/tests/diagnostics/autodiff.slang.expected
@@ -1,8 +1,5 @@
result code = -1
standard error = {
-tests/diagnostics/autodiff.slang(16): error 30510: loops inside a differentiable function need to provide either '[MaxIters(n)]' or '[ForceUnroll]' attribute.
- for (int i = 0; i < (int)x; i++)
- ^~~
tests/diagnostics/autodiff.slang(35): error 38031: 'no_diff' can only be used to decorate a call.
float x1 = no_diff x; // invalid use of no_diff here.
^~~~~~~