diff options
| author | Yong He <yonghe@outlook.com> | 2023-03-01 13:19:33 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-01 13:19:33 -0800 |
| commit | 6f31eae79d5b4297d0099c5779a9806a786cf9f8 (patch) | |
| tree | c2d6360994ee5730accab6236ed351ba682153a8 /tests | |
| parent | 6c26aa1f7e3e28e3053dffe686baa8e0499c624d (diff) | |
Implement derivatives for HLSL intrinsics. (#2684)
* Implement derivatives for HLSL intrinsics.
* Vector intrinsics.
* Add all intrinsics.
---------
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/autodiff-dstdlib/vector-cross.slang | 40 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/vector-cross.slang.expected.txt | 13 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/vector-length.slang | 36 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/vector-length.slang.expected.txt | 4 |
4 files changed, 93 insertions, 0 deletions
diff --git a/tests/autodiff-dstdlib/vector-cross.slang b/tests/autodiff-dstdlib/vector-cross.slang new file mode 100644 index 000000000..be08894cb --- /dev/null +++ b/tests/autodiff-dstdlib/vector-cross.slang @@ -0,0 +1,40 @@ +//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 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +[BackwardDifferentiable] +float3 crossImpl(float3 x, float3 y) +{ + return float3(x.y * y.z - y.y * x.z, + x.z * y.x - y.z * x.x, + x.x * y.y - y.x * x.y); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + let x = float3(-0.51, 0.74, 0.86); + let y = float3(1.43, -0.92, 4.36); + let dOut = float3(3.41, 6.55, 2.39); + var dpx = diffPair(x); + var dpy = diffPair(y); + __bwd_diff(cross)(dpx, dpy, dOut); + outputBuffer[0] = dpx.d[0]; + outputBuffer[1] = dpx.d[1]; + outputBuffer[2] = dpx.d[2]; + outputBuffer[3] = dpy.d[0]; + outputBuffer[4] = dpy.d[1]; + outputBuffer[5] = dpy.d[2]; + + __bwd_diff(crossImpl)(dpx, dpy, dOut); + outputBuffer[6] = dpx.d[0]; + outputBuffer[7] = dpx.d[1]; + outputBuffer[8] = dpx.d[2]; + outputBuffer[9] = dpy.d[0]; + outputBuffer[10] = dpy.d[1]; + outputBuffer[11] = dpy.d[2]; + } +} diff --git a/tests/autodiff-dstdlib/vector-cross.slang.expected.txt b/tests/autodiff-dstdlib/vector-cross.slang.expected.txt new file mode 100644 index 000000000..9d472f078 --- /dev/null +++ b/tests/autodiff-dstdlib/vector-cross.slang.expected.txt @@ -0,0 +1,13 @@ +type: float +-30.756802 +11.449901 +12.503700 +3.864400 +-4.151500 +5.863900 +-30.756804 +11.449901 +12.503700 +3.864400 +-4.151500 +5.863900 diff --git a/tests/autodiff-dstdlib/vector-length.slang b/tests/autodiff-dstdlib/vector-length.slang new file mode 100644 index 000000000..c5064e54e --- /dev/null +++ b/tests/autodiff-dstdlib/vector-length.slang @@ -0,0 +1,36 @@ +//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], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +float lengthDiffX(float3 x) +{ + return (length(float3(x.x + 0.001, x.yz)) - length(float3(x.x - 0.001, x.yz))) / 0.002; +} +float lengthDiffY(float3 x) +{ + return (length(float3(x.x, x.y + 0.001, x.z)) - length(float3(x.x, x.y - 0.001, x.z))) / 0.002; +} +float lengthDiffZ(float3 x) +{ + return (length(float3(x.xy, x.z + 0.001)) - length(float3(x.xy, x.z - 0.001))) / 0.002; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + let x = float3(12, 23, 31); + var dpx = diffPair(x); + __bwd_diff(length)(dpx, 1.0); + outputBuffer[0] = dpx.d[0]; + outputBuffer[1] = dpx.d[1]; + outputBuffer[2] = dpx.d[2]; + + // for reference: + //outputBuffer[3] = lengthDiffX(x); + //outputBuffer[4] = lengthDiffY(x); + //outputBuffer[5] = lengthDiffZ(x); + } +} diff --git a/tests/autodiff-dstdlib/vector-length.slang.expected.txt b/tests/autodiff-dstdlib/vector-length.slang.expected.txt new file mode 100644 index 000000000..3c3f3727d --- /dev/null +++ b/tests/autodiff-dstdlib/vector-length.slang.expected.txt @@ -0,0 +1,4 @@ +type: float +0.296862 +0.568986 +0.766895 |
