diff options
| author | Yong He <yonghe@outlook.com> | 2023-02-15 17:18:00 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-15 17:18:00 -0800 |
| commit | 86675155eb79532039ba79655ed14a76998e9f85 (patch) | |
| tree | e6749018fb853d0f1d228631dafeebf3fd4df4df | |
| parent | 266dc667485b38e16877cb9868873619e1b263e9 (diff) | |
Treat user defined backward derivative function as non differentiable. (#2650)
Co-authored-by: Yong He <yhe@nvidia.com>
| -rw-r--r-- | source/slang/slang-ir-check-differentiability.cpp | 5 | ||||
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 2 | ||||
| -rw-r--r-- | tests/autodiff/backward-diff-check.slang | 27 | ||||
| -rw-r--r-- | tests/autodiff/backward-diff-check.slang.expected.txt | 6 |
4 files changed, 33 insertions, 7 deletions
diff --git a/source/slang/slang-ir-check-differentiability.cpp b/source/slang/slang-ir-check-differentiability.cpp index e7d5a0e5c..a3f7ae7c0 100644 --- a/source/slang/slang-ir-check-differentiability.cpp +++ b/source/slang/slang-ir-check-differentiability.cpp @@ -165,7 +165,6 @@ public: HashSet<IRInst*> produceDiffSet; HashSet<IRInst*> expectDiffSet; - int differentiableInputs = 0; int differentiableOutputs = 0; for (auto param : funcInst->getFirstBlock()->getParams()) { @@ -173,8 +172,6 @@ public: { if (as<IROutTypeBase>(param->getFullType())) differentiableOutputs++; - if (!as<IROutType>(param->getFullType())) - differentiableInputs++; produceDiffSet.Add(param); } } @@ -186,8 +183,6 @@ public: if (differentiableOutputs == 0) sink->diagnose(funcInst, Diagnostics::differentiableFuncMustHaveOutput); - if (differentiableInputs == 0) - sink->diagnose(funcInst, Diagnostics::differentiableFuncMustHaveInput); DifferentiableLevel requiredDiffLevel = DifferentiableLevel::Forward; if (isBackwardDifferentiableFunc(funcInst)) diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index 9cf6aedf4..957f74d40 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -8436,8 +8436,6 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> else { originalSubBuilder->addUserDefinedBackwardDerivativeDecoration(originalFuncVal, derivativeFuncVal.val); - getBuilder()->addForwardDifferentiableDecoration(irFunc); - getBuilder()->addBackwardDifferentiableDecoration(irFunc); } } subContext->irBuilder->setInsertInto(irFunc); diff --git a/tests/autodiff/backward-diff-check.slang b/tests/autodiff/backward-diff-check.slang new file mode 100644 index 000000000..2718f31f1 --- /dev/null +++ b/tests/autodiff/backward-diff-check.slang @@ -0,0 +1,27 @@ +//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 0], stride=4):out,name=outputBuffer +RWStructuredBuffer<float> outputBuffer; + +typedef DifferentialPair<float> dpfloat; +typedef float.Differential dfloat; + +[BackwardDifferentiable] +float test() +{ + return 2.0f; +} + +float noDiffFunc(float x) { return x; } +[BackwardDerivativeOf(test)] +void d_test(float dOut) +{ + outputBuffer[0] = noDiffFunc(dOut); +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + __bwd_diff(test)(3.0f); +} diff --git a/tests/autodiff/backward-diff-check.slang.expected.txt b/tests/autodiff/backward-diff-check.slang.expected.txt new file mode 100644 index 000000000..72bbdbbcd --- /dev/null +++ b/tests/autodiff/backward-diff-check.slang.expected.txt @@ -0,0 +1,6 @@ +type: float +3.000000 +0.000000 +0.000000 +0.000000 +0.000000 |
