Skip to content
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

Make built-in type variable declarations global #738

Merged
merged 1 commit into from
Feb 8, 2024

Conversation

PetroZarytskyi
Copy link
Collaborator

@PetroZarytskyi PetroZarytskyi commented Jan 26, 2024

When we produce a gradient function we generally have a forward and reverse
sweep. In the forward sweep we accumulate the state and in the reverse sweep
we use that state to invert the program execution. The forward sweep generally
follows the sematics of the primal function and when neccessary stores the state
which would be needed but lost for the reverse sweep.

However, to minimize the stores onto the tape we need to reuse some of the
variables between the forward and the reverse sweeps which requires some
variables to be promoted to the enclosing lexical scope of both sweeps.

Fixes #659, fixes #681.

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

Copy link

codecov bot commented Jan 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b58816f) 94.60% compared to head (d225ab6) 94.63%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #738      +/-   ##
==========================================
+ Coverage   94.60%   94.63%   +0.03%     
==========================================
  Files          49       49              
  Lines        7190     7201      +11     
==========================================
+ Hits         6802     6815      +13     
+ Misses        388      386       -2     
Files Coverage Δ
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 96.13% <100.00%> (+0.04%) ⬆️
lib/Differentiator/StmtClone.cpp 58.53% <100.00%> (+0.92%) ⬆️
lib/Differentiator/VisitorBase.cpp 98.10% <100.00%> (+0.04%) ⬆️
Files Coverage Δ
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 96.13% <100.00%> (+0.04%) ⬆️
lib/Differentiator/StmtClone.cpp 58.53% <100.00%> (+0.92%) ⬆️
lib/Differentiator/VisitorBase.cpp 98.10% <100.00%> (+0.04%) ⬆️

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@parth-07
Copy link
Collaborator

Why do we need both this pull request and #688?

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

include/clad/Differentiator/Array.h Show resolved Hide resolved
lib/Differentiator/StmtClone.cpp Outdated Show resolved Hide resolved
lib/Differentiator/VisitorBase.cpp Outdated Show resolved Hide resolved
lib/Differentiator/VisitorBase.cpp Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
Copy link
Owner

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to see moving! Here is a round of comments.

include/clad/Differentiator/Array.h Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
@@ -2530,19 +2516,26 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
VarDeclDiff ReverseModeVisitor::DifferentiateVarDecl(const VarDecl* VD) {
StmtDiff initDiff;
Expr* VDDerivedInit = nullptr;
// We take the parent of the current scope because the main compound
// statement of the function has its own scope as well.
bool isInFunctionGlobalScope = getCurrentScope()->getParent()==m_DerivativeFnScope;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool isInFunctionGlobalScope = getCurrentScope()->getParent()==m_DerivativeFnScope;
bool isFunctionScope = getCurrentScope()->isFunctionScope();

Any clue why that does not work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the outermost compound statement is not considered function scope.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the outermost compound statement FnScope so now this option works. I did this in my last commit.

@@ -2530,19 +2516,26 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
VarDeclDiff ReverseModeVisitor::DifferentiateVarDecl(const VarDecl* VD) {
StmtDiff initDiff;
Expr* VDDerivedInit = nullptr;
// We take the parent of the current scope because the main compound
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If getCurrentScope()->isFunctionScope() is false for int f() { this probably means that when we create the scope for the first compound statement of the function we should also add the Scope::FnScope flag to the current scope.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea. I did this in my last commit. Thank you for pointing this out.

@@ -2752,6 +2725,9 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
llvm::SmallVector<Decl*, 4> declsDiff;
// Need to put array decls inlined.
llvm::SmallVector<Decl*, 4> localDeclsDiff;
// We take the parent of the current scope because the main compound
// statement of the function has its own scope as well.
bool isInFunctionGlobalScope = getCurrentScope()->getParent()==m_DerivativeFnScope;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See past comments.

lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Show resolved Hide resolved
lib/Differentiator/StmtClone.cpp Outdated Show resolved Hide resolved
lib/Differentiator/VisitorBase.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

github-actions bot commented Feb 6, 2024

clang-tidy review says "All clean, LGTM! 👍"

3 similar comments
Copy link
Contributor

github-actions bot commented Feb 6, 2024

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

github-actions bot commented Feb 6, 2024

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

github-actions bot commented Feb 7, 2024

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

github-actions bot commented Feb 8, 2024

clang-tidy review says "All clean, LGTM! 👍"

1 similar comment
Copy link
Contributor

github-actions bot commented Feb 8, 2024

clang-tidy review says "All clean, LGTM! 👍"

When we produce a gradient function we generally have a forward and reverse
sweep. In the forward sweep we accumulate the state and in the reverse sweep
we use that state to invert the program execution. The forward sweep generally
follows the sematics of the primal function and when neccessary stores the state
which would be needed but lost for the reverse sweep.

However, to minimize the stores onto the tape we need to reuse some of the
variables between the forward and the reverse sweeps which requires some
variables to be promoted to the enclosing lexical scope of both sweeps.

Fixes vgvassilev#659, fixes vgvassilev#681.
Copy link
Contributor

github-actions bot commented Feb 8, 2024

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Owner

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Good job!

@vgvassilev vgvassilev merged commit e0de8e7 into vgvassilev:master Feb 8, 2024
81 checks passed
@PetroZarytskyi PetroZarytskyi deleted the global-vars branch February 14, 2024 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants