Fix complex vjps for several unary ops#3766
Open
obchain wants to merge 1 commit into
Open
Conversation
Square, Sin, Sinh, Cosh, Tan, Tanh, and Log1p delegated their vjp to their jvp. For a holomorphic f the jvp is f'(z) * t, but the vjp must be cotangent * conj(f'(z)); delegating dropped the conjugate, so the gradient through these ops was wrong for complex inputs (e.g. the grad of Re(z**2) was 2*z instead of 2*conj(z)). Conjugate the cotangent into the jvp and conjugate the result, which yields cotangent * conj(f'(z)) and is a no-op for real inputs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Proposed changes
Fixes #3765.
Square,Sin,Sinh,Cosh,Tan,Tanh, andLog1pdelegated theirvjpto theirjvp. For a holomorphicfthe jvp isf'(z) * t, but the vjp must becotangent * conj(f'(z))(the convention already used byExp/Log). Delegating dropped the conjugate, so gradients through these ops were wrong for complex inputs — e.g. the gradient ofRe(z**2)was2*zinstead of2*conj(z).Each vjp now conjugates the cotangent, runs the jvp, and conjugates the result — i.e.
conj(jvp(conj(w)))=w * conj(f'(z))— which is a no-op for real inputs (so real gradients and the jvps are unchanged).Verified each op's complex gradient against finite differences and against the hand-computed
cotangent * conj(f'(z)); real gradients and all jvps are unchanged. Addedtest_complex_unary_vjps.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes