Skip to content

Commit

Permalink
Don't mutate cotangent in rule for exp (#588)
Browse files Browse the repository at this point in the history
* Don't mutate input cotangent

* Increment patch number

* Add test for not mutating if cotangent is adjoint
  • Loading branch information
sethaxen committed Feb 11, 2022
1 parent 8108a77 commit aab91c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "1.26"
version = "1.26.1"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 1 addition & 1 deletion src/rulesets/LinearAlgebra/matfun.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function rrule(::typeof(exp), A0::StridedMatrix{<:BlasFloat})
# Ensures ∂X is mutable. The outer `adjoint` is unwrapped without copy by
# the default _matfun_frechet_adjoint!
ΔX = unthunk(X̄)
∂X = ChainRulesCore.is_inplaceable_destination(ΔX) ? ΔX : convert(Matrix, ΔX')'
∂X = ChainRulesCore.is_inplaceable_destination(ΔX) ? copy(ΔX) : convert(Matrix, ΔX')'
∂A = _matfun_frechet_adjoint!(exp, ∂X, A, X, intermediates)
return NoTangent(), ∂A
end
Expand Down
9 changes: 9 additions & 0 deletions test/rulesets/LinearAlgebra/matfun.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
Y, back = rrule(exp, A)
@maybe_inferred back(rand_tangent(Y))
end
@testset "cotangent not mutated" begin
# https://github.com/JuliaDiff/ChainRules.jl/issues/512
A = [1.0 2.0; 3.0 4.0]
Y, back = rrule(exp, A)
ΔY′ = rand_tangent(Y)'
ΔY′copy = copy(ΔY′)
back(ΔY′)
@test ΔY′ == ΔY′copy
end
@testset "imbalanced A" begin
A = Float64[0 10 0 0; -1 0 0 0; 0 0 0 0; -2 0 0 0]
test_rrule(exp, A; check_inferred=false)
Expand Down

2 comments on commit aab91c6

@sethaxen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/54470

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.26.1 -m "<description of version>" aab91c6775c631bfd8c9f3117897ee9d22c34fdb
git push origin v1.26.1

Please sign in to comment.