EnzymeAD / EnzymeAD/Enzyme

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

Open
#2,086 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
LLVM
Stars
1.7k
Forks
188
Avg merge
1d 22h
Merged PRs (30d)
26

Description

The issue is probably due to my own inexperience.

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

~~~~c++
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{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

~~~~c++
const auto dfdx_2 =
__enzyme_fwddiff>(+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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.