willow-ahrens / Finch.jl

Sparse tensors in Julia and more! Datastructure-driven array programing language.
http://willowahrens.io/Finch.jl/
MIT License
158 stars 15 forks source link

Wrapping `SwizzleArray` in `LazyTensor` fails #457

Closed mtsokol closed 6 months ago

mtsokol commented 6 months ago

Hi @willow-ahrens,

Then passing SwizzleArray to LazyTensor constructor, to create a lazy object:

using Finch

A = zeros(Int64, (2, 4))
A_tns = Tensor(Dense(Dense(Element(0))), A)
A_sw = swizzle(A_tns, 1, 2)
Finch.LazyTensor(A_sw)

I get an error:

ERROR: MethodError: no method matching Finch.LazyTensor(::Finch.SwizzleArray{(1, 2), Tensor{DenseLevel{Int64, DenseLevel{Int64, ElementLevel{…}}}}})

Closest candidates are:
  Finch.LazyTensor(::Finch.LazyTensor)
   @ Finch ~/JuliaProjects/Finch.jl/src/interface/lazy.jl:39
  Finch.LazyTensor(::Number)
   @ Finch ~/JuliaProjects/Finch.jl/src/interface/lazy.jl:21
  Finch.LazyTensor(::Tensor)
   @ Finch ~/JuliaProjects/Finch.jl/src/interface/lazy.jl:31
  ...
willow-ahrens commented 6 months ago

Could you push a fix for this? We just need to convert the underlying tensor to a LazyTensor A, then permute the extrude field

B.extrude = A.extrude[dims]

and wrap the query in the appropriate reordering

idxs = [field(Symbol(:i_, n)) for n = 1:N]; 
B.data = reorder(relabel(A.data, idxs...), idxs[dims]...)
willow-ahrens commented 6 months ago

incidentally, this is also how we would support permutedims(Tensor) and swizzle(lazyTensor)

willow-ahrens commented 6 months ago

If you want, your PR could also add support for permutedims in the eager.jl file by converting to lazy, permuting dims, and converting back.

mtsokol commented 6 months ago

Sure! I will work on this on Monday.