-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: overload mul!
#230
Merged
+94
−9
Merged
feat: overload mul!
#230
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5f62262
feat: overload `mul!`
avik-pal 7ea5492
feat: overload the 5 arg version
avik-pal 84a8b1d
fix: add LinearAlgebra to test
avik-pal 54c7897
fix: special cases for alpha and beta
avik-pal d18a429
feat: directly emit stablehlo ops
avik-pal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -381,12 +381,24 @@ for (jlop, hloop, hlocomp, merge) in | |
end | ||
end | ||
|
||
function Base.:*( | ||
@nospecialize(lhs::TracedRArray{T,2}), @nospecialize(rhs::TracedRArray{T,2}) | ||
) where {T} | ||
lhsty = MLIR.IR.type(lhs.mlir_data) | ||
rhsty = MLIR.IR.type(rhs.mlir_data) | ||
resty = MLIR.IR.TensorType((size(lhs, 1), size(rhs, 2)), eltype(lhsty)) | ||
function LinearAlgebra.mul!( | ||
@nospecialize(C::TracedRArray{T1,2}), | ||
@nospecialize(A::TracedRArray{T2,2}), | ||
@nospecialize(B::TracedRArray{T3,2}), | ||
α::Number=true, | ||
β::Number=false, | ||
) where {T1,T2,T3} | ||
if size(C) != (size(A, 1), size(B, 2)) | ||
throw( | ||
DimensionMismatch( | ||
"C has size $(size(C)), A has size $(size(A)), B has size $(size(B))" | ||
), | ||
) | ||
end | ||
if size(A, 2) != size(B, 1) | ||
throw(DimensionMismatch("A has size $(size(A)), B has size $(size(B))")) | ||
end | ||
resty = MLIR.IR.TensorType(size(C), MLIR.IR.Type(T1)) | ||
dot_dimension_numbers = MLIR.API.stablehloDotDimensionNumbersGet( | ||
MLIR.IR.context(), 0, [], 0, [], 1, [1], 1, [0] | ||
) | ||
|
@@ -396,15 +408,25 @@ function Base.:*( | |
precar = MLIR.IR.Attribute([prec, prec]) | ||
res = MLIR.IR.result( | ||
MLIR.Dialects.stablehlo.dot_general( | ||
lhs.mlir_data, | ||
rhs.mlir_data; | ||
A.mlir_data, | ||
B.mlir_data; | ||
result_0=resty, | ||
dot_dimension_numbers=dot_dimension_numbers, | ||
precision_config=precar, | ||
), | ||
1, | ||
) | ||
return TracedRArray{T,2}((), res, (size(lhs, 1), size(rhs, 2))) | ||
if iszero(β) | ||
if isone(α) | ||
C.mlir_data = res | ||
else | ||
C.mlir_data = (TracedRArray{T1,2}((), res, size(C)) .* α).mlir_data | ||
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. doing a broadcast here probably won't optimize well for now (or rather we need to write optimizations on the batch op. Being able to driectly emit the elementwise mul of a broadcast will likely be a nontrivial perf win as a result |
||
end | ||
else | ||
res = TracedRArray{T1,2}((), res, size(C)) | ||
@. C = C * β + res * α | ||
end | ||
return C | ||
end | ||
|
||
function Enzyme.Compiler.active_reg_inner( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using LinearAlgebra, Reactant | ||
|
||
function muladd2(A, x, b) | ||
C = similar(A, promote_type(eltype(A), eltype(b)), size(A, 1), size(x, 2)) | ||
mul!(C, A, x) | ||
C .+= b | ||
return C | ||
end | ||
|
||
function muladd_5arg(A, x, b) | ||
C = similar(A, promote_type(eltype(A), eltype(b)), size(A, 1), size(x, 2)) | ||
C .= b | ||
mul!(C, A, x, 1, 1) | ||
return C | ||
end | ||
|
||
@testset begin | ||
A = Reactant.to_rarray(rand(4, 4)) | ||
x = Reactant.to_rarray(rand(4, 2)) | ||
b = Reactant.to_rarray(rand(4)) | ||
|
||
@test @jit(muladd2(A, x, b)) ≈ muladd2(A, x, b) | ||
@test @jit(muladd_5arg(A, x, b)) ≈ muladd2(A, x, b) | ||
|
||
# Mixed Precision | ||
x = Reactant.to_rarray(rand(Float32, 4, 2)) | ||
|
||
@test @jit(muladd2(A, x, b)) ≈ muladd2(A, x, b) | ||
@test @jit(muladd_5arg(A, x, b)) ≈ muladd2(A, x, b) | ||
|
||
C = similar(A, Float32, size(A, 1), size(x, 2)) | ||
@jit(mul!(C, A, x)) | ||
@test C ≈ A * x | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 it worth overriding the version with alpha and beta?
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.
this is ine too tho, obviously
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.
yeah we need to add that, else it does the for loop