diff options
| -rw-r--r-- | source/slang/slang-ir-autodiff-fwd.cpp | 3 | ||||
| -rw-r--r-- | source/slang/slang-ir-autodiff-rev.cpp | 3 | ||||
| -rw-r--r-- | source/slang/slang-ir-autodiff.cpp | 13 | ||||
| -rw-r--r-- | source/slang/slang-ir-autodiff.h | 2 | ||||
| -rw-r--r-- | tests/autodiff/reverse-loop-higher-order-diff-only.slang | 72 | ||||
| -rw-r--r-- | tests/autodiff/reverse-loop-higher-order-diff-only.slang.expected.txt | 6 |
6 files changed, 99 insertions, 0 deletions
diff --git a/source/slang/slang-ir-autodiff-fwd.cpp b/source/slang/slang-ir-autodiff-fwd.cpp index e78217fe3..444816ff7 100644 --- a/source/slang/slang-ir-autodiff-fwd.cpp +++ b/source/slang/slang-ir-autodiff-fwd.cpp @@ -1508,6 +1508,9 @@ IRFunc* ForwardDiffTranscriber::transcribeFuncHeaderImpl(IRBuilder* inBuilder, I builder.addForwardDifferentiableDecoration(diffFunc); if (isBackwardDifferentiableFunc(origFunc)) builder.addBackwardDifferentiableDecoration(diffFunc); + + // Transfer checkpoint hint decorations + copyCheckpointHints(&builder, origFunc, diffFunc); // Find and clone `DifferentiableTypeDictionaryDecoration` to the new diffFunc. if (auto dictDecor = origFunc->findDecoration<IRDifferentiableTypeDictionaryDecoration>()) diff --git a/source/slang/slang-ir-autodiff-rev.cpp b/source/slang/slang-ir-autodiff-rev.cpp index ecc36d6ba..70c43cdcb 100644 --- a/source/slang/slang-ir-autodiff-rev.cpp +++ b/source/slang/slang-ir-autodiff-rev.cpp @@ -340,6 +340,9 @@ namespace Slang builder.addNameHintDecoration(diffFunc, newNameSb.getUnownedSlice()); } + // Transfer checkpoint hint decorations + copyCheckpointHints(&builder, origFunc, diffFunc); + // Mark the generated derivative function itself as differentiable. builder.addBackwardDifferentiableDecoration(diffFunc); // Find and clone `DifferentiableTypeDictionaryDecoration` to the new diffFunc. diff --git a/source/slang/slang-ir-autodiff.cpp b/source/slang/slang-ir-autodiff.cpp index 3b3224e2f..f6a977994 100644 --- a/source/slang/slang-ir-autodiff.cpp +++ b/source/slang/slang-ir-autodiff.cpp @@ -749,6 +749,19 @@ IRInst* DifferentiableTypeConformanceContext::getExtractExistensialTypeWitness(I SLANG_UNIMPLEMENTED_X("TODO: Implement"); } + +void copyCheckpointHints(IRBuilder* builder, IRGlobalValueWithCode* oldInst, IRGlobalValueWithCode* newInst) +{ + for (auto decor = oldInst->getFirstDecoration(); decor; decor = decor->getNextDecoration()) + { + if (auto chkHint = as<IRCheckpointHintDecoration>(decor)) + { + SLANG_ASSERT(chkHint->getOperandCount() == 0); + builder->addDecoration(newInst, chkHint->getOp()); + } + } +} + void stripDerivativeDecorations(IRInst* inst) { for (auto decor = inst->getFirstDecoration(); decor; ) diff --git a/source/slang/slang-ir-autodiff.h b/source/slang/slang-ir-autodiff.h index 91b45c5be..fdbf5c65e 100644 --- a/source/slang/slang-ir-autodiff.h +++ b/source/slang/slang-ir-autodiff.h @@ -316,6 +316,8 @@ bool finalizeAutoDiffPass(IRModule* module); // Utility methods +void copyCheckpointHints(IRBuilder* builder, IRGlobalValueWithCode* oldInst, IRGlobalValueWithCode* newInst); + void stripDerivativeDecorations(IRInst* inst); bool isBackwardDifferentiableFunc(IRInst* func); diff --git a/tests/autodiff/reverse-loop-higher-order-diff-only.slang b/tests/autodiff/reverse-loop-higher-order-diff-only.slang new file mode 100644 index 000000000..6fc659a66 --- /dev/null +++ b/tests/autodiff/reverse-loop-higher-order-diff-only.slang @@ -0,0 +1,72 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type +//TEST:SIMPLE(filecheck=CHECK): -target hlsl -profile cs_5_0 -entry computeMain -line-directive-mode none + +//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] +[PreferRecompute] +float compute(float x, float y) +{ + return x * y * y; +} + +[BackwardDifferentiable] +[ForceInline] +float infinitesimal(float x) +{ + return x - detach(x); +} + +// Test that computeLoop compiles to just return 0. +// CHECK: float computeLoop{{[_0-9]*}}(float y{{[_0-9]*}}) +// CHECK-NOT: for{{.*}} +// CHECK: return 0 + +[BackwardDifferentiable] +[PreferRecompute] +float computeLoop(float y) +{ + float w = 0; + + for (int i = 0; i < 8; i++) + { + w += compute(i, y); + } + + return w - detach(w); +} + +// Since computeLoop is recomputed, test_simple_loop should have nothing to store +// therefore we check that there is no intermediate context type generated for test_simple_loop. + +// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}test_simple_loop{{[a-zA-Z0-9_]*}} +[BackwardDifferentiable] +float test_simple_loop(float y) +{ + float x = __fwd_diff(computeLoop)(diffPair(y, 1.0)).d; + return y + x; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + { + dpfloat dpa = dpfloat(1.0, 0.0); + + __bwd_diff(test_simple_loop)(dpa, 1.0f); + outputBuffer[0] = dpa.d; // Expect: 57.0 + } + + { + dpfloat dpa = dpfloat(0.4, 0.0); + + __bwd_diff(test_simple_loop)(dpa, 0.5f); + outputBuffer[1] = dpa.d; // Expect: 28.5 + } + + outputBuffer[2] = computeLoop(1.0); +} diff --git a/tests/autodiff/reverse-loop-higher-order-diff-only.slang.expected.txt b/tests/autodiff/reverse-loop-higher-order-diff-only.slang.expected.txt new file mode 100644 index 000000000..749e65457 --- /dev/null +++ b/tests/autodiff/reverse-loop-higher-order-diff-only.slang.expected.txt @@ -0,0 +1,6 @@ +type: float +57.000000 +28.500000 +0.000000 +0.000000 +0.000000 |
