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

Add support for differentiating functors in forward mode #240

Merged
merged 20 commits into from
Jul 8, 2021

Conversation

parth-07
Copy link
Collaborator

@parth-07 parth-07 commented Jun 8, 2021

This PR aims to add support for differentiating functors in the forward differentiation mode.

  • Implicitly differentiate operator() member function when functor is passed
  • Implicitly pass functor while executing derived function using CladFunction
  • Add tests
  • Add user-friendly errors using Sema diagnostics when an object of class without overloaded operator() or multiple overloads of call operator is passed to be differentiated.
  • Add demo

By differentiating functors, I mean, differentiating the operator() member function when a functor is passed, and implicitly executing the derived function using the passed functor.

Functors can be passed to clad::differentiate both by reference and as pointers. For example:

Experiment E; // a functor
auto d_fn = clad::differentiate(&E, "i");
auto d_fnRef = clad::differentiate(E, "i");
...
...
std::cout<<d_fn.execute(1,  3)<<"\n"; // no need to pass object while executing derived function.

Implemented solution

The solution is implemented in 2 steps:

  • Differentiate operator() member function whenever functor (class type object) is passed to be differentiated.
  • Execute derived function through the functor object.

1st Step Implementation:

DiffPlanner getArgFunction is responsible for obtaining function to be differentiated from the call expression of clad differentiation functions.

It currently behaves as follows:

  • Return DeclRefExpr node of the 1st argument (which is, the function to be differentiated), and update the RelevantAncestor parameter to the nearest ancestor of 1st argument which is of type ImplicitCastExpr or UnaryOperator.

getArgFunction has been modified such that, whenever a class type object is to be differentiated, it provides all the data for the operator() member function of the class type. By all the data, I am referring to DeclRefExpr of the operator() method, the correct nearest ancestor for the operator() method.

But since the operator() method hasn't actually passed, these nodes (DeclRefExpr and ancestors of the operator() method) does not actually exist, and needs to be created inside getArgFunction. This effectively converts calls to differentiate class type object to calls to differentiate a member function for the rest of the clad.

2nd Step Implementation:

CladFunction has been modified to save an object through which derived member functions should be called when the user does not explicitly pass an object while calling CladFunction::execute

This behaviour is summarised in the following code snippet:

SomeClass A, B;
auto d_memFn = clad::differentiate(&SomeClass::SomeMemberFn, "i");
d_memFn.setObject(&A); // set A as default object for executing derived fn
d_memFn.execute(1, 3); // calls A.derivedFn(1, 3);
d_memFn.execute(B, 1, 3); // calls B.derivedFn(1, 3);

When a functor object is passed to be differentiated, it is saved in the corresponding CladFunction object so that it can be automatically taken by the CladFunction::execute method to call the derived member function.

Modifications to clad::differentiate

clad::differentiate is modified to have 2 overloads, one for differentiating functions (and methods), and the other for differentiating functors.
2 separate overloads are required because function (and method) pointers need to be passed by value whereas objects need to be passed by reference (because we need to determine constness property (among others) of passed functor object)

@parth-07 parth-07 force-pushed the differentiate-functors branch 5 times, most recently from c7cd2b3 to 768b0c2 Compare June 11, 2021 07:58
@codecov
Copy link

codecov bot commented Jun 11, 2021

Codecov Report

Merging #240 (ef9299b) into master (1016c27) will increase coverage by 0.63%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #240      +/-   ##
==========================================
+ Coverage   84.39%   85.02%   +0.63%     
==========================================
  Files          21       21              
  Lines        2736     2832      +96     
==========================================
+ Hits         2309     2408      +99     
+ Misses        427      424       -3     
Impacted Files Coverage Δ
lib/Differentiator/DiffPlanner.cpp 98.40% <100.00%> (+1.73%) ⬆️
lib/Differentiator/HessianModeVisitor.cpp 98.30% <0.00%> (-0.06%) ⬇️
lib/Differentiator/ForwardModeVisitor.cpp 88.92% <0.00%> (+0.36%) ⬆️
lib/Differentiator/VisitorBase.cpp 95.20% <0.00%> (+0.39%) ⬆️
include/clad/Differentiator/Compatibility.h 75.00% <0.00%> (+6.25%) ⬆️
Impacted Files Coverage Δ
lib/Differentiator/DiffPlanner.cpp 98.40% <100.00%> (+1.73%) ⬆️
lib/Differentiator/HessianModeVisitor.cpp 98.30% <0.00%> (-0.06%) ⬇️
lib/Differentiator/ForwardModeVisitor.cpp 88.92% <0.00%> (+0.36%) ⬆️
lib/Differentiator/VisitorBase.cpp 95.20% <0.00%> (+0.39%) ⬆️
include/clad/Differentiator/Compatibility.h 75.00% <0.00%> (+6.25%) ⬆️

@parth-07 parth-07 force-pushed the differentiate-functors branch 2 times, most recently from f2d51ff to e3be8fd Compare June 13, 2021 15:25
@parth-07 parth-07 marked this pull request as ready for review June 13, 2021 21:26
@parth-07 parth-07 marked this pull request as draft June 26, 2021 00:51
@parth-07 parth-07 marked this pull request as ready for review June 30, 2021 01:53
@parth-07 parth-07 force-pushed the differentiate-functors branch 3 times, most recently from 45fe756 to 98108ef Compare June 30, 2021 04:05
include/clad/Differentiator/Differentiator.h Outdated Show resolved Hide resolved
include/clad/Differentiator/Differentiator.h Outdated Show resolved Hide resolved
include/clad/Differentiator/FunctionTraits.h Show resolved Hide resolved
include/clad/Differentiator/FunctionTraits.h Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Outdated Show resolved Hide resolved
include/clad/Differentiator/Differentiator.h Outdated Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Outdated Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Outdated Show resolved Hide resolved
test/ForwardMode/Functors.C Outdated Show resolved Hide resolved
@parth-07 parth-07 requested a review from vgvassilev July 2, 2021 01:53
include/clad/Differentiator/Differentiator.h Outdated Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Outdated Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Outdated Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Outdated Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Outdated Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Outdated Show resolved Hide resolved
@parth-07 parth-07 requested a review from vgvassilev July 3, 2021 14:51
lib/Differentiator/DiffPlanner.cpp Show resolved Hide resolved
include/clad/Differentiator/Differentiator.h Show resolved Hide resolved
demos/Functor.cpp Show resolved Hide resolved
lib/Differentiator/DiffPlanner.cpp Outdated Show resolved Hide resolved
- Use `Sema::LookupQualifiefName` instead of `DeclContext::lookup`
- Use diagnostics from Sema which checks for visibility
@parth-07 parth-07 requested a review from vgvassilev July 6, 2021 12:25
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! LGTM!

@vgvassilev vgvassilev merged commit cf28b24 into vgvassilev:master Jul 8, 2021
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.

3 participants