Skip to content
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

Implement p-norm. #350

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
AbstractFFTs = "0.4, 0.5, 1.0"
Adapt = "2.0, 3.0"
julia = "1.5"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
13 changes: 13 additions & 0 deletions src/host/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,16 @@ end
# TODO: implementation without the memory copy
LinearAlgebra.permutedims!(dest::AbstractGPUArray, src::AbstractGPUArray, perm) =
permutedims!(dest, src, Tuple(perm))


## norm

function LinearAlgebra.norm(v::AbstractGPUArray{T}, p::Real=2) where {T}
if p == Inf
maximum(abs.(v))
elseif p == -Inf
minimum(abs.(v))
else
mapreduce(x->abs(x)^p, +, v; init=zero(T))^(1/p)
end
end
6 changes: 6 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
9 changes: 8 additions & 1 deletion test/testsuite/linalg.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@testsuite "linalg" AT->begin
@testset "adjoint and transpose" begin
@testset "adjoint and trspose" begin
@test compare(adjoint, AT, rand(Float32, 32, 32))
@test compare(adjoint!, AT, rand(Float32, 32, 32), rand(Float32, 32, 32))
@test compare(transpose, AT, rand(Float32, 32, 32))
Expand Down Expand Up @@ -121,4 +121,11 @@ end
@test compare(rmul!, AT, rand(T, a), Ref(rand(T)))
@test compare(lmul!, AT, Ref(rand(T)), rand(T, b))
end

@testset "$p-norm($sz x $T)" for sz in [(2,), (2,2), (2,2,2)],
p in Any[1, 2, 3, Inf, -Inf],
T in supported_eltypes()
range = T <: Integer ? (T(1):T(10)) : T # prevent integer overflow
@test compare(norm, AT, rand(range, sz), Ref(p))
end
end