diff options
| author | winmad <winmad.wlf@gmail.com> | 2022-11-14 16:43:55 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-14 16:43:55 -0800 |
| commit | 25affe8e724fe4ee60a3b8ec2c494926930ba59f (patch) | |
| tree | 39d2d3d209a99152e80bf40c395002697d2c3338 /tests/autodiff-dstdlib | |
| parent | 368ec3116ea0f10f44acbf76b5dc9e34d6ff3d32 (diff) | |
Adding some math functions and their derivatives (#2497)
Diffstat (limited to 'tests/autodiff-dstdlib')
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-abs.slang | 43 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-abs.slang.expected.txt | 9 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-detach.slang | 43 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-detach.slang.expected.txt | 9 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-max.slang | 42 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-max.slang.expected.txt | 7 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-pow.slang | 43 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-pow.slang.expected.txt | 7 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-sqrt.slang | 40 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-sqrt.slang.expected.txt | 7 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-unary.slang | 40 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-unary.slang.expected.txt | 7 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-vector.slang | 26 | ||||
| -rw-r--r-- | tests/autodiff-dstdlib/dstdlib-vector.slang.expected.txt | 7 |
14 files changed, 330 insertions, 0 deletions
diff --git a/tests/autodiff-dstdlib/dstdlib-abs.slang b/tests/autodiff-dstdlib/dstdlib-abs.slang new file mode 100644 index 000000000..0da2de4c7 --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-abs.slang @@ -0,0 +1,43 @@ +//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], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; +typedef DifferentialPair<float2> dpfloat2; +typedef DifferentialPair<float3> dpfloat3; + +[ForwardDifferentiable] +float diffAbs(float x) +{ + return abs(x); +} + +[ForwardDifferentiable] +float3 diffAbs3(float3 x) +{ + return abs(x); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + { + dpfloat dpx = dpfloat(-3.0, 2.0); + dpfloat res = __fwd_diff(diffAbs)(dpx); + outputBuffer[0] = res.p; // Expect: 3.000000 + outputBuffer[1] = res.d; // Expect: -2.000000 + } + + { + dpfloat3 dpx = dpfloat3(float3(2.f, -3.f, -1.f), float3(-2.f, 3.f, -1.f)); + dpfloat3 res = __fwd_diff(diffAbs3)(dpx); + outputBuffer[2] = res.p[0]; // Expect: 2.000000 + outputBuffer[3] = res.d[0]; // Expect: -2.000000 + outputBuffer[4] = res.p[1]; // Expect: 3.000000 + outputBuffer[5] = res.d[1]; // Expect: -3.000000 + outputBuffer[6] = res.p[2]; // Expect: 1.000000 + outputBuffer[7] = res.d[2]; // Expect: 1.000000 + } +} diff --git a/tests/autodiff-dstdlib/dstdlib-abs.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-abs.slang.expected.txt new file mode 100644 index 000000000..18dcc32fe --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-abs.slang.expected.txt @@ -0,0 +1,9 @@ +type: float +3.000000 +-2.000000 +2.000000 +-2.000000 +3.000000 +-3.000000 +1.000000 +1.000000
\ No newline at end of file diff --git a/tests/autodiff-dstdlib/dstdlib-detach.slang b/tests/autodiff-dstdlib/dstdlib-detach.slang new file mode 100644 index 000000000..acb21a0ce --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-detach.slang @@ -0,0 +1,43 @@ +//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], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; +typedef DifferentialPair<float2> dpfloat2; +typedef DifferentialPair<float3> dpfloat3; + +[ForwardDifferentiable] +float diffDetach(float x) +{ + return detach(x); +} + +[ForwardDifferentiable] +float3 diffDetach3(float3 x) +{ + return detach(x); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + { + dpfloat dpx = dpfloat(-5.0, 1.0); + dpfloat res = __fwd_diff(diffDetach)(dpx); + outputBuffer[0] = res.p; // Expect: -5.000000 + outputBuffer[1] = res.d; // Expect: 0.000000 + } + + { + dpfloat3 dpx = dpfloat3(float3(2.f, -3.f, -1.f), float3(-2.f, 3.f, -1.f)); + dpfloat3 res = __fwd_diff(diffDetach3)(dpx); + outputBuffer[2] = res.p[0]; // Expect: 2.000000 + outputBuffer[3] = res.d[0]; // Expect: 0.000000 + outputBuffer[4] = res.p[1]; // Expect: -3.000000 + outputBuffer[5] = res.d[1]; // Expect: 0.000000 + outputBuffer[6] = res.p[2]; // Expect: -1.000000 + outputBuffer[7] = res.d[2]; // Expect: 0.000000 + } +} diff --git a/tests/autodiff-dstdlib/dstdlib-detach.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-detach.slang.expected.txt new file mode 100644 index 000000000..950affb9a --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-detach.slang.expected.txt @@ -0,0 +1,9 @@ +type: float +-5.000000 +0.000000 +2.000000 +0.000000 +-3.000000 +0.000000 +-1.000000 +0.000000
\ No newline at end of file diff --git a/tests/autodiff-dstdlib/dstdlib-max.slang b/tests/autodiff-dstdlib/dstdlib-max.slang new file mode 100644 index 000000000..d28b17e06 --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-max.slang @@ -0,0 +1,42 @@ +//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], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; +typedef DifferentialPair<float2> dpfloat2; + +[ForwardDifferentiable] +float diffMax(float x, float y) +{ + return max(x, y); +} + +[ForwardDifferentiable] +float2 diffMax2(float2 x, float2 y) +{ + return max(x, y); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + { + dpfloat dpx = dpfloat(2.0, 1.0); + dpfloat dpy = dpfloat(5.0, -2.0); + dpfloat res = __fwd_diff(diffMax)(dpx, dpy); + outputBuffer[0] = res.p; // Expect: 5.000000 + outputBuffer[1] = res.d; // Expect: -2.000000 + } + + { + dpfloat2 dpx = dpfloat2(float2(-3.0, 4.0), float2(-1.0, -1.0)); + dpfloat2 dpy = dpfloat2(float2(1.0, 2.0), float2(2.0, 2.0)); + dpfloat2 res = __fwd_diff(diffMax2)(dpx, dpy); + outputBuffer[2] = res.p[0]; // Expect: 1.000000 + outputBuffer[3] = res.d[0]; // Expect: 2.000000 + outputBuffer[4] = res.p[1]; // Expect: 4.000000 + outputBuffer[5] = res.d[1]; // Expect: -1.000000 + } +} diff --git a/tests/autodiff-dstdlib/dstdlib-max.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-max.slang.expected.txt new file mode 100644 index 000000000..cca137aea --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-max.slang.expected.txt @@ -0,0 +1,7 @@ +type: float +5.000000 +-2.000000 +1.000000 +2.000000 +4.000000 +-1.000000 diff --git a/tests/autodiff-dstdlib/dstdlib-pow.slang b/tests/autodiff-dstdlib/dstdlib-pow.slang new file mode 100644 index 000000000..66f919b03 --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-pow.slang @@ -0,0 +1,43 @@ +//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], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; +typedef DifferentialPair<float2> dpfloat2; +typedef DifferentialPair<float3> dpfloat3; + +[ForwardDifferentiable] +float diffPow(float x, float y) +{ + return pow(x, y); +} + +[ForwardDifferentiable] +float2 diffPow2(float2 x, float2 y) +{ + return pow(x, y); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + { + dpfloat dpx = dpfloat(5.0, 1.0); + dpfloat dpy = dpfloat(3.0, 0.0); + dpfloat res = __fwd_diff(diffPow)(dpx, dpy); + outputBuffer[0] = res.p; // Expect: 125.000000 + outputBuffer[1] = res.d; // Expect: 75.000000 + } + + { + dpfloat2 dpx = dpfloat2(float2(10.0, 3.0), float2(0.0, -2.0)); + dpfloat2 dpy = dpfloat2(float2(2.5, 4.0), float2(1.0, 1.0)); + dpfloat2 res = __fwd_diff(diffPow2)(dpx, dpy); + outputBuffer[2] = res.p[0]; // Expect: 316.227722 + outputBuffer[3] = res.d[0]; // Expect: 728.141235 + outputBuffer[4] = res.p[1]; // Expect: 81.000000 + outputBuffer[5] = res.d[1]; // Expect: -127.012398 + } +} diff --git a/tests/autodiff-dstdlib/dstdlib-pow.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-pow.slang.expected.txt new file mode 100644 index 000000000..8ac728ccf --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-pow.slang.expected.txt @@ -0,0 +1,7 @@ +type: float +125.000000 +75.000000 +316.227722 +728.141235 +81.000000 +-127.012398
\ No newline at end of file diff --git a/tests/autodiff-dstdlib/dstdlib-sqrt.slang b/tests/autodiff-dstdlib/dstdlib-sqrt.slang new file mode 100644 index 000000000..9ef69ea34 --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-sqrt.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], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; +typedef DifferentialPair<float2> dpfloat2; +typedef DifferentialPair<float3> dpfloat3; + +[ForwardDifferentiable] +float diffSqrt(float x) +{ + return sqrt(x); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + { + dpfloat dpx = dpfloat(2.0, 1.0); + dpfloat res = __fwd_diff(diffSqrt)(dpx); + outputBuffer[0] = res.p; // Expect: 1.414214 + outputBuffer[1] = res.d; // Expect: 0.353553 + } + + { + dpfloat dpx = dpfloat(100.0, -3.0); + dpfloat res = __fwd_diff(diffSqrt)(dpx); + outputBuffer[2] = res.p; // Expect: 10.000000 + outputBuffer[3] = res.d; // Expect: -0.150000 + } + + { + dpfloat dpx = dpfloat(0.0, 1.0); + dpfloat res = __fwd_diff(diffSqrt)(dpx); + outputBuffer[4] = res.p; // Expect: 0.000000 + outputBuffer[5] = res.d; // Expect: 0.000000 + } +} diff --git a/tests/autodiff-dstdlib/dstdlib-sqrt.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-sqrt.slang.expected.txt new file mode 100644 index 000000000..887fd13ca --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-sqrt.slang.expected.txt @@ -0,0 +1,7 @@ +type: float +1.414214 +0.353553 +10.000000 +-0.150000 +0.000000 +0.000000
\ No newline at end of file diff --git a/tests/autodiff-dstdlib/dstdlib-unary.slang b/tests/autodiff-dstdlib/dstdlib-unary.slang new file mode 100644 index 000000000..4d869996d --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-unary.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], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; + +[ForwardDifferentiable] +float f(float x) +{ + return exp(x); +} + +[ForwardDifferentiable] +float g(float x) +{ + return sin(x); +} + +[ForwardDifferentiable] +float h(float x) +{ + return cos(x); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + { + dpfloat dpa = dpfloat(2.0, 1.0); + + outputBuffer[0] = f(dpa.p); // Expect: 7.389056 + outputBuffer[1] = __fwd_diff(f)(dpa).d; // Expect: 7.389056 + outputBuffer[2] = g(dpa.p); // Expect: 0.909297 + outputBuffer[3] = __fwd_diff(g)(dpa).d; // Expect: -0.416146 + outputBuffer[4] = h(dpa.p); // Expect: -0.416146 + outputBuffer[5] = __fwd_diff(h)(dpa).d; // Expect: -0.909297 + } +} diff --git a/tests/autodiff-dstdlib/dstdlib-unary.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-unary.slang.expected.txt new file mode 100644 index 000000000..2e7338f5c --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-unary.slang.expected.txt @@ -0,0 +1,7 @@ +type: float +7.389056 +7.389056 +0.909297 +-0.416147 +-0.416147 +-0.909297 diff --git a/tests/autodiff-dstdlib/dstdlib-vector.slang b/tests/autodiff-dstdlib/dstdlib-vector.slang new file mode 100644 index 000000000..ba66f0756 --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-vector.slang @@ -0,0 +1,26 @@ +//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], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; + +[ForwardDifferentiable] +float f(float x) +{ + float3 vx = float3(x, 2*x, 3*x); + float3 vexpx = exp(vx); + return vexpx.x + vexpx.y + vexpx.z; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) +{ + { + dpfloat dpa = dpfloat(2.0, 1.0); + + outputBuffer[0] = f(dpa.p); // Expect: 465.415999 + outputBuffer[1] = __fwd_diff(f)(dpa).d; // Expect: 1326.871736 + } +}
\ No newline at end of file diff --git a/tests/autodiff-dstdlib/dstdlib-vector.slang.expected.txt b/tests/autodiff-dstdlib/dstdlib-vector.slang.expected.txt new file mode 100644 index 000000000..3ab3a05e5 --- /dev/null +++ b/tests/autodiff-dstdlib/dstdlib-vector.slang.expected.txt @@ -0,0 +1,7 @@ +type: float +465.415999 +1326.871736 +0.000000 +0.000000 +0.000000 +0.000000 |
