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

Invalid results in forward mode with the new C++ interface for a function returning a std::array #2086

Open
thelfer opened this issue Sep 26, 2024 · 2 comments

Comments

@thelfer
Copy link

thelfer commented Sep 26, 2024

The issue is probably due to my own inexperience.

The following snippet returns the value of the function rather than its derivative:

    const double x = 1.0;
    const double dx = 1.0;
    const auto c = [](const double& x) {
      return std::array{std::cos(x), 2*std::sin(x)};
    };
    const auto dfdx = ::enzyme::get<0>(enzyme::autodiff<::enzyme::Forward>(
        +c, ::enzyme::Duplicated<const double&>{x, dx}));
    std::cout << "dfdx:\n" << dfdx[0] << ", expected:  " << -std::sin(1) << "\n"
              << dfdx[1] << ", expected: " << 2 * std::cos(1) << std::endl;

In contrast, the old interface behaves correctly

   const auto dfdx_2 =
       __enzyme_fwddiff<std::array<double, 2>>(+c, enzyme_dup, &x, &dx);
   std::cout << "dfdx_2:\n" << dfdx_2[0] << ", expected:  " << -std::sin(1) << "\n"
             << dfdx_2[1] << ", expected: " << 2 * std::cos(1) << std::endl;

Any help would be appreciated ! @samuelpmish ?

P.S. enzyme::autodiff<::enzyme::Forward> works fine for scalar valuated functions.

@samuelpmish
Copy link
Collaborator

looking at the generated assembly, it shows that enzyme::autodiff is returning the value of the function in your example (not the derivative w.r.t. x):

https://fwd.gymni.ch/0Z2DlV

I'm not sure what the cause is, but we can look into it. I forget: is enzyme::forward(f, args) intended to return both {f, df} or just df?

@thelfer
Copy link
Author

thelfer commented Sep 26, 2024

@samuelpmish Thanks for watching.

In the scalar case, only df is returned, so I would expect the same in this case. This is also the case when using the raw __enzyme_fwddiff.

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

2 participants