EnzymeAD / EnzymeAD/Enzyme

Derivative through a view into a Simbody matrix fails

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

Description

The following example tries to take the derivative of function which sums elements of a Simbody `Matrix` accessed through a sub-vector of a view into a column of the matrix (i.e., a `VectorView`). This example was built with LLVM 18 with and Enzyme v0.0.157.

This example is part of my Simbody-Enzyme testing repo [here](https://github.com/nickbianco/SimbodyEnzymeSandbox/blob/main/src/exampleMatrixView.cpp). Simbody is compiled with [static libraries and LTO enabled](https://github.com/nickbianco/SimbodyEnzymeSandbox/blob/b295ce6e18ef8074f1c356cb6987f56d1c8d3a84/dependencies/CMakeLists.txt#L31). The executable is compiled with [`LLDEnzymeFlags` and `LLDEnzymePrintFlags`](https://github.com/nickbianco/SimbodyEnzymeSandbox/blob/b295ce6e18ef8074f1c356cb6987f56d1c8d3a84/src/CMakeLists.txt#L37C50-L37C84).

```c++
#include
#include "Simbody.h"

using namespace SimTK;

extern int enzyme_dup;

template
return_type __enzyme_fwddiff(void *, T...);

Real foo(const Matrix& mat) {
// Grab the second column as a VectorView and from that view,
// grab a subvector (another VectorView) from the second element
// to the third element. Finally, sum the elements of the subvector.
VectorView vec = mat.col(1)(1, 2);
return vec(0) + vec(1);
}

int main () {

Matrix mat = Test::randMatrix(4, 4);
Matrix dmat(4, 4, 0.0);
dmat(1, 1) = 1.0;
dmat(1, 2) = 1.0;

Real dfoo = __enzyme_fwddiff((void*)foo,
enzyme_dup, &mat, &dmat);

return 0;
}
```

Building this example fails during type analysis with the following error message:

```
[build] ld.lld: error: dependencies/install/simbody/include/simbody/SimTKcommon/internal/BigMatrix.h:217:26: in function preprocess__Z3fooRKN5SimTK7Matrix_IdEE double (ptr): Enzyme: Cannot deduce type of copy call void @llvm.memcpy.p0.p0.i64(ptr align 8 %45, ptr align 4 %3, i64 20, i1 false) #30, !dbg !14589, !tbaa.struct !14493, !noalias !14586
```

This error points to [this line in Simbody](https://github.com/simbody/simbody/blob/f853397efad00798a169ff3dd1c52f5e20a24ba9/SimTKcommon/BigMatrix/include/SimTKcommon/internal/BigMatrix.h#L217), at the construction of a "MatrixHelper" class used here to create a modifiable view into a block of the original matrix (presumably the same sub-vector as above). I can provide more output around the build error above for context if requested.

Adding `LLDEnzymeLooseTypeFlags` allows the executable to compile, but upon running I receive the error message "Attempting to call an indirect active function whose runtime value is inactive". Replacing foo with:

```
Real bar(const Matrix& mat) {
// Grab and sum the same elements as in foo(), but directly.
return mat.getElt(1, 1) + mat.getElt(1, 2);
}
```

compiles without `LLDEnzymeLooseTypeFlags`, but also produces "Attempting to call an indirect active function whose runtime value is inactive".

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.