From 974ad05daed1be2d4187c8f37cd4ce9cc2f4a407 Mon Sep 17 00:00:00 2001 From: Vassil Vassilev Date: Tue, 18 Jul 2023 08:50:21 +0000 Subject: [PATCH] [clad] Bump clad version to v1.2. This new release includes some improvements: * Add experimental support for forward vector mode * Add pushforwards for std::floor and std::ceil * Improve AD function interfaces with bitmasked options. For example: clad::differentiate<(f) will be equivalent to clad::differentiate<<1, clad::opts::vector_mode>(f) and will request the first order derivative of f in forward vector mode. * LLVM16 Support See more at: https://github.com/vgvassilev/clad/blob/v1.2/docs/internalDocs/ReleaseNotes.md --- .../cling/tools/plugins/clad/CMakeLists.txt | 16 +-- .../clad/patches/clad_array_size_type.patch | 28 ---- .../tools/plugins/clad/patches/clad_fma.patch | 132 ------------------ .../clad/patches/clad_local_array.patch | 100 ------------- 4 files changed, 8 insertions(+), 268 deletions(-) delete mode 100644 interpreter/cling/tools/plugins/clad/patches/clad_array_size_type.patch delete mode 100644 interpreter/cling/tools/plugins/clad/patches/clad_fma.patch delete mode 100644 interpreter/cling/tools/plugins/clad/patches/clad_local_array.patch diff --git a/interpreter/cling/tools/plugins/clad/CMakeLists.txt b/interpreter/cling/tools/plugins/clad/CMakeLists.txt index 7e17b3e716d35..ec0402183472e 100644 --- a/interpreter/cling/tools/plugins/clad/CMakeLists.txt +++ b/interpreter/cling/tools/plugins/clad/CMakeLists.txt @@ -63,18 +63,18 @@ set(_clad_cmake_logging_settings list(APPEND _clad_cmake_logging_settings LOG_OUTPUT_ON_FAILURE ON) -list(APPEND _clad_patches_list "clad_array_size_type.patch" "clad_local_array.patch" "clad_fma.patch") -set(_clad_patch_command - ${CMAKE_COMMAND} -E copy_directory - ${CMAKE_SOURCE_DIR}/interpreter/cling/tools/plugins/clad/patches - && git checkout - && git apply --ignore-space-change --ignore-whitespace ${_clad_patches_list} - ) +#list(APPEND _clad_patches_list "patch1.patch" "patch2.patch") +#set(_clad_patch_command +# ${CMAKE_COMMAND} -E copy_directory +# ${CMAKE_SOURCE_DIR}/interpreter/cling/tools/plugins/clad/patches +# && git checkout +# && git apply --ignore-space-change --ignore-whitespace ${_clad_patches_list} +# ) ExternalProject_Add( clad GIT_REPOSITORY https://github.com/vgvassilev/clad.git - GIT_TAG v1.1 + GIT_TAG v1.2 UPDATE_COMMAND "" PATCH_COMMAND ${_clad_patch_command} CMAKE_ARGS -G ${CMAKE_GENERATOR} diff --git a/interpreter/cling/tools/plugins/clad/patches/clad_array_size_type.patch b/interpreter/cling/tools/plugins/clad/patches/clad_array_size_type.patch deleted file mode 100644 index 44039346dc5ad..0000000000000 --- a/interpreter/cling/tools/plugins/clad/patches/clad_array_size_type.patch +++ /dev/null @@ -1,28 +0,0 @@ -From 262025e0dc6485700ea938dd3ee873e34a9ebe62 Mon Sep 17 00:00:00 2001 -From: Garima Singh -Date: Tue, 25 Apr 2023 09:25:18 +0200 -Subject: [PATCH] Fix failure on 32-bit machines due to ambigious cast. - -Using `clad::array`'s `[]` operator causes an ambiguity error on 32 bit -machines when using a non-`std::size_t` type as the subscript -expression. This happens because `clad::array` can be implicity cast to -`double*` through the overloaded dereference operator. Changing the -subscript expression type in `clad::array` to `std::ptrdiff_t` solves -this issue. ---- - include/clad/Differentiator/Array.h | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git a/include/clad/Differentiator/Array.h b/include/clad/Differentiator/Array.h -index c5f7b464..59c5178f 100644 ---- a/include/clad/Differentiator/Array.h -+++ b/include/clad/Differentiator/Array.h -@@ -54,7 +54,7 @@ template class array { - CUDA_HOST_DEVICE T* ptr() { return m_arr; } - /// Returns the reference to the location at the index of the underlying - /// array -- CUDA_HOST_DEVICE T& operator[](std::size_t i) { return m_arr[i]; } -+ CUDA_HOST_DEVICE T& operator[](std::ptrdiff_t i) { return m_arr[i]; } - /// Returns the reference to the underlying array - CUDA_HOST_DEVICE T& operator*() { return *m_arr; } - diff --git a/interpreter/cling/tools/plugins/clad/patches/clad_fma.patch b/interpreter/cling/tools/plugins/clad/patches/clad_fma.patch deleted file mode 100644 index 754377bd0e176..0000000000000 --- a/interpreter/cling/tools/plugins/clad/patches/clad_fma.patch +++ /dev/null @@ -1,132 +0,0 @@ -From 7fd9185c94febbc92c0e6dd90e9efe77c98acb8d Mon Sep 17 00:00:00 2001 -From: Garima Singh -Date: Thu, 20 Apr 2023 17:22:50 +0200 -Subject: [PATCH] Add std::fma to custom derivatives. - ---- - .../clad/Differentiator/BuiltinDerivatives.h | 21 ++++++++++++ - test/FirstDerivative/BuiltinDerivatives.C | 12 +++++++ - test/Gradient/Assignments.C | 32 ++++++++++++++++++- - 3 files changed, 64 insertions(+), 1 deletion(-) - -diff --git a/include/clad/Differentiator/BuiltinDerivatives.h b/include/clad/Differentiator/BuiltinDerivatives.h -index 3c41d892e..7e7b04f59 100644 ---- a/include/clad/Differentiator/BuiltinDerivatives.h -+++ b/include/clad/Differentiator/BuiltinDerivatives.h -@@ -110,6 +110,25 @@ CUDA_HOST_DEVICE void pow_pullback(T1 x, T2 exponent, T3 d_y, - *d_exponent += t.pushforward * d_y; - } - -+template -+CUDA_HOST_DEVICE ValueAndPushforward -+fma_pushforward(T1 a, T2 b, T3 c, T1 d_a, T2 d_b, T3 d_c) { -+ auto val = ::std::fma(a, b, c); -+ auto derivative = d_a * b + a * d_b + d_c; -+ return {val, derivative}; -+} -+ -+template -+CUDA_HOST_DEVICE void fma_pullback(T1 a, T2 b, T3 c, T4 d_y, -+ clad::array_ref d_a, -+ clad::array_ref d_b, -+ clad::array_ref d_c) { -+ *d_a += b * d_y; -+ *d_b += a * d_y; -+ *d_c += d_y; -+} -+ - } // namespace std - // These are required because C variants of mathematical functions are - // defined in global namespace. -@@ -120,7 +139,9 @@ using std::log_pushforward; - using std::pow_pushforward; - using std::sin_pushforward; - using std::sqrt_pushforward; -+using std::fma_pushforward; - using std::pow_pullback; -+using std::fma_pullback; - } // namespace custom_derivatives - } // namespace clad - -diff --git a/test/FirstDerivative/BuiltinDerivatives.C b/test/FirstDerivative/BuiltinDerivatives.C -index bd9eed2e1..cbf2d2854 100644 ---- a/test/FirstDerivative/BuiltinDerivatives.C -+++ b/test/FirstDerivative/BuiltinDerivatives.C -@@ -247,6 +247,15 @@ double f11(double x, double y) { - // CHECK-NEXT: } - // CHECK-NEXT: } - -+double f12(double a, double b) { return std::fma(a, b, b); } -+ -+//CHECK: double f12_darg1(double a, double b) { -+//CHECK-NEXT: double _d_a = 0; -+//CHECK-NEXT: double _d_b = 1; -+//CHECK-NEXT: ValueAndPushforward _t0 = clad::custom_derivatives::fma_pushforward(a, b, b, _d_a, _d_b, _d_b); -+//CHECK-NEXT: return _t0.pushforward; -+//CHECK-NEXT: } -+ - int main () { //expected-no-diagnostics - float f_result[2]; - double d_result[2]; -@@ -313,5 +322,8 @@ int main () { //expected-no-diagnostics - - TEST_GRADIENT(f11, /*numOfDerivativeArgs=*/2, -1, 1, &d_result[0], &d_result[1]); // CHECK-EXEC: {-4.00, 0.00} - -+ auto f12_darg1 = clad::differentiate(f12, 1); -+ printf("Result is = %f\n", f12_darg1.execute(2, 1)); //CHECK-EXEC: Result is = 3.000000 -+ - return 0; - } -diff --git a/test/Gradient/Assignments.C b/test/Gradient/Assignments.C -index c19cf3cb4..d62505aff 100644 ---- a/test/Gradient/Assignments.C -+++ b/test/Gradient/Assignments.C -@@ -1,4 +1,4 @@ --// RUN: %cladclang %s -I%S/../../include -lstdc++ -oReverseAssignments.out 2>&1 | FileCheck %s -+// RUN: %cladclang %s -I%S/../../include -lstdc++ -lm -oReverseAssignments.out 2>&1 | FileCheck %s - // RUN: ./ReverseAssignments.out | FileCheck -check-prefix=CHECK-EXEC %s - //CHECK-NOT: {{.*error|warning|note:.*}} - -@@ -812,6 +812,35 @@ double f18(double i, double j, double k) { - // CHECK-NEXT: } - // CHECK-NEXT: } - -+double f19(double a, double b) { -+ return std::fma(a, b, b); -+} -+ -+//CHECK: void f19_grad(double a, double b, clad::array_ref _d_a, clad::array_ref _d_b) { -+//CHECK-NEXT: double _t0; -+//CHECK-NEXT: double _t1; -+//CHECK-NEXT: double _t2; -+//CHECK-NEXT: _t0 = a; -+//CHECK-NEXT: _t1 = b; -+//CHECK-NEXT: _t2 = b; -+//CHECK-NEXT: double f19_return = std::fma(_t0, _t1, _t2); -+//CHECK-NEXT: goto _label0; -+//CHECK-NEXT: _label0: -+//CHECK-NEXT: { -+//CHECK-NEXT: double _grad0 = 0.; -+//CHECK-NEXT: double _grad1 = 0.; -+//CHECK-NEXT: double _grad2 = 0.; -+//CHECK-NEXT: clad::custom_derivatives::fma_pullback(_t0, _t1, _t2, 1, &_grad0, &_grad1, &_grad2); -+//CHECK-NEXT: double _r0 = _grad0; -+//CHECK-NEXT: * _d_a += _r0; -+//CHECK-NEXT: double _r1 = _grad1; -+//CHECK-NEXT: * _d_b += _r1; -+//CHECK-NEXT: double _r2 = _grad2; -+//CHECK-NEXT: * _d_b += _r2; -+//CHECK-NEXT: } -+//CHECK-NEXT: } -+ -+ - #define TEST(F, x, y) \ - { \ - result[0] = 0; \ -@@ -872,4 +901,5 @@ int main() { - TEST(f16, 3, 4); // CHECK-EXEC: {16.00, 12.00} - VAR_TEST(f17, "i", 3, 4, 5, &result[0]); // CHECK-EXEC: {2.00} - VAR_TEST(f18, "i, j", 3, 4, 5, &result[0], &result[1]); // CHECK-EXEC: {3.00, 2.00} -+ TEST(f19, 1, 2); // CHECK-EXEC: {2.00, 2.00} - } diff --git a/interpreter/cling/tools/plugins/clad/patches/clad_local_array.patch b/interpreter/cling/tools/plugins/clad/patches/clad_local_array.patch deleted file mode 100644 index aa69b6bbdfca0..0000000000000 --- a/interpreter/cling/tools/plugins/clad/patches/clad_local_array.patch +++ /dev/null @@ -1,100 +0,0 @@ -From 4baee3583b36259809b361935df8b7d414a88898 Mon Sep 17 00:00:00 2001 -From: Garima Singh -Date: Wed, 19 Apr 2023 18:15:43 +0200 -Subject: [PATCH] Fill clad::array with 0s when assigned an empty brace-init - list. - ---- - include/clad/Differentiator/Array.h | 2 +- - test/Arrays/ArrayInputsReverseMode.C | 60 +++++++++++++++++++++++++++- - 2 files changed, 60 insertions(+), 2 deletions(-) - -diff --git a/include/clad/Differentiator/Array.h b/include/clad/Differentiator/Array.h -index 38445024..c5f7b464 100644 ---- a/include/clad/Differentiator/Array.h -+++ b/include/clad/Differentiator/Array.h -@@ -86,7 +86,7 @@ template class array { - /// Initializes the clad::array to the given array - CUDA_HOST_DEVICE array& operator=(T* arr) { - for (std::size_t i = 0; i < m_size; i++) -- m_arr[i] = arr[i]; -+ m_arr[i] = arr ? arr[i] : 0; - return *this; - } - /// Performs element wise addition -diff --git a/test/Arrays/ArrayInputsReverseMode.C b/test/Arrays/ArrayInputsReverseMode.C -index 0c38c680..381c3f4f 100644 ---- a/test/Arrays/ArrayInputsReverseMode.C -+++ b/test/Arrays/ArrayInputsReverseMode.C -@@ -322,6 +322,59 @@ double func5(int k) { - //CHECK-NEXT: * _d_k += _d_n; - //CHECK-NEXT: } - -+double func6(double seed) { -+ double sum = 0; -+ for (int i = 0; i < 3; i++) { -+ double arr[3] = {seed, seed * i, seed + i}; -+ sum += addArr(arr, 3); -+ } -+ return sum; -+} -+ -+//CHECK: void func6_grad(double seed, clad::array_ref _d_seed) { -+//CHECK-NEXT: double _d_sum = 0; -+//CHECK-NEXT: unsigned long _t0; -+//CHECK-NEXT: int _d_i = 0; -+//CHECK-NEXT: clad::tape _t1 = {}; -+//CHECK-NEXT: clad::tape _t2 = {}; -+//CHECK-NEXT: clad::array _d_arr(3UL); -+//CHECK-NEXT: clad::tape > _t3 = {}; -+//CHECK-NEXT: double sum = 0; -+//CHECK-NEXT: _t0 = 0; -+//CHECK-NEXT: for (int i = 0; i < 3; i++) { -+//CHECK-NEXT: _t0++; -+//CHECK-NEXT: double arr[3] = {seed, clad::push(_t2, seed) * clad::push(_t1, i), seed + i}; -+//CHECK-NEXT: clad::push(_t3, arr , 3UL); -+//CHECK-NEXT: sum += addArr(arr, 3); -+//CHECK-NEXT: } -+//CHECK-NEXT: double func6_return = sum; -+//CHECK-NEXT: goto _label0; -+//CHECK-NEXT: _label0: -+//CHECK-NEXT: _d_sum += 1; -+//CHECK-NEXT: for (; _t0; _t0--) { -+//CHECK-NEXT: { -+//CHECK-NEXT: double _r_d0 = _d_sum; -+//CHECK-NEXT: _d_sum += _r_d0; -+//CHECK-NEXT: clad::array _r3 = clad::pop(_t3); -+//CHECK-NEXT: int _grad1 = 0; -+//CHECK-NEXT: addArr_pullback(_r3, 3, _r_d0, _d_arr, &_grad1); -+//CHECK-NEXT: clad::array _r2(_d_arr); -+//CHECK-NEXT: int _r4 = _grad1; -+//CHECK-NEXT: _d_sum -= _r_d0; -+//CHECK-NEXT: } -+//CHECK-NEXT: { -+//CHECK-NEXT: * _d_seed += _d_arr[0]; -+//CHECK-NEXT: double _r0 = _d_arr[1] * clad::pop(_t1); -+//CHECK-NEXT: * _d_seed += _r0; -+//CHECK-NEXT: double _r1 = clad::pop(_t2) * _d_arr[1]; -+//CHECK-NEXT: _d_i += _r1; -+//CHECK-NEXT: * _d_seed += _d_arr[2]; -+//CHECK-NEXT: _d_i += _d_arr[2]; -+//CHECK-NEXT: _d_arr = {}; -+//CHECK-NEXT: } -+//CHECK-NEXT: } -+//CHECK-NEXT: } -+ - int main() { - double arr[] = {1, 2, 3}; - auto f_dx = clad::gradient(f); -@@ -357,5 +410,10 @@ int main() { - int dk = 0; - // Should evaluate to k*3 - df.execute(10, &dk); -- printf("Result = {%.2d}", dk); // CHECK-EXEC: Result = {30} -+ printf("Result = {%.2d}\n", dk); // CHECK-EXEC: Result = {30} -+ -+ auto localArray = clad::gradient(func6); -+ double dseed = 0; -+ localArray.execute(1, &dseed); -+ printf("Result = {%.2f}", dseed); // CHECK-EXEC: Result = {9.00} - }