summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com>2023-09-20 15:22:51 -0400
committerGitHub <noreply@github.com>2023-09-20 12:22:51 -0700
commit25c79ada2c0fcc6c5ecb3e71ca073109adc1d7eb (patch)
tree47f244aef7b89bf7ebeddac8c63c936531fb0b49
parent73292d9f3a1c790f72802dfe4cce57a1353dece6 (diff)
Fix `atan2` stdlib derivative + add tests. (#3218)
* Fix atan2 stdlib derivative. Add tests for atan2 * Create dstdlib-atan2.slang.expected.txt * Update tests
-rw-r--r--source/slang/diff.meta.slang6
-rw-r--r--tests/autodiff-dstdlib/dstdlib-atan2.slang85
-rw-r--r--tests/autodiff-dstdlib/dstdlib-atan2.slang.expected.txt11
3 files changed, 99 insertions, 3 deletions
diff --git a/source/slang/diff.meta.slang b/source/slang/diff.meta.slang
index 495b6b989..75c57018c 100644
--- a/source/slang/diff.meta.slang
+++ b/source/slang/diff.meta.slang
@@ -1259,7 +1259,7 @@ __generic<T : __BuiltinFloatingPointType>
DifferentialPair<T> __d_atan2(DifferentialPair<T> dpy, DifferentialPair<T> dpx)
{
T.Differential dx = __mul_p_d(-dpy.p / (dpx.p * dpx.p + dpy.p * dpy.p), dpx.d);
- T.Differential dy = __mul_p_d(-dpx.p / (dpx.p * dpx.p + dpy.p * dpy.p), dpy.d);
+ T.Differential dy = __mul_p_d(dpx.p / (dpx.p * dpx.p + dpy.p * dpy.p), dpy.d);
return DifferentialPair<T>(
atan2(dpy.p, dpx.p),
T.dadd(dx, dy));
@@ -1271,8 +1271,8 @@ __generic<T : __BuiltinFloatingPointType>
[BackwardDerivativeOf(atan2)]
void __d_atan2(inout DifferentialPair<T> dpy, inout DifferentialPair<T> dpx, T.Differential dOut)
{
- dpx = diffPair(dpx.p, __mul_p_d(-dpy.p / (dpx.p * dpx.p + dpy.p * dpy.p), dpx.d));
- dpy = diffPair(dpy.p, __mul_p_d(-dpx.p / (dpx.p * dpx.p + dpy.p * dpy.p), dpy.d));
+ dpx = diffPair(dpx.p, __mul_p_d(-dpy.p / (dpx.p * dpx.p + dpy.p * dpy.p), dOut));
+ dpy = diffPair(dpy.p, __mul_p_d(dpx.p / (dpx.p * dpx.p + dpy.p * dpy.p), dOut));
}
VECTOR_MATRIX_BINARY_DIFF_IMPL(atan2)
diff --git a/tests/autodiff-dstdlib/dstdlib-atan2.slang b/tests/autodiff-dstdlib/dstdlib-atan2.slang
new file mode 100644
index 000000000..c15222336
--- /dev/null
+++ b/tests/autodiff-dstdlib/dstdlib-atan2.slang
@@ -0,0 +1,85 @@
+//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
+//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<float> outputBuffer;
+
+typedef DifferentialPair<float> dpfloat;
+typedef DifferentialPair<float2> dpfloat2;
+typedef DifferentialPair<float3> dpfloat3;
+
+[BackwardDifferentiable]
+float diffAtan2(float x, float y)
+{
+ return atan2(x, y);
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
+{
+ float eps = 1e-4;
+ {
+ dpfloat dpx = dpfloat(5.0, 1.0);
+ dpfloat dpy = dpfloat(3.0, 0.0);
+ dpfloat res = __fwd_diff(diffAtan2)(dpx, dpy);
+ outputBuffer[0] = res.d;
+ }
+
+ {
+ dpfloat dpx = dpfloat(5.0, 0.0);
+ dpfloat dpy = dpfloat(3.0, 1.0);
+ dpfloat res = __fwd_diff(diffAtan2)(dpx, dpy);
+ outputBuffer[1] = res.d;
+ }
+
+ // Test the other 3 quadrants
+ {
+ dpfloat dpx = dpfloat(-5.0, 1.0);
+ dpfloat dpy = dpfloat(3.0, 0.0);
+ dpfloat res = __fwd_diff(diffAtan2)(dpx, dpy);
+ outputBuffer[2] = res.d;
+ }
+
+ {
+ dpfloat dpx = dpfloat(-5.0, 0.0);
+ dpfloat dpy = dpfloat(3.0, 1.0);
+ dpfloat res = __fwd_diff(diffAtan2)(dpx, dpy);
+ outputBuffer[3] = res.d;
+ }
+
+ {
+ dpfloat dpx = dpfloat(-5.0, 1.0);
+ dpfloat dpy = dpfloat(-3.0, 0.0);
+ dpfloat res = __fwd_diff(diffAtan2)(dpx, dpy);
+ outputBuffer[4] = res.d;
+ }
+
+ {
+ dpfloat dpx = dpfloat(-5.0, 0.0);
+ dpfloat dpy = dpfloat(-3.0, 1.0);
+ dpfloat res = __fwd_diff(diffAtan2)(dpx, dpy);
+ outputBuffer[5] = res.d;
+ }
+
+ {
+ dpfloat dpx = dpfloat(5.0, 1.0);
+ dpfloat dpy = dpfloat(-3.0, 0.0);
+ dpfloat res = __fwd_diff(diffAtan2)(dpx, dpy);
+ outputBuffer[6] = res.d;
+ }
+
+ {
+ dpfloat dpx = dpfloat(5.0, 0.0);
+ dpfloat dpy = dpfloat(-3.0, 1.0);
+ dpfloat res = __fwd_diff(diffAtan2)(dpx, dpy);
+ outputBuffer[7] = res.d;
+ }
+
+ {
+ dpfloat dpx = diffPair(5.0);
+ dpfloat dpy = diffPair(3.0);
+ __bwd_diff(diffAtan2)(dpx, dpy, 1.0);
+ outputBuffer[8] = dpx.d; // Should be equal to outputBuffer[0]
+ outputBuffer[9] = dpy.d; // Should be equal to outputBuffer[2]
+ }
+}
diff --git a/tests/autodiff-dstdlib/dstdlib-atan2.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-atan2.slang.expected.txt
new file mode 100644
index 000000000..5adffdaef
--- /dev/null
+++ b/tests/autodiff-dstdlib/dstdlib-atan2.slang.expected.txt
@@ -0,0 +1,11 @@
+type: float
+0.088235
+-0.147059
+0.088235
+0.147059
+-0.088235
+0.147059
+-0.088235
+-0.147059
+0.088235
+-0.147059