-
Notifications
You must be signed in to change notification settings - Fork 69
Split pullbacks into separate files #244
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
33281b0
Split pullbacks into separate files
kshyatt 2b20afa
Fix StridedViews version for now
kshyatt fce1435
Fix ordering
kshyatt b20f806
Format
kshyatt 3a5ebad
Fix Project.toml
kshyatt 149de59
index magic
lkdvos 6f0aa85
ChainRules using common pullback functions
lkdvos c538e5a
only check public functions are included in docs
lkdvos 99fed35
update docstrings
lkdvos dc8fb44
cleaner syntax
lkdvos 914b47b
support complex _needs_tangent
lkdvos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| function tensoradd_pullback!(ΔC, ΔA, C, A, α, β, pA, conjA::Bool, ba...) | ||
|
kshyatt marked this conversation as resolved.
Outdated
|
||
| ipA = invperm(linearize(pA)) | ||
| ΔAc = eltype(ΔC) <: Complex && eltype(ΔA) <: Real ? zerovector(A, VectorInterface.promote_add(ΔC, α)) : ΔA | ||
| tensoradd!(ΔAc, ΔC, (ipA, ()), conjA, conjA ? α : conj(α), One(), ba...) | ||
| if eltype(ΔC) <: Complex && eltype(ΔA) <: Real | ||
| ΔA .+= real.(ΔAc) | ||
| end | ||
|
kshyatt marked this conversation as resolved.
Outdated
|
||
| Δα = if _needs_tangent(α) | ||
| tensorscalar( | ||
| tensorcontract( | ||
| A, ((), linearize(pA)), !conjA, | ||
| ΔC, (trivtuple(numind(pA)), ()), false, | ||
| ((), ()), One(), ba... | ||
| ) | ||
| ) | ||
| else | ||
| nothing | ||
| end | ||
| Δβ = if _needs_tangent(β) | ||
| tensorscalar( | ||
| tensorcontract( | ||
| C, ((), trivtuple(numind(pA))), true, | ||
| ΔC, (trivtuple(numind(pA)), ()), false, | ||
| ((), ()), One(), ba... | ||
| ) | ||
| ) | ||
| else | ||
| nothing | ||
| end | ||
| scale!(ΔC, conj(β)) | ||
| return ΔC, ΔA, Δα, Δβ | ||
| end | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| function tensorcontract_pullback!(ΔC, ΔA, ΔB, C, A, B, α, β, pA, pB, pAB, conjA::Bool, conjB::Bool, ba...) | ||
|
kshyatt marked this conversation as resolved.
Outdated
|
||
| ipAB = invperm(linearize(pAB)) | ||
| pdC = ( | ||
| TupleTools.getindices(ipAB, trivtuple(numout(pA))), | ||
| TupleTools.getindices(ipAB, numout(pA) .+ trivtuple(numin(pB))), | ||
| ) | ||
| ipA = (invperm(linearize(pA)), ()) | ||
| ipB = (invperm(linearize(pB)), ()) | ||
| conjΔC = conjA | ||
| conjB′ = conjA ? conjB : !conjB | ||
| ΔAc = eltype(ΔC) <: Complex && eltype(ΔA) <: Real ? zerovector(A, VectorInterface.promote_add(ΔC, α)) : ΔA | ||
| tensorcontract!( | ||
| ΔAc, | ||
| ΔC, pdC, conjΔC, | ||
| B, reverse(pB), conjB′, | ||
| ipA, | ||
| conjA ? α : conj(α), One(), ba... | ||
| ) | ||
| if eltype(ΔC) <: Complex && eltype(ΔA) <: Real | ||
| ΔA .+= real.(ΔAc) | ||
| end | ||
| conjΔC = conjB | ||
| conjA′ = conjB ? conjA : !conjA | ||
| ΔBc = eltype(ΔC) <: Complex && eltype(ΔB) <: Real ? zerovector(B, VectorInterface.promote_add(ΔC, α)) : ΔB | ||
| tensorcontract!( | ||
| ΔBc, | ||
| A, reverse(pA), conjA′, | ||
| ΔC, pdC, conjΔC, | ||
| ipB, | ||
| conjB ? α : conj(α), One(), ba... | ||
| ) | ||
| if eltype(ΔC) <: Complex && eltype(ΔB) <: Real | ||
| ΔB .+= real.(ΔBc) | ||
| end | ||
| Δα = if _needs_tangent(α) | ||
| C_αβ = tensorcontract(A, pA, conjA, B, pB, conjB, pAB, One(), ba...) | ||
| # TODO: consider using `inner` | ||
| tensorscalar( | ||
| tensorcontract( | ||
| C_αβ, ((), trivtuple(numind(pAB))), true, | ||
| ΔC, (trivtuple(numind(pAB)), ()), false, | ||
| ((), ()), One(), ba... | ||
| ) | ||
| ) | ||
| else | ||
| nothing | ||
| end | ||
| Δβ = if _needs_tangent(β) | ||
| # TODO: consider using `inner` | ||
| tensorscalar( | ||
| tensorcontract( | ||
| C, ((), trivtuple(numind(pAB))), true, | ||
| ΔC, (trivtuple(numind(pAB)), ()), false, | ||
| ((), ()), One(), ba... | ||
| ) | ||
| ) | ||
| else | ||
| nothing | ||
| end | ||
| scale!(ΔC, conj(β)) | ||
| return ΔC, ΔA, ΔB, Δα, Δβ | ||
| end | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| function tensortrace_pullback!(ΔC, ΔA, C, A, α, β, p, q, conjA, ba...) | ||
| ip = invperm((linearize(p)..., q[1]..., q[2]...)) | ||
| Es = map(q[1], q[2]) do i1, i2 | ||
| one( | ||
| TensorOperations.tensoralloc_add( | ||
| TensorOperations.scalartype(A), A, ((i1,), (i2,)), conjA | ||
| ) | ||
| ) | ||
| end | ||
| E = _kron(Es, ba) | ||
| ΔAc = eltype(ΔC) <: Complex && eltype(ΔA) <: Real ? zerovector(A, VectorInterface.promote_add(ΔC, α)) : ΔA | ||
| tensorproduct!( | ||
| ΔAc, ΔC, (trivtuple(numind(p)), ()), conjA, | ||
| E, ((), trivtuple(numind(q))), conjA, | ||
| (ip, ()), | ||
| conjA ? α : conj(α), One(), ba... | ||
| ) | ||
| if eltype(ΔC) <: Complex && eltype(ΔA) <: Real | ||
| ΔA .+= real.(ΔAc) | ||
| end | ||
| C_αβ = tensortrace(A, p, q, false, One(), ba...) | ||
| Δα = if _needs_tangent(α) | ||
| tensorscalar( | ||
| tensorcontract( | ||
| C_αβ, ((), trivtuple(numind(p))), | ||
| !conjA, | ||
| ΔC, (trivtuple(numind(p)), ()), false, | ||
| ((), ()), One(), ba... | ||
| ) | ||
| ) | ||
| else | ||
| nothing | ||
| end | ||
| Δβ = if _needs_tangent(β) | ||
| tensorscalar( | ||
| tensorcontract( | ||
| C, ((), trivtuple(numind(p))), true, | ||
| ΔC, (trivtuple(numind(p)), ()), false, | ||
| ((), ()), One(), ba... | ||
| ) | ||
| ) | ||
| else | ||
| nothing | ||
| end | ||
| scale!(ΔC, conj(β)) | ||
| return ΔC, ΔA, Δα, Δβ | ||
| end |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.