-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from JuliaGPU/vc/norm
Fix norm
- Loading branch information
Showing
7 changed files
with
120 additions
and
6 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
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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Hacks that can be ripped out once we support device overrides | ||
|
||
function LinearAlgebra.norm(v::ROCArray{T}, p::Real=2) where {T} | ||
if p == Inf | ||
maximum(abs.(v)) | ||
elseif p == -Inf | ||
minimum(abs.(v)) | ||
else | ||
mapreduce(x->AMDGPU.pow(AMDGPU.abs(x), p), +, v; init=zero(T))^(1/p) | ||
end | ||
end | ||
|
||
@inline function ssqs(x::T, y::T) where T<:Real | ||
k::Int = 0 | ||
ρ = x*x + y*y | ||
# FIXME: isfinite, isinf returns i32 | ||
if isfinite(ρ) == Int32(0) && (isinf(x) == Int32(1) || isinf(y) == Int32(1)) | ||
ρ = convert(T, Inf) | ||
elseif isinf(ρ) == Int32(1) || (ρ==0 && (x!=0 || y!=0)) || ρ<nextfloat(zero(T))/(2*eps(T)^2) | ||
m::T = max(abs(x), abs(y)) | ||
k = m==0 ? m : exponent(m) | ||
xk, yk = ldexp(x,-k), ldexp(y,-k) | ||
ρ = xk*xk + yk*yk | ||
end | ||
ρ, k | ||
end | ||
|
||
@inline function sqrt(z::Complex) | ||
z = float(z) | ||
x, y = reim(z) | ||
if x==y==0 | ||
return Complex(zero(x),y) | ||
end | ||
ρ, k::Int = ssqs(x, y) | ||
# FIXME: isfinite returns i32 | ||
if isfinite(x) == Int32(1) | ||
ρ=ldexp(abs(x),-k)+sqrt(ρ) | ||
end | ||
if isodd(k) | ||
k = div(k-1,2) | ||
else | ||
k = div(k,2)-1 | ||
ρ += ρ | ||
end | ||
ρ = ldexp(sqrt(ρ),k) #sqrt((abs(z)+abs(x))/2) without over/underflow | ||
ξ = ρ | ||
η = y | ||
if ρ != 0 | ||
# FIXME: isfinite returns i32 | ||
if isfinite(η) == Int32(1) | ||
η=(η/ρ)/2 | ||
end | ||
if x<0 | ||
ξ = abs(η) | ||
η = copysign(ρ,y) | ||
end | ||
end | ||
Complex(ξ,η) | ||
end | ||
|
||
|
||
import Statistics | ||
function Statistics.corzm(x::ROCArray{<:Any, 2}, vardim::Int=1) | ||
c = Statistics.unscaled_covzm(x, vardim) | ||
return Statistics.cov2cor!(c, sqrt.(LinearAlgebra.diag(c))) | ||
end |
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
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
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