-
Notifications
You must be signed in to change notification settings - Fork 123
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
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ 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
... and 1 file with indirect coverage changes
|
There was a problem hiding this 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]; |
There was a problem hiding this comment.
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];
^
74affff
to
00a9790
Compare
// 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") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
00a9790
to
9b56257
Compare
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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]
namespace class_functions { | |
namespace custom_derivatives::class_functions { |
include/clad/Differentiator/Array.h:474:
- } // namespace class_functions
- } // namespace custom_derivatives
+ } // namespace custom_derivatives
956c862
to
cae7c17
Compare
cae7c17
to
66717fe
Compare
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:
double[5]
), use the same type for the adjoint.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 asresize
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.