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

Delay the differentiation process until the end of TU. #766

Merged
merged 4 commits into from
Mar 14, 2024
Merged

Conversation

vgvassilev
Copy link
Owner

Before this patch clad attaches itself as a first consumer and applies AD before code generation. However, that is limited since clang sends every top-level declaration to codegen which limits the amount of flexibility clad has. For example, we have to instantiate all pending templates at every HandleTopLevelDecl calls; we cannot really differentiate virtual functions whose classes have sent their key function to CodeGen; and in general we perform actions which are semantically useful for the end of the translation unit.

This patch makes clad a single consumer of clang which dispatches to the others. That's done by delaying all calls to the consumers until the end of the TU where clad can replay the exact sequence of calls to the other consumers as if they were directly connected to the frontend.

Fixes #248

@vgvassilev vgvassilev marked this pull request as draft February 15, 2024 20:01
Copy link

codecov bot commented Feb 15, 2024

Codecov Report

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

Project coverage is 94.97%. Comparing base (8a77f81) to head (72fa856).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #766      +/-   ##
==========================================
+ Coverage   94.86%   94.97%   +0.11%     
==========================================
  Files          49       49              
  Lines        7357     7543     +186     
==========================================
+ Hits         6979     7164     +185     
- Misses        378      379       +1     
Files Coverage Δ
lib/Differentiator/VisitorBase.cpp 97.89% <100.00%> (ø)
tools/ClangPlugin.cpp 96.52% <100.00%> (+2.50%) ⬆️
tools/ClangPlugin.h 93.89% <97.18%> (+3.72%) ⬆️
Files Coverage Δ
lib/Differentiator/VisitorBase.cpp 97.89% <100.00%> (ø)
tools/ClangPlugin.cpp 96.52% <100.00%> (+2.50%) ⬆️
tools/ClangPlugin.h 93.89% <97.18%> (+3.72%) ⬆️

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

tools/ClangPlugin.cpp Outdated Show resolved Hide resolved
tools/ClangPlugin.cpp Show resolved Hide resolved
tools/ClangPlugin.cpp Show resolved Hide resolved
tools/ClangPlugin.h Show resolved Hide resolved
tools/ClangPlugin.h Outdated Show resolved Hide resolved
@vgvassilev vgvassilev force-pushed the issue-248 branch 2 times, most recently from b479740 to 55c80ca Compare February 16, 2024 08:38
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/Sins.h Outdated Show resolved Hide resolved
include/clad/Differentiator/Sins.h Show resolved Hide resolved
include/clad/Differentiator/Sins.h Show resolved Hide resolved
include/clad/Differentiator/Sins.h Outdated Show resolved Hide resolved
include/clad/Differentiator/Sins.h Outdated Show resolved Hide resolved
tools/ClangPlugin.cpp Show resolved Hide resolved
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/Sins.h Show resolved Hide resolved
include/clad/Differentiator/Sins.h Show resolved Hide resolved
include/clad/Differentiator/Sins.h Show resolved Hide resolved
include/clad/Differentiator/Sins.h Outdated Show resolved Hide resolved
@vgvassilev vgvassilev force-pushed the issue-248 branch 2 times, most recently from 39341b1 to 07723ff Compare February 18, 2024 15:43
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

tools/ClangPlugin.cpp Outdated Show resolved Hide resolved
tools/ClangPlugin.cpp Outdated Show resolved Hide resolved
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

tools/ClangPlugin.cpp Show resolved Hide resolved
tools/ClangPlugin.cpp Show resolved Hide resolved
tools/ClangPlugin.cpp Outdated Show resolved Hide resolved
tools/ClangPlugin.h Outdated Show resolved Hide resolved
tools/ClangPlugin.h Outdated Show resolved Hide resolved
tools/ClangPlugin.h Outdated Show resolved Hide resolved
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

tools/ClangPlugin.h Outdated Show resolved Hide resolved
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

tools/ClangPlugin.h Outdated Show resolved Hide resolved
tools/ClangPlugin.h Outdated Show resolved Hide resolved
@vgvassilev vgvassilev force-pushed the issue-248 branch 5 times, most recently from 79b9564 to 7cbbb0f Compare March 10, 2024 08:38
@vgvassilev vgvassilev self-assigned this Mar 10, 2024
@vgvassilev vgvassilev added this to the v1.5 milestone Mar 10, 2024
@vgvassilev vgvassilev marked this pull request as ready for review March 10, 2024 10:46
@vgvassilev vgvassilev requested review from vaithak and parth-07 March 10, 2024 10:46
@@ -76,13 +76,13 @@ int main() {
// the indexes of the array by using the format arr[0:<last index of arr>]
auto hessian_all = clad::hessian(weighted_avg, "arr[0:2], weights[0:2]");
// Generates the Hessian matrix for weighted_avg w.r.t. to arr.
auto hessian_arr = clad::hessian(weighted_avg, "arr[0:2]");
// auto hessian_arr = clad::hessian(weighted_avg, "arr[0:2]");
Copy link
Owner Author

Choose a reason for hiding this comment

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

I am not particularly happy with this commit, however, this is the only way to make progress here. I believe the demo fails due to the fact we schedule the hessians recursively. This seems hard to debug because it happens on clang release builds and clad for runtime11, runtime10, and in some cases runtime9.

More information: https://github.com/vgvassilev/clad/actions/runs/8216311966

Perhaps once we fix the way we order diff plans the issue will go away.

@vgvassilev vgvassilev force-pushed the issue-248 branch 2 times, most recently from dc91959 to 794d579 Compare March 10, 2024 12:46
@vgvassilev vgvassilev force-pushed the issue-248 branch 4 times, most recently from 7fc3293 to 481e013 Compare March 10, 2024 18:55
test/Gradient/Gradients.C Show resolved Hide resolved
include/clad/Differentiator/Sins.h Show resolved Hide resolved
tools/ClangPlugin.cpp Outdated Show resolved Hide resolved
tools/ClangPlugin.cpp Outdated Show resolved Hide resolved

// FIXME: We should replace this comparison with the canonical decl
// from the differentiation plan...
return ND->getName().contains("_darg");
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to ignore directly referenced _grad, _hessian and _jacobian calls as well. Perhaps we should discourage users from directly calling the generated functions. This way, we would not need to ignore the warning.

Copy link
Owner Author

Choose a reason for hiding this comment

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

We need to ignore directly referenced _grad, _hessian and _jacobian calls as well.

Added.

Perhaps we should discourage users from directly calling the generated functions.

I thought about the same but I hesitate because this way we can do cross translation unit derivative communication because that's what the language does for everything else.

This way, we would not need to ignore the warning.

This warning triggers only when we put a forward declaration in a anonymous namespace. That's quite silly thing to do generally but...

Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought about the same but I hesitate because this way we can do cross translation unit derivative communication because that's what the language does for everything else.

Oh, yes, these function declarations can be very helpful for cross-translation derivative communication. We should definitely document about this in the documentation and how users can depend upon and use this functionality.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be helpful to add an interface which users can easily use for determining the derivative type of a given function. Typing the derivative function type by hand can be error-prone and cumbersome

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes, I am not sure how. If you have an idea maybe we should open an issue?

tools/ClangPlugin.h Outdated Show resolved Hide resolved
tools/ClangPlugin.cpp Show resolved Hide resolved
tools/ClangPlugin.cpp Show resolved Hide resolved
tools/ClangPlugin.cpp Outdated Show resolved Hide resolved
tools/ClangPlugin.cpp Outdated Show resolved Hide resolved
@vaithak
Copy link
Collaborator

vaithak commented Mar 12, 2024

Should we add a detailed description explaining these changes. It would be a helpful reference for us and even new contributors to understand the request flow.
We can have the following sections in that explanation: how did it work earlier and what cases went wrong. How does this solution improve and fix those and some details to walkthrough this implementation.

@vgvassilev
Copy link
Owner Author

Should we add a detailed description explaining these changes. It would be a helpful reference for us and even new contributors to understand the request flow. We can have the following sections in that explanation: how did it work earlier and what cases went wrong. How does this solution improve and fix those and some details to walkthrough this implementation.

Do you mean in the commit message or the clad documentation?

@vaithak
Copy link
Collaborator

vaithak commented Mar 12, 2024

Do you mean in the commit message or the clad documentation?

In the commit message should be sufficient for now, once we have the diff plans also finalized, we can add them in the documentation too if required.

@vgvassilev
Copy link
Owner Author

Do you mean in the commit message or the clad documentation?

In the commit message should be sufficient for now, once we have the diff plans also finalized, we can add them in the documentation too if required.

I thought I capture that in the very first commit message:

[Delay the differentiation process until the end of TU.](https://github.com/vgvassilev/clad/pull/766/commits/f0c71c987f0005f1c5435ea4e4e95ef57b6aa41e)

Before this patch clad attaches itself as a first consumer and applies AD before
code generation. However, that is limited since clang sends every top-level
declaration to codegen which limits the amount of flexibility clad has. For
example, we have to instantiate all pending templates at every
HandleTopLevelDecl calls; we cannot really differentiate virtual functions
whose classes have sent their key function to CodeGen; and in general we perform
actions which are semantically useful for the end of the translation unit.

This patch makes clad a single consumer of clang which dispatches to the others.
That's done by delaying all calls to the consumers until the end of the TU where
clad can replay the exact sequence of calls to the other consumers as if they
were directly connected to the frontend.

What else is missing?

@vaithak
Copy link
Collaborator

vaithak commented Mar 12, 2024

My bad, didn't notice it was in the commit message itself. This should be fine.

@vgvassilev vgvassilev force-pushed the issue-248 branch 3 times, most recently from 4735b41 to 51c8df5 Compare March 13, 2024 09:43
Copy link
Collaborator

@parth-07 parth-07 left a comment

Choose a reason for hiding this comment

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

Looks good.

tools/ClangPlugin.cpp Show resolved Hide resolved
@vgvassilev vgvassilev force-pushed the issue-248 branch 2 times, most recently from 66cde27 to 0d89cdd Compare March 14, 2024 15:41
Before this patch clad attaches itself as a first consumer and applies AD before
code generation. However, that is limited since clang sends every top-level
declaration to codegen which limits the amount of flexibility clad has. For
example, we have to instantiate all pending templates at every
HandleTopLevelDecl calls; we cannot really differentiate virtual functions
whose classes have sent their key function to CodeGen; and in general we perform
actions which are semantically useful for the end of the translation unit.

This patch makes clad a single consumer of clang which dispatches to the others.
That's done by delaying all calls to the consumers until the end of the TU where
clad can replay the exact sequence of calls to the other consumers as if they
were directly connected to the frontend.

Fixes #248
We should be able to diagnose these problems as reported in #810.
The demo fails due to the fact we schedule the hessians recursively. This seems
hard to debug because it happens on clang release builds and clad for runtime11,
runtime10, and in some cases runtime9.

More information: https://github.com/vgvassilev/clad/actions/runs/8216311966
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

DelayedCallInfo(CallKind K, clang::DeclGroupRef DGR)
: m_Kind(K), m_DGR(DGR) {}
DelayedCallInfo(CallKind K, const clang::Decl* D)
: m_Kind(K), m_DGR(const_cast<clang::Decl*>(D)) {}
Copy link
Contributor

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]

          : m_Kind(K), m_DGR(const_cast<clang::Decl*>(D)) {}
                             ^

tools/ClangPlugin.h Outdated Show resolved Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@vgvassilev vgvassilev merged commit d2df900 into master Mar 14, 2024
88 checks passed
@vgvassilev vgvassilev deleted the issue-248 branch March 14, 2024 16:17
vgvassilev added a commit that referenced this pull request May 4, 2024
This patch enhances the work done in #766 where we assumed the clients are going
to be well-behaved such as clang or clang-repl. However, we can have clients
which connect the clang components in a non-standard way and we should provide a
reasonable fallback.

This patch detects if the setup is non-standard and teaches clad to handle
requests as they come, pretty much the same way as before #766 was implemented.
Of course this comes with a cost when we try to differentiate declarations that
are defined later in the translation unit. We expect such setups to be rare and
mostly in the cases of incremental processing and old repls such as Cling.
vgvassilev added a commit that referenced this pull request May 5, 2024
This patch enhances the work done in #766 where we assumed the clients are going
to be well-behaved such as clang or clang-repl. However, we can have clients
which connect the clang components in a non-standard way and we should provide a
reasonable fallback.

This patch detects if the setup is non-standard and teaches clad to handle
requests as they come, pretty much the same way as before #766 was implemented.
Of course this comes with a cost when we try to differentiate declarations that
are defined later in the translation unit. We expect such setups to be rare and
mostly in the cases of incremental processing and old repls such as Cling.
vgvassilev added a commit that referenced this pull request May 8, 2024
This patch enhances the work done in #766 where we assumed the clients are going
to be well-behaved such as clang or clang-repl. However, we can have clients
which connect the clang components in a non-standard way and we should provide a
reasonable fallback.

This patch detects if the setup is non-standard and teaches clad to handle
requests as they come, pretty much the same way as before #766 was implemented.
Of course this comes with a cost when we try to differentiate declarations that
are defined later in the translation unit. We expect such setups to be rare and
mostly in the cases of incremental processing and old repls such as Cling.
vgvassilev added a commit that referenced this pull request May 8, 2024
This patch enhances the work done in #766 where we assumed the clients are going
to be well-behaved such as clang or clang-repl. However, we can have clients
which connect the clang components in a non-standard way and we should provide a
reasonable fallback.

This patch detects if the setup is non-standard and teaches clad to handle
requests as they come, pretty much the same way as before #766 was implemented.
Of course this comes with a cost when we try to differentiate declarations that
are defined later in the translation unit. We expect such setups to be rare and
mostly in the cases of incremental processing and old repls such as Cling.
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.

Differentiation fails if function is defined after the call to clad differentiation functions.
3 participants