summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorSai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com>2023-05-06 03:03:25 -0400
committerGitHub <noreply@github.com>2023-05-06 00:03:25 -0700
commit271dc1b98d3887b6297c5407dc67692716687f4d (patch)
treea714a41f6a490000545e82cadd20561a020b0a1e /tests
parent0602eaaba32bdbaf3f99ab8987e97419cba395aa (diff)
Don't store loop induction values + fix minor issue (#2872)
Diffstat (limited to 'tests')
-rw-r--r--tests/autodiff/long-loop.slang36
-rw-r--r--tests/autodiff/long-loop.slang.expected.txt2
-rw-r--r--tests/autodiff/reverse-inout-param-4.slang49
-rw-r--r--tests/autodiff/reverse-inout-param-4.slang.expected.txt6
-rw-r--r--tests/autodiff/reverse-loop-diff-only-2.slang85
-rw-r--r--tests/autodiff/reverse-loop-diff-only-2.slang.expected.txt6
6 files changed, 184 insertions, 0 deletions
diff --git a/tests/autodiff/long-loop.slang b/tests/autodiff/long-loop.slang
new file mode 100644
index 000000000..69652dbf0
--- /dev/null
+++ b/tests/autodiff/long-loop.slang
@@ -0,0 +1,36 @@
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
+//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
+//TEST:SIMPLE(filecheck=CHECK): -target hlsl -profile cs_5_0 -entry computeMain -line-directive-mode none
+
+//TEST_INPUT:ubuffer(data=[0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+[BackwardDifferentiable]
+float sin_series(float x, int iterations)
+{
+ float result = x;
+ float term = x;
+ [MaxIters(30)]
+ for (int i = 1; i < iterations; i++)
+ {
+ term *= -1.0f * x * x / ((2 * i) * (2 * i + 1));
+ result += term;
+ }
+ return result;
+}
+
+// Check that the intermediate context of sin_series does not have an array for `i`.
+
+// CHECK: struct s_bwd_sin_series_Intermediates
+// CHECK-NOT: int {{[A-Za-z0-9_]+}}[{{.*}}]
+// CHECK: }
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
+{
+ var x = diffPair(float.getPi(), 1.0);
+
+ __bwd_diff(sin_series)(x, 30, 1.0f);
+
+ outputBuffer[0] = x.d; // -1.0
+}
diff --git a/tests/autodiff/long-loop.slang.expected.txt b/tests/autodiff/long-loop.slang.expected.txt
new file mode 100644
index 000000000..77bdc5ea4
--- /dev/null
+++ b/tests/autodiff/long-loop.slang.expected.txt
@@ -0,0 +1,2 @@
+type: float
+-1.0
diff --git a/tests/autodiff/reverse-inout-param-4.slang b/tests/autodiff/reverse-inout-param-4.slang
new file mode 100644
index 000000000..5a15fdc94
--- /dev/null
+++ b/tests/autodiff/reverse-inout-param-4.slang
@@ -0,0 +1,49 @@
+//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):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -shaderobj
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+struct D : IDifferentiable
+{
+ float n;
+ float m;
+}
+
+[PreferRecompute]
+[BackwardDifferentiable]
+void g(no_diff float p, inout float x)
+{
+ x = p * ((x+1)*(x+1));
+}
+
+[PreferRecompute]
+[BackwardDifferentiable]
+void f(no_diff float p, inout float x)
+{
+ g(p, x);
+ g(p, x);
+}
+[BackwardDifferentiable]
+float f_ref(no_diff float p, float x)
+{
+ float y1 = p * (x+1)*(x+1);
+ float y2 = p * (y1+1)*(y1+1);
+ return y2;
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ var x = diffPair(2.0, 1.0);
+
+ __bwd_diff(f)(3.0, x);
+
+ outputBuffer[0] = x.p; // should be 2, since bwd_diff does not write back new primal val.
+ outputBuffer[1] = x.d; // 3024
+
+ var refVal = __fwd_diff(f_ref)(3.0, diffPair(2.0, 1.0)).d;
+ outputBuffer[2] = refVal; // 3024
+
+}
diff --git a/tests/autodiff/reverse-inout-param-4.slang.expected.txt b/tests/autodiff/reverse-inout-param-4.slang.expected.txt
new file mode 100644
index 000000000..2df174e2f
--- /dev/null
+++ b/tests/autodiff/reverse-inout-param-4.slang.expected.txt
@@ -0,0 +1,6 @@
+type: float
+2.000000
+3024.000000
+3024.000000
+0.000000
+0.000000
diff --git a/tests/autodiff/reverse-loop-diff-only-2.slang b/tests/autodiff/reverse-loop-diff-only-2.slang
new file mode 100644
index 000000000..aad405b48
--- /dev/null
+++ b/tests/autodiff/reverse-loop-diff-only-2.slang
@@ -0,0 +1,85 @@
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
+//TEST:SIMPLE(filecheck=CHECK): -target hlsl -profile cs_5_0 -entry computeMain -line-directive-mode none
+
+//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;
+
+// Test that compute does not have a context.
+// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}_compute_{{[a-zA-Z0-9_]*}}
+
+[BackwardDifferentiable]
+[PreferRecompute]
+float compute(float x, float y, out float k)
+{
+ k = y * 2;
+ return x * y;
+}
+
+[BackwardDifferentiable]
+[ForceInline]
+float infinitesimal(float x)
+{
+ return x - detach(x);
+}
+
+// Test that computeLoop compiles to just return 0.
+// CHECK: float computeLoop{{[_0-9]*}}(float y{{[_0-9]*}})
+// CHECK-NOT: for{{.*}}
+// CHECK: return 0
+
+// Test that computeLoop's intermediates have no float sitting
+// around (must not cache the outvar from 'compute()')
+// CHECK: struct s_bwd_computeLoop_Intermediates
+// CHECK-NEXT: {
+// CHECK-NOT: {{[A-Za-z0-9_]+}} {{[A-Za-z0-9_]+}}[{{.*}}]
+// CHECK: }
+
+[BackwardDifferentiable]
+[PreferRecompute]
+float computeLoop(float y)
+{
+ float w = 0;
+
+ for (int i = 0; i < 8; i++)
+ {
+ float k = float(0.f);
+ w += compute(i, y, k);
+ w += k * k;
+ }
+
+ return w - detach(w);
+}
+
+// Since computeLoop is recomputed, test_simple_loop should have nothing to store
+// therefore we check that there is no intermediate context type generated for test_simple_loop.
+
+// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}test_simple_loop{{[a-zA-Z0-9_]*}}
+[BackwardDifferentiable]
+float test_simple_loop(float y)
+{
+ float x = computeLoop(y);
+ return y + x;
+}
+
+[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: 29.0
+ }
+
+ {
+ dpfloat dpa = dpfloat(0.4, 0.0);
+
+ __bwd_diff(test_simple_loop)(dpa, 0.5f);
+ outputBuffer[1] = dpa.d; // Expect: 14.5
+ }
+
+ outputBuffer[2] = computeLoop(1.0);
+}
diff --git a/tests/autodiff/reverse-loop-diff-only-2.slang.expected.txt b/tests/autodiff/reverse-loop-diff-only-2.slang.expected.txt
new file mode 100644
index 000000000..fedac0520
--- /dev/null
+++ b/tests/autodiff/reverse-loop-diff-only-2.slang.expected.txt
@@ -0,0 +1,6 @@
+type: float
+93.000000
+27.300001
+0.000000
+0.000000
+0.000000