From b282c88d9743fc9bb60ef27cfa5d9cf58cccd60b Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Thu, 3 Jul 2025 23:53:02 -0700 Subject: Make copysign function differentiable (#7585) * Initial plan * Implement copysign forward and backward derivatives Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Fix copysign test format to use expected.txt file Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Add wgsl support to copysign and fix y==0 derivative case Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Add wgsl support to copysign helper functions Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> * Fix copysign derivative to return 0 when either x or y is 0 Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: csyonghe <2652293+csyonghe@users.noreply.github.com> --- tests/autodiff-dstdlib/dstdlib-copysign.slang | 108 +++++++++++++++++++++ .../dstdlib-copysign.slang.expected.txt | 21 ++++ 2 files changed, 129 insertions(+) create mode 100644 tests/autodiff-dstdlib/dstdlib-copysign.slang create mode 100644 tests/autodiff-dstdlib/dstdlib-copysign.slang.expected.txt (limited to 'tests') diff --git a/tests/autodiff-dstdlib/dstdlib-copysign.slang b/tests/autodiff-dstdlib/dstdlib-copysign.slang new file mode 100644 index 000000000..30140c605 --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-copysign.slang @@ -0,0 +1,108 @@ +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE_EX:-wgpu -compute -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +typedef DifferentialPair dpfloat; +typedef DifferentialPair dpfloat2; + +[BackwardDifferentiable] +float diffCopysign(float x, float y) +{ + return copysign(x, y); +} + +[BackwardDifferentiable] +float2 diffCopysign(float2 x, float2 y) +{ + return copysign(x, y); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + // Test 1: forward diff copysign(3.0, -1.0) with dx=2.0, dy=1.0 + { + dpfloat dpx = dpfloat(3.0, 2.0); + dpfloat dpy = dpfloat(-1.0, 1.0); + dpfloat res = __fwd_diff(diffCopysign)(dpx, dpy); + outputBuffer[0] = res.p; + outputBuffer[1] = res.d; + } + + // Test 2: forward diff copysign(-2.0, 4.0) with dx=1.5, dy=-0.5 + { + dpfloat dpx = dpfloat(-2.0, 1.5); + dpfloat dpy = dpfloat(4.0, -0.5); + dpfloat res = __fwd_diff(diffCopysign)(dpx, dpy); + outputBuffer[2] = res.p; + outputBuffer[3] = res.d; + } + + // Test 3: forward diff copysign(0.0, -1.0) with dx=3.0, dy=2.0 + { + dpfloat dpx = dpfloat(0.0, 3.0); + dpfloat dpy = dpfloat(-1.0, 2.0); + dpfloat res = __fwd_diff(diffCopysign)(dpx, dpy); + outputBuffer[4] = res.p; + outputBuffer[5] = res.d; + } + + // Test 4: vector forward diff + { + dpfloat2 dpx = dpfloat2(float2(5.0, -3.0), float2(1.0, 2.0)); + dpfloat2 dpy = dpfloat2(float2(-2.0, 4.0), float2(0.5, -1.0)); + dpfloat2 res = __fwd_diff(diffCopysign)(dpx, dpy); + outputBuffer[6] = res.p[0]; + outputBuffer[7] = res.d[0]; + outputBuffer[8] = res.p[1]; + outputBuffer[9] = res.d[1]; + } + + // Test 5: backward diff copysign(4.0, -2.0) + { + dpfloat dpx = dpfloat(4.0, 0.0); + dpfloat dpy = dpfloat(-2.0, 0.0); + __bwd_diff(diffCopysign)(dpx, dpy, 1.0); + outputBuffer[10] = dpx.d; + outputBuffer[11] = dpy.d; + } + + // Test 6: backward diff copysign(-3.0, 5.0) + { + dpfloat dpx = dpfloat(-3.0, 0.0); + dpfloat dpy = dpfloat(5.0, 0.0); + __bwd_diff(diffCopysign)(dpx, dpy, 2.0); + outputBuffer[12] = dpx.d; + outputBuffer[13] = dpy.d; + } + + // Test 7: copysign with y=0 - derivative should be 0 + { + dpfloat dpx = dpfloat(3.0, 2.0); + dpfloat dpy = dpfloat(0.0, 1.0); + dpfloat res = __fwd_diff(diffCopysign)(dpx, dpy); + outputBuffer[14] = res.p; + outputBuffer[15] = res.d; + } + + // Test 8: copysign with x=0 - derivative should be 0 + { + dpfloat dpx = dpfloat(0.0, 2.0); + dpfloat dpy = dpfloat(-1.0, 1.0); + dpfloat res = __fwd_diff(diffCopysign)(dpx, dpy); + outputBuffer[16] = res.p; + outputBuffer[17] = res.d; + } + + // Test 9: vector backward diff + { + dpfloat2 dpx = dpfloat2(float2(2.0, -1.0), float2(0.0, 0.0)); + dpfloat2 dpy = dpfloat2(float2(-3.0, 4.0), float2(0.0, 0.0)); + __bwd_diff(diffCopysign)(dpx, dpy, float2(1.0, 3.0)); + outputBuffer[18] = dpx.d[0]; + outputBuffer[19] = dpx.d[1]; + } +} \ No newline at end of file diff --git a/tests/autodiff-dstdlib/dstdlib-copysign.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-copysign.slang.expected.txt new file mode 100644 index 000000000..71f3de9fd --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-copysign.slang.expected.txt @@ -0,0 +1,21 @@ +type: float +-3.000000 +-2.000000 +2.000000 +-1.500000 +0.000000 +0.000000 +-5.000000 +-1.000000 +3.000000 +-2.000000 +-1.000000 +0.000000 +-2.000000 +0.000000 +3.000000 +0.000000 +0.000000 +0.000000 +-1.000000 +-3.000000 \ No newline at end of file -- cgit v1.2.3