Skip to content

[BUG] call(::BlockSparseArrayInterface, ::typeof(similar), ::BlockSparseArray is ambiguous #98

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

Closed
ogauthe opened this issue Apr 8, 2025 · 7 comments · Fixed by #99
Closed
Labels
bug Something isn't working

Comments

@ogauthe
Copy link
Collaborator

ogauthe commented Apr 8, 2025

This issue is triggered by vector-vector contraction in #95
a_dest, dimnames_dest = contract(a3, (1, 2), a3, (2, 1)) # from test_tensoralgebraext

ERROR: MethodError: call(::BlockSparseArrayInterface, ::typeof(similar), ::BlockSparseArray{…}, ::Type{…}, ::Tuple{…}) is ambiguous.

Candidates:
  call(::AbstractBlockSparseArrayInterface, ::typeof(similar), a::AbstractArray, elt::Type, axes::Tuple)
    @ BlockSparseArrays ~/Documents/itensor/BlockSparseArrays.jl/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl:230
  call(interface::DerivableInterfaces.AbstractArrayInterface, ::typeof(similar), a::AbstractArray, T::Type, axes::Tuple{Base.OneTo, Vararg{Base.OneTo}})
    @ DerivableInterfaces ~/.julia/packages/DerivableInterfaces/ZNBYA/src/abstractarrayinterface.jl:99

Possible fix, define
  call(::AbstractBlockSparseArrayInterface, ::typeof(similar), ::AbstractArray, ::Type, ::Tuple{Base.OneTo, Vararg{…}})

Stacktrace:
  [1] (::DerivableInterfaces.InterfaceFunction{…})(::BlockSparseArray{…}, ::Vararg{…}; kwargs::@Kwargs{})
    @ DerivableInterfaces ~/.julia/packages/DerivableInterfaces/ZNBYA/src/interface_function.jl:17
  [2] similar(a::BlockSparseArray{…}, elt::Type, axes::Tuple{…})
    @ BlockSparseArrays ~/Documents/itensor/BlockSparseArrays.jl/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl:323
  [3] similar(a::BlockSparseArray{…}, dims::Tuple{…})
    @ BlockArrays ~/.julia/packages/BlockArrays/XczLX/src/abstractblockarray.jl:37
  [4] blockreshape(a::BlockSparseArray{…}, axes::Tuple{…})
    @ BlockSparseArrays ~/Documents/itensor/BlockSparseArrays.jl/src/BlockArraysExtensions/BlockArraysExtensions.jl:278
  [5] matricize(::BlockSparseArraysTensorAlgebraExt.BlockReshapeFusion, a::BlockSparseArray{…}, biperm::TensorAlgebra.BlockedTrivialPermutation{…})
    @ BlockSparseArraysTensorAlgebraExt ~/Documents/itensor/BlockSparseArrays.jl/ext/BlockSparseArraysTensorAlgebraExt/BlockSparseArraysTensorAlgebraExt.jl:22
  [6] matricize(style::BlockSparseArraysTensorAlgebraExt.BlockReshapeFusion, a::BlockSparseArray{…}, biperm::TensorAlgebra.BlockedPermutation{…})
    @ TensorAlgebra ~/Documents/itensor/TensorAlgebra.jl/src/matricize.jl:64
  [7] matricize(a::BlockSparseArray{…}, biperm::TensorAlgebra.BlockedPermutation{…})
    @ TensorAlgebra ~/Documents/itensor/TensorAlgebra.jl/src/matricize.jl:57
  [8] contract!(::TensorAlgebra.Matricize, a_dest::BlockSparseArray{…}, biperm_dest::TensorAlgebra.BlockedPermutation{…}, a1::BlockSparseMatrix{…}, biperm1::TensorAlgebra.BlockedPermutation{…}, a2::BlockSparseMatrix{…}, biperm2::TensorAlgebra.BlockedPermutation{…}, α::Bool, β::Bool)
    @ TensorAlgebra ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract_matricize/contract.jl:14
  [9] contract(alg::TensorAlgebra.Matricize, biperm_dest::TensorAlgebra.BlockedPermutation{…}, a1::BlockSparseMatrix{…}, biperm1::TensorAlgebra.BlockedPermutation{…}, a2::BlockSparseMatrix{…}, biperm2::TensorAlgebra.BlockedPermutation{…}, α::Bool; kwargs::@Kwargs{})
    @ TensorAlgebra ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract.jl:122
 [10] contract(alg::TensorAlgebra.Matricize, biperm_dest::TensorAlgebra.BlockedPermutation{…}, a1::BlockSparseMatrix{…}, biperm1::TensorAlgebra.BlockedPermutation{…}, a2::BlockSparseMatrix{…}, biperm2::TensorAlgebra.BlockedPermutation{…}, α::Bool)
    @ TensorAlgebra ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract.jl:111
 [11] contract(alg::TensorAlgebra.Matricize, labels_dest::Tuple{}, a1::BlockSparseMatrix{…}, labels1::Tuple{…}, a2::BlockSparseMatrix{…}, labels2::Tuple{…}, α::Bool; kwargs::@Kwargs{})
    @ TensorAlgebra ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract.jl:92
 [12] contract(alg::TensorAlgebra.Matricize, labels_dest::Tuple{}, a1::BlockSparseMatrix{…}, labels1::Tuple{…}, a2::BlockSparseMatrix{…}, labels2::Tuple{…}, α::Bool)
    @ TensorAlgebra ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract.jl:81
 [13] contract(alg::TensorAlgebra.Matricize, a1::BlockSparseMatrix{…}, labels1::Tuple{…}, a2::BlockSparseMatrix{…}, labels2::Tuple{…}, α::Bool; kwargs::@Kwargs{})
    @ TensorAlgebra ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract.jl:49
 [14] contract
    @ ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract.jl:39 [inlined]
 [15] #contract#27
    @ ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract.jl:36 [inlined]
 [16] contract
    @ ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract.jl:27 [inlined]
 [17] contract(a1::BlockSparseMatrix{…}, labels1::Tuple{…}, a2::BlockSparseMatrix{…}, labels2::Tuple{…})
    @ TensorAlgebra ~/Documents/itensor/TensorAlgebra.jl/src/contract/contract.jl:27
 [18] top-level scope
    @ REPL[75]:1
Some type information was truncated. Use `show(err)` to see complete types.
@ogauthe ogauthe added the bug Something isn't working label Apr 8, 2025
@mtfishman
Copy link
Member

Thanks for the report, looking into it now.

@mtfishman
Copy link
Member

mtfishman commented Apr 8, 2025

It seems like the issue is caused by the fact that this line: https://github.com/ogauthe/BlockSparseArrays.jl/blob/6c2204bf9b18e53bb9c9c11827681c91bce8222c/ext/BlockSparseArraysTensorAlgebraExt/BlockSparseArraysTensorAlgebraExt.jl#L21 constructs axes (Base.OneTo(1), Base.OneTo(1)), since the input axes are empty so that's the best guess that trivial_axis has in this line: https://github.com/ITensor/TensorAlgebra.jl/blob/e235d04e685e84f84ab33676422c06c612954581/src/matricize.jl#L36.

Maybe there could be a version of fuseaxes and trivial_axis that accepts the array as an input, so then we can customize them to output blockedrange([1]) in this case.

@mtfishman
Copy link
Member

Fundamentally, the error is being thrown because that similar call on BlockSparseArray doesn't anticipate that a non-blocked range (Base.OneTo) might be passed, we can and should handle that case more gracefully but I think that is a bit orthogonal to this issue, and we should pass a blocked range in this case in the first place.

@mtfishman
Copy link
Member

I was able to fix the original issue with just changes to this package, see #99. But I think we should also make TensorAlgebra.fuseaxes and TensorAlgebra.trivial_axis more customizable.

Maybe for now you can incorporate #99 into #95.

@ogauthe
Copy link
Collaborator Author

ogauthe commented Apr 8, 2025

There are similar issues with graded arrays, I will work on fuseaxes.

@ogauthe
Copy link
Collaborator Author

ogauthe commented Apr 9, 2025

minimal example that triggers error on main:

using BlockSparseArrays: BlockSparseArray
a = BlockSparseArray{Float64}(undef)
similar(a, Float64, (Base.OneTo(1),))
ERROR: MethodError: call(::BlockSparseArrays.BlockSparseArrayInterface, ::typeof(similar), ::BlockSparseArray{…}, ::Type{…}, ::Tuple{…}) is ambiguous.

Candidates:
  call(::BlockSparseArrays.AbstractBlockSparseArrayInterface, ::typeof(similar), a::AbstractArray, elt::Type, axes::Tuple)
    @ BlockSparseArrays ~/.julia/packages/BlockSparseArrays/1g9zF/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl:230
  call(interface::DerivableInterfaces.AbstractArrayInterface, ::typeof(similar), a::AbstractArray, T::Type, axes::Tuple{Base.OneTo, Vararg{Base.OneTo}})
    @ DerivableInterfaces ~/.julia/packages/DerivableInterfaces/ZNBYA/src/abstractarrayinterface.jl:99

Possible fix, define
  call(::BlockSparseArrays.AbstractBlockSparseArrayInterface, ::typeof(similar), ::AbstractArray, ::Type, ::Tuple{…})

Stacktrace:
 [1] (::DerivableInterfaces.InterfaceFunction{…})(::BlockSparseArray{…}, ::Vararg{…}; kwargs::@Kwargs{})
   @ DerivableInterfaces ~/.julia/packages/DerivableInterfaces/ZNBYA/src/interface_function.jl:17
 [2] similar(a::BlockSparseArray{…}, elt::Type, axes::Tuple{…})
   @ BlockSparseArrays ~/.julia/packages/BlockSparseArrays/1g9zF/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl:323
 [3] top-level scope
   @ REPL[3]:1
Some type information was truncated. Use `show(err)` to see complete types.

@mtfishman
Copy link
Member

That should be fixed by #99.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants