-
Notifications
You must be signed in to change notification settings - Fork 18
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)).") | ||
|
||
function LinearAlgebra.transpose!(B::AnyTracedRVector, A::AnyTracedRMatrix) | ||
LinearAlgebra.check_transpose_axes((size(B,1), size(B,2)), size(A)) | ||
set_mlir_data!(B, get_mlir_data(Ops.reshape(A, length(B)))) | ||
end | ||
|
||
function LinearAlgebra.transpose!(B::AnyTracedRMatrix, A::AnyTracedRVector) | ||
LinearAlgebra.check_transpose_axes(size(B), (size(A, 1), size(A, 2))) | ||
set_mlir_data!(B, get_mlir_data(Ops.broadcast_in_dim(A, [2], [1, length(A)]))) | ||
end | ||
|
||
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]))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should do a promote and set here. Base implements it like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noted. I'll go fix this. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 or should the promotion handle it and error in that case? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,3 +183,29 @@ end | |
end | ||
end | ||
end | ||
|
||
@testset "transpose!" begin | ||
v = zeros(5) | ||
M = rand(1, 5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind not using There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
v_ra = Reactant.to_rarray(v) | ||
M_ra = Reactant.to_rarray(M) | ||
|
||
@jit transpose!(v_ra, M_ra) | ||
@test v_ra ≈ transpose!(v, M) | ||
|
||
v = rand(7) | ||
M = zeros(1, 7) | ||
v_ra = Reactant.to_rarray(v) | ||
M_ra = Reactant.to_rarray(M) | ||
|
||
@jit transpose!(M_ra, v_ra) | ||
@test M_ra ≈ transpose!(M, v) | ||
|
||
A = rand(3, 7) | ||
B = rand(7, 3) | ||
A_ra = Reactant.to_rarray(A) | ||
B_ra = Reactant.to_rarray(B) | ||
@jit transpose!(B_ra, A_ra) | ||
@test B_ra ≈ transpose!(B, A) | ||
end | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 arraydest
, which shouldhave a size corresponding to
(size(src,2),size(src,1))
. No in-place transposition issupported and unexpected results will happen if
src
anddest
have overlapping memoryregions.
"""
So yeah, I was just trying to mimic it exactly.
There was a problem hiding this comment.
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