diff options
| author | Yong He <yonghe@outlook.com> | 2023-04-26 17:37:04 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-26 17:37:04 -0700 |
| commit | fc54adee1f7f0ba18591fc84ce5d51ac23afa954 (patch) | |
| tree | 4727ed6109ac50e95c49aadcebc0fb8b95495739 /tests/autodiff/dynamic-object-bwd-diff-2.slang | |
| parent | 61eb17b0b556ccc06f65f921bb0a4ea2784c4e20 (diff) | |
Autodiff support for dynamically dispatched generic method. (#2846)
* Autodiff support for dynamically dispatched generic method.
* Fix.
* Support dynamically dispatched generic type.
---------
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'tests/autodiff/dynamic-object-bwd-diff-2.slang')
| -rw-r--r-- | tests/autodiff/dynamic-object-bwd-diff-2.slang | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/autodiff/dynamic-object-bwd-diff-2.slang b/tests/autodiff/dynamic-object-bwd-diff-2.slang new file mode 100644 index 000000000..bb0a69b28 --- /dev/null +++ b/tests/autodiff/dynamic-object-bwd-diff-2.slang @@ -0,0 +1,61 @@ +// Test calling backward differentiable function through dynamic dispatch, where the interface +// being dispatched inherits from IDifferentiable, so that `this` is differentiable. + +//DISABLED_TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type +//DISABLED_TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +[anyValueSize(16)] +interface IInterface : IDifferentiable +{ + [BackwardDifferentiable] + float calc(float x); +} + +struct C : IInterface +{ + [BackwardDifferentiable] + float calc(float x) { return 2 * x; } +} + +struct A : IInterface +{ + float a; + [BackwardDifferentiable] + float calc(float x) + { + return a * x * x; + } +}; + + +[BackwardDifferentiable] +float run(int id, float x, no_diff float y) +{ + IInterface obj = createDynamicObject<IInterface>(id, y); + C c = {}; + return obj.calc(x); +} + +//TEST_INPUT: type_conformance A:IInterface = 0 +//TEST_INPUT: type_conformance C:IInterface = 1 + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + var p = diffPair(3.0); + + __bwd_diff(run)(0, p, 0.5, 1.0f); + outputBuffer[0] = p.d; // A.calc, expect 3 + } + + { + var p = diffPair(3.0); + + __bwd_diff(run)(1, p, 1.5, 1.0f); + outputBuffer[1] = p.d; // c.calc, expect 2 + } +} |
