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

Incorrect derivative produced when array derivative elements needs to be accessed in expressions without any corresponding dfdx #465

Closed
parth-07 opened this issue Jun 27, 2022 · 3 comments

Comments

@parth-07
Copy link
Collaborator

parth-07 commented Jun 27, 2022

Reproducible example:

double sq(double& elem) {
  elem = elem * elem;
  return elem;
}

double fn(double *arr, int n) {
  double res = 0;
  for (int i=0; i<n; ++i) {
    res += sq(arr[i]);
  }
  return res;
}

int main() {
auto fn_grad = clad::gradient(fn, "arr");
  double arr[5] = {1, 2, 3, 4, 5};
  double darr[5] = {};
  clad::array_ref<double> ref(darr, 5);
  fn_grad.execute(arr, 5, ref);
  for (int i=0; i < 5; ++i)
    std::cout<<darr[I]<<" ";
}

Actual Output: 4282 0 0 0 0
Expected Output: 2 4 6 8 10

@parth-07 parth-07 changed the title Incorrect derivative produced when array derivative elements needs to accessed in expressions without any corresponding dfdx Incorrect derivative produced when array derivative elements needs to be accessed in expressions without any corresponding dfdx Jun 27, 2022
@ro4i7
Copy link

ro4i7 commented Mar 11, 2023

Hello @parth-07 @vgvassilev

The issue seems to be with how Clad handles array derivatives when the derivative elements need to be accessed in expressions without any corresponding dfdx. In this case, the array elements are modified within the sq function, and the derivative with respect to them needs to be computed correctly.

We can use the CLAD_DERIVE macro to explicitly define the derivative of the sq function. This can be done as follows:

CLAD_DEFINE_DERIVATIVE(sq, (double& elem), {
  return 2 * elem;
});

Here, we define the derivative of sq to be 2 * elem. This corresponds to the derivative of elem * elem with respect to elem.

With this change, the expected output is obtained.

double sq(double& elem) {
  elem = elem * elem;
  return elem;
}

CLAD_DEFINE_DERIVATIVE(sq, (double& elem), {
  return 2 * elem;
});

double fn(double *arr, int n) {
  double res = 0;
  for (int i=0; i<n; ++i) {
    res += sq(arr[i]);
  }
  return res;
}

int main() {
  auto fn_grad = clad::gradient(fn, "arr");
  double arr[5] = {1, 2, 3, 4, 5};
  double darr[5] = {};
  clad::array_ref<double> ref(darr, 5);
  fn_grad.execute(arr, 5, ref);
  for (int i=0; i < 5; ++i)
    std::cout<<darr[i]<<" "; // outputs "2 4 6 8 10"
}

By defining the derivative of sq, we ensure that Clad computes the correct derivative with respect to arr[i]

@parth-07
Copy link
Collaborator Author

@ro4i7

This issue focuses on how the given function can be automatically differentiated by Clad.

@ShounakDas101
Copy link
Contributor

Screenshot from 2023-04-26 11-49-01
This is the output for my changes

PetroZarytskyi added a commit to PetroZarytskyi/clad that referenced this issue Dec 1, 2023
vgvassilev pushed a commit to PetroZarytskyi/clad that referenced this issue Dec 1, 2023
This patch request optimizes storing and restoring in the reverse mode of Clad
and introduces TBR analysis to determine when variables should be stored.

Fixes vgvassilev#465, vgvassilev#441, vgvassilev#439. Partially resolves vgvassilev#429 and vgvassilev#606.
vgvassilev pushed a commit to PetroZarytskyi/clad that referenced this issue Dec 1, 2023
This patch optimizes storing and restoring in the reverse mode of Clad and
introduces TBR analysis to determine when variables should be stored.

Fixes vgvassilev#465, vgvassilev#441, vgvassilev#439. Partially resolves vgvassilev#429 and vgvassilev#606.
vgvassilev pushed a commit to PetroZarytskyi/clad that referenced this issue Dec 1, 2023
This patch optimizes storing and restoring in the reverse mode of Clad and
introduces TBR analysis to determine when variables should be stored.

Fixes vgvassilev#465, vgvassilev#441, vgvassilev#439, vgvassilev#429. Partially resolves vgvassilev#606.
vgvassilev pushed a commit to PetroZarytskyi/clad that referenced this issue Dec 1, 2023
This patch optimizes storing and restoring in the reverse mode of Clad and
introduces TBR analysis to determine when variables should be stored.

Fixes vgvassilev#465, vgvassilev#441, vgvassilev#439, vgvassilev#429. Partially resolves vgvassilev#606.
vgvassilev pushed a commit to PetroZarytskyi/clad that referenced this issue Dec 1, 2023
This patch optimizes storing and restoring in the reverse mode of Clad and
introduces TBR analysis to determine when variables should be stored.

Fixes vgvassilev#465, vgvassilev#441, vgvassilev#439, vgvassilev#429. Partially resolves vgvassilev#606.
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

No branches or pull requests

3 participants