diff options
| author | Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> | 2022-07-11 23:18:06 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-11 23:18:06 -0400 |
| commit | b513d0deef521318ad943d820dd37029075a33c4 (patch) | |
| tree | cc6dc625ae381e0461724c5b137e1a034b03e636 /tests | |
| parent | 9261c7a23ddf061fe9f5bfc3376f09f3c0513bff (diff) | |
Added support for differentiating calls to basic functions, as well as arithmetic on the float3 type (#2313)
* Added support for differentiating calls to basic functions, as well as arithmetic on the float3 type
* Added test expected result
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/autodiff/local-redecl-custom-jvp.slang (renamed from tests/autodiff/redecl-custom-jvp.slang) | 0 | ||||
| -rw-r--r-- | tests/autodiff/local-redecl-custom-jvp.slang.expected.txt (renamed from tests/autodiff/redecl-custom-jvp.slang.expected.txt) | 0 | ||||
| -rw-r--r-- | tests/autodiff/nested-jvp.slang | 54 | ||||
| -rw-r--r-- | tests/autodiff/nested-jvp.slang.expected.txt | 5 | ||||
| -rw-r--r-- | tests/autodiff/test-intrinsics.slang | 5 | ||||
| -rw-r--r-- | tests/autodiff/vector-arithmetic-jvp.slang | 30 | ||||
| -rw-r--r-- | tests/autodiff/vector-arithmetic-jvp.slang.expected.txt | 5 |
7 files changed, 99 insertions, 0 deletions
diff --git a/tests/autodiff/redecl-custom-jvp.slang b/tests/autodiff/local-redecl-custom-jvp.slang index 2bc7cd582..2bc7cd582 100644 --- a/tests/autodiff/redecl-custom-jvp.slang +++ b/tests/autodiff/local-redecl-custom-jvp.slang diff --git a/tests/autodiff/redecl-custom-jvp.slang.expected.txt b/tests/autodiff/local-redecl-custom-jvp.slang.expected.txt index 965f2cb48..965f2cb48 100644 --- a/tests/autodiff/redecl-custom-jvp.slang.expected.txt +++ b/tests/autodiff/local-redecl-custom-jvp.slang.expected.txt diff --git a/tests/autodiff/nested-jvp.slang b/tests/autodiff/nested-jvp.slang new file mode 100644 index 000000000..c37641c91 --- /dev/null +++ b/tests/autodiff/nested-jvp.slang @@ -0,0 +1,54 @@ +//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; + +[__custom_jvp(pow_jvp)] +float pow_(float x, float n) +{ + return pow<float>(x, n); +} + + +[__custom_jvp(max_jvp)] +float max_(float x, float y) +{ + return max<float>(x, y); +} + + +float pow_jvp(float x, float n, float dx, float dn) +{ + return dx * n * pow(x, n-1) + dn * pow(x, n) * log(x); +} + + +float max_jvp(float x, float y, float dx, float dy) +{ + return (x > y) ? dx : dy; +} + + +/* Fresnel Schlick example */ +__differentiate_jvp float3 fresnel(float3 f0, float3 f90, float cosTheta) +{ + return f0 + (f90 - f0) * pow_(max_(1 - cosTheta, 0.0), 5); +} + + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + float3 f0 = float3(0.2, 0.2, 0.2); + float3 f90 = float3(0.7, 0.7, 0.7); + float cosTheta = 0.5; + + float3 d_f0 = float3(0.1, 0.1, 0.1); + float3 d_f90 = float3(0.9, 0.9, 0.9); + float d_cosTheta = 1.0; + + outputBuffer[0] = __jvp(fresnel)(f0, f90, cosTheta, d_f0, d_f90, d_cosTheta).y; // Expect: -0.031250 + } +} diff --git a/tests/autodiff/nested-jvp.slang.expected.txt b/tests/autodiff/nested-jvp.slang.expected.txt new file mode 100644 index 000000000..107153351 --- /dev/null +++ b/tests/autodiff/nested-jvp.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +-0.031250 +0.0 +0.0 +0.0
\ No newline at end of file diff --git a/tests/autodiff/test-intrinsics.slang b/tests/autodiff/test-intrinsics.slang index 3fa53e85a..189004543 100644 --- a/tests/autodiff/test-intrinsics.slang +++ b/tests/autodiff/test-intrinsics.slang @@ -4,3 +4,8 @@ float pow_(float x, float n) { return pow(x, n); } + +float max_(float x, float y) +{ + return max(x, y); +}
\ No newline at end of file diff --git a/tests/autodiff/vector-arithmetic-jvp.slang b/tests/autodiff/vector-arithmetic-jvp.slang new file mode 100644 index 000000000..5852b49a7 --- /dev/null +++ b/tests/autodiff/vector-arithmetic-jvp.slang @@ -0,0 +1,30 @@ +//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; + +__differentiate_jvp float3 f(float3 x) +{ + return x; +} + +__differentiate_jvp float3 g(float3 x, float3 y) +{ + float3 a = x + y; + float3 b = x - y; + return a * b + 2 * x * y; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + float3 a = float3(2.0, 2.0, 2.0); + float3 b = float3(1.5, 1.5, 1.5); + float3 da = float3(1.0, 1.0, 1.0); + + outputBuffer[0] = __jvp(f)(a, da).z; // Expect: 1 + outputBuffer[1] = __jvp(g)(a, b, da, float3(2.0, 1.0, 0.0)).y; // Expect: 8 + } +} diff --git a/tests/autodiff/vector-arithmetic-jvp.slang.expected.txt b/tests/autodiff/vector-arithmetic-jvp.slang.expected.txt new file mode 100644 index 000000000..adbb9a448 --- /dev/null +++ b/tests/autodiff/vector-arithmetic-jvp.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +1.0 +8.0 +0.0 +0.0
\ No newline at end of file |
