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

Differentiate array parameters in forward mode #1062

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

PetroZarytskyi
Copy link
Collaborator

@PetroZarytskyi PetroZarytskyi commented Aug 28, 2024

Currently, we don't create an adjoint of an array parameter in the forward mode because we don't know the size of the array. This means considering it non-differentiable, which is not always the case. This PR does two things:

  1. If the parameter is a const size array (e.g. double[5]), use the same type for the adjoint.
  2. Otherwise the size is unknown so we have to extend the array dynamically before accessing members.

There are downsides to the approach in 2. : extending the array involves reallocating memory, which costs performance and will cause a segfault if the user stored a pointer to some of the array's elements. However, alternatives are banning array parameters or considering them non-differentiable as before, which will fail way more frequently and produce warnings at max.

Also, please note the function extend that I introduced is not the same as resize since the latter can shrink the array if its argument is less than the size of the array. extend only makes the array bigger.

Fixes #1035.

Copy link

codecov bot commented Aug 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.39%. Comparing base (685bcbf) to head (9b56257).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1062      +/-   ##
==========================================
- Coverage   94.40%   94.39%   -0.02%     
==========================================
  Files          55       55              
  Lines        8430     8468      +38     
==========================================
+ Hits         7958     7993      +35     
- Misses        472      475       +3     
Files with missing lines Coverage Δ
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/BaseForwardModeVisitor.cpp 98.47% <100.00%> (-0.07%) ⬇️
lib/Differentiator/VisitorBase.cpp 97.04% <100.00%> (+0.14%) ⬆️

... and 1 file with indirect coverage changes

Files with missing lines Coverage Δ
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/BaseForwardModeVisitor.cpp 98.47% <100.00%> (-0.07%) ⬇️
lib/Differentiator/VisitorBase.cpp 97.04% <100.00%> (+0.14%) ⬆️

... and 1 file with indirect coverage changes

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

/// elements if the current array size is less than `size`.
CUDA_HOST_DEVICE void extend(std::size_t size) {
if (size > m_size) {
T* extendedArr = new T[size];
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: initializing non-owner 'T *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory]

      T* extendedArr = new T[size];
      ^

lib/Differentiator/BaseForwardModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/BaseForwardModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/BaseForwardModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/VisitorBase.cpp Outdated Show resolved Hide resolved
// but we need to be able to differentiate it to prevent segfaults in
// hessians. Once we add support for methods that change their objects, this
// section should be removed.
if (FDName == "extend")
Copy link
Owner

Choose a reason for hiding this comment

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

Can we not provide a pullback for this interface?

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 thought this was not possible but I forgot we now support custom _forw functions. I fixed this. Thank you for pointing this out.

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

@@ -446,6 +461,20 @@ operator/(const array<T>& arr1, const array<U>& arr2) {
arr2);
}

namespace custom_derivatives {
namespace class_functions {
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: nested namespaces can be concatenated [modernize-concat-nested-namespaces]

Suggested change
namespace class_functions {
namespace custom_derivatives::class_functions {

include/clad/Differentiator/Array.h:474:

- } // namespace class_functions
- } // namespace custom_derivatives
+ } // namespace custom_derivatives

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.

Unnecessary warning when differentiating a function that mutates its array arg
2 participants