From 68f80f0793cff96b1fc784059a76605888593cd8 Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Fri, 14 Jul 2023 15:47:06 -0400 Subject: Robustness fixes around reverse-mode differentiation of variables & inout parameters (#2985) * Add a new test case for checking loop in the reverse mode * Create duplicate var for primal value to avoid inconsistent inputs to backward call Fixes an issue with inout parameters where the backprop call may use the post-call value of the var instead of the pre-call value. * `IRStore`s transpose to `IRLoad` and an `IRStore(0)` to clear differential. Fixes some subtle issues around transposing * Simplify test * Delete out-edited.hlsl --------- Co-authored-by: Lifan Wu Co-authored-by: Yong He --- tests/autodiff/path-tracer/pt-loop.slang | 115 +++++++++++++++++++++ .../path-tracer/pt-loop.slang.expected.txt | 4 + 2 files changed, 119 insertions(+) create mode 100644 tests/autodiff/path-tracer/pt-loop.slang create mode 100644 tests/autodiff/path-tracer/pt-loop.slang.expected.txt (limited to 'tests') diff --git a/tests/autodiff/path-tracer/pt-loop.slang b/tests/autodiff/path-tracer/pt-loop.slang new file mode 100644 index 000000000..2e865b80a --- /dev/null +++ b/tests/autodiff/path-tracer/pt-loop.slang @@ -0,0 +1,115 @@ +//Tests automatic synthesis of Differential type requirement. + +//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], stride=4):out,name=outputBuffer +RWStructuredBuffer outputBuffer; + +struct PathData : IDifferentiable +{ + float3 thp; + uint length; + bool terminated; + bool isHit; + + [BackwardDifferentiable] + __init() + { + this.thp = float3(1.f); + this.length = 0; + this.terminated = false; + this.isHit = false; + } +} + +bool traceRayInline(uint length) +{ + if (length < 2) return true; + else return false; +} + +float3 getAlbedo(uint length) +{ + return float3(0.9f, 1.f, 1.f); +} + +float3 getAlbedoDerivative(uint length) +{ + return float3(1.f, 0.f, 0.f); +} + +[ForwardDerivativeOf(getAlbedo)] +[TreatAsDifferentiable] +DifferentialPair __fwd_d_getAlbedo(uint length) +{ + float3 primalValue = getAlbedo(length); + float3 derivativeValue = no_diff getAlbedoDerivative(length); + return DifferentialPair(primalValue, derivativeValue); +} + +[BackwardDerivativeOf(getAlbedo)] +[TreatAsDifferentiable] +void __bwd_d_getAlbedo(uint length, float3 dOut) +{ + outputBuffer[2] += dOut.x; +} + +[BackwardDifferentiable] +void handleHit(inout PathData pathData) +{ + if (pathData.length >= 2) + { + pathData.terminated = true; + return; + } + + float3 albedo = getAlbedo(pathData.length); + pathData.thp *= albedo; + pathData.length++; +} + +[BackwardDifferentiable] +[PreferRecompute] +float3 tracePath() +{ + PathData pathData = PathData(); + + if (traceRayInline(pathData.length)) + { + pathData.isHit = true; + } + else + { + pathData.terminated = true; + pathData.isHit = false; + } + + [MaxIters(4)] + while (!pathData.terminated) + { + if (pathData.isHit) + { + handleHit(pathData); + + //pathData.isHit = traceRayInline(pathData.length); + if (!traceRayInline(pathData.length)) pathData.isHit = false; + else pathData.isHit = true; + } + else + { + pathData.terminated = true; + } + } + return pathData.thp; +} + +[numthreads(1, 1, 1)] +void computeMain(uint3 dispathThreadID: SV_DispatchThreadID) +{ + DifferentialPair dpThp = __fwd_diff(tracePath)(); + outputBuffer[0] = dpThp.p.x; + outputBuffer[1] = dpThp.d.x; + + __bwd_diff(tracePath)(float3(1.f, 0.f, 0.f)); +} diff --git a/tests/autodiff/path-tracer/pt-loop.slang.expected.txt b/tests/autodiff/path-tracer/pt-loop.slang.expected.txt new file mode 100644 index 000000000..5731f4ce2 --- /dev/null +++ b/tests/autodiff/path-tracer/pt-loop.slang.expected.txt @@ -0,0 +1,4 @@ +type: float +0.81 +1.8 +1.8 \ No newline at end of file -- cgit v1.2.3