From 25affe8e724fe4ee60a3b8ec2c494926930ba59f Mon Sep 17 00:00:00 2001 From: winmad Date: Mon, 14 Nov 2022 16:43:55 -0800 Subject: Adding some math functions and their derivatives (#2497) --- tests/autodiff-dstdlib/dstdlib-abs.slang | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/autodiff-dstdlib/dstdlib-abs.slang (limited to 'tests/autodiff-dstdlib/dstdlib-abs.slang') 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 outputBuffer; + +typedef DifferentialPair dpfloat; +typedef DifferentialPair dpfloat2; +typedef DifferentialPair 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 + } +} -- cgit v1.2.3