Skip to content

Commit

Permalink
Fix depwarns on v0.7-
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Oct 11, 2017
1 parent b1c6a82 commit 670817b
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 44 deletions.
21 changes: 21 additions & 0 deletions src/TensorOperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ module TensorOperations
import Base.Iterators.flatten
import Base.setindex

if VERSION < v"0.7.0-DEV.843"
import Base: Val
(::Type{Val})(x) = (Base.@_pure_meta; Val{x}())
# Also add methods for Val(x) that were previously Val{x}
import Base: reshape
reshape(parent::AbstractArray, ndims::Val{N}) where {N} = reshape(parent, Val{N})
import Base: ntuple
ntuple(f::F, ::Val{N}) where {F,N} = ntuple(f, Val{N})
end

# 0.7.0-DEV.1993
@static if !isdefined(Base, :EqualTo)
struct EqualTo{T} <: Function
x::T
EqualTo(x::T) where {T} = new{T}(x)
end
(f::EqualTo)(y) = isequal(f.x, y)
const equalto = EqualTo
export equalto
end


export tensorcopy, tensoradd, tensortrace, tensorcontract, tensorproduct, scalar
export tensorcopy!, tensoradd!, tensortrace!, tensorcontract!, tensorproduct!
Expand Down
2 changes: 1 addition & 1 deletion src/auxiliary/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ end
Expr(:block, meta, ex)
end

tinvperm(t::NTuple{N,Int}) where {N}= ntuple(n->findfirst(t,n), Val{N})
tinvperm(t::NTuple{N,Int}) where {N}= ntuple(n->findfirst(equalto(n), t), Val(N))

# Based on Tim Holy's Cartesian
function _sreplace(ex::Expr, s::Symbol, v)
Expand Down
4 changes: 2 additions & 2 deletions src/auxiliary/stridedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ corresponding to a selection of those of `op(A)`, where the selection is specifi
"""
@inline function similar_from_indices(::Type{T}, indices::NTuple{N,Int}, A::StridedArray, ::Type{Val{CA}}=Val{:N}) where {T,N,CA}
srcdims = size(A)
return similar(A, T, ntuple(n->srcdims[indices[n]], Val{N}))
return similar(A, T, ntuple(n->srcdims[indices[n]], Val(N)))
end

"""
Expand All @@ -39,7 +39,7 @@ selection is specified by `indices` (which contains integers between `1` and
"""
function similar_from_indices(::Type{T}, indices::NTuple{N,Int}, A::StridedArray, B::StridedArray, ::Type{Val{CA}}=Val{:N}, ::Type{Val{CB}}=Val{:N}) where {T,N,CA,CB}
srcdims = tuple(size(A)...,size(B)...)
return similar(A, T, ntuple(n->srcdims[indices[n]], Val{N}))
return similar(A, T, ntuple(n->srcdims[indices[n]], Val(N)))
end

"""
Expand Down
6 changes: 5 additions & 1 deletion src/auxiliary/strideddata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ end
NormalStridedData{N,T} = StridedData{N,T,:N}
ConjugatedStridedData{N,T} = StridedData{N,T,:C}

StridedSubArray{T,N,A<:Array,I<:Tuple{Vararg{Union{Colon,Range{Int64},Int64}}},LD} = SubArray{T,N,A,I,LD}
import Base.StridedReshapedArray

StridedSubArray{T,N,A<:Union{DenseArray{T},StridedReshapedArray{T}},I<:Tuple{Vararg{Union{Base.RangeIndex, Base.AbstractCartesianIndex}}}} = SubArray{T,N,A,I}

StridedData(a::Array{T}, strides::NTuple{N,Int} = strides(a), ::Type{Val{C}} = Val{:N}) where {N,T,C} =
StridedData{N,T,C}(vec(a), strides, 1)
StridedData(a::StridedReshapedArray{T}, strides::NTuple{N,Int} = strides(a), ::Type{Val{C}} = Val{:N}) where {N,T,C} =
StridedData{N,T,C}(vec(a.parent), strides, 1)
StridedData(a::StridedSubArray{T}, strides::NTuple{N,Int} = strides(a), ::Type{Val{C}} = Val{:N}) where {N,T,C} =
StridedData{N,T,C}(vec(a.parent), strides, Base.first_index(a))

Expand Down
4 changes: 2 additions & 2 deletions src/auxiliary/unique2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ function unique2(itr)
out = collect(itr)
i = 1
while i < length(out)
inext = findnext(out,out[i],i+1)
inext = findnext(equalto(out[i]), out, i+1)
if inext == 0
i += 1
continue
end
while inext != 0
deleteat!(out,inext)
inext = findnext(out,out[i],i+1)
inext = findnext(equalto(out[i]), out, i+1)
end
deleteat!(out,i)
end
Expand Down
18 changes: 9 additions & 9 deletions src/implementation/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tunique(src::Tuple, dst::Tuple) = src[1] in dst ? tunique((tail(src)..., src[1])
# Extract index information
#---------------------------
function add_indices(IA::NTuple{NA,Any}, IC::NTuple{NC,Any}) where {NA,NC}
indCinA = map(l->findfirst(IA,l), IC)
indCinA = map(l->findfirst(equalto(l), IA), IC)
(NA == NC && isperm(indCinA)) || throw(IndexError("invalid index specification: $IA to $IC"))
return indCinA
end
Expand All @@ -28,9 +28,9 @@ function trace_indices(IA::NTuple{NA,Any}, IC::NTuple{NC,Any}) where {NA,NC}
isodd(length(IA)-length(IC)) && throw(IndexError("invalid trace specification: $IA to $IC"))
Itrace = tunique(tsetdiff(IA, IC))

cindA1 = map(l->findfirst(IA, l), Itrace)
cindA2 = map(l->findnext(IA, l, findfirst(IA, l)+1), Itrace)
indCinA = map(l->findfirst(IA, l), IC)
cindA1 = map(l->findfirst(equalto(l), IA), Itrace)
cindA2 = map(l->findnext(equalto(l), IA, findfirst(equalto(l), IA)+1), Itrace)
indCinA = map(l->findfirst(equalto(l), IA), IC)

pA = (indCinA..., cindA1..., cindA2...)
(isperm(pA) && length(pA) == NA) || throw(IndexError("invalid trace specification: $IA to $IC"))
Expand All @@ -46,11 +46,11 @@ function contract_indices(IA::NTuple{NA,Any}, IB::NTuple{NB,Any}, IC::NTuple{NC,
IopenB = tsetdiff(IB, Icontract)

# to indices
cindA = map(l->findfirst(IA, l), Icontract)
cindB = map(l->findfirst(IB, l), Icontract)
oindA = map(l->findfirst(IA, l), IopenA)
oindB = map(l->findfirst(IB, l), IopenB)
indCinoAB = map(l->findfirst((IopenA..., IopenB...), l), IC)
cindA = map(l->findfirst(equalto(l), IA), Icontract)
cindB = map(l->findfirst(equalto(l), IB), Icontract)
oindA = map(l->findfirst(equalto(l), IA), IopenA)
oindB = map(l->findfirst(equalto(l), IB), IopenB)
indCinoAB = map(l->findfirst(equalto(l), (IopenA..., IopenB...)), IC)

if !isperm((oindA..., cindA...)) || !isperm((oindB..., cindB...)) || !isperm(indCinoAB)
throw(IndexError("invalid contraction pattern: $IA and $IB to $IC"))
Expand Down
28 changes: 14 additions & 14 deletions test/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ C2 = kron(reshape(B,(25,25)),reshape(A,(25,25)))

# tensorcopy!
Abig = randn((30,30,30,30))
A = view(Abig,1+3*(0:9),2+2*(0:6),5+4*(0:6),4+3*(0:8))
A = view(Abig,1 3*(0:9),2 2*(0:6),5 4*(0:6),4 3*(0:8))
p = [3,1,4,2]
Cbig = zeros(Complex128,(50,50,50,50))
C = view(Cbig,13+(0:6),11+4*(0:9),15+4*(0:8),4+3*(0:6))
C = view(Cbig,13 (0:6),11 4*(0:9),15 4*(0:8),4 3*(0:6))
Acopy = tensorcopy(A,1:4,1:4)
Ccopy = tensorcopy(C,1:4,1:4)
TensorOperations.tensorcopy!(A,1:4,C,p)
Expand All @@ -82,7 +82,7 @@ TensorOperations.tensorcopy!(Acopy,1:4,Ccopy,p)

# tensoradd!
Cbig = zeros(Complex128,(50,50,50,50))
C = view(Cbig,13+(0:6),11+4*(0:9),15+4*(0:8),4+3*(0:6))
C = view(Cbig,13 (0:6),11 4*(0:9),15 4*(0:8),4 3*(0:6))
Acopy = tensorcopy(A,1:4,p)
Ccopy = tensorcopy(C,1:4,1:4)
alpha = randn()
Expand All @@ -96,16 +96,16 @@ Ccopy = beta*Ccopy+alpha*Acopy

# tensortrace!
Abig = rand((30,30,30,30))
A = view(Abig,1+3*(0:8),2+2*(0:14),5+4*(0:6),7+2*(0:8))
A = view(Abig,1 3*(0:8),2 2*(0:14),5 4*(0:6),7 2*(0:8))
Bbig = rand(Complex128,(50,50))
B = view(Bbig,13+(0:14),3+5*(0:6))
B = view(Bbig,13 (0:14),3 5*(0:6))
Acopy = tensorcopy(A,1:4)
Bcopy = tensorcopy(B,1:2)
alpha = randn()
beta = randn()
TensorOperations.tensortrace!(alpha,A,[:a,:b,:c,:a],beta,B,[:b,:c])
Bcopy = beta*Bcopy
for i = 1+(0:8)
for i = 1 (0:8)
Bcopy += alpha*view(A,i,:,:,i)
end
@test vecnorm(B-Bcopy)<eps()*vecnorm(B+Bcopy)*sqrt(length(B))
Expand All @@ -116,30 +116,30 @@ end

# tensorcontract!
Abig = rand((30,30,30,30))
A = view(Abig,1+3*(0:8),2+2*(0:14),5+4*(0:6),7+2*(0:8))
A = view(Abig,1 3*(0:8),2 2*(0:14),5 4*(0:6),7 2*(0:8))
Bbig = rand(Complex128,(50,50,50))
B = view(Bbig,3+5*(0:6),7+2*(0:7),13+(0:14))
B = view(Bbig,3 5*(0:6),7 2*(0:7),13 (0:14))
Cbig = rand(Complex64,(40,40,40))
C = view(Cbig,3+2*(0:8),13+(0:8),7+3*(0:7))
C = view(Cbig,3 2*(0:8),13 (0:8),7 3*(0:7))
Acopy = tensorcopy(A,1:4)
Bcopy = tensorcopy(B,1:3)
Ccopy = tensorcopy(C,1:3)
alpha = randn()
beta = randn()
Ccopy = beta*Ccopy
for d = 1+(0:8),a = 1+(0:8),e = 1+(0:7)
for b = 1+(0:14),c = 1+(0:6)
for d = 1 (0:8),a = 1 (0:8),e = 1 (0:7)
for b = 1 (0:14),c = 1 (0:6)
Ccopy[d,a,e] += alpha*A[a,b,c,d]*conj(B[c,e,b])
end
end
TensorOperations.tensorcontract!(alpha,A,[:a,:b,:c,:d],'N',B,[:c,:e,:b],'C',beta,C,[:d,:a,:e];method = :BLAS)
@test vecnorm(C-Ccopy)<eps(Float32)*vecnorm(C+Ccopy)*sqrt(length(C))
Cbig = rand(Complex64,(40,40,40))
C = view(Cbig,3+2*(0:8),13+(0:8),7+3*(0:7))
C = view(Cbig,3 2*(0:8),13 (0:8),7 3*(0:7))
Ccopy = tensorcopy(C,1:3)
Ccopy = beta*Ccopy
for d = 1+(0:8),a = 1+(0:8),e = 1+(0:7)
for b = 1+(0:14),c = 1+(0:6)
for d = 1 (0:8),a = 1 (0:8),e = 1 (0:7)
for b = 1 (0:14),c = 1 (0:6)
Ccopy[d,a,e] += alpha*A[a,b,c,d]*conj(B[c,e,b])
end
end
Expand Down
10 changes: 9 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
if VERSION < v"0.7.0-DEV.2005"
const Test = Base.Test
end

# Until problems with .+ transforming ranges to arrays are settled
(s::Int, r::StepRange{Int,Int}) = (first(r)+s):step(r):(last(r)+s)
(s::Int, r::UnitRange{Int}) = (first(r)+s):(last(r)+s)

using Test
using TensorOperations
using Base.Test

include("methods.jl")
include("tensor.jl")
28 changes: 14 additions & 14 deletions test/tensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ end
@test vecnorm(D1) sqrt(abs(@tensor scalar(D1[d,f,h]*conj(D1[d,f,h]))))

Abig=randn((30,30,30,30))
A=view(Abig,1+3*(0:9),2+2*(0:6),5+4*(0:6),4+3*(0:8))
A=view(Abig,1 3*(0:9),2 2*(0:6),5 4*(0:6),4 3*(0:8))
p=[3,1,4,2]
Cbig=zeros(Complex128,(50,50,50,50))
C=view(Cbig,13+(0:6),11+4*(0:9),15+4*(0:8),4+3*(0:6))
C=view(Cbig,13 (0:6),11 4*(0:9),15 4*(0:8),4 3*(0:6))
Acopy = copy(A)
Ccopy = copy(C)
@tensor C[3,1,4,2] = A[1,2,3,4]
Expand All @@ -103,7 +103,7 @@ end
end

Cbig=zeros(Complex128,(50,50,50,50))
C=view(Cbig,13+(0:6),11+4*(0:9),15+4*(0:8),4+3*(0:6))
C=view(Cbig,13 (0:6),11 4*(0:9),15 4*(0:8),4 3*(0:6))
Acopy=permutedims(copy(A), p)
Ccopy=copy(C)
alpha=randn()
Expand All @@ -122,14 +122,14 @@ end
end

Abig=rand((30,30,30,30))
A=view(Abig,1+3*(0:8),2+2*(0:14),5+4*(0:6),7+2*(0:8))
A=view(Abig,1 3*(0:8),2 2*(0:14),5 4*(0:6),7 2*(0:8))
Bbig=rand(Complex128,(50,50))
B=view(Bbig,13+(0:14),3+5*(0:6))
B=view(Bbig,13 (0:14),3 5*(0:6))
Acopy=copy(A)
Bcopy=copy(B)
alpha=randn()
@tensor B[b,c] += alpha*A[a,b,c,a]
for i=1+(0:8)
for i=1 (0:8)
Bcopy+=alpha*view(A,i,:,:,i)
end
@test vecnorm(B-Bcopy)<eps()*vecnorm(B+Bcopy)*sqrt(length(B))
Expand All @@ -147,28 +147,28 @@ end
end

Abig=rand((30,30,30,30))
A=view(Abig,1+3*(0:8),2+2*(0:14),5+4*(0:6),7+2*(0:8))
A=view(Abig,1 3*(0:8),2 2*(0:14),5 4*(0:6),7 2*(0:8))
Bbig=rand(Complex128,(50,50,50))
B=view(Bbig,3+5*(0:6),7+2*(0:7),13+(0:14))
B=view(Bbig,3 5*(0:6),7 2*(0:7),13 (0:14))
Cbig=rand(Complex64,(40,40,40))
C=view(Cbig,3+2*(0:8),13+(0:8),7+3*(0:7))
C=view(Cbig,3 2*(0:8),13 (0:8),7 3*(0:7))
Acopy=copy(A)
Bcopy=copy(B)
Ccopy=copy(C)
alpha=randn()
for d=1+(0:8),a=1+(0:8),e=1+(0:7)
for b=1+(0:14),c=1+(0:6)
for d=1 (0:8),a=1 (0:8),e=1 (0:7)
for b=1 (0:14),c=1 (0:6)
Ccopy[d,a,e] -=alpha*A[a,b,c,d]*conj(B[c,e,b])
end
end
@tensor C[d,a,e] -= alpha*A[a,b,c,d]*conj(B[c,e,b])
@test vecnorm(C-Ccopy)<eps(Float32)*vecnorm(C+Ccopy)*sqrt(length(C))
Cbig=rand(Complex64,(40,40,40))
C=view(Cbig,3+2*(0:8),13+(0:8),7+3*(0:7))
C=view(Cbig,3 2*(0:8),13 (0:8),7 3*(0:7))
Ccopy=copy(C)

for d=1+(0:8),a=1+(0:8),e=1+(0:7)
for b=1+(0:14),c=1+(0:6)
for d=1 (0:8),a=1 (0:8),e=1 (0:7)
for b=1 (0:14),c=1 (0:6)
Ccopy[d,a,e] += alpha*A[a,b,c,d]*conj(B[c,e,b])
end
end
Expand Down

3 comments on commit 670817b

@garrison
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is leading to some warnings under julia 0.6:

julia> using TensorOperations
WARNING: Method definition ntuple(F, Base.Val{N}) in module Compat at /home/garrison/.julia/v0.6/Compat/src/Compat.jl:525 overwritten in module TensorOperations at /home/garrison/.julia/v0.6/TensorOperations/src/TensorOperations.jl:13.
WARNING: Method definition reshape(AbstractArray{T, N} where N where T, Base.Val{N}) in module Compat at /home/garrison/.julia/v0.6/Compat/src/Compat.jl:523 overwritten in module TensorOperations at /home/garrison/.julia/v0.6/TensorOperations/src/TensorOperations.jl:11.
WARNING: Method definition (::Type{Base.Val{T} where T})(Any) in module Compat at /home/garrison/.julia/v0.6/Compat/src/Compat.jl:520 overwritten in module TensorOperations at /home/garrison/.julia/v0.6/TensorOperations/src/TensorOperations.jl:8.

@Jutho
Copy link
Owner Author

@Jutho Jutho commented on 670817b Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting. I believe this is now fixed. Could you check with latest master?

@garrison
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works perfectly for me now. Thanks for the fix!

Please sign in to comment.