Skip to content
Open
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
11 changes: 10 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
cuBLAS = "182d3088-87b7-4494-8cad-fc6afaa545bc"
CUDACore = "bd0ed864-bdfe-4181-a5ed-ce625a5fdea2"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
GPUArrays = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[extensions]
VectorInterfaceChainRulesCoreExt = "ChainRulesCore"
VectorInterfaceCUDACoreExt = "CUDACore"
VectorInterfacecuBLASExt = "cuBLAS"
VectorInterfaceEnzymeExt = "Enzyme"
VectorInterfaceGPUArraysExt = "GPUArrays"
VectorInterfaceMooncakeExt = "Mooncake"
VectorInterfaceStaticArraysExt = "StaticArrays"

Expand All @@ -23,7 +29,10 @@ projects = ["test"]

[compat]
ChainRulesCore = "1"
Enzyme = "0.13.131"
CUDACore = "6"
cuBLAS = "6"
Enzyme = "0.13.220"
GPUArrays = "12.3"
LinearAlgebra = "1"
Mooncake = "0.5"
StaticArrays = "1"
Expand Down
9 changes: 9 additions & 0 deletions ext/VectorInterfaceCUDACoreExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module VectorInterfaceCUDACoreExt

using VectorInterface, CUDACore

import CUDACore: CuRef

CUDACore.CuRef{T}(x::VectorInterface.Scalar{T, <:CuVector{T}}) where {T} = Base.unsafe_convert(CuRef{T}, pointer(x.a))

end
20 changes: 16 additions & 4 deletions ext/VectorInterfaceEnzymeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ function EnzymeRules.reverse(
α::Annotation{<:Number},
) where {RT}
αval, Cval = cache
Δα = if !isa(α, Const) && !is_inactive(config, C)
Δα = if isa(α, Duplicated) && !is_inactive(config, C)
α.dval .+= project_scalar(α.val, inner(Cval, C.dval))
nothing
elseif !isa(α, Const) && !is_inactive(config, C)
project_scalar(α.val, inner(Cval, C.dval))
elseif !isa(α, Const)
zero(α.val)
Expand Down Expand Up @@ -127,7 +130,10 @@ function EnzymeRules.reverse(
) where {RT}
Aval, αval = cache
!is_inactive(config, A) && !is_inactive(config, C) && project_add!(A.dval, C.dval, conj(αval))
Δα = if !isa(α, Const) && !is_inactive(config, C)
Δα = if isa(α, Duplicated) && !is_inactive(config, C)
α.dval .+= project_scalar(α.val, inner(Aval, C.dval))
nothing
elseif !isa(α, Const) && !is_inactive(config, C)
project_scalar(α.val, inner(Aval, C.dval))
elseif !isa(α, Const)
zero(α.val)
Expand Down Expand Up @@ -194,14 +200,20 @@ function EnzymeRules.reverse(
β::Annotation{<:Number},
) where {RT}
Aval, αval, βval, Cval = cache
Δα = if !isa(α, Const) && !is_inactive(config, C)
Δα = if isa(α, Duplicated) && !is_inactive(config, C)
α.dval .+= project_scalar(α.val, inner(Aval, C.dval))
nothing
elseif !isa(α, Const) && !is_inactive(config, C)
project_scalar(α.val, inner(Aval, C.dval))
elseif !isa(α, Const)
zero(α.val)
else
nothing
end
Δβ = if !isa(β, Const) && !is_inactive(config, C)
Δβ = if isa(β, Duplicated) && !is_inactive(config, C)
β.dval .+= project_scalar(β.val, inner(Cval, C.dval))
nothing
elseif !isa(β, Const) && !is_inactive(config, C)
project_scalar(β.val, inner(Cval, C.dval))
elseif !isa(β, Const)
zero(β.val)
Expand Down
7 changes: 7 additions & 0 deletions ext/VectorInterfaceGPUArraysExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module VectorInterfaceGPUArraysExt

using VectorInterface, GPUArrays

GPUArrays.blas_scalar(::Type{T}, s::VectorInterface.Scalar{T, <:AnyGPUVector{T}}) where {T} = s

end
33 changes: 25 additions & 8 deletions ext/VectorInterfaceMooncakeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ _needs_tangent(x) = _needs_tangent(typeof(x))
_needs_tangent(::Type{T}) where {T <: Number} =
Mooncake.rdata_type(Mooncake.tangent_type(T)) !== NoRData

scalar_tangent(Δα) = Δα
scalar_tangent(Δα::Mooncake.MutableTangent{<:NamedTuple{(:a,)}}) = Scalar(Δα.fields.a)

function scalar_rdata!(α_Δα::CoDual{<:Number}, f)
α = primal(α_Δα)
return _needs_tangent(α) ? project_scalar(α, f()) : NoRData()
end
function scalar_rdata!(α_Δα::CoDual{<:Scalar}, f)
α, Δα = extract(α_Δα)
Δα.fields.a .+= project_scalar(zero(scalartype(α)), f())
return NoRData()
end

# scale
# -----
@is_primitive DefaultCtx Tuple{typeof(scale!), AbstractArray, Number}
Expand All @@ -44,7 +57,7 @@ function Mooncake.rrule!!(::CoDual{typeof(scale!)}, C_ΔC::CoDual, α_Δα::CoDu

function scale_pullback(::NoRData)
copy!(C, C_cache)
Δαr = _needs_tangent(α) ? project_scalar(α, inner(C, ΔC)) : NoRData()
Δαr = scalar_rdata!(α_Δα, () -> inner(C, ΔC))
scale!(ΔC, conj(α))
return NoRData(), NoRData(), Δαr
end
Expand All @@ -55,7 +68,8 @@ end
function Mooncake.frule!!(::Dual{typeof(scale!)}, C_ΔC::Dual, α_Δα::Dual{<:Number})
# prepare arguments
C, ΔC = arrayify(C_ΔC)
α, Δα = extract(α_Δα)
α = primal(α_Δα)
Δα = scalar_tangent(Mooncake.tangent(α_Δα))

if !isa(Δα, NoTangent)
add!(ΔC, C, Δα, α)
Expand All @@ -82,7 +96,7 @@ function Mooncake.rrule!!(::CoDual{typeof(scale!)}, C_ΔC::CoDual, A_ΔA::CoDual
function scale_pullback(::NoRData)
copy!(C, C_cache)
project_add!(ΔA, ΔC, conj(α))
Δαr = _needs_tangent(α) ? project_scalar(α, inner(A, ΔC)) : NoRData()
Δαr = scalar_rdata!(α_Δα, () -> inner(A, ΔC))
zerovector!(ΔC)
return NoRData(), NoRData(), NoRData(), Δαr
end
Expand All @@ -94,7 +108,8 @@ function Mooncake.frule!!(::Dual{typeof(scale!)}, C_ΔC::Dual, A_ΔA::Dual, α_
# prepare arguments
C, ΔC = arrayify(C_ΔC)
A, ΔA = arrayify(A_ΔA)
α, Δα = extract(α_Δα)
α = primal(α_Δα)
Δα = scalar_tangent(Mooncake.tangent(α_Δα))

scale!(ΔC, ΔA, α)
!isa(Δα, NoTangent) && add!(ΔC, A, Δα, One())
Expand All @@ -121,8 +136,8 @@ function Mooncake.rrule!!(::CoDual{typeof(add!)}, C_ΔC::CoDual, A_ΔA::CoDual,
function add_pullback(::NoRData)
copy!(C, C_cache)

Δαr = _needs_tangent(α) ? project_scalar(α, inner(A, ΔC)) : NoRData()
Δβr = _needs_tangent(β) ? project_scalar(β, inner(C, ΔC)) : NoRData()
Δαr = scalar_rdata!(α_Δα, () -> inner(A, ΔC))
Δβr = scalar_rdata!(β_Δβ, () -> inner(C, ΔC))
project_add!(ΔA, ΔC, conj(α))
scale!(ΔC, conj(β))

Expand All @@ -136,8 +151,10 @@ function Mooncake.frule!!(::Dual{typeof(add!)}, C_ΔC::Dual, A_ΔA::Dual, α_Δ
# prepare arguments
C, ΔC = arrayify(C_ΔC)
A, ΔA = arrayify(A_ΔA)
α, Δα = extract(α_Δα)
β, Δβ = extract(β_Δβ)
α = primal(α_Δα)
Δα = scalar_tangent(Mooncake.tangent(α_Δα))
β = primal(β_Δβ)
Δβ = scalar_tangent(Mooncake.tangent(β_Δβ))
add!(ΔC, ΔA, α, β)
!isa(Δα, NoTangent) && add!(ΔC, A, Δα, One())
!isa(Δβ, NoTangent) && add!(ΔC, C, Δβ, One())
Expand Down
31 changes: 31 additions & 0 deletions ext/VectorInterfacecuBLASExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module VectorInterfacecuBLASExt

using VectorInterface, cuBLAS, LinearAlgebra
using cuBLAS.CUDACore: CuVector, StridedCuVector, DenseCuArray, GPUArrays

const cuBLASVector{T <: LinearAlgebra.BlasFloat} = Union{DenseCuArray{T}, StridedCuVector{T}}

function VectorInterface.inner(x::cuBLASVector{T}, y::cuBLASVector{T}) where {T <: Real}
n = length(x)
n == length(y) || throw(DimensionMismatch("dot product arguments have lengths $(length(x)) and $(length(y))"))
result = Scalar(similar(x, 1))
cuBLAS.dot(n, x, y, result)
return result
end

function VectorInterface.inner(x::cuBLASVector{T}, y::cuBLASVector{T}) where {T <: Complex}
n = length(x)
n == length(y) || throw(DimensionMismatch("dot product arguments have lengths $(length(x)) and $(length(y))"))
result = Scalar(similar(x, 1))
cuBLAS.dotc(n, x, y, result)
return result
end

function VectorInterface.innernorm(x::cuBLASVector)
n = length(x)
result = Scalar(similar(x, real(eltype(x)), 1))
cuBLAS.nrm2(length(x), x, result)
return result
end

end
11 changes: 6 additions & 5 deletions src/VectorInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ module VectorInterface
using LinearAlgebra: LinearAlgebra, norm, BlasFloat

# General interface: export
export scalartype
export scalartype, Scalar
export zerovector, zerovector!, zerovector!!
export scale, scale!, scale!!
export add, add!, add!!
export inner, norm
export inner, norm, innernorm
export One, Zero

include("interface.jl")

# Auxiliary type for representing constant 1 or 0 in vector addition / linear combinations
include("onezero.jl")
include("scalar.jl")

# Auxiliary methods for determining types
promote_scale(x, α::Number) = promote_scale(scalartype(x), typeof(α))
promote_scale(x, α::Number) = promote_scale(scalartype(x), scalartype(α))
promote_scale(::Type{Tx}, ::Type{Tα}) where {Tx, Tα <: Number} = Base.promote_op(scale, Tx, Tα)
promote_scale(x, y, α::Number) = promote_scale(scalartype(x), scalartype(y), typeof(α))
promote_scale(x, y, α::Number) = promote_scale(scalartype(x), scalartype(y), scalartype(α))
function promote_scale(::Type{Tx}, ::Type{Ty}, ::Type{Tα}) where {Tx, Ty, Tα <: Number}
return Base.promote_op(scale, Tx, Ty, Tα)
end

function promote_add(x, y, α::Number = One(), β::Number = One())
return promote_add(scalartype(x), scalartype(y), typeof(α), typeof(β))
return promote_add(scalartype(x), scalartype(y), scalartype(α), scalartype(β))
end
function promote_add(
::Type{Ty}, ::Type{Tx},
Expand Down
50 changes: 37 additions & 13 deletions src/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ const BLASVector{T <: BlasFloat} = Union{DenseArray{T}, StridedVector{T}}
#------------
scalartype(::Type{A}) where {T, A <: AbstractArray{T}} = scalartype(T) # recursive

_blasscalar(::Type{T}, α::Number) where {T} = convert(T, α)
_blasscalar(::Type{T}, α::Scalar{T}) where {T} = α
_blasscalar(::Type{T}, α::Scalar) where {T} = Scalar(convert.(T, α.a))

# When broadcasting over an array of nested vectors, scalars (in particular a `Scalar`,
# whose `broadcastable` is its underlying array) must be passed whole to each element.
_bc(::AbstractArray{<:Number}, α) = α
_bc(::AbstractArray, α) = Ref(α)

# zerovector & zerovector!!
#---------------------------
# because broadcasting treats zero-dimensional arrays special
Expand Down Expand Up @@ -36,14 +45,18 @@ zerovector!!(x::AbstractArray) = zerovector!(x)
# scale, scale! & scale!!
#-------------------------
# because broadcasting treats zero-dimensional arrays special, use map

scale(x::BLASVector, α::Scalar) = x .* α
# handle 0-dimensional arrays properly
scale(x::DenseArray{T, 0}, α::Scalar) where {T} = map(Base.Fix2(scale, α), x)
scale(x::AbstractArray, α::Number) = map(Base.Fix2(scale, α), x)
function scale!(x::AbstractArray, α::Number)
α === One() && return x
x .= scale!!.(x, α)
x .= scale!!.(x, _bc(x, α))
return x
end
function scale!(y::AbstractArray, x::AbstractArray, α::Number)
y .= scale!!.(y, x, α)
y .= scale!!.(y, x, _bc(y, α))
return y
end

Expand All @@ -52,20 +65,27 @@ function scale!!(x::AbstractArray, α::Number)
if promote_scale(x, α) <: scalartype(x)
return scale!(x, α)
else
return scale!!.(x, α)
return scale!!.(x, _bc(x, α))
end
end
function scale!!(y::AbstractArray, x::AbstractArray, α::Number)
if promote_scale(y, x, α) <: scalartype(y)
return scale!(y, x, α)
else
return scale!!.(y, x, α)
return scale!!.(y, x, _bc(y, α))
end
end

# add, add! & add!!
#-------------------
# because broadcasting treats zero-dimensional arrays special, use map
function add(y::BLASVector, x::BLASVector, α::Number, β::Number)
return @. β * y + α * x
end
# resolve ambiguity and handle 0-dimensional arrays properly
function add(y::DenseArray{T, 0}, x::BLASVector, α::Number, β::Number) where {T}
_add(y, x) = add(y, x, α, β)
return map(_add, y, x)
end
function add(y::AbstractArray, x::AbstractArray, α::Number, β::Number)
_add(y, x) = add(y, x, α, β)
return map(_add, y, x)
Expand All @@ -76,11 +96,11 @@ function add!(
y::BLASVector{T}, x::BLASVector{T}, α::Number, β::Number
) where {T <: BlasFloat}
if β === One()
LinearAlgebra.axpy!(convert(T, α), x, y)
LinearAlgebra.axpy!(_blasscalar(T, α), x, y)
elseif β === Zero() || β === false
scale!(y, x, convert(T, α))
scale!(y, x, _blasscalar(T, α))
else
LinearAlgebra.axpby!(convert(T, α), x, convert(T, β), y)
LinearAlgebra.axpby!(_blasscalar(T, α), x, _blasscalar(T, β), y)
end
return y
end
Expand All @@ -89,7 +109,7 @@ function add!(y::AbstractArray, x::AbstractArray, α::Number, β::Number)
ax = axes(x)
ay = axes(y)
ax == ay || throw(DimensionMismatch("Output axes $ay differ from input axes $ax"))
y .= add!!.(y, x, α, β) # might error
y .= add!!.(y, x, _bc(y, α), _bc(y, β)) # might error
return y
end

Expand All @@ -100,20 +120,24 @@ function add!!(y::AbstractArray, x::AbstractArray, α::Number, β::Number)
ax = axes(x)
ay = axes(y)
ax == ay || throw(DimensionMismatch("Output axes $ay differ from input axes $ax"))
return add!!.(y, x, α, β)
return add!!.(y, x, _bc(y, α), _bc(y, β))
end
end

# inner
#-------
function inner(x::BLASVector{T}, y::BLASVector{T}) where {T <: BlasFloat}
return LinearAlgebra.dot(x, y)
function inner(x::BLASVector, y::BLASVector)
return LinearAlgebra.dot(vec(x), vec(y))
end
function inner(x::AbstractArray, y::AbstractArray)
ax = axes(x)
ay = axes(y)
ax == ay || throw(DimensionMismatch("Non-matching axes $ax and $ay"))
T = promote_inner(x, y)
return mapreduce(inner, +, x, y; init = zero(T))
# this version is friendlier to GPU arrays with mixed element types
return mapreduce(inner, +, x, y; init = zero(T))
end

# innernorm
#-------
innernorm(x::BLASVector) = LinearAlgebra.norm(vec(x), 2)
4 changes: 4 additions & 0 deletions src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,7 @@ function inner(x, y)
throw(ArgumentError(_error_message(inner, T)))
end
end

# innernorm
# ---------
innernorm(x) = √real(inner(x, x))
8 changes: 8 additions & 0 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,11 @@ add!!(y, x, α::Number) = add!!(y, x, α, One())
Compute the inner product between `x` and `y`.
"""
function inner end

# 2-norm
"""
innernorm(x)

Compute the `2`-norm of `x`.
"""
function innernorm end
Loading
Loading