-
Notifications
You must be signed in to change notification settings - Fork 38
use row major when building attributes #307
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2328706
use row major when building attributes
Pangoraw 42c0080
format
Pangoraw ba6bb68
opt for d=1
Pangoraw cae90dd
reproducable test
Pangoraw 2b89698
workaround for 0 dim array
Pangoraw 8ed8c99
transpose padding
Pangoraw 2188ca3
2,N' -> N,2
Pangoraw a7f82fd
make_causal_mask
Pangoraw 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -109,7 +109,7 @@ function NNlib.conv!( | |||||
| #! format: on | ||||||
|
|
||||||
| padding = Reactant.MLIR.IR.DenseElementsAttribute( | ||||||
| reshape(collect(padding), (num_spatial_dims, 2)) | ||||||
| reshape(collect(padding), (2, num_spatial_dims))' | ||||||
| ) | ||||||
| result_type = Reactant.MLIR.IR.TensorType(size(y), Reactant.MLIR.IR.Type(T)) | ||||||
|
|
||||||
|
|
@@ -163,7 +163,7 @@ function reduce_window(f, x::AnyTracedRArray{T,N}, pdims; init) where {T,N} | |||||
| end | ||||||
|
|
||||||
| padding = Reactant.MLIR.IR.DenseElementsAttribute( | ||||||
| reshape([padding..., 0, 0, 0, 0], (N, 2)) | ||||||
| reshape([padding..., 0, 0, 0, 0], (2, N))' | ||||||
|
Collaborator
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.
Suggested change
|
||||||
| ) | ||||||
|
|
||||||
| output_shape = (output_spatial_shapes..., size(x, N - 1), size(x, N)) | ||||||
|
|
@@ -306,7 +306,7 @@ function NNlib.make_causal_mask(x::AnyTracedRArray; dims::Int=2) | |||||
| len = size(x, dims) | ||||||
| # directly generating booleans were causing an incorrect constant attribute generation | ||||||
| # but the optimized IR removes the type case so we are probably ok | ||||||
| mask = MLIR.IR.DenseElementsAttribute(collect(triu(fill(1, (len, len)))')) | ||||||
| mask = MLIR.IR.DenseElementsAttribute(collect(triu(fill(1, (len, len))))) | ||||||
| return Reactant.promote_to( | ||||||
| TracedRArray{Bool,2}, | ||||||
| TracedRArray{Int,2}( | ||||||
|
|
||||||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -492,6 +492,10 @@ function Base.fill(::Core.Type{Attribute}, value, shape) | |
| return Base.fill(value, shaped_type) | ||
| end | ||
|
|
||
| to_row_major(x) = permutedims(x, ndims(x):-1:1) | ||
| to_row_major(x::AbstractVector) = x | ||
|
Member
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. From the error logs it looks like this also needs a 0-dim specialization |
||
| to_row_major(x::AbstractArray{T,0}) where {T} = x | ||
|
|
||
| """ | ||
| DenseElementsAttribute(array::AbstractArray) | ||
|
|
||
|
|
@@ -501,66 +505,86 @@ function DenseElementsAttribute(values::AbstractArray{Bool}) | |
| shaped_type = TensorType(size(values), Type(Bool)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrBoolGet( | ||
| shaped_type, length(values), AbstractArray{Cint}(values) | ||
| shaped_type, length(values), AbstractArray{Cint}(to_row_major(values)) | ||
| ), | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{UInt8}) | ||
| shaped_type = TensorType(size(values), Type(UInt8)) | ||
| return Attribute(API.mlirDenseElementsAttrUInt8Get(shaped_type, length(values), values)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrUInt8Get(shaped_type, length(values), to_row_major(values)) | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{Int8}) | ||
| shaped_type = TensorType(size(values), Type(Int8)) | ||
| return Attribute(API.mlirDenseElementsAttrInt8Get(shaped_type, length(values), values)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrInt8Get(shaped_type, length(values), to_row_major(values)) | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{UInt16}) | ||
| shaped_type = TensorType(size(values), Type(UInt16)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrUInt16Get(shaped_type, length(values), values) | ||
| API.mlirDenseElementsAttrUInt16Get( | ||
| shaped_type, length(values), to_row_major(values) | ||
| ), | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{Int16}) | ||
| shaped_type = TensorType(size(values), Type(Int16)) | ||
| return Attribute(API.mlirDenseElementsAttrInt16Get(shaped_type, length(values), values)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrInt16Get(shaped_type, length(values), to_row_major(values)) | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{UInt32}) | ||
| shaped_type = TensorType(size(values), Type(UInt32)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrUInt32Get(shaped_type, length(values), values) | ||
| API.mlirDenseElementsAttrUInt32Get( | ||
| shaped_type, length(values), to_row_major(values) | ||
| ), | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{Int32}) | ||
| shaped_type = TensorType(size(values), Type(Int32)) | ||
| return Attribute(API.mlirDenseElementsAttrInt32Get(shaped_type, length(values), values)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrInt32Get(shaped_type, length(values), to_row_major(values)) | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{UInt64}) | ||
| shaped_type = TensorType(size(values), Type(UInt64)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrUInt64Get(shaped_type, length(values), values) | ||
| API.mlirDenseElementsAttrUInt64Get( | ||
| shaped_type, length(values), to_row_major(values) | ||
| ), | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{Int64}) | ||
| shaped_type = TensorType(size(values), Type(Int64)) | ||
| return Attribute(API.mlirDenseElementsAttrInt64Get(shaped_type, length(values), values)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrInt64Get(shaped_type, length(values), to_row_major(values)) | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{Float32}) | ||
| shaped_type = TensorType(size(values), Type(Float32)) | ||
| return Attribute(API.mlirDenseElementsAttrFloatGet(shaped_type, length(values), values)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrFloatGet(shaped_type, length(values), to_row_major(values)) | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{Float64}) | ||
| shaped_type = TensorType(size(values), Type(Float64)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrDoubleGet(shaped_type, length(values), values) | ||
| API.mlirDenseElementsAttrDoubleGet( | ||
| shaped_type, length(values), to_row_major(values) | ||
| ), | ||
| ) | ||
| end | ||
|
|
||
|
|
@@ -569,16 +593,17 @@ end | |
| function DenseElementsAttribute(values::AbstractArray{Float16}) | ||
| shaped_type = TensorType(size(values), Type(Float16)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrFloat16Get(shaped_type, length(values), values) | ||
| API.mlirDenseElementsAttrFloat16Get( | ||
| shaped_type, length(values), to_row_major(values) | ||
| ), | ||
| ) | ||
| end | ||
|
|
||
| function DenseElementsAttribute(values::AbstractArray{<:Complex}) | ||
| shaped_type = TensorType(size(values), Type(eltype(values))) | ||
| # TODO: row major | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrRawBufferGet( | ||
| shaped_type, length(values) * Base.elsize(values), values | ||
| shaped_type, length(values) * Base.elsize(values), to_row_major(values) | ||
| ), | ||
| ) | ||
| end | ||
|
|
@@ -592,7 +617,9 @@ function DenseElementsAttribute(values::AbstractArray{String}) | |
| # TODO may fail because `Type(String)` is not defined | ||
| shaped_type = TensorType(size(values), Type(String)) | ||
| return Attribute( | ||
| API.mlirDenseElementsAttrStringGet(shaped_type, length(values), values) | ||
| API.mlirDenseElementsAttrStringGet( | ||
| shaped_type, length(values), to_row_major(values) | ||
| ), | ||
| ) | ||
| end | ||
|
|
||
|
|
@@ -663,25 +690,29 @@ function DenseArrayAttribute end | |
|
|
||
| @llvmversioned min = v"16" DenseArrayAttribute( | ||
| values::AbstractArray{Bool}; context::Context=context() | ||
| ) = Attribute(API.mlirDenseBoolArrayGet(context, length(values), values)) | ||
| ) = Attribute( | ||
| API.mlirDenseBoolArrayGet( | ||
| context, length(values), AbstractArray{Cint}(to_row_major(values)) | ||
| ), | ||
| ) | ||
| @llvmversioned min = v"16" DenseArrayAttribute( | ||
| values::AbstractArray{Int8}; context::Context=context() | ||
| ) = Attribute(API.mlirDenseI8ArrayGet(context, length(values), values)) | ||
| ) = Attribute(API.mlirDenseI8ArrayGet(context, length(values), to_row_major(values))) | ||
| @llvmversioned min = v"16" DenseArrayAttribute( | ||
| values::AbstractArray{Int16}; context::Context=context() | ||
| ) = Attribute(API.mlirDenseI16ArrayGet(context, length(values), values)) | ||
| ) = Attribute(API.mlirDenseI16ArrayGet(context, length(values), to_row_major(values))) | ||
| @llvmversioned min = v"16" DenseArrayAttribute( | ||
| values::AbstractArray{Int32}; context::Context=context() | ||
| ) = Attribute(API.mlirDenseI32ArrayGet(context, length(values), values)) | ||
| ) = Attribute(API.mlirDenseI32ArrayGet(context, length(values), to_row_major(values))) | ||
| @llvmversioned min = v"16" DenseArrayAttribute( | ||
| values::AbstractArray{Int64}; context::Context=context() | ||
| ) = Attribute(API.mlirDenseI64ArrayGet(context, length(values), values)) | ||
| ) = Attribute(API.mlirDenseI64ArrayGet(context, length(values), to_row_major(values))) | ||
| @llvmversioned min = v"16" DenseArrayAttribute( | ||
| values::AbstractArray{Float32}; context::Context=context() | ||
| ) = Attribute(API.mlirDenseF32ArrayGet(context, length(values), values)) | ||
| ) = Attribute(API.mlirDenseF32ArrayGet(context, length(values), to_row_major(values))) | ||
| @llvmversioned min = v"16" DenseArrayAttribute( | ||
| values::AbstractArray{Float64}; context::Context=context() | ||
| ) = Attribute(API.mlirDenseF64ArrayGet(context, length(values), values)) | ||
| ) = Attribute(API.mlirDenseF64ArrayGet(context, length(values), to_row_major(values))) | ||
|
|
||
| @llvmversioned min = v"16" Attribute(values::AbstractArray) = DenseArrayAttribute(values) | ||
|
|
||
|
|
||
This file contains hidden or 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.
don't call
'/adjointbecause it will conjugate complex matricesThere 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 did not know about that. As you said, it does not apply here but I will be cautious in the future 👍