From 4a49769c5b6b351b3c1c9a9968b3926839504606 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Mon, 8 Jul 2024 18:48:08 -0400 Subject: Treat global variables and parameters as non-differentiable when checking derivative data-flow (#4526) Global parameters are by-default not differentiable (even if they are of a differentiable type), because our auto-diff passes do not touch anything outside of function bodies. The solution is to use wrapper objects with differentiable getter/setter methods (and we should provide a few such objects in the stdlib). Fixes: #3289 This is a potentially breaking change: User code that was previously working with global variables of a differentiable type will now throw an error (previously the gradient would be dropped without warning). The solution is to use `detach()` to keep same behavior as before or rewrite the access using differentiable getter/setter methods. --- tests/autodiff/warn-on-shared-memory-access.slang | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/autodiff/warn-on-shared-memory-access.slang (limited to 'tests') diff --git a/tests/autodiff/warn-on-shared-memory-access.slang b/tests/autodiff/warn-on-shared-memory-access.slang new file mode 100644 index 000000000..bccf8b1fa --- /dev/null +++ b/tests/autodiff/warn-on-shared-memory-access.slang @@ -0,0 +1,32 @@ +//TEST:SIMPLE(filecheck=CHECK): -target hlsl -line-directive-mode none + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +groupshared float s_shared; + +[BackwardDifferentiable] +float get_thread_5_value(float v, uint group_thread_id) +{ + if(group_thread_id == 5) + { + // Using 'detach(v)' makes the error go away + s_shared = v; + // CHECK: tests/autodiff/warn-on-shared-memory-access.slang(14): error 41024: derivative is lost during assignment to non-differentiable location, use 'detach()' to clarify intention. + // CHECK: s_shared = v; + // CHECK: ^ + } + GroupMemoryBarrierWithGroupSync(); + return s_shared; +} + +[shader("compute")] +[numthreads(128, 1, 1)] +void computeMain(uint3 group_thread_id: SV_GroupThreadID, uint3 dispatch_thread_id: SV_DispatchThreadID) +{ + DifferentialPair value = diffPair(3.f, 0.f); + + bwd_diff(get_thread_5_value)(value, group_thread_id.x, 1.0f); + + outputBuffer[dispatch_thread_id.x] = value.d; +} \ No newline at end of file -- cgit v1.2.3