-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support of cuda kernels as pullback functions #1114
Merged
vgvassilev
merged 11 commits into
vgvassilev:master
from
kchristin22:cuda-pullback-host-functions
Oct 29, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f70b8b5
Add support of kernel pullback functions
kchristin22 dd9ff38
Fix suggestion of clang-tidy about const KCE
kchristin22 0fa6572
Fix suggestion of clang-tidy about const KCE
kchristin22 cb37e89
Make cudaMemcpy a plus-assign op
kchristin22 176aeba
Fix suggestions and format
kchristin22 cc0fc39
Lose unnecessary sync calls to GPU in tests and add them in cudaMemcp…
kchristin22 be2c90f
Fix cuda tests
kchristin22 d63e035
Fix cuda tests 2
kchristin22 6a3f79a
Fix cuda tests 3
kchristin22 0d23492
Fix tests after master rebase
kchristin22 5f8b54b
Specify true args in BuildCallToCustomDerivativeOrNumericalDiff
kchristin22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -471,8 +471,9 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
if (m_ExternalSource) | ||
m_ExternalSource->ActAfterCreatingDerivedFnParams(params); | ||
|
||
// if the function is a global kernel, all its parameters reside in the | ||
// global memory of the GPU | ||
// if the function is a global kernel, all the adjoint parameters reside in | ||
// the global memory of the GPU. To facilitate the process, all the params | ||
// of the kernel are added to the set. | ||
if (m_DiffReq->hasAttr<clang::CUDAGlobalAttr>()) | ||
for (auto* param : params) | ||
m_CUDAGlobalArgs.emplace(param); | ||
|
@@ -631,7 +632,12 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
if (!m_DiffReq.CUDAGlobalArgsIndexes.empty()) | ||
for (auto index : m_DiffReq.CUDAGlobalArgsIndexes) | ||
m_CUDAGlobalArgs.emplace(m_Derivative->getParamDecl(index)); | ||
|
||
// if the function is a global kernel, all the adjoint parameters reside in | ||
// the global memory of the GPU. To facilitate the process, all the params | ||
// of the kernel are added to the set. | ||
else if (m_DiffReq->hasAttr<clang::CUDAGlobalAttr>()) | ||
for (auto* param : params) | ||
m_CUDAGlobalArgs.emplace(param); | ||
m_Derivative->setBody(nullptr); | ||
|
||
if (!m_DiffReq.DeclarationOnly) { | ||
|
@@ -1667,6 +1673,10 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
return StmtDiff(Clone(CE)); | ||
} | ||
|
||
Expr* CUDAExecConfig = nullptr; | ||
if (const auto* KCE = dyn_cast<CUDAKernelCallExpr>(CE)) | ||
CUDAExecConfig = Clone(KCE->getConfig()); | ||
|
||
// If the function is non_differentiable, return zero derivative. | ||
if (clad::utils::hasNonDifferentiableAttribute(CE)) { | ||
// Calling the function without computing derivatives | ||
|
@@ -1675,10 +1685,11 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
ClonedArgs.push_back(Clone(CE->getArg(i))); | ||
|
||
SourceLocation validLoc = clad::utils::GetValidSLoc(m_Sema); | ||
Expr* Call = m_Sema | ||
.ActOnCallExpr(getCurrentScope(), Clone(CE->getCallee()), | ||
validLoc, ClonedArgs, validLoc) | ||
.get(); | ||
Expr* Call = | ||
m_Sema | ||
.ActOnCallExpr(getCurrentScope(), Clone(CE->getCallee()), | ||
validLoc, ClonedArgs, validLoc, CUDAExecConfig) | ||
.get(); | ||
// Creating a zero derivative | ||
auto* zero = ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, | ||
/*val=*/0); | ||
|
@@ -1825,7 +1836,8 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
Expr* call = | ||
m_Sema | ||
.ActOnCallExpr(getCurrentScope(), Clone(CE->getCallee()), Loc, | ||
llvm::MutableArrayRef<Expr*>(CallArgs), Loc) | ||
llvm::MutableArrayRef<Expr*>(CallArgs), Loc, | ||
CUDAExecConfig) | ||
.get(); | ||
return call; | ||
} | ||
|
@@ -1940,7 +1952,9 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
OverloadedDerivedFn = | ||
m_Builder.BuildCallToCustomDerivativeOrNumericalDiff( | ||
customPushforward, pushforwardCallArgs, getCurrentScope(), | ||
const_cast<DeclContext*>(FD->getDeclContext())); | ||
const_cast<DeclContext*>(FD->getDeclContext()), | ||
/*forCustomDerv=*/true, /*namespaceShouldExist=*/true, | ||
CUDAExecConfig); | ||
if (OverloadedDerivedFn) | ||
asGrad = false; | ||
} | ||
|
@@ -2041,7 +2055,9 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
OverloadedDerivedFn = | ||
m_Builder.BuildCallToCustomDerivativeOrNumericalDiff( | ||
customPullback, pullbackCallArgs, getCurrentScope(), | ||
const_cast<DeclContext*>(FD->getDeclContext())); | ||
const_cast<DeclContext*>(FD->getDeclContext()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use const_cast [cppcoreguidelines-pro-type-const-cast] const_cast<DeclContext*>(FD->getDeclContext()),
^ |
||
/*forCustomDerv=*/true, /*namespaceShouldExist=*/true, | ||
CUDAExecConfig); | ||
if (baseDiff.getExpr()) | ||
pullbackCallArgs.erase(pullbackCallArgs.begin()); | ||
} | ||
|
@@ -2057,10 +2073,11 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
CXXScopeSpec(), m_Derivative->getNameInfo(), m_Derivative) | ||
.get(); | ||
|
||
OverloadedDerivedFn = m_Sema | ||
.ActOnCallExpr(getCurrentScope(), selfRef, | ||
Loc, pullbackCallArgs, Loc) | ||
.get(); | ||
OverloadedDerivedFn = | ||
m_Sema | ||
.ActOnCallExpr(getCurrentScope(), selfRef, Loc, | ||
pullbackCallArgs, Loc, CUDAExecConfig) | ||
.get(); | ||
} else { | ||
if (m_ExternalSource) | ||
m_ExternalSource->ActBeforeDifferentiatingCallExpr( | ||
|
@@ -2112,14 +2129,14 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
OverloadedDerivedFn = GetSingleArgCentralDiffCall( | ||
Clone(CE->getCallee()), DerivedCallArgs[0], | ||
/*targetPos=*/0, | ||
/*numArgs=*/1, DerivedCallArgs); | ||
/*numArgs=*/1, DerivedCallArgs, CUDAExecConfig); | ||
asGrad = !OverloadedDerivedFn; | ||
} else { | ||
auto CEType = getNonConstType(CE->getType(), m_Context, m_Sema); | ||
OverloadedDerivedFn = GetMultiArgCentralDiffCall( | ||
Clone(CE->getCallee()), CEType.getCanonicalType(), | ||
CE->getNumArgs(), dfdx(), PreCallStmts, PostCallStmts, | ||
DerivedCallArgs, CallArgDx); | ||
DerivedCallArgs, CallArgDx, CUDAExecConfig); | ||
} | ||
CallExprDiffDiagnostics(FD, CE->getBeginLoc()); | ||
if (!OverloadedDerivedFn) { | ||
|
@@ -2137,7 +2154,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
OverloadedDerivedFn = | ||
m_Sema | ||
.ActOnCallExpr(getCurrentScope(), BuildDeclRef(pullbackFD), | ||
Loc, pullbackCallArgs, Loc) | ||
Loc, pullbackCallArgs, Loc, CUDAExecConfig) | ||
.get(); | ||
} | ||
} | ||
|
@@ -2250,7 +2267,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
call = m_Sema | ||
.ActOnCallExpr(getCurrentScope(), | ||
BuildDeclRef(calleeFnForwPassFD), Loc, | ||
CallArgs, Loc) | ||
CallArgs, Loc, CUDAExecConfig) | ||
.get(); | ||
} | ||
auto* callRes = StoreAndRef(call); | ||
|
@@ -2285,7 +2302,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
|
||
call = m_Sema | ||
.ActOnCallExpr(getCurrentScope(), Clone(CE->getCallee()), Loc, | ||
CallArgs, Loc) | ||
CallArgs, Loc, CUDAExecConfig) | ||
.get(); | ||
return StmtDiff(call); | ||
} | ||
|
@@ -2295,7 +2312,8 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
llvm::SmallVectorImpl<Stmt*>& PreCallStmts, | ||
llvm::SmallVectorImpl<Stmt*>& PostCallStmts, | ||
llvm::SmallVectorImpl<Expr*>& args, | ||
llvm::SmallVectorImpl<Expr*>& outputArgs) { | ||
llvm::SmallVectorImpl<Expr*>& outputArgs, | ||
Expr* CUDAExecConfig /*=nullptr*/) { | ||
int printErrorInf = m_Builder.shouldPrintNumDiffErrs(); | ||
llvm::SmallVector<Expr*, 16U> NumDiffArgs = {}; | ||
NumDiffArgs.push_back(targetFuncCall); | ||
|
@@ -2336,7 +2354,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context, | |
Name, NumDiffArgs, getCurrentScope(), | ||
/*OriginalFnDC=*/nullptr, | ||
/*forCustomDerv=*/false, | ||
/*namespaceShouldExist=*/false); | ||
/*namespaceShouldExist=*/false, CUDAExecConfig); | ||
} | ||
|
||
StmtDiff ReverseModeVisitor::VisitUnaryOperator(const UnaryOperator* UnOp) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: do not use const_cast [cppcoreguidelines-pro-type-const-cast]