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 error estimation by removing _EERepl_ and _delta_ #773

Merged
merged 5 commits into from
Mar 12, 2024

Conversation

PetroZarytskyi
Copy link
Collaborator

@PetroZarytskyi PetroZarytskyi commented Feb 16, 2024

This PR simplifies error estimation by removing variables with prefixes _EERepl_ and _delta_. This became possible since we started reusing cloned forward pass variables in the reverse pass. Merging this PR will also unlock #758.
Fixes #757.

Copy link

codecov bot commented Feb 16, 2024

Codecov Report

Attention: Patch coverage is 92.64706% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 94.83%. Comparing base (e930a4a) to head (6d32ee5).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #773      +/-   ##
==========================================
- Coverage   94.89%   94.83%   -0.07%     
==========================================
  Files          49       49              
  Lines        7478     7333     -145     
==========================================
- Hits         7096     6954     -142     
+ Misses        382      379       -3     
Files Coverage Δ
include/clad/Differentiator/ErrorEstimator.h 100.00% <100.00%> (ø)
include/clad/Differentiator/EstimationModel.h 100.00% <ø> (ø)
...e/clad/Differentiator/MultiplexExternalRMVSource.h 100.00% <ø> (ø)
include/clad/Differentiator/VisitorBase.h 100.00% <100.00%> (ø)
lib/Differentiator/ErrorEstimator.cpp 99.02% <100.00%> (+0.52%) ⬆️
lib/Differentiator/EstimationModel.cpp 100.00% <ø> (ø)
lib/Differentiator/MultiplexExternalRMVSource.cpp 90.52% <100.00%> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 96.59% <100.00%> (+<0.01%) ⬆️
include/clad/Differentiator/ExternalRMVSource.h 25.64% <0.00%> (+1.95%) ⬆️
Files Coverage Δ
include/clad/Differentiator/ErrorEstimator.h 100.00% <100.00%> (ø)
include/clad/Differentiator/EstimationModel.h 100.00% <ø> (ø)
...e/clad/Differentiator/MultiplexExternalRMVSource.h 100.00% <ø> (ø)
include/clad/Differentiator/VisitorBase.h 100.00% <100.00%> (ø)
lib/Differentiator/ErrorEstimator.cpp 99.02% <100.00%> (+0.52%) ⬆️
lib/Differentiator/EstimationModel.cpp 100.00% <ø> (ø)
lib/Differentiator/MultiplexExternalRMVSource.cpp 90.52% <100.00%> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 96.59% <100.00%> (+<0.01%) ⬆️
include/clad/Differentiator/ExternalRMVSource.h 25.64% <0.00%> (+1.95%) ⬆️

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

lib/Differentiator/ErrorEstimator.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ErrorEstimator.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ErrorEstimator.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ErrorEstimator.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ErrorEstimator.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ErrorEstimator.cpp Outdated Show resolved Hide resolved
@PetroZarytskyi PetroZarytskyi force-pushed the remove-eerepl branch 2 times, most recently from 639fafb to 80052c5 Compare February 16, 2024 16:45
Copy link
Contributor

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

@vgvassilev
Copy link
Owner

@grimmmyshini, could you take a look?

@PetroZarytskyi PetroZarytskyi force-pushed the remove-eerepl branch 2 times, most recently from e1b3715 to dd15512 Compare February 16, 2024 17:55
Copy link
Contributor

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

1 similar comment
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! 👍"

//CHECK-NEXT: * _d_x;
//CHECK-NEXT: }
//CHECK-NEXT: * _d_y += _d_z;
//CHECK-NEXT: _delta_x += std::abs(* _d_x * _EERepl_x0 * {{.+}});
Copy link
Contributor

Choose a reason for hiding this comment

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

I am making a comment here, but this applies to all the other tests that have been changed similarly.

1.) You cannot get rid of the EERepl vars (at least to my understanding) so easily. In error estimation, we need to "replay" the value of the variable of interest as it was when we started. (Essentially, every variable is a unique instantiation and is never overwritten). For e.g. the following program:

x = x + 1;
x = y; 
y = 20 + x;

Essentially means this to the error estimator:

x_1 = x_0 + 1;
x_2 = y_0; 
y_1 = 20 + x_2;

Then, the final error estimation is essentially this:

final_error = dx_0 * x_0 + dx_1 * x_1 + dx_2 * x_2 + ...

So, you cannot simply replace the eerepl objects with 'x' because most functions will overwrite values multiple times.

2.) You cannot just get rid of the delta vars as you may require them while printing/logging. Sure, we can change this but it makes it significantly easier for us to identify errors through these names in the code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

1.) You cannot get rid of the EERepl vars...

Since 1.3, we started reusing the original variables (x and y) in the reverse sweep. We now restore their values in the reverse sweep. So at every point in the reverse sweep, x has the same value it had in the corresponding point in the forward sweep, not the last value it was equal to (like it was previously). This is why the error estimation statement has been moved to the top of the enclosing reverse-sweep block (because we want the value of x after the assignment). Hopefully, I understand your concern correctly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

2.) You cannot just get rid of the delta vars...

Can't we instead rely on custom error evaluation here (getErrorVal)? I think we can get the same information this way without adding excessive code. Anyway, this seems like something we could discuss in more detail in Slack.

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Oh I see! In that case it's fine, you can ignore that comment.

  2. Yeah I mean it's probably okay to get rid of it if you see a worthwhile performance boost. I don't have a strong opinion against it.

@grimmmyshini
Copy link
Contributor

grimmmyshini commented Feb 28, 2024

I have left a comment in the code addressing one of my major concerns with this PR. That comment applies to every test that has been changed. This PR should also be run on the benchmarks we have for the paper. The resulting error values should also be cross-checked because, as this PR stands, I do not think it is correct and should not be merged.

Copy link
Contributor

github-actions bot commented Mar 5, 2024

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

1 similar comment
Copy link
Contributor

github-actions bot commented Mar 5, 2024

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

//CHECK-NEXT: * _d_x += _d_m * x;
//CHECK-NEXT: * _d_x += x * _d_m;
//CHECK-NEXT: _d_m = 0;
//CHECK-NEXT: m = clad::pop(_t1);
//CHECK-NEXT: float _r0 = clad::pop(_EERepl_m0);
//CHECK-NEXT: _delta_m += std::abs(_d_m * _r0 * {{.+}});
//CHECK-NEXT: }
Copy link
Collaborator Author

@PetroZarytskyi PetroZarytskyi Mar 5, 2024

Choose a reason for hiding this comment

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

We should notice that before this PR, we used _d_m after it was set to 0. This is clearly not intended because we should use the old derivative value. This happens every time we declare a variable inside a loop. On the other hand, moving the error statement to the top solves the problem.

@PetroZarytskyi
Copy link
Collaborator Author

PetroZarytskyi commented Mar 5, 2024

@grimmmyshini @vgvassilev
This screenshot shows the discrepancy in the benchmark results before (on the right) and after (on the left) this PR:
Screenshot 2024-03-05 at 23 37 25
During our meeting last Sunday, we concluded that the most problematic part is the beta variable.
This is the part of the derivative code that causes this discrepancy:
Before the PR:

{
    double _r57 = clad::pop(_t125);
    double _r58 = _d_beta / _r57;
     _d_rtrans += _r58;
    double _r59 = _d_beta * -clad::pop(_t126) / (_r57 * _r57);
     _d_oldrtrans += _r59;
     _d_beta = 0;              <-- _d_beta is set to 0
     double _r60 = clad::pop(_EERepl_beta0);
     _delta_beta += clad::getErrorVal(_d_beta, _r60, "beta");    <-- the error is 0
}

After the PR:

{
    _final_error += clad::getErrorVal(_d_beta, beta, "beta");  <-- the old value of _d_beta is used
    _d_rtrans += _d_beta / oldrtrans;
    double _r1 = _d_beta * -rtrans / (oldrtrans * oldrtrans);
    _d_oldrtrans += _r1;
    _d_beta = 0;
    beta = clad::pop(_t48);
}

From what I understand, the new behavior is correct here. You can also see my comment above describing the same in one of the tests. Luckily, this doesn't affect the conclusions drawn in the article.

Copy link
Contributor

github-actions bot commented Mar 6, 2024

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

@vgvassilev
Copy link
Owner

@grimmmyshini, ping.

@grimmmyshini
Copy link
Contributor

I see! Well, it's weird that we missed it in the first go but no issue. If you have verified all the error results from the tests, then this PR is ready to go. 👍

Copy link
Contributor

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

@vgvassilev
Copy link
Owner

@PetroZarytskyi, any chances to increase the testing coverage of this patch?

@PetroZarytskyi
Copy link
Collaborator Author

@vgvassilev

@PetroZarytskyi, any chances to increase the testing coverage of this patch?

I looked into the CodeDev report. At first, I was confused how it was possible that the coverage of every single test either increased or didn't change. Then I realized that I made ErrorEstimator.cpp way smaller and that decreased the average project coverage. This happened because the coverage of ErrorEstimator.cpp is higher than average. So I think making the coverage higher again would involve doing that for lines unrelated to the PR.

@vgvassilev
Copy link
Owner

@vgvassilev

@PetroZarytskyi, any chances to increase the testing coverage of this patch?

I looked into the CodeDev report. At first, I was confused how it was possible that the coverage of every single test either increased or didn't change. Then I realized that I made ErrorEstimator.cpp way smaller and that decreased the average project coverage. This happened because the coverage of ErrorEstimator.cpp is higher than average. So I think making the coverage higher again would involve doing that for lines unrelated to the PR.

That was my take as well. Thanks for the explanation. Let's move forward.

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.

Well done! Thank you! We can ignore the codecov as we reduced the code significantly and that's what makes codecov unhappy but makes everybody else happy...

@vgvassilev vgvassilev merged commit 55835f0 into vgvassilev:master Mar 12, 2024
81 of 83 checks passed
@PetroZarytskyi PetroZarytskyi deleted the remove-eerepl branch March 12, 2024 21:55
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.

Remove excessive stores from the error estimation mode
3 participants