diff options
| author | Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> | 2023-04-05 14:08:04 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-05 11:08:04 -0700 |
| commit | 5d0fe48c01e84f2f6247145ea92da9a63ce03734 (patch) | |
| tree | d0cffe5181084192ea9d9840027f2b7f7933ef8f /tests | |
| parent | 492c6f2fd1a64c9d60a968b55bfd1000cc2ae8e7 (diff) | |
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 <yonghe@outlook.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/autodiff/high-order-forward-diff-struct.slang | 50 | ||||
| -rw-r--r-- | tests/autodiff/high-order-forward-diff-struct.slang.expected.txt | 5 |
2 files changed, 55 insertions, 0 deletions
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<float> 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<float>(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<float>(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 |
