diff --git a/Project.toml b/Project.toml index 8e93c98..b4c36c1 100644 --- a/Project.toml +++ b/Project.toml @@ -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" @@ -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" diff --git a/ext/VectorInterfaceCUDACoreExt.jl b/ext/VectorInterfaceCUDACoreExt.jl new file mode 100644 index 0000000..e8cef53 --- /dev/null +++ b/ext/VectorInterfaceCUDACoreExt.jl @@ -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 diff --git a/ext/VectorInterfaceEnzymeExt.jl b/ext/VectorInterfaceEnzymeExt.jl index 9954a24..d4362bd 100644 --- a/ext/VectorInterfaceEnzymeExt.jl +++ b/ext/VectorInterfaceEnzymeExt.jl @@ -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) @@ -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) @@ -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) diff --git a/ext/VectorInterfaceGPUArraysExt.jl b/ext/VectorInterfaceGPUArraysExt.jl new file mode 100644 index 0000000..730a76f --- /dev/null +++ b/ext/VectorInterfaceGPUArraysExt.jl @@ -0,0 +1,7 @@ +module VectorInterfaceGPUArraysExt + +using VectorInterface, GPUArrays + +GPUArrays.blas_scalar(::Type{T}, s::VectorInterface.Scalar{T, <:AnyGPUVector{T}}) where {T} = s + +end diff --git a/ext/VectorInterfaceMooncakeExt.jl b/ext/VectorInterfaceMooncakeExt.jl index 00ad349..952eace 100644 --- a/ext/VectorInterfaceMooncakeExt.jl +++ b/ext/VectorInterfaceMooncakeExt.jl @@ -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} @@ -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 @@ -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, Δα, α) @@ -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 @@ -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()) @@ -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(β)) @@ -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()) diff --git a/ext/VectorInterfacecuBLASExt.jl b/ext/VectorInterfacecuBLASExt.jl new file mode 100644 index 0000000..891608d --- /dev/null +++ b/ext/VectorInterfacecuBLASExt.jl @@ -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 diff --git a/src/VectorInterface.jl b/src/VectorInterface.jl index df0e684..1146477 100644 --- a/src/VectorInterface.jl +++ b/src/VectorInterface.jl @@ -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}, diff --git a/src/abstractarray.jl b/src/abstractarray.jl index e1a6d67..b7fdaf9 100644 --- a/src/abstractarray.jl +++ b/src/abstractarray.jl @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 @@ -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) diff --git a/src/fallbacks.jl b/src/fallbacks.jl index 8a3807c..f603443 100644 --- a/src/fallbacks.jl +++ b/src/fallbacks.jl @@ -205,3 +205,7 @@ function inner(x, y) throw(ArgumentError(_error_message(inner, T))) end end + +# innernorm +# --------- +innernorm(x) = √real(inner(x, x)) diff --git a/src/interface.jl b/src/interface.jl index 3b03fba..73cbc90 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -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 diff --git a/src/minimalvec.jl b/src/minimalvec.jl index d85bfb4..7960b03 100644 --- a/src/minimalvec.jl +++ b/src/minimalvec.jl @@ -53,6 +53,14 @@ _ismutable(v::MinimalVec) = _ismutable(typeof(v)) scalartype(::Type{<:MinimalVec{M, V}}) where {M, V} = scalartype(V) +Base.similar(m::MinimalVec{M}, args...) where {M} = MinimalVec{M}(similar(m.vec, args...)) +Base.view(m::MinimalVec, args...) = view(m.vec, args...) +Base.length(m::MinimalVec) = length(m.vec) +Base.size(m::MinimalVec) = (length(m.vec),) +Base.ndims(::Type{<:MinimalVec}) = 1 +Base.collect(m::MinimalVec{M}) where {M} = MinimalVec{M}(collect(m.vec)) +Base.getindex(v::MinimalVec, args...) = getindex(v.vec, args...) + function zerovector(v::MinimalVec, S::Type{<:Number}) return MinimalVec{_ismutable(v)}(zerovector(v.vec, S)) end @@ -107,4 +115,4 @@ function add!!(y::MinimalVec, x::MinimalVec, α::Number, β::Number) end inner(x::MinimalVec, y::MinimalVec) = inner(x.vec, y.vec) -LinearAlgebra.norm(x::MinimalVec) = LinearAlgebra.norm(x.vec) +innernorm(x::MinimalVec) = innernorm(x.vec) diff --git a/src/scalar.jl b/src/scalar.jl new file mode 100644 index 0000000..4c3d972 --- /dev/null +++ b/src/scalar.jl @@ -0,0 +1,90 @@ +mutable struct Scalar{T, A} <: Number + a::A + Scalar{T, A}(a::A) where {T <: Number, A <: AbstractVector{T}} = new(a) + Scalar{T, A}(s::T) where {T <: Number, A <: AbstractVector{T}} = Scalar{T, A}(fill!(A(undef, 1), s)) + Scalar{T, A}(s::T2) where {T <: Number, T2 <: Number, A <: AbstractVector{T}} = Scalar{T, A}(fill!(A(undef, 1), convert(T, s))) + Scalar{T, A}(s::Scalar{T, A}) where {T <: Number, A <: AbstractVector{T}} = s + Scalar{T, A}(::One) where {T <: Number, A <: AbstractVector{T}} = new(fill!(A(undef, 1), one(T))) + Scalar{T, A}(::Zero) where {T <: Number, A <: AbstractVector{T}} = new(fill!(A(undef, 1), zero(T))) +end +Scalar{T}(a::A) where {T <: Number, A <: AbstractVector{T}} = Scalar{T, A}(a) +Scalar{T}(s::Scalar{T, A}) where {T <: Number, A <: AbstractVector{T}} = s +function Scalar(a::A) where {T <: Number, A <: AbstractArray{T}} + length(a) == 1 || throw(ArgumentError(lazy"underlying array for Scalar must have length 1")) + return Scalar{T}(vec(a)) +end +Scalar(s::Scalar{T, A}) where {T <: Number, A <: AbstractVector{T}} = s + +scalartype(::Scalar{T, A}) where {T, A} = T +scalartype(::Type{Scalar{T, A}}) where {T, A} = T + +Base.setindex!(s::Scalar{T, A}, v::T) where {T <: Number, A} = fill!(s.a, v) + +for op in (Symbol(*), Symbol(/), Symbol(-), Symbol(+)) + @eval Base.$op(s1::Scalar, s2::Scalar) = Scalar(broadcast($op, s1.a, s2.a)) + @eval Base.$op(s::Scalar, α::Number) = Scalar(broadcast($op, s.a, α)) + @eval Base.$op(α::Number, s::Scalar) = Scalar(broadcast($op, α, s.a)) +end +Base.:*(s::Scalar, α::One) = s +Base.:*(α::One, s::Scalar) = s +Base.:*(s::Scalar, α::Zero) = Scalar(scale(s.a, α)) +Base.:*(α::Zero, s::Scalar) = Scalar(scale(s.a, α)) +# `true`/`false` act as strong one/zero, as in Base; avoid launching a kernel for `true * s` +Base.:*(s::Scalar, α::Bool) = α ? s : s * Zero() +Base.:*(α::Bool, s::Scalar) = α ? s : Zero() * s +Base.:/(s::Scalar, α::One) = s +Base.:/(s::Scalar, α::Zero) = throw(DivideError()) +Base.:/(α::One, s::Scalar) = inv(s) +Base.:/(α::Zero, s::Scalar) = s * α +Base.:+(s::Scalar, α::Zero) = s +Base.:+(α::Zero, s::Scalar) = s +Base.:-(s::Scalar, α::Zero) = s +Base.:-(α::Zero, s::Scalar) = -s + +Base.:-(s::Scalar) = Scalar(-1 .* s.a) + +# Comparisons return a host `Bool`, so they must read back from the device. A `Scalar` holds +# exactly one element, so read that element (one transfer) instead of running an `all` +# reduction (memset + reduction kernel + transfer). Scalar-vs-Scalar comparisons fuse into a +# single broadcast so that they still cost only one transfer. +_hostvalue(s::Scalar) = first(collect(s.a)) +_fusedcmp(op, x::Scalar, y::Scalar) = first(collect(broadcast(op, x.a, y.a))) + +Base.iszero(s::Scalar) = iszero(_hostvalue(s)) +Base.:(==)(s1::Scalar, s2::Scalar) = _fusedcmp(==, s1, s2) +Base.isless(x::Scalar, y::Number) = isless(_hostvalue(x), y) +Base.isless(x::Number, y::Scalar) = isless(x, _hostvalue(y)) +Base.isless(x::Scalar, y::Scalar) = _fusedcmp(isless, x, y) +Base.:<(x::Scalar, y::Number) = _hostvalue(x) < y +Base.:<(x::Number, y::Scalar) = x < _hostvalue(y) +Base.:<(x::Scalar, y::Scalar) = _fusedcmp(<, x, y) +Base.copyto!(s::Scalar, v) = (s.a .= v; return s) + +Base.broadcastable(s::Scalar) = s.a + +Base.inv(s::Scalar) = Scalar(inv.(s.a)) +Base.abs(s::Scalar) = Scalar(abs.(s.a)) +Base.real(s::Scalar) = Scalar(Base.real.(s.a)) +Base.imag(s::Scalar) = Scalar(Base.imag.(s.a)) +Base.conj(s::Scalar) = Scalar(Base.conj.(s.a)) +Base.sqrt(s::Scalar) = Scalar(Base.sqrt.(s.a)) +Base.rtoldefault(::Type{<:Scalar{T}}) where {T} = Base.rtoldefault(T) +Base.isapprox(x::Scalar, y::Number; kwargs...) = all(_x -> isapprox(_x, y; kwargs...), x.a) +Base.isapprox(x::Scalar, y::Scalar; kwargs...) = all(splat((a, b) -> isapprox(a, b; kwargs...)), zip(x.a, y.a)) + +Base.hypot(x::Scalar, y::Scalar) = Scalar(hypot.(x.a, y.a)) +Base.hypot(x::Scalar, y::Number) = Scalar(hypot.(x.a, y)) +Base.hypot(x::Scalar, y::Scalar, xs::Scalar) = Scalar(hypot.(x.a, y.a, xs.a)) +Base.hypot(x::Scalar, y::Scalar, xs::Number) = Scalar(hypot.(x.a, y.a, xs)) +Base.hypot(x::Scalar, y::Number, xs::Number) = Scalar(hypot.(x.a, y, xs)) +Base.hypot(x::Scalar, y::Number, xs::Scalar) = Scalar(hypot.(x.a, y, xs.a)) + +Base.eps(s::Scalar) = eps(typeof(s)) +Base.eps(::Type{Scalar{T, A}}) where {T, A} = eps(T) +# for Julia 1.10, which doesn't have eps(::Type{<:Complex}) +Base.eps(::Type{Scalar{T, A}}) where {T <: Complex, A} = sqrt(2 * one(real(T))) * eps(real(T)) + + +# split this in two to avoid ambiguity +(::Type{T})(s::Scalar) where {T <: Real} = T(first(collect(s.a))) +(::Type{T})(s::Scalar) where {T <: Complex} = T(first(collect(s.a))) diff --git a/test/Project.toml b/test/Project.toml index 94fe686..6ec97ea 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -4,6 +4,7 @@ name = "VectorInterfaceTests" Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" +CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9" EnzymeTestUtils = "12d8515a-0907-448a-8884-5fe00fdf1c5a" JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb" @@ -23,6 +24,7 @@ VectorInterface = {path = ".."} Aqua = "0.8" ChainRulesCore = "1" ChainRulesTestUtils = "1" +CUDA = "6" Enzyme = "0.13.131" EnzymeTestUtils = "0.2.6" Mooncake = "0.5" diff --git a/test/cuarray.jl b/test/cuarray.jl new file mode 100644 index 0000000..3d3ae60 --- /dev/null +++ b/test/cuarray.jl @@ -0,0 +1,168 @@ +module CuVec +using VectorInterface +using CUDA +using Test +using TestExtras + +deepcollect(x::CuArray) = collect(x) +deepcollect(x::Number) = x +deepcollect(x::Scalar) = deepcollect(x.a) + +if CUDA.functional() + + x = CuVector(randn(3)) + y = CuVector(randn(3)) + nan_y = CuVector(vcat(randn(2), NaN)) + + @testset "scalartype" begin + s = @testinferred scalartype(x) + @test s == Float64 + end + + @testset "zerovector" begin + z = @testinferred zerovector(x) + @test z isa CuVector{Float64} + @test all(iszero, deepcollect(z)) + @test all(deepcollect(z) .=== zero(scalartype(x))) + z1 = @testinferred zerovector!!(deepcopy(x)) + @test z1 isa CuVector{Float64} + @test all(deepcollect(z1) .=== zero(scalartype(x))) + + z3 = @testinferred zerovector(x, ComplexF64) + @test z3 isa CuVector{ComplexF64} + @test all(deepcollect(z3) .=== zero(ComplexF64)) + z4 = @testinferred zerovector!!(x, ComplexF64) + @test z4 isa CuVector{ComplexF64} + @test all(deepcollect(z4) .=== zero(ComplexF64)) + end + + @testset "scale" begin + for α in (randn(), Scalar(CuVector([randn()]))) + z = @testinferred scale(x, α) + @test z isa CuVector{Float64} + @test all(deepcollect(z) .== deepcollect(x) .* deepcollect(α)) + + z = @testinferred scale(x, α) + @test z isa CuVector{Float64} + @test all(deepcollect(z) .== deepcollect(x) .* deepcollect(α)) + + z2 = @testinferred scale!!(deepcopy(x), α) + @test z2 isa CuVector{Float64} + @test deepcollect(z2) ≈ deepcollect(x) .* deepcollect(α) + z2 = @testinferred scale!!(deepcopy(y), x, α) + @test z2 isa CuVector{Float64} + @test deepcollect(z2) ≈ deepcollect(x) .* deepcollect(α) + end + + for α in (randn(ComplexF64), Scalar(CuVector([randn(ComplexF64)]))) + z4 = @testinferred scale(x, α) + @test z4 isa CuVector{ComplexF64} + @test deepcollect(z4) ≈ (deepcollect(α) .* deepcollect(x)) + z5 = @testinferred scale!!(x, α) + @test z5 isa CuVector{ComplexF64} + @test deepcollect(z5) ≈ (deepcollect(α) .* deepcollect(x)) + + z6 = @testinferred scale!!(zerovector(x), x, α) + @test z6 isa CuVector{ComplexF64} + @test deepcollect(z6) ≈ (deepcollect(α) .* deepcollect(x)) + end + + for α in (randn(Float64), Scalar(CuVector([randn(Float64)]))) + ycomplex = zerovector(y, ComplexF64) + z8 = @testinferred scale!!(ycomplex, x, α) + @test scalartype(z8) == ComplexF64 + @test all(deepcollect(z8) .== deepcollect(α) .* deepcollect(x)) + end + end + + @testset "add" begin + for (α, β) in ( + (randn(), randn()), + (Scalar(CuVector([randn()])), Scalar(CuVector([randn()]))), + ) + z = add(y, x) + @test z isa CuVector{Float64} + @test all(deepcollect(z) .== deepcollect(x) .+ deepcollect(y)) + z = add(y, x, α) + @test deepcollect(z) ≈ muladd.(deepcollect(x), deepcollect(α)[], deepcollect(y)) + z = add(y, x, α, β) + @test deepcollect(z) ≈ muladd.(deepcollect(x), deepcollect(α)[], deepcollect(y) .* deepcollect(β)[]) + + z2 = @testinferred add!!(deepcopy(y), x) + @test z2 isa CuVector{Float64} + @test deepcollect(z2) ≈ (deepcollect(x) .+ deepcollect(y)) + z2 = @testinferred add!!(deepcopy(y), x, α) + @test deepcollect(z2) ≈ (muladd.(deepcollect(x), deepcollect(α)[], deepcollect(y))) + z2 = @testinferred add!!(deepcopy(y), x, α, β) + @test deepcollect(z2) ≈ (muladd.(deepcollect(x), deepcollect(α)[], deepcollect(y) .* deepcollect(β)[])) + end + + for (α, β) in ( + (randn(ComplexF64), randn(ComplexF64)), + (Scalar(CuVector([randn(ComplexF64)])), Scalar(CuVector([randn(ComplexF64)]))), + ) + z4 = add(y, x, α) + @test z4 isa CuVector{ComplexF64} + @test deepcollect(z4) ≈ (muladd.(deepcollect(x), deepcollect(α), deepcollect(y))) + z4 = add(y, x, α, β) + @test deepcollect(z4) ≈ (muladd.(deepcollect(x), deepcollect(α), deepcollect(y) .* deepcollect(β))) + + z5 = @testinferred add!!(deepcopy(y), x, α) + @test z5 isa CuVector{ComplexF64} + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), deepcollect(α), deepcollect(y))) + z5 = @testinferred add!!(deepcopy(y), x, α, β) + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), deepcollect(α), deepcollect(y) .* deepcollect(β))) + end + + for α in (randn(ComplexF64), Scalar(CuVector([randn(ComplexF64)]))) + # test strong zero + α = randn(ComplexF64) + z6 = add(y, x, α, Zero()) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), deepcollect(α), deepcollect(y) .* Zero())) + z6 = add(y, x, α, false) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), deepcollect(α), deepcollect(y) .* false)) + end + + for α in (randn(scalartype(x)), Scalar(CuVector([randn(scalartype(x))]))) + z6 = deepcopy(nan_y) + z6 = @testinferred add!(z6, x, α, Zero()) + @test !any(isnan, z6) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), deepcollect(α), deepcollect(nan_y) .* Zero())) + z6 = deepcopy(nan_y) + z6 = @testinferred add!(z6, x, α, false) + @test !any(isnan, z6) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), deepcollect(α), deepcollect(nan_y) .* false)) + z6 = deepcopy(nan_y) + z6 = @testinferred add!(z6, x, α, 0.0) + @test any(isnan, z6) + end + end + + @testset "inner" begin + s = @testinferred inner(x, y) + @test convert(eltype(x), s) ≈ inner(deepcollect(x), deepcollect(y)) + + for (α, β) in ( + (randn(ComplexF64), randn(ComplexF64)), + (Scalar(CuVector([randn(ComplexF64)])), Scalar(CuVector([randn(ComplexF64)]))), + ) + s2 = @testinferred inner(scale(x, α), scale(y, β)) + @test convert(ComplexF64, s2) ≈ inner(deepcollect(α) .* deepcollect(x), deepcollect(β) .* deepcollect(y)) + end + end + + @testset "norm" begin + @testset for p in (-1, 1, 2, 3.5, Inf) + s = @testinferred norm(x, p) + @test s ≈ norm(deepcollect(x), p) + end + end + + @testset "innernorm" begin + s = @testinferred innernorm(x) + @test s isa Scalar + @test convert(real(eltype(x)), s) ≈ innernorm(deepcollect(x)) + end +end + +end diff --git a/test/cuarray_complicated.jl b/test/cuarray_complicated.jl new file mode 100644 index 0000000..93004dc --- /dev/null +++ b/test/cuarray_complicated.jl @@ -0,0 +1,194 @@ +using VectorInterface +using Test +using TestExtras +using CUDA + +using VectorInterface: One + +if CUDA.functional() + + deepcollect(x::CuArray) = collect(x) + deepcollect(x) = vcat(map(deepcollect, x)...) + deepcollect(x::Number) = x + deepcollect(x::Scalar) = deepcollect(x.a) + + x = (CUDA.randn(Float64, 2, 2), CUDA.rand(Float64, 2, 2), (CUDA.randn(Float64, 5, 2), CUDA.randn(Float64, 3, 2), CUDA.randn(Float64, 2, 2)), CUDA.randn(Float64, 1, 2), (CUDA.randn(Float64, 4, 2),)) + y = (CUDA.randn(Float64, 2, 2), CUDA.rand(Float64, 2, 2), (CUDA.randn(Float64, 5, 2), CUDA.randn(Float64, 3, 2), CUDA.randn(Float64, 2, 2)), CUDA.randn(Float64, 1, 2), (CUDA.randn(Float64, 4, 2),)) + + + @testset "scalartype" begin + s = @constinferred scalartype(x) + @test s == Float64 + @test_throws ArgumentError scalartype(()) + @test_throws ArgumentError scalartype((randn(Float64, 2), randn(ComplexF64, 3))) + end + + @testset "zerovector" begin + z = @constinferred zerovector(x) + @test all(iszero, deepcollect(z)) + @test all(deepcollect(z) .=== zero(scalartype(x))) + z1 = @constinferred zerovector!!(deepcopy(x)) + @test all(deepcollect(z1) .=== zero(scalartype(x))) + + z2 = @constinferred zerovector(x, ComplexF64) + @test all(deepcollect(z2) .=== zero(ComplexF64)) + z3 = @constinferred zerovector!!(deepcopy(x), ComplexF64) + @test all(deepcollect(z3) .=== zero(ComplexF64)) + + xy = [deepcopy(x), deepcopy(y)] + z4 = @constinferred zerovector!(xy) + @test all(deepcollect(z4) .=== zero(scalartype(xy))) + @test_throws MethodError zerovector!(xy, ComplexF64) + end + + @testset "scale" begin + for α in (randn(), Scalar(CuVector([randn()]))) + z = @constinferred scale(x, α) + @test all(deepcollect(z) .== deepcollect(α) .* deepcollect(x)) + z = @constinferred scale(x, One()) + @test all(deepcollect(z) .== deepcollect(x)) + z2 = @constinferred scale!!(deepcopy(x), α) + @test deepcollect(z2) ≈ (deepcollect(α) .* deepcollect(x)) + z2 = @constinferred scale!!(deepcopy(x), One()) + @test all(deepcollect(z2) .== deepcollect(x)) + xcopy = deepcopy(x) + z2 = @constinferred scale!!(deepcopy(y), xcopy, α) + @test deepcollect(z2) ≈ (deepcollect(α) .* deepcollect(x)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + + xy = [deepcopy(x), deepcopy(y)] + z3 = @constinferred scale!(deepcopy(xy), α) + @test deepcollect(z3) ≈ (deepcollect(α) .* deepcollect(xy)) + xycopy = deepcopy(xy) + z3 = @constinferred scale!(zerovector(xy), xycopy, α) + @test deepcollect(z3) ≈ (deepcollect(α) .* deepcollect(xy)) + @test all(deepcollect(xycopy) .== deepcollect(xy)) + end + + for α in (randn(ComplexF64), Scalar(CuVector([randn(ComplexF64)]))) + z4 = @constinferred scale(x, α) + @test deepcollect(z4) ≈ (deepcollect(α) .* deepcollect(x)) + xcopy = deepcopy(x) + z5 = @constinferred scale!!(xcopy, α) + @test deepcollect(z5) ≈ (deepcollect(α) .* deepcollect(x)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + + xcopy = deepcopy(x) + z6 = @constinferred scale!!(zerovector(x), xcopy, α) + @test deepcollect(z6) ≈ (deepcollect(α) .* deepcollect(x)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + xz = @constinferred zerovector(x, ComplexF64) + z6 = @constinferred scale!!(xz, xcopy, α) + @test deepcollect(z6) ≈ (deepcollect(α) .* deepcollect(x)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + end + end + + @testset "add" begin + z = @constinferred add(y, x) + @test all(deepcollect(z) .== deepcollect(x) .+ deepcollect(y)) + z = @constinferred add(y, x, One(), One()) + @test all(deepcollect(z) .== deepcollect(x) .+ deepcollect(y)) + z = @constinferred add(y, x, One()) + @test all(deepcollect(z) .== deepcollect(x) .+ deepcollect(y)) + z2 = @constinferred add!!(deepcopy(y), deepcopy(x)) + @test deepcollect(z2) ≈ (deepcollect(x) .+ deepcollect(y)) + z2 = @constinferred add!!(deepcopy(y), deepcopy(x), One(), One()) + @test all(deepcollect(z2) .== deepcollect(x) .+ deepcollect(y)) + z2 = @constinferred add!!(deepcopy(y), deepcopy(x), One()) + @test all(deepcollect(z2) .== deepcollect(x) .+ deepcollect(y)) + xy = [deepcopy(x), deepcopy(y)] + yx = [deepcopy(y), deepcopy(x)] + z3 = @constinferred add!(deepcopy(xy), deepcopy(yx)) + @test deepcollect(z3) ≈ (deepcollect(xy) .+ deepcollect(yx)) + + for (α, β) in ( + (randn(), randn()), + (Scalar(CuVector([randn()])), Scalar(CuVector([randn()]))), + ) + a, b = deepcollect(α)[], deepcollect(β)[] + # for some reason, on some Julia versions on some platforms, but only in test mode + # there is a small floating point discrepancy, which makes exact comparisons fail + z = @constinferred add(y, x, α) + @test deepcollect(z) ≈ muladd.(deepcollect(x), a, deepcollect(y)) + z = @constinferred add(y, x, α, β) + @test deepcollect(z) ≈ muladd.(deepcollect(x), a, deepcollect(y) .* b) + z = @constinferred add(y, x, One(), β) + @test deepcollect(z) ≈ muladd.(deepcollect(y), b, deepcollect(x)) + z = @constinferred add(y, x, α, One()) + @test deepcollect(z) ≈ muladd.(deepcollect(x), a, deepcollect(y)) + + z2 = @constinferred add!!(deepcopy(y), deepcopy(x), α) + @test deepcollect(z2) ≈ (muladd.(deepcollect(x), a, deepcollect(y))) + z2 = @constinferred add!!(deepcopy(y), deepcopy(x), α, β) + @test deepcollect(z2) ≈ (muladd.(deepcollect(x), a, deepcollect(y) .* b)) + z2 = @constinferred add!!(deepcopy(y), deepcopy(x), One(), β) + @test deepcollect(z2) ≈ muladd.(deepcollect(y), b, deepcollect(x)) + z2 = @constinferred add!!(deepcopy(y), deepcopy(x), α, One()) + @test deepcollect(z2) ≈ muladd.(deepcollect(x), a, deepcollect(y)) + + z3 = @constinferred add!(deepcopy(xy), deepcopy(yx), α) + @test deepcollect(z3) ≈ (muladd.(deepcollect(yx), a, deepcollect(xy))) + z3 = @constinferred add!(deepcopy(xy), deepcopy(yx), α, β) + @test deepcollect(z3) ≈ (muladd.(deepcollect(yx), a, deepcollect(xy) .* b)) + z3 = @constinferred add!!(deepcopy(xy), deepcopy(yx), α, β) + @test deepcollect(z3) ≈ (muladd.(deepcollect(yx), a, deepcollect(xy) .* b)) + end + + for (α, β) in ( + (randn(ComplexF64), randn(ComplexF64)), + (Scalar(CuVector([randn(ComplexF64)])), Scalar(CuVector([randn(ComplexF64)]))), + ) + a, b = deepcollect(α)[], deepcollect(β)[] + z4 = @constinferred add(y, x, α) + @test deepcollect(z4) ≈ (muladd.(deepcollect(x), a, deepcollect(y))) + z4 = @constinferred add(y, x, α, β) + @test deepcollect(z4) ≈ (muladd.(deepcollect(x), a, deepcollect(y) .* b)) + + z5 = @constinferred add!!(deepcopy(y), deepcopy(x), α) + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), a, deepcollect(y))) + z5 = @constinferred add!!(deepcopy(y), deepcopy(x), α, β) + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), a, deepcollect(y) .* b)) + + xy = [deepcopy(x), deepcopy(y)] + yx = [deepcopy(y), deepcopy(x)] + z5 = @constinferred add!!(deepcopy(xy), deepcopy(yx), α, β) + @test deepcollect(z5) ≈ (muladd.(deepcollect(yx), a, deepcollect(xy) .* b)) + + # test strong zero + z6 = @constinferred add(y, x, α, Zero()) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), a, deepcollect(y) .* Zero())) + z6 = @constinferred add(y, x, α, false) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), a, deepcollect(y) .* false)) + end + end + + @testset "inner" begin + s = @constinferred inner(x, y) + @test convert(Float64, s) ≈ inner(deepcollect(x), deepcollect(y)) + + for (α, β) in ( + (randn(ComplexF64), randn(ComplexF64)), + (Scalar(CuVector([randn(ComplexF64)])), Scalar(CuVector([randn(ComplexF64)]))), + ) + s2 = @testinferred inner(scale(x, α), scale(y, β)) + @test convert(ComplexF64, s2) ≈ inner(deepcollect(α)[] .* deepcollect(x), deepcollect(β)[] .* deepcollect(y)) + end + end + + @testset "norm" begin + @testset for p in (-1, 1, 2, 3.5, Inf) + # LinearAlgebra.norm on a nested container takes the p-norm of the + # 2-norms of its elements, not the p-norm of the flattened vector + ref = norm(map(xi -> norm(deepcollect(xi)), x), p) + # LinearAlgebra.norm on heterogeneous tuples only infers from Julia 1.12 on + s = VERSION ≥ v"1.12" ? (@testinferred norm(x, p)) : norm(x, p) + @test convert(Float64, s) ≈ ref + end + end + + @testset "innernorm" begin + s = @testinferred innernorm(x) + @test convert(Float64, s) ≈ innernorm(deepcollect(x)) + end +end diff --git a/test/enzyme.jl b/test/enzyme.jl index 66eb1ac..2138dc9 100644 --- a/test/enzyme.jl +++ b/test/enzyme.jl @@ -32,7 +32,21 @@ eltypes = (Float64, ComplexF64) test_forward(scale!!, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα); atol, rtol) end + # Scalar + α = Scalar([randn(T)]) + for Tα in (Const, Duplicated) + test_reverse(scale, Duplicated, (x, Duplicated), (α, Tα); atol, rtol) + test_reverse(scale!!, Duplicated, (x, Duplicated), (α, Tα); atol, rtol) + test_reverse(scale!!, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα); atol, rtol) + end + for Tα in (Const, Duplicated) + test_forward(scale, Duplicated, (x, Duplicated), (α, Tα); atol, rtol) + test_forward(scale!!, Duplicated, (x, Duplicated), (α, Tα); atol, rtol) + test_forward(scale!!, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα); atol, rtol) + end + # MinimalMVec + α = randn(T) mx = MinimalMVec(x) my = MinimalMVec(y) for Tα in (Const, Active) @@ -79,10 +93,22 @@ end test_forward(add, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα), (β, Tβ); atol, rtol) test_forward(add!!, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα), (β, Tβ); atol, rtol) end - + # Scalar + α = Scalar([randn(T)]) + β = Scalar([randn(T)]) + for Tα in (Const, Duplicated), Tβ in (Const, Duplicated) + test_reverse(add, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα), (β, Tβ); atol, rtol) + test_reverse(add!!, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα), (β, Tβ); atol, rtol) + end + for Tα in (Const, Duplicated), Tβ in (Const, Duplicated) + test_forward(add, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα), (β, Tβ); atol, rtol) + test_forward(add!!, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα), (β, Tβ); atol, rtol) + end # MinimalMVec mx = MinimalMVec(x) my = MinimalMVec(y) + α = randn(T) + β = randn(T) for Tα in (Const, Active), Tβ in (Const, Active) test_reverse(add, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα), (β, Tβ); atol, rtol) test_reverse(add!!, Duplicated, (y, Duplicated), (x, Duplicated), (α, Tα), (β, Tβ); atol, rtol) diff --git a/test/jlarray.jl b/test/jlarray.jl index d423495..7bd9a27 100644 --- a/test/jlarray.jl +++ b/test/jlarray.jl @@ -1,4 +1,4 @@ -module StaticSVec +module JLArrays using VectorInterface using JLArrays using Test @@ -6,6 +6,7 @@ using TestExtras deepcollect(x::JLArray) = collect(x) deepcollect(x::Number) = x +deepcollect(x::Scalar) = deepcollect(x.a) x = JLVector(randn(3)) y = JLVector(randn(3)) @@ -21,7 +22,7 @@ end @test z isa JLVector{Float64} @test all(iszero, deepcollect(z)) @test all(deepcollect(z) .=== zero(scalartype(x))) - z1 = @constinferred zerovector!!(x) + z1 = @constinferred zerovector!!(deepcopy(x)) @test z1 isa JLVector{Float64} @test all(deepcollect(z1) .=== zero(scalartype(x))) @@ -34,96 +35,130 @@ end end @testset "scale" begin - α = randn() - z = @constinferred scale(x, α) - @test z isa JLVector{Float64} - @test all(deepcollect(z) .== α .* deepcollect(x)) - - z2 = @constinferred scale!!(x, α) - @test z2 isa JLVector{Float64} - @test deepcollect(z2) ≈ (α .* deepcollect(x)) - z2 = @constinferred scale!!(y, x, α) - @test z2 isa JLVector{Float64} - @test deepcollect(z2) ≈ (α .* deepcollect(x)) - - α = randn(ComplexF64) - z4 = @constinferred scale(x, α) - @test z4 isa JLVector{ComplexF64} - @test deepcollect(z4) ≈ (α .* deepcollect(x)) - z5 = @constinferred scale!!(x, α) - @test z5 isa JLVector{ComplexF64} - @test deepcollect(z5) ≈ (α .* deepcollect(x)) - - z6 = @constinferred scale!!(zerovector(x), x, α) - @test z6 isa JLVector{ComplexF64} - @test deepcollect(z6) ≈ (α .* deepcollect(x)) - - ycomplex = zerovector(y, ComplexF64) - α = randn(Float64) - z8 = @constinferred scale!!(ycomplex, x, α) - @test scalartype(z8) == ComplexF64 - @test all(deepcollect(z8) .== α .* deepcollect(x)) + for α in (randn(), Scalar(JLVector([randn()]))) + z = @constinferred scale(x, α) + @test z isa JLVector{Float64} + @test all(deepcollect(z) .== deepcollect(x) .* α) + + z = @constinferred scale(x, α) + @test z isa JLVector{Float64} + @test all(deepcollect(z) .== deepcollect(x) .* α) + + z2 = @constinferred scale!!(deepcopy(x), α) + @test z2 isa JLVector{Float64} + @test deepcollect(z2) ≈ deepcollect(x) .* deepcollect(α) + z2 = @constinferred scale!!(deepcopy(y), x, α) + @test z2 isa JLVector{Float64} + @test deepcollect(z2) ≈ deepcollect(x) .* deepcollect(α) + end + + for α in (randn(ComplexF64), Scalar(JLVector([randn(ComplexF64)]))) + z4 = @constinferred scale(x, α) + @test z4 isa JLVector{ComplexF64} + @test deepcollect(z4) ≈ (α .* deepcollect(x)) + z5 = @constinferred scale!!(x, α) + @test z5 isa JLVector{ComplexF64} + @test deepcollect(z5) ≈ (α .* deepcollect(x)) + + z6 = @constinferred scale!!(zerovector(x), x, α) + @test z6 isa JLVector{ComplexF64} + @test deepcollect(z6) ≈ (α .* deepcollect(x)) + end + + for α in (randn(Float64), Scalar(JLVector([randn(Float64)]))) + ycomplex = zerovector(y, ComplexF64) + z8 = @constinferred scale!!(ycomplex, x, α) + @test scalartype(z8) == ComplexF64 + @test all(deepcollect(z8) .== α .* deepcollect(x)) + end end @testset "add" begin - α, β = randn(2) - z = add(y, x) - @test z isa JLVector{Float64} - @test all(deepcollect(z) .== deepcollect(x) .+ deepcollect(y)) - z = add(y, x, α) - @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y)) - z = add(y, x, α, β) - @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y) .* β) - - z2 = @constinferred add!!(y, x) - @test z2 isa JLVector{Float64} - @test deepcollect(z2) ≈ (deepcollect(x) .+ deepcollect(y)) - z2 = @constinferred add!!(y, x, α) - @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) - z2 = @constinferred add!!(y, x, α, β) - @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) - - α, β = randn(ComplexF64, 2) - z4 = add(y, x, α) - @test z4 isa JLVector{ComplexF64} - @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) - z4 = add(y, x, α, β) - @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) - - z5 = @constinferred add!!(y, x, α) - @test z5 isa JLVector{ComplexF64} - @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) - z5 = @constinferred add!!(y, x, α, β) - @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) - - # test strong zero - α = randn(ComplexF64) - z6 = add(y, x, α, Zero()) - @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* Zero())) - z6 = add(y, x, α, false) - @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* false)) - - α = randn(scalartype(x)) - z6 = deepcopy(nan_y) - z6 = @constinferred add!(z6, x, α, Zero()) - @test !any(isnan, z6) - @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(nan_y) .* Zero())) - z6 = deepcopy(nan_y) - z6 = @constinferred add!(z6, x, α, false) - @test !any(isnan, z6) - @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(nan_y) .* false)) - z6 = deepcopy(nan_y) - z6 = @constinferred add!(z6, x, α, 0.0) - @test any(isnan, z6) + for (α, β) in ( + (randn(), randn()), + (Scalar(JLVector([randn()])), Scalar(JLVector([randn()]))), + ) + z = add(y, x) + @test z isa JLVector{Float64} + @test all(deepcollect(z) .== deepcollect(x) .+ deepcollect(y)) + z = add(y, x, α) + @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y)) + z = add(y, x, α, β) + @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y) .* β) + + z2 = @constinferred add!!(deepcopy(y), x) + @test z2 isa JLVector{Float64} + @test deepcollect(z2) ≈ (deepcollect(x) .+ deepcollect(y)) + z2 = @constinferred add!!(deepcopy(y), x, α) + @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) + z2 = @constinferred add!!(deepcopy(y), x, α, β) + @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) + end + + for (α, β) in ( + (randn(ComplexF64), randn(ComplexF64)), + (Scalar(JLVector([randn(ComplexF64)])), Scalar(JLVector([randn(ComplexF64)]))), + ) + z4 = add(y, x, α) + @test z4 isa JLVector{ComplexF64} + @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) + z4 = add(y, x, α, β) + @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) + + z5 = @constinferred add!!(y, x, α) + @test z5 isa JLVector{ComplexF64} + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) + z5 = @constinferred add!!(y, x, α, β) + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) + end + + for α in (randn(ComplexF64), Scalar(JLVector([randn(ComplexF64)]))) + # test strong zero + α = randn(ComplexF64) + z6 = add(y, x, α, Zero()) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* Zero())) + z6 = add(y, x, α, false) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* false)) + end + + for α in (randn(scalartype(x)), Scalar(JLVector([randn(scalartype(x))]))) + z6 = deepcopy(nan_y) + z6 = @constinferred add!(z6, x, α, Zero()) + @test !any(isnan, z6) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(nan_y) .* Zero())) + z6 = deepcopy(nan_y) + z6 = @constinferred add!(z6, x, α, false) + @test !any(isnan, z6) + @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(nan_y) .* false)) + z6 = deepcopy(nan_y) + z6 = @constinferred add!(z6, x, α, 0.0) + @test any(isnan, z6) + end end @testset "inner" begin s = @constinferred inner(x, y) @test s ≈ inner(deepcollect(x), deepcollect(y)) - α, β = randn(ComplexF64, 2) - s2 = @constinferred inner(scale(x, α), scale(y, β)) - @test s2 ≈ inner(α * deepcollect(x), β * deepcollect(y)) + for (α, β) in ( + (randn(ComplexF64), randn(ComplexF64)), + (Scalar(JLVector([randn(ComplexF64)])), Scalar(JLVector([randn(ComplexF64)]))), + ) + s2 = @constinferred inner(scale(x, α), scale(y, β)) + @test s2 ≈ inner(α * deepcollect(x), β * deepcollect(y)) + end +end + +@testset "norm" begin + for p in (-1, 1, 2, 3.5, Inf) + s = @constinferred norm(x, p) + @test s ≈ norm(deepcollect(x), p) + end +end + +@testset "innernorm" begin + s = @constinferred innernorm(x) + @test s ≈ innernorm(deepcollect(x)) end end diff --git a/test/mooncake.jl b/test/mooncake.jl index 5ffa7a7..10f4af0 100644 --- a/test/mooncake.jl +++ b/test/mooncake.jl @@ -32,8 +32,14 @@ eltypes = (Float32, Float64, ComplexF64) Mooncake.TestUtils.test_rule(rng, scale, x, α; atol, rtol, is_primitive = false) Mooncake.TestUtils.test_rule(rng, scale!!, x, α; atol, rtol, is_primitive = false) Mooncake.TestUtils.test_rule(rng, scale!!, y, x, α; atol, rtol, is_primitive = false) + # Scalar + α = Scalar([randn(T)]) + Mooncake.TestUtils.test_rule(rng, scale, x, α; atol, rtol, is_primitive = false) + Mooncake.TestUtils.test_rule(rng, scale!!, x, α; atol, rtol, is_primitive = false) + Mooncake.TestUtils.test_rule(rng, scale!!, y, x, α; atol, rtol, is_primitive = false) # MinimalMVec + α = randn(T) mx = MinimalMVec(x) my = MinimalMVec(y) Mooncake.TestUtils.test_rule(rng, scale, mx, α; atol, rtol, is_primitive = false) @@ -59,8 +65,15 @@ end β = randn(T) Mooncake.TestUtils.test_rule(rng, add, y, x, α, β; atol, rtol, is_primitive = false) Mooncake.TestUtils.test_rule(rng, add!!, y, x, α, β; atol, rtol, is_primitive = false) + # Scalar + α = Scalar([randn(T)]) + β = Scalar([randn(T)]) + Mooncake.TestUtils.test_rule(rng, add, y, x, α, β; atol, rtol, is_primitive = false) + Mooncake.TestUtils.test_rule(rng, add!!, y, x, α, β; atol, rtol, is_primitive = false) # MinimalMVec + α = randn(T) + β = randn(T) mx = MinimalMVec(x) my = MinimalMVec(y) Mooncake.TestUtils.test_rule(rng, add, my, mx, α, β; atol, rtol, is_primitive = false) diff --git a/test/runtests.jl b/test/runtests.jl index 1a0952a..c650956 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,7 +16,7 @@ fast = !isnothing(args.custom["fast"]) # On Buildkite (GPU CI runner): only run CUDA and AMDGPU tests if get(ENV, "BUILDKITE", "false") == "true" - f(str) = startswith(first(str), "cuda") || startswith(first(str), "amd") + f(str) = startswith(first(str), "cuarray") || startswith(first(str), "rocarray") filter!(f, testsuite) end diff --git a/test/scalar.jl b/test/scalar.jl new file mode 100644 index 0000000..73a83d9 --- /dev/null +++ b/test/scalar.jl @@ -0,0 +1,92 @@ +using Test +using VectorInterface + +@testset "Scalar basics" begin + s = Scalar([2.0]) + @test s * 0.5 == Scalar{Float64, Vector{Float64}}(One()) + @test s * Scalar([0.5]) == Scalar{Float64, Vector{Float64}}(One()) + @test s * One() === One() * s === s + @test s / One() === s + @test One() / s == inv(s) + @test s / 0.5 == Scalar{Float64, Vector{Float64}}([4.0]) + @test s / Scalar([0.5]) == Scalar{Float64, Vector{Float64}}([4.0]) + @test 0.5 / s == Scalar{Float64, Vector{Float64}}([0.25]) + @test s * Zero() == Zero() * s == Scalar{Float64, Vector{Float64}}(Zero()) + @test s + Zero() === Zero() + s === s + @test s + 2s == Scalar{Float64, Vector{Float64}}(6.0) + @test s + 4 == Scalar{Float64, Vector{Float64}}(6.0) + @test 4 + s == Scalar{Float64, Vector{Float64}}(6.0) + @test s - 2s == Scalar{Float64, Vector{Float64}}(-2.0) + @test s - 4 == Scalar{Float64, Vector{Float64}}(-2.0) + @test s - Zero() == s + @test 4 - s == Scalar{Float64, Vector{Float64}}(2.0) + @test -s == Scalar{Float64, Vector{Float64}}(-2.0) + @test inv(s) == Scalar{Float64, Vector{Float64}}(0.5) + @test isless(s, 10.0) + @test isless(1.4, s) + @test iszero(s * Zero()) + @test eps(typeof(s)) == eps(typeof(2.0)) + s2 = Scalar(fill(2.0, 1, 1)) + @test s2 isa Scalar{Float64, Vector{Float64}} + @test s2 == s + sc = Scalar([4.0 - 5.0 * im]) + @test real(sc) == Scalar{ComplexF64, Vector{ComplexF64}}(4.0) + @test imag(sc) == Scalar([-5.0]) + @test conj(sc) == Scalar([4.0 + 5.0im]) + @static if VERSION > v"1.13.0-rc" + @test eps(typeof(sc)) == eps(typeof(4.0 - 5.0 * im)) + end +end + +@testset "Scalar Bool multiplication" begin + s = Scalar([2.0]) + # `true` is a strong one: no new Scalar is created + @test true * s === s + @test s * true === s + # `false` is a strong zero, as in Base (`false * NaN == 0`) + @test false * s == s * false == Scalar([0.0]) + nan = Scalar([NaN]) + @test iszero(false * nan) + @test iszero(nan * false) + @test isnan(Float64(true * nan)) + @test false * Scalar([2.0im]) == Scalar([0.0im]) +end + +@testset "Scalar comparisons" begin + a, b = Scalar([1.0]), Scalar([2.0]) + for (x, y) in ((a, b), (b, a), (a, a)) + vx, vy = Float64(x), Float64(y) + @test (x < y) == (vx < vy) + @test (x > y) == (vx > vy) + @test (x == y) == (vx == vy) + @test isless(x, y) == isless(vx, vy) + for c in (0.5, 1.0, 1.5, 3.0) + @test (x < c) == (vx < c) + @test (c < x) == (c < vx) + @test (x > c) == (vx > c) + @test (c > x) == (c > vx) + @test isless(x, c) == isless(vx, c) + @test isless(c, x) == isless(c, vx) + end + end + # chained comparison, as in the iterative-refinement loops of KrylovKit + @test eps(Float64) < a < 0.75 * b + @test !(eps(Float64) < b < 0.75 * b) + + @test iszero(Scalar([0.0])) + @test !iszero(a) + + # NaN: `<`, `>` and `==` follow IEEE semantics, `isless` follows Base's total order + nan = Scalar([NaN]) + @test !(nan < 1.0) + @test !(1.0 < nan) + @test !(nan > 1.0) + @test !(1.0 > nan) + @test !(nan < a) + @test !(a < nan) + @test !(nan == nan) + @test isless(1.0, nan) + @test !isless(nan, 1.0) + @test isless(a, nan) + @test !isless(nan, a) +end diff --git a/test/simple.jl b/test/simple.jl index 466e7e2..79ce34b 100644 --- a/test/simple.jl +++ b/test/simple.jl @@ -86,66 +86,95 @@ end end @testset "add" begin - α, β = randn(2) + z = @constinferred add(y, x) @test all(deepcollect(z) .== deepcollect(x) .+ deepcollect(y)) - z = @constinferred add(y, x, α) - # for some reason, on some Julia versions on some platforms, but only in test mode - # there is a small floating point discrepancy, which makes the following test fail: - # @test all(deepcollect(z) .== muladd.(deepcollect(x), α, deepcollect(y))) - @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y)) - z = @constinferred add(y, x, α, β) - # for some reason, on some Julia versions on some platforms, but only in test mode - # there is a small floating point discrepancy, which makes the following test fail: - # @test all(deepcollect(z) .== muladd.(deepcollect(x), α, deepcollect(y) .* β)) - @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y) .* β) - - α, β = randn(2) - xcopy = deepcopy(x) - z2 = @constinferred add!!(deepcopy(y), xcopy) - @test deepcollect(z2) ≈ (deepcollect(x) .+ deepcollect(y)) - @test all(deepcollect(xcopy) .== deepcollect(x)) - z2 = @constinferred add!!(deepcopy(y), xcopy, α) - @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) - @test all(deepcollect(xcopy) .== deepcollect(x)) - z2 = @constinferred add!!(deepcopy(y), xcopy, α, β) - @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) - @test all(deepcollect(xcopy) .== deepcollect(x)) - - α, β = randn(2) - z3 = @constinferred add!(deepcopy(y), xcopy) - @test deepcollect(z3) ≈ (deepcollect(y) .+ deepcollect(x)) - @test all(deepcollect(xcopy) .== deepcollect(x)) - z3 = @constinferred add!(deepcopy(y), xcopy, α) - @test all(deepcollect(xcopy) .== deepcollect(x)) - @test deepcollect(z3) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) - z3 = @constinferred add!(deepcopy(y), xcopy, α, β) - @test deepcollect(z3) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) - @test all(deepcollect(xcopy) .== deepcollect(x)) - - α, β = randn(ComplexF64, 2) - z4 = @constinferred add(y, x, α) - @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) - z4 = @constinferred add(y, x, α, β) - @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) - - α, β = randn(ComplexF64, 2) - z5 = @constinferred add!!(deepcopy(y), xcopy, α) - @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) - @test all(deepcollect(xcopy) .== deepcollect(x)) - z5 = @constinferred add!!(deepcopy(y), xcopy, α, β) - @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) - @test all(deepcollect(xcopy) .== deepcollect(x)) - - α, β = randn(ComplexF64, 2) - z5 = @constinferred add!!(deepcopy(y), xcopy, α) - @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) - @test all(deepcollect(xcopy) .== deepcollect(x)) - z5 = @constinferred add!!(deepcopy(y), xcopy, α, β) - @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) - @test all(deepcollect(xcopy) .== deepcollect(x)) + for (α, β) in ( + (randn(), randn()), + (Scalar([randn()]), Scalar([randn()])), + ) + z = @constinferred add(y, x, α) + # for some reason, on some Julia versions on some platforms, but only in test mode + # there is a small floating point discrepancy, which makes the following test fail: + # @test all(deepcollect(z) .== muladd.(deepcollect(x), α, deepcollect(y))) + @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y)) + z = @constinferred add(y, x, α, β) + # for some reason, on some Julia versions on some platforms, but only in test mode + # there is a small floating point discrepancy, which makes the following test fail: + # @test all(deepcollect(z) .== muladd.(deepcollect(x), α, deepcollect(y) .* β)) + @test deepcollect(z) ≈ muladd.(deepcollect(x), α, deepcollect(y) .* β) + end + + for (α, β) in ( + (randn(), randn()), + (Scalar([randn()]), Scalar([randn()])), + ) + xcopy = deepcopy(x) + z2 = @constinferred add!!(deepcopy(y), xcopy) + @test deepcollect(z2) ≈ (deepcollect(x) .+ deepcollect(y)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + z2 = @constinferred add!!(deepcopy(y), xcopy, α) + @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) + @test all(deepcollect(xcopy) .== deepcollect(x)) + z2 = @constinferred add!!(deepcopy(y), xcopy, α, β) + @test deepcollect(z2) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + end + + for (α, β) in ( + (randn(), randn()), + (Scalar([randn()]), Scalar([randn()])), + ) + xcopy = deepcopy(x) + z3 = @constinferred add!(deepcopy(y), xcopy) + @test deepcollect(z3) ≈ (deepcollect(y) .+ deepcollect(x)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + z3 = @constinferred add!(deepcopy(y), xcopy, α) + @test all(deepcollect(xcopy) .== deepcollect(x)) + @test deepcollect(z3) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) + z3 = @constinferred add!(deepcopy(y), xcopy, α, β) + @test deepcollect(z3) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + end + + for (α, β) in ( + (randn(ComplexF64), randn(ComplexF64)), + (Scalar([randn(ComplexF64)]), Scalar([randn(ComplexF64)])), + ) + z4 = @constinferred add(y, x, α) + @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) + z4 = @constinferred add(y, x, α, β) + @test deepcollect(z4) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) + end + + for (α, β) in ( + (randn(ComplexF64), randn(ComplexF64)), + (Scalar([randn(ComplexF64)]), Scalar([randn(ComplexF64)])), + ) + xcopy = deepcopy(x) + z5 = @constinferred add!!(deepcopy(y), xcopy, α) + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) + @test all(deepcollect(xcopy) .== deepcollect(x)) + z5 = @constinferred add!!(deepcopy(y), xcopy, α, β) + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + end + + for (α, β) in ( + (randn(ComplexF64), randn(ComplexF64)), + (Scalar([randn(ComplexF64)]), Scalar([randn(ComplexF64)])), + ) + xcopy = deepcopy(x) + z5 = @constinferred add!!(deepcopy(y), xcopy, α) + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y))) + @test all(deepcollect(xcopy) .== deepcollect(x)) + z5 = @constinferred add!!(deepcopy(y), xcopy, α, β) + @test deepcollect(z5) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* β)) + @test all(deepcollect(xcopy) .== deepcollect(x)) + end # test strong zero + xcopy = deepcopy(x) α = randn(ComplexF64) z6 = @constinferred add(y, x, α, Zero()) @test deepcollect(z6) ≈ (muladd.(deepcollect(x), α, deepcollect(y) .* Zero()))