diff options
| author | Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> | 2024-07-08 18:48:08 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-08 18:48:08 -0400 |
| commit | 4a49769c5b6b351b3c1c9a9968b3926839504606 (patch) | |
| tree | ac9588f413dddc3a7c62cb24f36ab5fc51eb7d4a /tests | |
| parent | 2cb65a89f33ac1c4bad216e576069edbdc04933f (diff) | |
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.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/autodiff/warn-on-shared-memory-access.slang | 32 |
1 files changed, 32 insertions, 0 deletions
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<float> 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<float> 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 |
