Skip to content
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

adding transpose!() to linear algebra #808

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tharittk
Copy link
Contributor

Hello, I'm an applicant to GSoC this summer. Avik told me that LinearAlgebra.jl is good for getting up to speed.

I started with what I assume is the most simple function.

I hope I took the right approach by re-using the functions from Ops.jl

Please advise if I make any mistakes or anything I can do better.

Copy link
Collaborator

@mofeing mofeing left a comment

Choose a reason for hiding this comment

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

lgtm


@testset "transpose!" begin
v = zeros(5)
M = rand(1, 5)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you mind not using rand? we had problems tracking numerical stability and bugs due to rand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem. I will make a change to that. Also, the rest of the test set uses rand. Should I also change them?


function LinearAlgebra.transpose!(B::AnyTracedRMatrix, A::AnyTracedRMatrix)
LinearAlgebra.check_transpose_axes(size(B), size(A))
set_mlir_data!(B, get_mlir_data(Ops.transpose(A, [2,1])))
Copy link
Collaborator

Choose a reason for hiding this comment

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

@avik-pal @wsmoses Should we enforce same eltype for both args? if not, there can be undetected problems when we pass two arrays of same size but different eltype.

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 do a promote and set here. Base implements it like B[j,i] = f(A[i,j]) so its auto promoted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

noted. I'll go fix this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

i know we do promote here but is it the correct thing? e.g. in transpose!(::TracedRArray{Int}, ::TracedRArray{Float64}), promotion of eltype will be Float64 and the result we are storing will be wrong.

or should the promotion handle it and error in that case?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think let's update Ops.convert to throw errors in cases where Julia would also throw an error.

Then we just do Ops.convert(..., Ops.transpose(A, [2,1]))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm wrestling with the Ops.convert() for a while now. It looks like the stablehlo spec is that it performs unsafe truncation by default while Julia does not. And I could not find any Julia function that takes T1, T2 and be able to tell whether type conversion will be safe.

Julia seems to determine if the conversion is OK (like throwing InexactError or not) during run-time by explicitly converting the value to type T (i.e. 1.0 -> 1 is ok, for example).

In the case of TracedRNumber, maybe we can just do T(x) directly and let Julia throw error - if any (not sure yet how to go from mlir_dat -> Julia primitives).

In case of TracedRArray, I don't really know. Is there anything or some obvious solutions I am missing?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah I see what's going on. Unfortunately stablehlo doesn't seem to have conversion mode supported (openxla/stablehlo#180). We can do the convert here and just defer to stablehlo to handle the truncation.

cc @wsmoses any thoughts here?

@@ -397,4 +397,21 @@ function LinearAlgebra._kron!(C::AnyTracedRMatrix, A::AnyTracedRMatrix, B::AnyTr
return C
end

LinearAlgebra.transpose(a::AnyTracedRArray) = error("transpose not defined for $(typeof(a)).")
Copy link
Member

Choose a reason for hiding this comment

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

is there a reason to add this? Shouldn't it fall back to the inplace versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The stdlib LinearAlgebra.jl has this:
transpose(a::AbstractArray) = error("transpose not defined for $(typeof(a)). Consider using `permutedims` for higher-dimensional arrays.")

The transpose!() docstring also mentions about not supporting the in-place version.

"""
transpose!(dest,src)

Transpose array src and store the result in the preallocated array dest, which should
have a size corresponding to (size(src,2),size(src,1)). No in-place transposition is
supported and unexpected results will happen if src and dest have overlapping memory
regions.
"""

So yeah, I was just trying to mimic it exactly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

i guess that then you can leave it unimplemented and it will call the default method which will error like is supposed to

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