Skip to content

Commit

Permalink
normalize not just vectors (#602)
Browse files Browse the repository at this point in the history
* normalise arrays not just vectors

* versions
  • Loading branch information
mcabbott authored May 7, 2022
1 parent 2cc27e2 commit c5dbe03
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 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.28.4"
version = "1.29.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
4 changes: 2 additions & 2 deletions src/rulesets/LinearAlgebra/norm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ end
##### `normalize`
#####

function rrule(::typeof(normalize), x::AbstractVector{<:Number}, p::Real)
function rrule(::typeof(normalize), x::AbstractArray{<:Number}, p::Real)
nrm, inner_pullback = rrule(norm, x, p)
Ty = typeof(first(x) / nrm)
y = copyto!(similar(x, Ty), x)
Expand All @@ -273,7 +273,7 @@ function rrule(::typeof(normalize), x::AbstractVector{<:Number}, p::Real)
return y, normalize_pullback
end

function rrule(::typeof(normalize), x::AbstractVector{<:Number})
function rrule(::typeof(normalize), x::AbstractArray{<:Number})
nrm = LinearAlgebra.norm2(x)
Ty = typeof(first(x) / nrm)
y = copyto!(similar(x, Ty), x)
Expand Down
12 changes: 9 additions & 3 deletions test/rulesets/LinearAlgebra/norm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,21 @@ end
# ===================================

@testset "normalize" begin
@testset "x::Vector{$T}" for T in (Float64, ComplexF64)
@testset "x::Array{$T}" for T in (Float64, ComplexF64)
x = randn(T, 3)
test_rrule(normalize, x)
@test rrule(normalize, x)[2](ZeroTangent()) === (NoTangent(), ZeroTangent())

test_rrule(normalize, rand(T, 3, 4))
test_rrule(normalize, adjoint(rand(T, 5)))
end
@testset "x::Vector{$T}, p=$p" for T in (Float64, ComplexF64),
p in (1.0, 2.0, -Inf, Inf, 2.5) # skip p=0, since FD is unstable
@testset "x::Array{$T}, p=$p" for T in (Float64, ComplexF64), p in (1.0, 2.0, -Inf, Inf, 2.5)
# skip p=0, since FD is unstable
x = randn(T, 3)
test_rrule(normalize, x, p)
@test rrule(normalize, x, p)[2](ZeroTangent()) === (NoTangent(), ZeroTangent(), ZeroTangent())

test_rrule(normalize, rand(T, 3, 4), p)
test_rrule(normalize, adjoint(rand(T, 5)), p)
end
end

2 comments on commit c5dbe03

@mcabbott
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/59853

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.29.0 -m "<description of version>" c5dbe030af390599848830ff43a5dffc04be69e2
git push origin v1.29.0

Please sign in to comment.