-
Notifications
You must be signed in to change notification settings - Fork 436
Make copysign function differentiable #7585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4a29684
Initial plan
Copilot 47457d3
Implement copysign forward and backward derivatives
Copilot 79f85b8
Fix copysign test format to use expected.txt file
Copilot e08ba19
Add wgsl support to copysign and fix y==0 derivative case
Copilot 06f45b5
Add wgsl support to copysign helper functions
Copilot 30eeb79
Fix copysign derivative to return 0 when either x or y is 0
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<float> outputBuffer; | ||
|
|
||
| typedef DifferentialPair<float> dpfloat; | ||
| typedef DifferentialPair<float2> dpfloat2; | ||
|
|
||
| [BackwardDifferentiable] | ||
| float diffCopysign(float x, float y) | ||
| { | ||
| return copysign(x, y); | ||
kaizhangNV marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| [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; | ||
kaizhangNV marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // 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]; | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
tests/autodiff-dstdlib/dstdlib-copysign.slang.expected.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.