From 5d0fe48c01e84f2f6247145ea92da9a63ce03734 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Wed, 5 Apr 2023 14:08:04 -0400 Subject: Register `IDifferentiable` witnesses for the differential associated types of registered types. (#2777) * Create high-order-forward-diff-struct.slang * Add a small pass to register differentials for differentials of registered types * Remove type reg pass and apply logic in differentiable type loading instead * Update slang-ir-autodiff.cpp * Fix comments. --------- Co-authored-by: Yong He --- .../autodiff/high-order-forward-diff-struct.slang | 50 ++++++++++++++++++++++ ...gh-order-forward-diff-struct.slang.expected.txt | 5 +++ 2 files changed, 55 insertions(+) create mode 100644 tests/autodiff/high-order-forward-diff-struct.slang create mode 100644 tests/autodiff/high-order-forward-diff-struct.slang.expected.txt (limited to 'tests') diff --git a/tests/autodiff/high-order-forward-diff-struct.slang b/tests/autodiff/high-order-forward-diff-struct.slang new file mode 100644 index 000000000..7e39640a1 --- /dev/null +++ b/tests/autodiff/high-order-forward-diff-struct.slang @@ -0,0 +1,50 @@ +//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; + +struct MyStruct : IDifferentiable +{ + float x; + uint i; + + [ForwardDifferentiable] + __init(float x, uint i) + { + this.x = x; + this.i = i; + } +}; + +[ForwardDifferentiable] +float mySqr(MyStruct s) +{ + if (s.i >= 1) + return s.x * s.x; + else + return 0.f; +} + +[ForwardDifferentiable] +float f(float x) +{ + MyStruct ms = MyStruct(x * x, 1); + return mySqr(ms); +} + +[ForwardDifferentiable] +float df(float x) +{ + return __fwd_diff(f)(DifferentialPair(x, 1.0)).d; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + // Given f(x) = x^4, + // f''(x) = 12 * x^2 + // Expect f''(4) = 192 + outputBuffer[0] = __fwd_diff(df)(DifferentialPair(4.0, 1.0)).d; +} + diff --git a/tests/autodiff/high-order-forward-diff-struct.slang.expected.txt b/tests/autodiff/high-order-forward-diff-struct.slang.expected.txt new file mode 100644 index 000000000..0f08247f0 --- /dev/null +++ b/tests/autodiff/high-order-forward-diff-struct.slang.expected.txt @@ -0,0 +1,5 @@ +type: float +192.000000 +0.000000 +0.000000 +0.000000 -- cgit v1.2.3