-
Notifications
You must be signed in to change notification settings - Fork 66
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
inelegant solution to #1821 #1934
base: main
Are you sure you want to change the base?
Conversation
Alright, this looks like it works. Again, while this may be a fix this solution is not very nice and involves a lot of redundancy, but I think anything better would be significantly more complicated. I certainly would advise giving this a good think before merging. Keep in mind that if a user is really determined, they can create this on their own using only the existing public API ( |
idx = idxs[i] | ||
res = similar(parent(x)) | ||
for idx2 ∈ CartesianIndices(x) | ||
@inbounds res[idx2] = _is_symmed_index(idx, idx2) ? 1 : 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need the check if symmed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is checking if it's either the index idx
or its transpose. In my testing enzyme would return the wrong result if I did not set both elements (as if I had not wrapped it in Symmetric
even though I had). I don't understand why that is.
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.