Skip to content

Conversation

@mofeing
Copy link
Collaborator

@mofeing mofeing commented Dec 7, 2024

the implementation forces to perform the dimension permutation (unlike the semantics of PermuteDimsArray which just stores the unaffected array and an affine map) because MLIR tensor doesn't work with MLIR affine maps and we can optimize the stablehlo.transpose anyway


# force permutation of dims, because we can optimize it anyway
# TODO should we add a method for `PermutedDimsArray` with type params?
PermutedDimsArray(x::TracedRArray, perm) = permutedims(x, perm)
Copy link
Member

Choose a reason for hiding this comment

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

I don’t think this is correct since this will create a new array. Thus if you have

a
b = PermuteDimsArray(a)

a change to b should result in a change to a, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no, because a must still reference the array before the tranposition

b is another array; it's just that the transposition is performed lazily on getindex/setindex!

Copy link
Member

Choose a reason for hiding this comment

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


julia> x = ones(4,4)
4×4 Matrix{Float64}:
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0

julia> y = PermutedDimsArray(x, (2,1))
4×4 PermutedDimsArray(::Matrix{Float64}, (2, 1)) with eltype Float64:
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0

julia> y[3,4] = 2
2

julia> y
4×4 PermutedDimsArray(::Matrix{Float64}, (2, 1)) with eltype Float64:
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  2.0
 1.0  1.0  1.0  1.0

julia> x
4×4 Matrix{Float64}:
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0
 1.0  1.0  2.0  1.0

julia> 

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, you're right

what would the solution be then? my problem is with these lines https://github.com/bsc-quantic/Tenet.jl/blob/c7dbf2513d2edd80829e978319e05fd720bf7cfc/src/Numerics.jl#L21-L31

should i specialize +(::TracedRArray, ::PermuteDimsArray{TracedRArray}) and -(::TracedRArray, ::PermuteDimsArray{TracedRArray}) to call Ops.transpose just inside those methods?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should specialize for AnyTracedRArray and call materialize_traced_array for the rhs arguments

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

yeah think so

Copy link
Collaborator

Choose a reason for hiding this comment

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

@mofeing can you check if #342 handles your problem

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah, it fixes it!

@mofeing
Copy link
Collaborator Author

mofeing commented Dec 9, 2024

Closing in favor of #342

@mofeing mofeing closed this Dec 9, 2024
@giordano giordano deleted the ss/permutedimsarray branch January 17, 2025 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants