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

Bug: Adding dummy loop breaks gradient in special circumstances #635

Closed
guitargeek opened this issue Oct 5, 2023 · 1 comment · Fixed by #640
Closed

Bug: Adding dummy loop breaks gradient in special circumstances #635

guitargeek opened this issue Oct 5, 2023 · 1 comment · Fixed by #640

Comments

@guitargeek
Copy link
Contributor

guitargeek commented Oct 5, 2023

Simply adding a dummy loop with one iteration breaks certain gradients.

These minimal conditions are required to reproduce the problem:

  1. A loop
  2. Some "repacking" parameters from an array in a new array (commonly done in RooFit when forwarding subsets of parameters)
  3. Doing some calculations with the repacked parameters in another function

My setup: Arch Linux with Clad master and clang 16.06.

I hope this problem can be fixed, as right now it is blocking me from implementing numeric integration routines in RooFit (similar to the reproducer, but the loop is not trivial).

Reproducer:

// Compile with clang++ -std=c++11 -fplugin=/usr/lib/clad.so cladIssue.cxx -o cladIssue
// Add the -DFIXED to see the fixed version

#include "clad/Differentiator/Differentiator.h"

#include <iostream>
#include <vector>

double func(double *params)
{
   return 1.0 / (params[0] * params[0]);
}

double wrapper(double *params)
{
   double out = 0.0;

#ifndef FIXED
   for (std::size_t i = 0; i < 1; ++i)
#else
#endif
   {
      double paramsPrime[] = {params[0]};
      out = out + func(paramsPrime);
   }

   return out;
}

int main()
{
   auto grad = clad::gradient(wrapper, "params");

   double x = 2.0;
   double result = 0.0;
   grad.execute(&x, &result);

   std::cout << result << std::endl;
}
@vaithak
Copy link
Collaborator

vaithak commented Oct 14, 2023

Hi @guitargeek,
There was an issue in the code generated for for-loops containing a function call with an array as a parameter.
I have verified that the above code works after the fix proposed in #640.

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 a pull request may close this issue.

2 participants