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

Reimplement jacobians using the vectorized forward mode #1121

Merged
merged 4 commits into from
Nov 19, 2024

Conversation

PetroZarytskyi
Copy link
Collaborator

@PetroZarytskyi PetroZarytskyi commented Oct 22, 2024

Previously, jacobians were based on the non-vectorized reverse mode, which was mostly incapable of capturing multiple outputs. The implementation worked in a few particular cases. For example, it was not possible to differentiate function calls or declare variables inside the original function body.
This PR implements jacobians using the vectorized forward mode. At the very least, this will solve the issues described above and give a way forward to solve other ones. This also means introducing features to the vectorized fwd mode will introduce the same features to jacobians.
Let's take a look at the new signature of jacobians. Since the function to be differentiated is expected to have multiple outputs, we should expect the output in the form of array/pointer/reference parameters (just like before). And for every output parameter, we should generate a corresponding adjoint parameter for the user to acquire the results. Since there is no way to specify which parameters are used as output and which are not, adjoints are generated for all array/pointer/reference parameters. For example:

void f(double a, double b, double* c)  
 --> 
void f_jac(double a, double b, double* c, <matrix<double>* _d_c)

or

void f(double a, double b, double* c, double[7] t) 
 -->
void f_jac(double a, double b, double* c, double[7] t,
 array_ref<matrix<double>> _d_c, matrix<double>* _d_t)

This signature is also similar to the old one. e.g.

df.execute(a, b, c, result); // old behavior
df.execute(a, b, c, &result); // new behavior

However, the behavior differs for multiple output parameters, which the old jacobians did not support.

Note: the same functionality can be achieved by using the vectorized reverse mode, which should probably be implemented at some point. However, the old code for jacobians is unlikely to be useful for that, and there is not much point in keeping it.

Fixes #472, Fixes #1057, Fixes #480, Fixes #527

Copy link

codecov bot commented Oct 22, 2024

Codecov Report

Attention: Patch coverage is 95.98540% with 11 lines in your changes missing coverage. Please review.

Project coverage is 94.43%. Comparing base (a7de6af) to head (29ad5f6).
Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
lib/Differentiator/JacobianModeVisitor.cpp 93.97% 10 Missing ⚠️
lib/Differentiator/VectorForwardModeVisitor.cpp 50.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1121      +/-   ##
==========================================
- Coverage   94.48%   94.43%   -0.06%     
==========================================
  Files          50       51       +1     
  Lines        8850     8928      +78     
==========================================
+ Hits         8362     8431      +69     
- Misses        488      497       +9     
Files with missing lines Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 96.55% <ø> (-0.87%) ⬇️
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/BaseForwardModeVisitor.cpp 98.76% <100.00%> (+<0.01%) ⬆️
lib/Differentiator/DerivativeBuilder.cpp 100.00% <100.00%> (ø)
lib/Differentiator/DiffPlanner.cpp 98.65% <100.00%> (-0.01%) ⬇️
lib/Differentiator/ReverseModeVisitor.cpp 95.54% <100.00%> (-0.14%) ⬇️
lib/Differentiator/VisitorBase.cpp 97.63% <100.00%> (+0.34%) ⬆️
lib/Differentiator/VectorForwardModeVisitor.cpp 99.69% <50.00%> (-0.31%) ⬇️
lib/Differentiator/JacobianModeVisitor.cpp 93.97% <93.97%> (ø)

... and 1 file with indirect coverage changes

Files with missing lines Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 96.55% <ø> (-0.87%) ⬇️
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/BaseForwardModeVisitor.cpp 98.76% <100.00%> (+<0.01%) ⬆️
lib/Differentiator/DerivativeBuilder.cpp 100.00% <100.00%> (ø)
lib/Differentiator/DiffPlanner.cpp 98.65% <100.00%> (-0.01%) ⬇️
lib/Differentiator/ReverseModeVisitor.cpp 95.54% <100.00%> (-0.14%) ⬇️
lib/Differentiator/VisitorBase.cpp 97.63% <100.00%> (+0.34%) ⬆️
lib/Differentiator/VectorForwardModeVisitor.cpp 99.69% <50.00%> (-0.31%) ⬇️
lib/Differentiator/JacobianModeVisitor.cpp 93.97% <93.97%> (ø)

... and 1 file with indirect coverage changes

---- 🚨 Try these New Features:

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

struct JacobianDerivedFnTraits<R (C::*)(Args...) cv vol ref noex> { \
using type = void (C::*)(Args..., SelectLast_t<Args...>) cv vol ref noex; \
};
#define JacobianDerivedFnTraits_AddSPECS(var, cv, vol, ref, noex) \
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: function-like macro 'JacobianDerivedFnTraits_AddSPECS' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage]

#define JacobianDerivedFnTraits_AddSPECS(var, cv, vol, ref, noex)            \
        ^

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@vgvassilev This is tricky to address and it's not related to the PR. Do we want to fix this now?

Copy link
Owner

Choose a reason for hiding this comment

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

No but we should probably open an issue to track it?

clad::utils::ComputeEffectiveFnName(FD) + "_pushforward";
callDiff = m_Builder.BuildCallToCustomDerivativeOrNumericalDiff(
customPushforward, customDerivativeArgs, getCurrentScope(),
const_cast<DeclContext*>(FD->getDeclContext()));
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]

        const_cast<DeclContext*>(FD->getDeclContext()));
        ^

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@vgvassilev I don't think there's a way to avoid this easily right now. The same thing is done in all visitors. Should I silence the warning?

Copy link
Owner

Choose a reason for hiding this comment

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

No, that needs deeper refactoring

lib/Differentiator/JacobianModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/JacobianModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/JacobianModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/JacobianModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/JacobianModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/VectorForwardModeVisitor.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

lib/Differentiator/JacobianModeVisitor.cpp Show resolved Hide resolved
lib/Differentiator/JacobianModeVisitor.cpp Show resolved Hide resolved
lib/Differentiator/JacobianModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/JacobianModeVisitor.cpp Show resolved Hide resolved
lib/Differentiator/JacobianModeVisitor.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

lib/Differentiator/JacobianModeVisitor.cpp Outdated Show resolved Hide resolved
@vgvassilev
Copy link
Owner

Could we write a few benchmarks to make sure we do not regress in performance?

@PetroZarytskyi PetroZarytskyi force-pushed the jac branch 4 times, most recently from ac0c084 to 8b40e97 Compare October 25, 2024 18:06
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/VisitorBase.cpp Outdated Show resolved Hide resolved
include/clad/Differentiator/Array.h Outdated Show resolved Hide resolved
include/clad/Differentiator/ArrayExpression.h Outdated Show resolved Hide resolved
include/clad/Differentiator/ArrayRef.h Outdated Show resolved Hide resolved
// or equal to 0, then log(base) is undefined, and therefore if user only
// requested directional derivative of base^exp w.r.t base -- which is valid
// --, the result would be undefined because as per C++ valid number + NaN * 0
// = NaN.
Copy link
Owner

Choose a reason for hiding this comment

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

Why do we need this overload?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was tricky to make this pushforward work as both vectorized and non-vectorized. The main reason is that the output type is hard to deduce. Also the line auto derivative = ... was created as array_expression, which was a problem for a couple of reasons. I reimplemented this with better templates in the last commit. Does this look better?

template <typename T>
CUDA_HOST_DEVICE ValueAndPushforward<T, T> acos_pushforward(T x, T d_x) {
template <typename T, typename dT>
CUDA_HOST_DEVICE ValueAndPushforward<T, dT> acos_pushforward(T x, dT d_x) {
Copy link
Owner

Choose a reason for hiding this comment

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

Are these changes not good for a separate PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They can exist on their own but they will have no use. The types don't match in the vectorized fwd mode because those will be T and clad::array<T> respectively.

include/clad/Differentiator/Differentiator.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

lib/Differentiator/VisitorBase.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

lib/Differentiator/JacobianModeVisitor.h Show resolved Hide resolved
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!

struct JacobianDerivedFnTraits<R (C::*)(Args...) cv vol ref noex> { \
using type = void (C::*)(Args..., SelectLast_t<Args...>) cv vol ref noex; \
};
#define JacobianDerivedFnTraits_AddSPECS(var, cv, vol, ref, noex) \
Copy link
Owner

Choose a reason for hiding this comment

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

No but we should probably open an issue to track it?

@PetroZarytskyi PetroZarytskyi force-pushed the jac branch 2 times, most recently from f25aac9 to a42eceb Compare November 19, 2024 15:33
 Previously, jacobians were based on the non-vectorized reverse mode, which was mostly incapable of capturing multiple outputs. The implementation worked in a few particular cases. For example, it was not possible to differentiate function calls or declare variables inside the original function body.
 This PR implements jacobians using the vectorized forward mode. At the very least, this will solve the issues described above and give a way forward to solve other ones. This also means introducing features to the vectorized fwd mode will introduce the same features to jacobians.
 Let's take a look at the new signature of jacobians. Since the function to be differentiated is expected to have multiple outputs, we should expect the output in the form of array/pointer/reference parameters (just like before). And for every output parameter, we should generate a corresponding adjoint parameter for the user to acquire the results. Since there is no way to specify which parameters are used as output and which are not, adjoints are generated for all array/pointer/reference parameters. For example:

```
void f(double a, double b, double* c)
 -->
void f_jac(double a, double b, double* c, <matrix<double>* _d_c)
```

or

```
void f(double a, double b, double* c, double[7] t)
 -->
void f_jac(double a, double b, double* c, double[7] t,
 array_ref<matrix<double>> _d_c, matrix<double>* _d_t)
```

This signature is also similar to the old one. e.g.
```
df.execute(a, b, c, result); // old behavior
df.execute(a, b, c, &result); // new behavior
```
However, the behavior differs for multiple output parameters, which the old jacobians did not support.

 Note: the same functionality can be achieved by using the vectorized reverse mode, which should probably be implemented at some point. However, the old code for jacobians is unlikely to be useful for that, and there is not much point in keeping it.

Fixes vgvassilev#472, Fixes vgvassilev#1057, Fixes vgvassilev#480, Fixes vgvassilev#527
@vgvassilev vgvassilev merged commit 8d916fe into vgvassilev:master Nov 19, 2024
90 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants