diff options
| author | Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> | 2023-03-24 19:50:51 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-24 16:50:51 -0700 |
| commit | 7292edbd3eba3da7e8490ad19169a7d18283057a (patch) | |
| tree | b49fb1ba6a76d9775f788057d91b22b88b4fc19c /tests | |
| parent | e794de0d63e6de9be564c971fd40486ecf631293 (diff) | |
Added `[BackwardDifferentiable]` tags for intrinsic + builtin methods (#2732)
* Added higher-order differentiability decorators for built-ins + preliminary tests
* Update diff.meta.slang
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/autodiff/high-order-builtins-1.slang | 47 | ||||
| -rw-r--r-- | tests/autodiff/high-order-builtins-1.slang.expected.txt | 5 | ||||
| -rw-r--r-- | tests/autodiff/high-order-builtins-2.slang | 47 | ||||
| -rw-r--r-- | tests/autodiff/high-order-builtins-2.slang.expected.txt | 5 |
4 files changed, 104 insertions, 0 deletions
diff --git a/tests/autodiff/high-order-builtins-1.slang b/tests/autodiff/high-order-builtins-1.slang new file mode 100644 index 000000000..6b0c33ca6 --- /dev/null +++ b/tests/autodiff/high-order-builtins-1.slang @@ -0,0 +1,47 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -shaderobj +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +[BackwardDifferentiable] +float f(float x) +{ + return x * x; +} + +[BackwardDifferentiable] +float outerF(float x) +{ + return f(sin(x)); +} + +[BackwardDifferentiable] +float df(float x) +{ + return __fwd_diff(outerF)(DifferentialPair<float>(x, 1.0)).d; // 4*sin^3(x) +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + // Given f(x) = sin^2(x), + // f'(x) = 2*sin(x)*cos(x) + // f''(x) = 2*cos^2(x) - 2*sin^2(x) + // + + // Expect f''(4) = -0.291 + { + var p = diffPair(4.0, 0.0); + __bwd_diff(df)(p, 1.0); + outputBuffer[0] = p.d; + } + + // Expect f''(4) = -0.653643 + { + var p = diffPair(2.0, 0.0); + __bwd_diff(df)(p, 0.5); + outputBuffer[1] = p.d; + } +} diff --git a/tests/autodiff/high-order-builtins-1.slang.expected.txt b/tests/autodiff/high-order-builtins-1.slang.expected.txt new file mode 100644 index 000000000..4fa4ade6d --- /dev/null +++ b/tests/autodiff/high-order-builtins-1.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +-0.291000 +-0.653643 +0.000000 +0.000000 diff --git a/tests/autodiff/high-order-builtins-2.slang b/tests/autodiff/high-order-builtins-2.slang new file mode 100644 index 000000000..b15dee7c7 --- /dev/null +++ b/tests/autodiff/high-order-builtins-2.slang @@ -0,0 +1,47 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -shaderobj +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +[BackwardDifferentiable] +float f(float x) +{ + return sin(x) + x * x; +} + +[BackwardDifferentiable] +float outerF(float x) +{ + return f(pow(x, 3)); +} + +[BackwardDifferentiable] +float df(float x) +{ + return __fwd_diff(outerF)(DifferentialPair<float>(x, 1.0)).d; // x^3 + x^6 +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + // Given f(x) = sin(x^3) + x^6 + // f'(x) = cos(x^3)*(3*x^2) + 6*x^5 + // f''(x) = 6*cos(x^3)*x - 9*sin(x^3)*(x^4) + 30*x^4 + // + + // Expect f''(4) = -0.291 + { + var p = diffPair(1.0, 0.0); + __bwd_diff(df)(p, 1.0); + outputBuffer[0] = p.d; + } + + // Expect f''(4) = -0.653643 + { + var p = diffPair(2.0, 0.0); + __bwd_diff(df)(p, 0.5); + outputBuffer[1] = p.d; + } +} diff --git a/tests/autodiff/high-order-builtins-2.slang.expected.txt b/tests/autodiff/high-order-builtins-2.slang.expected.txt new file mode 100644 index 000000000..9f5d74b29 --- /dev/null +++ b/tests/autodiff/high-order-builtins-2.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +25.668574 +167.893206 +0.000000 +0.000000 |
