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

Fix custom reverse_forws for operators #1076

Merged
merged 1 commit into from
Sep 3, 2024

Conversation

gojakuch
Copy link
Collaborator

@gojakuch gojakuch commented Sep 3, 2024

Previously, nullptr used to be set as the derivative of a call in the reverse mode, if there was a custom reverse_forw function available. This issue was overlooked at first, since it doesn't cause any trouble, unless someone decides to use nested operators (such as expressions of the form a[i] = x*x).

Fixes: #1070

Previously, nullptr used to be set as the derivative of a
call in the reverse mode, if there was a custom reverse_forw
function available. This issue was overlooked at first, since
it doesn't cause any trouble, unless someone decides to use nested
operators (such as expressions of the form `a[i] = x*x`).

Fixes: vgvassilev#1070
Copy link
Contributor

github-actions bot commented Sep 3, 2024

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

Copy link

codecov bot commented Sep 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.41%. Comparing base (c294ea5) to head (c2f0670).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1076   +/-   ##
=======================================
  Coverage   94.41%   94.41%           
=======================================
  Files          55       55           
  Lines        8410     8410           
=======================================
  Hits         7940     7940           
  Misses        470      470           
Files with missing lines Coverage Δ
lib/Differentiator/ReverseModeVisitor.cpp 97.96% <100.00%> (ø)
Files with missing lines Coverage Δ
lib/Differentiator/ReverseModeVisitor.cpp 97.96% <100.00%> (ø)

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.

Great catch! LGTM!

@vgvassilev vgvassilev merged commit fad4a69 into vgvassilev:master Sep 3, 2024
89 checks passed
// CHECK-NEXT: double _t6 = _t5.value;
// CHECK-NEXT: _t5.value = x * x;
// CHECK-NEXT: std::vector<double> _t7 = a;
// CHECK-NEXT: clad::ValueAndAdjoint<double &, double &> _t8 = {{.*}}operator_subscript_reverse_forw(&a, 1, &_d_a, _r1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nullptr was being passed to reverse_forws intentionally. We cannot pass adjoints uniformally for non-reference/non-pointer cases. Now, the generated code cannot be compiled because _r1 is being passed to the reverse_forw function call even though it is defined later in the reverse-pass.

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 know, I've opened an issue about this (#1066) this week and I think it was partially introduced in the original PR about custom reverse_forws for constructors, so I didn't really register this as an issue. it's still better this way because it at least has the correct logic now. but such misuses of yet-to-be-declared variables are happening all over in the reverse mode (at least that's what I thought), so I didn't pay that much attention to this. I'll add this as a note to #1066 or open a separate issue about this. thanks for your comment, cos I'd have forgotten to track this otherwise.

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's still better this way because it at least has the correct logic now.

I don't think it is the correct logic now. The reverse_forw call now refers to a variable that is defined much later, that does not seem like a correct logic to me.

but such misuses of yet-to-be-declared variables are happening all over in the reverse mode (at least that's what I thought)

Please correct me if I am wrong, I think @PetroZarytskyi made significant efforts to reduce such cases. We certainly should not knowingly add more such cases.

I'll add this as a note to #1066 or open a separate issue about this. thanks for your comment, cos I'd have forgotten to track this otherwise.

I think passing nullptr where adjoint is unavailable in the forward-pass is the right way to go. @vgvassilev What do you recommend here?

Copy link
Owner

Choose a reason for hiding this comment

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

I think I see both points. @gojakuch is unblocked by this change on the one hand and on the other it is problematic. Do we have an alternative way to fix his example cases where the nullptr approach breaks?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do we have an alternative way to fix his example cases where the nullptr approach breaks?
I opened #1079 yesterday to track this. I think there should be a way, I'll get back to this.

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.

Incorrect std::vector differentiation in the reverse mode
3 participants