Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a particularly inelegant solution to #1821.
What I do here for
Symmetric
andHermitian
is take a redundant basis. This looks a bit strange because I both set the individual elements and also set the type of the resulting matrix. The reason for this is that if I do not set all the elements of the underlying buffer, Enzyme's autodiff returns the wrong result, but the wrapper type must also be set because currentlyBatchDuplicated
only allows arguments and bases to be of the same type. I also go out of my way to callsimilar
here (e.g. rather thanzeros
) to make it less likely we will run afoul ofBatchDuplicated
.I think this works in general but I have not exhaustively checked it. I'm not sure how enzyme would be expected to deal with complex arguments here, so I've only tested purely real cases so far.
To see an example of how it works, consider symmetric matrix$X$ and
$$f(X) = X_{11}^2 + 2 X_{12}^2 + 3 X_{21}^2 - X_{22}^2$$ $X = \left(\matrix{a & b \cr b & c}\right)$ so that
$$f(X) = a^2 + 5 b^2 - c^2$$ $b$ is clearly $10b$ .
For extra clarity, write
The derivative with respect to the off-diagonal element
I impelement this in Julia via
Then, for example
so that
This also works if the underlying data for
X
is[1.0 2.0; 0.0 3.0]
.This will remain a draft until I come up with more comprehensive tests for it.