Skip to content

Commit 551bbb5

Browse files
saipraveenb25csyongheexpipiplus1
authored
Add checking for differentiability of the primal substitute function. (shader-slang#6277)
Co-authored-by: Yong He <yonghe@outlook.com> Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
1 parent 0b4e463 commit 551bbb5

5 files changed

+118
-0
lines changed

source/slang/slang-check-decl.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -12114,6 +12114,27 @@ static void checkDerivativeAttribute(
1211412114
imaginaryArguments.directions,
1211512115
imaginaryArguments.thisArg,
1211612116
imaginaryArguments.thisArgDirection);
12117+
12118+
// For primal-substitute we'd also want to make sure that the differentiability
12119+
// level of the target is as high as the funcDecl itself
12120+
//
12121+
if (auto declRefExpr = as<DeclRefExpr>(attr->funcExpr))
12122+
{
12123+
if (auto declRef = declRefExpr->declRef)
12124+
{
12125+
auto targetDiffLevel = visitor->getShared()->getFuncDifferentiableLevel(
12126+
declRef.as<FunctionDeclBase>().getDecl());
12127+
auto currDiffLevel = visitor->getShared()->getFuncDifferentiableLevel(funcDecl);
12128+
if (targetDiffLevel < currDiffLevel)
12129+
{
12130+
visitor->getSink()->diagnose(
12131+
attr->loc,
12132+
Diagnostics::primalSubstituteTargetMustHaveHigherDifferentiabilityLevel,
12133+
declRefExpr->declRef.getDecl(),
12134+
funcDecl);
12135+
}
12136+
}
12137+
}
1211712138
}
1211812139

1211912140
static void checkCudaKernelAttribute(

source/slang/slang-diagnostic-defs.h

+6
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,12 @@ DIAGNOSTIC(
12081208
Error,
12091209
overloadedFuncUsedWithDerivativeOfAttributes,
12101210
"cannot resolve overloaded functions for derivative-of attributes.")
1211+
DIAGNOSTIC(
1212+
31158,
1213+
Error,
1214+
primalSubstituteTargetMustHaveHigherDifferentiabilityLevel,
1215+
"primal substitute function for differentiable method must also be differentiable. Use "
1216+
"[Differentiable] or [TreatAsDifferentiable] (for empty derivatives)")
12111217

12121218
DIAGNOSTIC(31200, Warning, deprecatedUsage, "$0 has been deprecated: $1")
12131219
DIAGNOSTIC(31201, Error, modifierNotAllowed, "modifier '$0' is not allowed here.")

source/slang/slang-ir-autodiff.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2471,6 +2471,7 @@ void stripAutoDiffDecorationsFromChildren(IRInst* parent)
24712471
case kIROp_BackwardDerivativePrimalDecoration:
24722472
case kIROp_BackwardDerivativePrimalContextDecoration:
24732473
case kIROp_BackwardDerivativePrimalReturnDecoration:
2474+
case kIROp_PrimalSubstituteDecoration:
24742475
case kIROp_AutoDiffOriginalValueDecoration:
24752476
case kIROp_UserDefinedBackwardDerivativeDecoration:
24762477
case kIROp_IntermediateContextFieldDifferentialTypeDecoration:
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -shaderobj -output-using-type -g0
2+
3+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBufferPrimal
4+
RWStructuredBuffer<float> outputBufferPrimal;
5+
6+
//TEST_INPUT:ubuffer(data=[1 2 3 4], stride=4):name=gradBuffer
7+
RWStructuredBuffer<float> gradBuffer;
8+
9+
struct BufferWithGrad
10+
{
11+
RWStructuredBuffer<float> primal;
12+
RWStructuredBuffer<float> grad;
13+
14+
[Differentiable]
15+
void add(float value) { primal[0] = primal[0] + detach(value); }
16+
17+
[PrimalSubstituteOf(add), Differentiable]
18+
void add_subst(float value)
19+
{
20+
}
21+
22+
[BackwardDerivativeOf(add)]
23+
void add_bwd(inout DifferentialPair<float> d)
24+
{
25+
d = diffPair(d.p, grad[0]);
26+
}
27+
}
28+
29+
[Differentiable]
30+
void diffCall(BufferWithGrad result)
31+
{
32+
result.add(1.0f);
33+
}
34+
35+
[shader("compute")]
36+
[numthreads(1, 1, 1)]
37+
void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
38+
{
39+
BufferWithGrad bg = {outputBufferPrimal, gradBuffer};
40+
diffCall(bg);
41+
bwd_diff(diffCall)(bg);
42+
43+
// CHECK: type: float
44+
// CHECK-NEXT: 1.0
45+
// CHECK-NEXT: 0.0
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -target spirv -entry computeMain -stage compute
2+
3+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBufferPrimal
4+
RWStructuredBuffer<float> outputBufferPrimal;
5+
6+
//TEST_INPUT:ubuffer(data=[1 2 3 4], stride=4):name=gradBuffer
7+
RWStructuredBuffer<float> gradBuffer;
8+
9+
struct BufferWithGrad
10+
{
11+
RWStructuredBuffer<float> primal;
12+
RWStructuredBuffer<float> grad;
13+
14+
[Differentiable]
15+
void add(float value) { primal[0] = primal[0] + detach(value); }
16+
17+
// check for diagnostic:
18+
// CHECK-DAG: ([[# @LINE+1]]): error 31158
19+
[PrimalSubstituteOf(add)]
20+
void add_subst(float value)
21+
{
22+
}
23+
24+
[BackwardDerivativeOf(add)]
25+
void add_bwd(inout DifferentialPair<float> d)
26+
{
27+
d = diffPair(d.p, grad[0]);
28+
}
29+
}
30+
31+
[Differentiable]
32+
void diffCall(BufferWithGrad result)
33+
{
34+
result.add(1.0f);
35+
}
36+
37+
[shader("compute")]
38+
[numthreads(1, 1, 1)]
39+
void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
40+
{
41+
BufferWithGrad bg = {outputBufferPrimal, gradBuffer};
42+
diffCall(bg);
43+
bwd_diff(diffCall)(bg);
44+
}

0 commit comments

Comments
 (0)