-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
only do minimal change to rule for \ to convert to array
Also make second Y not scalar more coercing some things into arrays some of the time cleaner def with a helper function
- Loading branch information
Showing
2 changed files
with
53 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -343,28 +343,71 @@ function rrule(::typeof(\), A::AbstractVecOrMat{<:Real}, B::AbstractVecOrMat{<:R | |
|
||
Y = A \ B | ||
|
||
Atf = factorize(A') | ||
|
||
function backslash_pullback(ȳ) | ||
Ȳ = unthunk(ȳ) | ||
|
||
Ȳf = Ȳ | ||
@static if VERSION >= v"1.9" | ||
# Need to ensure Ȳ is an array since since https://github.com/JuliaLang/julia/pull/44358 | ||
Ȳ isa AbstractArray || Ȳ = [Ȳ] | ||
if !isa(Ȳ, AbstractArray) | ||
Ȳf = [Ȳ] | ||
end | ||
end | ||
Yf = Y | ||
@static if VERSION >= v"1.9" | ||
# Need to ensure Yf is an array since since https://github.com/JuliaLang/julia/pull/44358 | ||
if !isa(Y, AbstractArray) | ||
Yf = [Y] | ||
end | ||
end | ||
Atf = factorize(A') | ||
#@info "vars" typeof(Ȳ) typeof(Y) typeof(Yf) typeof(A) typeof(B) | ||
∂A = @thunk begin | ||
B̄ = Atf \ Ȳ | ||
B̄ = A' \ Ȳf | ||
Ā = -B̄ * Y' | ||
Ā = add!!(Ā, ((B - A * Y) * B̄') / Atf) | ||
Ā = add!!(Ā, Atf \ Y * (Ȳ' - B̄'A)) | ||
t = (B - A * Y) * B̄' | ||
@static if VERSION >= v"1.9" | ||
# Need to ensure t is an array since since https://github.com/JuliaLang/julia/pull/44358 | ||
if !isa(t, AbstractArray) | ||
t = [t] | ||
end | ||
end | ||
Ā = add!!(Ā, t / A') | ||
Ā = add!!(Ā, A' \ Yf * (Ȳ' - B̄'A)) | ||
project_A(Ā) | ||
end | ||
∂B = @thunk project_B(Atf \ Ȳ) | ||
∂B = @thunk project_B(A' \ Ȳf) | ||
return NoTangent(), ∂A, ∂B | ||
end | ||
return Y, backslash_pullback | ||
end | ||
|
||
@static if VERSION >= v"1.9" | ||
# Need to ensure things are not scalar since since https://github.com/JuliaLang/julia/pull/44358 | ||
_maybe_descalar(x) = x isa AbstractArray ? x : [x] | ||
else | ||
_maybe_descalar(x) = x | ||
end | ||
|
||
function rrule(A::AbstractVecOrMat{<:Real}, B::AbstractVecOrMat{<:Real}) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
oxinabox
Author
Member
|
||
Y = A \ B | ||
|
||
|
||
function backslash_pullback(ȳ) | ||
Ȳ = unthunk(ȳ) | ||
|
||
∂A = @thunk begin | ||
B̄ = A' \ _maybe_descalar(Ȳ) | ||
Ā = -B̄ * Y' | ||
Ā += _maybe_descalar((B - A * Y) * B̄') / A' | ||
Ā += (A' \ _maybe_descalar(Y)) * (Ȳ' - B̄'A) | ||
(Ā) | ||
end | ||
∂B = @thunk (A' \ _maybe_descalar(Ȳ)) | ||
return ∂A, ∂B | ||
end | ||
return Y, backslash_pullback | ||
end | ||
|
||
##### | ||
##### `\`, `/` matrix-scalar_rule | ||
##### | ||
|
This file contains 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
I'm definitely missing the context of this PR sorry, but just wondering what the intention is with this definition and if its not possibly accidentally missing something? Afaik
(A::AbstractVecOrMat{<:Real})(B::AbstractVecOrMat{<:Real})
isn't defined in Julia and it certainly doesn't correspond toA \ B
as line 392 suggests no? Ran into this because I do have some(A::CustomArray)(B::CustomArray)
defined and this broke that (which I could fix, but when I tracked it down saw this and I can't quite make sense of it). @oxinabox