Skip to content

[BUG] BlockArraysExtensions breaks TensorAlgebra.contract #57

@ogauthe

Description

@ogauthe

BlockArraysExtensions breaks TensorAlgebra.contract. All tests introduced in ITensor/TensorAlgebra.jl#34 pass when BlockSparseArrays.jl is not loaded (with the exception of zero-dim output type).

using BlockArrays: BlockArray, blockedrange
using TensorAlgebra: contract

d = blockedrange([2, 3])
a = BlockArray(ones((d,d,d,d)))
a_dest, dimnames_dest = contract(a, (1, -1, 2, -2), a, (2, -3, 1, -4))  # ok

using BlockSparseArrays   # load BlockSparseArraysTensorAlgebraExt and BlockArraysExtension
a_dest, dimnames_dest = contract(a, (1, -1, 2, -2), a, (2, -3, 1, -4))  # now crashes
ERROR: BoundsError: attempt to access Int64 at index [2]
Stacktrace:
  [1] getindex
    @ ./number.jl:98 [inlined]
  [2] #13
    @ ~/.julia/packages/BlockSparseArrays/RsnSQ/src/BlockArraysExtensions/BlockArraysExtensions.jl:281 [inlined]
  [3] map
    @ ./tuple.jl:356 [inlined]
  [4] blockreshape(a::BlockArray{Float64, 4, Array{…}, NTuple{…}}, axes::Tuple{BlockedOneTo{…}, BlockedOneTo{…}})
    @ BlockSparseArrays ~/.julia/packages/BlockSparseArrays/RsnSQ/src/BlockArraysExtensions/BlockArraysExtensions.jl:281
  [5] fusedims
    @ ~/.julia/packages/BlockSparseArrays/RsnSQ/ext/BlockSparseArraysTensorAlgebraExt/BlockSparseArraysTensorAlgebraExt.jl:19 [inlined]
  [6] fusedims
    @ ~/.julia/packages/TensorAlgebra/yUoY0/src/fusedims.jl:33 [inlined]
  [7] fusedims
    @ ~/.julia/packages/TensorAlgebra/yUoY0/src/fusedims.jl:42 [inlined]
  [8] fusedims
    @ ~/.julia/packages/TensorAlgebra/yUoY0/src/fusedims.jl:67 [inlined]
  [9] fusedims
    @ ~/.julia/packages/TensorAlgebra/yUoY0/src/fusedims.jl:72 [inlined]
 [10] contract!(alg::TensorAlgebra.Matricize, a_dest::BlockArray{…}, biperm_dest::TensorAlgebra.BlockedPermutation{…}, a1::BlockArray{…}, biperm1::TensorAlgebra.BlockedPermutation{…}, a2::BlockArray{…}, biperm2::TensorAlgebra.BlockedPermutation{…}, α::Bool, β::Bool)
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract_matricize/contract.jl:14
 [11] #contract#33
    @ ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract.jl:122 [inlined]
 [12] contract(alg::TensorAlgebra.Matricize, biperm_dest::TensorAlgebra.BlockedPermutation{…}, a1::BlockArray{…}, biperm1::TensorAlgebra.BlockedPermutation{…}, a2::BlockArray{…}, biperm2::TensorAlgebra.BlockedPermutation{…}, α::Bool)
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract.jl:111
 [13] contract(alg::TensorAlgebra.Matricize, labels_dest::NTuple{…}, a1::BlockArray{…}, labels1::NTuple{…}, a2::BlockArray{…}, labels2::NTuple{…}, α::Bool; kwargs::@Kwargs{})
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract.jl:92
 [14] contract(alg::TensorAlgebra.Matricize, labels_dest::NTuple{…}, a1::BlockArray{…}, labels1::NTuple{…}, a2::BlockArray{…}, labels2::NTuple{…}, α::Bool)
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract.jl:81
 [15] contract(alg::TensorAlgebra.Matricize, a1::BlockArray{…}, labels1::NTuple{…}, a2::BlockArray{…}, labels2::NTuple{…}, α::Bool; kwargs::@Kwargs{})
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract.jl:49
 [16] contract
    @ ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract.jl:39 [inlined]
 [17] #contract#27
    @ ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract.jl:36 [inlined]
 [18] contract
    @ ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract.jl:27 [inlined]
 [19] contract(a1::BlockArray{…}, labels1::NTuple{…}, a2::BlockArray{…}, labels2::NTuple{…})
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/yUoY0/src/contract/contract.jl:27
 [20] top-level scope
    @ REPL[7]:1
Some type information was truncated. Use `show(err)` to see complete types.

I find it surprising that TensorAlgebra.contract(::BlockArray) behavior changes whether or not BlockSparseArrays is loaded. I believe we should refactor package extensions to avoid this, probably creating a new TensorAlgebra/BlockArraysExtension. We may then make TensorAlgebra a strong dependency of BlockSparseArray, then the extension will always be loaded to be used with a BlockSparseArray.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions