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

Simplify derivative statements in VisitBinaryOperator #690

Merged
merged 2 commits into from
Dec 29, 2023

Conversation

PetroZarytskyi
Copy link
Collaborator

Every time we visit multiplication or division in the reverse mode, we store expressions for the derivatives of the LHS and the RHS.

dr = StoreAndRef(dr, direction::reverse); 
dl = StoreAndRef(dl, direction::reverse); 

This makes the derivative code less readable and often produces unused variables. For instance, this code

x = 5 * y;

will be differentiated as

x = 5 * y;
...
double _r_d0 = _d_x;
double _r0 = _r_d0 * y;
double _r1 = 5 * _r_d0;
_d_y += _r1;
_d_x -= _r_d0;

while could be just

x = 5 * y;
...
double _r_d0 = _d_x;
_d_y += 5 * _r_d0;
_d_x -= _r_d0;

@PetroZarytskyi PetroZarytskyi changed the title Simplify derivative statements in VisitBinaryOperator. Simplify derivative statements in VisitBinaryOperator Dec 26, 2023
Copy link
Contributor

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

Copy link
Contributor

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

Copy link

codecov bot commented Dec 26, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6a42b46) 94.37% compared to head (63d9290) 94.46%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #690      +/-   ##
==========================================
+ Coverage   94.37%   94.46%   +0.08%     
==========================================
  Files          48       48              
  Lines        7062     7066       +4     
==========================================
+ Hits         6665     6675      +10     
+ Misses        397      391       -6     
Files Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 97.67% <ø> (ø)
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 96.03% <100.00%> (+0.31%) ⬆️
lib/Differentiator/VisitorBase.cpp 97.81% <100.00%> (ø)
Files Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 97.67% <ø> (ø)
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 96.03% <100.00%> (+0.31%) ⬆️
lib/Differentiator/VisitorBase.cpp 97.81% <100.00%> (ø)

Copy link
Contributor

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

Copy link
Contributor

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

1 similar comment
Copy link
Contributor

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

Ldiff = Visit(L, dl);
StmtDiff LStored = Ldiff;
if (utils::ContainsFunctionCalls(LStored.getExpr()) || LStored.getExpr()->HasSideEffects(m_Context))
Copy link
Owner

Choose a reason for hiding this comment

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

Can we move these checks into UsefulToStore? I believe there is even a fixme there.

Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor

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! Great work!

@vgvassilev vgvassilev merged commit 654faee into vgvassilev:master Dec 29, 2023
76 checks passed
@PetroZarytskyi PetroZarytskyi deleted the optimize-dfdx branch December 29, 2023 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants