From 77af111867eb72f26b460c5925be47aa22c71556 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Thu, 30 Jun 2022 19:24:24 -0400 Subject: Added `[__custom_jvp(func)]` attribute, and modified the derivative pass to only process referenced functions. (#2309) * Added JVPTranscriber to handle differentiation of load, store, var, param and return instructions, as well as conversion of data and function types * Changed class names to be more in line with convention. Added correct type checking for __jvp() and verified that simple calls with only loads and stores are processed correctly * Added logic to differentiate basic arithmetic and literals inside IRConstruct and fixed the way parameters are differentiated * Replaced some SLANG_UNEXPECTED macro uses with diagnostics instead * Added work-list-based on-demand generation of derivative functions * Fixed up a couple of TODOs * Added attribute [__custom_jvp(f)] to assign a custom derivative function to a declaration * Added a test for CustomJVPAttribute on a redeclaration of an imported function * Moving arithmetic test to new folder * Moving arithmetic test to new folder (2) * Added missing test module * Fixed a minor note Co-authored-by: Yong He --- tests/autodiff/arithmetic-jvp.slang | 45 ++++++++++++++++++++++ tests/autodiff/arithmetic-jvp.slang.expected.txt | 5 +++ tests/autodiff/redecl-custom-jvp.slang | 28 ++++++++++++++ .../autodiff/redecl-custom-jvp.slang.expected.txt | 5 +++ tests/autodiff/test-intrinsics.slang | 6 +++ tests/ir/derivative-op-ir-test.slang | 39 ------------------- tests/ir/derivative-op-ir-test.slang.expected.txt | 5 --- 7 files changed, 89 insertions(+), 44 deletions(-) create mode 100644 tests/autodiff/arithmetic-jvp.slang create mode 100644 tests/autodiff/arithmetic-jvp.slang.expected.txt create mode 100644 tests/autodiff/redecl-custom-jvp.slang create mode 100644 tests/autodiff/redecl-custom-jvp.slang.expected.txt create mode 100644 tests/autodiff/test-intrinsics.slang delete mode 100644 tests/ir/derivative-op-ir-test.slang delete mode 100644 tests/ir/derivative-op-ir-test.slang.expected.txt (limited to 'tests') diff --git a/tests/autodiff/arithmetic-jvp.slang b/tests/autodiff/arithmetic-jvp.slang new file mode 100644 index 000000000..8632aa332 --- /dev/null +++ b/tests/autodiff/arithmetic-jvp.slang @@ -0,0 +1,45 @@ +//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 outputBuffer; + +__differentiate_jvp float f(float x) +{ + return x; +} + +float g_jvp_(float x, float dx) +{ + return 2 * dx; +} + +[__custom_jvp(g_jvp_)] +float g(float x) +{ + return x + x; +} + +__differentiate_jvp float h(float x, float y) +{ + float m = x + y; + float n = x - y; + return m * n + 2 * x * y; +} + + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + float a = 2.0; + float b = 1.5; + float da = 1.0; + float db = 1.0; + + outputBuffer[0] = __jvp(f)(a, da); // Expect: 1 + outputBuffer[1] = __jvp(f)(a, 0.0); // Expect: 0 + outputBuffer[2] = __jvp(g)(a, da); // Expect: 2 + outputBuffer[3] = __jvp(h)(a, b, da, db); // Expect: 8 + } +} diff --git a/tests/autodiff/arithmetic-jvp.slang.expected.txt b/tests/autodiff/arithmetic-jvp.slang.expected.txt new file mode 100644 index 000000000..0545c08a1 --- /dev/null +++ b/tests/autodiff/arithmetic-jvp.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +1.0 +0.0 +2.0 +8.0 \ No newline at end of file diff --git a/tests/autodiff/redecl-custom-jvp.slang b/tests/autodiff/redecl-custom-jvp.slang new file mode 100644 index 000000000..2bc7cd582 --- /dev/null +++ b/tests/autodiff/redecl-custom-jvp.slang @@ -0,0 +1,28 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +import test_intrinsics; + +float my_pow_jvp(float x, float n, float dx, float dn) +{ + return dx * n * pow(x, n-1) + dn * pow(x, n) * log(x); +} + +[__custom_jvp(my_pow_jvp)] +float _pow(float, float); + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + float a = 5.0; + float n = 2; + float da = 1.0; + float dn = 0; + + outputBuffer[0] = __jvp(_pow)(a, n, da, dn); // Expect: 10.0 + outputBuffer[1] = __jvp(_pow)(a, n, 0.0, 1.0); // Expect: 40.23595 + } +} diff --git a/tests/autodiff/redecl-custom-jvp.slang.expected.txt b/tests/autodiff/redecl-custom-jvp.slang.expected.txt new file mode 100644 index 000000000..965f2cb48 --- /dev/null +++ b/tests/autodiff/redecl-custom-jvp.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +10.0 +40.23595 +0.0 +0.0 \ No newline at end of file diff --git a/tests/autodiff/test-intrinsics.slang b/tests/autodiff/test-intrinsics.slang new file mode 100644 index 000000000..3fa53e85a --- /dev/null +++ b/tests/autodiff/test-intrinsics.slang @@ -0,0 +1,6 @@ +//TEST_IGNORE_FILE: + +float pow_(float x, float n) +{ + return pow(x, n); +} diff --git a/tests/ir/derivative-op-ir-test.slang b/tests/ir/derivative-op-ir-test.slang deleted file mode 100644 index 7addccdc2..000000000 --- a/tests/ir/derivative-op-ir-test.slang +++ /dev/null @@ -1,39 +0,0 @@ -//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 outputBuffer; - -__differentiate_jvp float f(float x) -{ - return x; -} - -__differentiate_jvp float g(float x) -{ - return x + x; -} - -__differentiate_jvp float h(float x, float y) -{ - float m = x + y; - float n = x - y; - return m * n + 2 * x * y; -} - - -[numthreads(1, 1, 1)] -void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) -{ - { - float a = 2.0; - float b = 1.5; - float da = 1.0; - float db = 1.0; - - outputBuffer[0] = __jvp(f)(a, da); // Expect: 1 - outputBuffer[1] = __jvp(f)(a, 0.0); // Expect: 0 - outputBuffer[2] = __jvp(g)(a, da); // Expect: 2 - outputBuffer[3] = __jvp(h)(a, b, da, db); // Expect: 8 - } -} diff --git a/tests/ir/derivative-op-ir-test.slang.expected.txt b/tests/ir/derivative-op-ir-test.slang.expected.txt deleted file mode 100644 index 0545c08a1..000000000 --- a/tests/ir/derivative-op-ir-test.slang.expected.txt +++ /dev/null @@ -1,5 +0,0 @@ -type: float -1.0 -0.0 -2.0 -8.0 \ No newline at end of file -- cgit v1.2.3