Skip to content

[mlir][sparse] Change tests to use new syntax for ELL and slice #67569

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 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,33 @@ def SparseTensorEncodingAttr : SparseTensor_Attr<"SparseTensorEncoding",
}>
... tensor<20x30xf32, #BSR_explicit> ...

// ELL format.
// In the simple format for matrix, one array stores values and another
// array stores column indices. The arrays have the same number of rows
// as the original matrix, but only have as many columns as
// the maximum number of nonzeros on a row of the original matrix.
// There are many variants for ELL such as jagged diagonal scheme.
// To implement ELL, map provides a notion of "counting a
// dimension", where every stored element with the same coordinate
// is mapped to a new slice. For instance, ELL storage of a 2-d
// tensor can be defined with the mapping (i, j) -> (#i, i, j)
// using the notation of [Chou20]. Lacking the # symbol in MLIR's
// affine mapping, we use a free symbol c to define such counting,
// together with a constant that denotes the number of resulting
// slices. For example, the mapping [c](i, j) -> (c * 3 * i, i, j)
// with the level-types ["dense", "dense", "compressed"] denotes ELL
// storage with three jagged diagonals that count the dimension i.
#ELL = #sparse_tensor.encoding<{
map = [c](i, j) -> (c * 3 * i : dense, i : dense, j : compressed)
}>
... tensor<?x?xf64, #ELL> ...

// CSR slice (offset = 0, size = 4, stride = 1 on the first dimension;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also add the #ELL here, so we can explain the "free" symbol (for now, since we may implement this very differently)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

// offset = 0, size = 8, and a dynamic stride on the second dimension).
#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (0, 4, 1), (0, 8, ?) ]
map = (i : #sparse_tensor<slice(0, 4, 1)>,
j : #sparse_tensor<slice(0, 8, ?)>) ->
(i : dense, j : compressed)
}>
... tensor<?x?xf64, #CSC_SLICE> ...

Expand Down
3 changes: 1 addition & 2 deletions mlir/test/CAPI/sparse_tensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ static int testRoundtripEncoding(MlirContext ctx) {
// clang-format off
const char *originalAsm =
"#sparse_tensor.encoding<{ "
"lvlTypes = [ \"dense\", \"compressed\", \"compressed\"], "
"dimToLvl = affine_map<(d0, d1)[s0] -> (s0, d0, d1)>, "
"map = [s0](d0, d1) -> (s0 : dense, d0 : compressed, d1 : compressed), "
"posWidth = 32, crdWidth = 64 }>";
// clang-format on
MlirAttribute originalAttr =
Expand Down
3 changes: 1 addition & 2 deletions mlir/test/Dialect/SparseTensor/convert_sparse2sparse.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
}>

#COOSlice = #sparse_tensor.encoding<{
lvlTypes = [ "compressed_nu", "singleton" ],
dimSlices = [ (2, 2, 1), (12, 13, 1) ]
map = (d0 : #sparse_tensor<slice(2, 2, 1)>, d1 : #sparse_tensor<slice(12, 13, 1)>) -> (d0 : compressed(nonunique), d1 : singleton)
}>

// CHECK-LABEL: func @sparse_nop_convert(
Expand Down
9 changes: 3 additions & 6 deletions mlir/test/Dialect/SparseTensor/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ func.func @mismatch_values_types(%arg0: tensor<?xf64, #SparseVector>) -> memref<
// -----

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (1, 4, 1), (1, 4, 2) ]
map = (d0 : #sparse_tensor<slice(1, 4, 1)>, d1 : #sparse_tensor<slice(1, 4, 2)>) -> (d0 : dense, d1 : compressed)
}>

func.func @sparse_slice_offset(%arg0: tensor<2x8xf64, #CSR_SLICE>) -> index {
Expand All @@ -214,8 +213,7 @@ func.func @sparse_slice_offset(%arg0: tensor<2x8xf64, #CSR_SLICE>) -> index {
// -----

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (1, 4, 1), (1, 4, 2) ]
map = (d0 : #sparse_tensor<slice(1, 4, 1)>, d1 : #sparse_tensor<slice(1, 4, 2)>) -> (d0 : dense, d1 : compressed)
}>

func.func @sparse_slice_stride(%arg0: tensor<2x8xf64, #CSR_SLICE>) -> index {
Expand Down Expand Up @@ -400,8 +398,7 @@ func.func @invalid_out_dense(%arg0: tensor<10xf64>, %arg1: !llvm.ptr<i8>) {
// -----

#CSR = #sparse_tensor.encoding<{
lvlTypes = ["dense", "compressed"],
dimSlices = [ (1, 4, 1), (1, 4, 2) ]
map = (d0 : #sparse_tensor<slice(1, 4, 1)>, d1 : #sparse_tensor<slice(1, 4, 2)>) -> (d0 : dense, d1 : compressed)
}>

func.func @sparse_convert_to_slice(%arg0: tensor<10x?xf32>) -> tensor<10x10xf32, #CSR> {
Expand Down
3 changes: 1 addition & 2 deletions mlir/test/Dialect/SparseTensor/invalid_encoding.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ func.func private @tensor_invalid_key(%arg0: tensor<16x32xf32, #a>) -> ()
// -----

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (-1, ?, 1), (?, 4, 2) ] // expected-error{{expect positive value or ? for slice offset/size/stride}}
map = (d0 : #sparse_tensor<slice(-1, ?, 1)>, d1 : #sparse_tensor<slice(?, 4, 2)>) -> (d0 : dense, d1 : compressed)// expected-error{{expect positive value or ? for slice offset/size/stride}}
}>
func.func private @sparse_slice(tensor<?x?xf64, #CSR_SLICE>)

Expand Down
3 changes: 1 addition & 2 deletions mlir/test/Dialect/SparseTensor/pre_rewriting.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
}>

#Slice = #sparse_tensor.encoding<{
lvlTypes = [ "compressed_nu", "singleton" ],
dimSlices = [ (?, 1, 1), (?, 3, 1) ]
map = (d0 : #sparse_tensor<slice(?, 1, 1)>, d1 : #sparse_tensor<slice(?, 3, 1)>) -> (d0 : compressed(nonunique), d1 : singleton)
}>

#sel_trait = {
Expand Down
15 changes: 5 additions & 10 deletions mlir/test/Dialect/SparseTensor/roundtrip.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ func.func @sparse_values(%arg0: tensor<128xf64, #SparseVector>) -> memref<?xf64>
// -----

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (1, 4, 1), (1, 4, 2) ]
map = (d0 : #sparse_tensor<slice(1, 4, 1)>, d1 : #sparse_tensor<slice(1, 4, 2)>) -> (d0 : dense, d1 : compressed)
}>

// CHECK-LABEL: func @sparse_slice_offset(
Expand All @@ -159,8 +158,7 @@ func.func @sparse_slice_offset(%arg0: tensor<2x8xf64, #CSR_SLICE>) -> index {
// -----

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (1, 4, 1), (1, 4, 2) ]
map = (d0 : #sparse_tensor<slice(1, 4, 1)>, d1 : #sparse_tensor<slice(1, 4, 2)>) -> (d0 : dense, d1 : compressed)
}>

// CHECK-LABEL: func @sparse_slice_stride(
Expand Down Expand Up @@ -188,8 +186,7 @@ func.func @sparse_metadata_init() -> !sparse_tensor.storage_specifier<#SparseVec

#SparseVector = #sparse_tensor.encoding<{map = (d0) -> (d0 : compressed)}>
#SparseVector_Slice = #sparse_tensor.encoding<{
lvlTypes = ["compressed"],
dimSlices = [ (?, ?, ?) ]
map = (d0 : #sparse_tensor<slice(?, ?, ?)>) -> (d0 : compressed)
}>

// CHECK-LABEL: func @sparse_metadata_init(
Expand Down Expand Up @@ -220,8 +217,7 @@ func.func @sparse_get_md(%arg0: !sparse_tensor.storage_specifier<#SparseVector>)
// -----

#SparseVector_Slice = #sparse_tensor.encoding<{
lvlTypes = ["compressed"],
dimSlices = [ (?, ?, ?) ]
map = (d0 : #sparse_tensor<slice(?, ?, ?)>) -> (d0 : compressed)
}>

// CHECK-LABEL: func @sparse_get_md(
Expand All @@ -237,8 +233,7 @@ func.func @sparse_get_md(%arg0: !sparse_tensor.storage_specifier<#SparseVector_S
// -----

#SparseVector = #sparse_tensor.encoding<{
lvlTypes = ["compressed"],
dimSlices = [ (?, ?, ?) ]
map = (d0 : #sparse_tensor<slice(?, ?, ?)>) -> (d0 : compressed)
}>

// CHECK-LABEL: func @sparse_get_md(
Expand Down
20 changes: 3 additions & 17 deletions mlir/test/Dialect/SparseTensor/roundtrip_encoding.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func.func private @sparse_bcsr(tensor<10x60xf64, #BCSR>)
// -----

#ELL = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "dense", "compressed" ],
dimToLvl = affine_map<(i,j)[c] -> (c*4*i, i, j)>
map = [s0](d0, d1) -> (d0 * (s0 * 4) : dense, d0 : dense, d1 : compressed)
}>

// CHECK-LABEL: func private @sparse_ell(
Expand All @@ -112,8 +111,7 @@ func.func private @sparse_ell(tensor<?x?xf64, #ELL>)
// -----

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (1, 4, 1), (1, 4, 2) ]
map = (d0 : #sparse_tensor<slice(1, 4, 1)>, d1 : #sparse_tensor<slice(1, 4, 2)>) -> (d0 : dense, d1 : compressed)
}>

// CHECK-LABEL: func private @sparse_slice(
Expand All @@ -123,19 +121,7 @@ func.func private @sparse_slice(tensor<?x?xf64, #CSR_SLICE>)
// -----

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (1, 4, 1), (1, 4, 2) ]
}>

// CHECK-LABEL: func private @sparse_slice(
// CHECK-SAME: tensor<?x?xf64, #sparse_tensor.encoding<{ lvlTypes = [ "dense", "compressed" ], dimSlices = [ (1, 4, 1), (1, 4, 2) ] }>>
func.func private @sparse_slice(tensor<?x?xf64, #CSR_SLICE>)

// -----

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (1, ?, 1), (?, 4, 2) ]
map = (d0 : #sparse_tensor<slice(1, ?, 1)>, d1 : #sparse_tensor<slice(?, 4, 2)>) -> (d0 : dense, d1 : compressed)
}>

// CHECK-LABEL: func private @sparse_slice(
Expand Down
3 changes: 1 addition & 2 deletions mlir/test/Dialect/SparseTensor/sparse_extract_slice.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
}>

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (0, 4, 1), (0, 8, 1) ]
map = (d0 : #sparse_tensor<slice(0, 4, 1)>, d1 : #sparse_tensor<slice(0, 8, 1)>) -> (d0 : dense, d1 : compressed)
}>

// CHECK-LABEL: func.func @sparse_slice(
Expand Down
6 changes: 2 additions & 4 deletions mlir/test/Dialect/SparseTensor/sparse_foreach.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ func.func @sparse_foreach_constant() -> () {
}

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "compressed", "compressed" ],
dimSlices = [ (0, 4, 1), (2, 4, 1) ]
map = (d0 : #sparse_tensor<slice(0, 4, 1)>, d1 : #sparse_tensor<slice(2, 4, 1)>) -> (d0 : compressed, d1 : compressed)
}>

#CSR_SLICE_DYN = #sparse_tensor.encoding<{
lvlTypes = [ "compressed", "compressed" ],
dimSlices = [ (?, ?, ?), (?, ?, ?) ]
map = (d0 : #sparse_tensor<slice(?, ?, ?)>, d1 : #sparse_tensor<slice(?, ?, ?)>) -> (d0 : compressed, d1 : compressed)
}>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,23 @@
}>

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (1, 4, 1), (1, 4, 2) ]
map = (d0 : #sparse_tensor<slice(1, 4, 1)>, d1 : #sparse_tensor<slice(1, 4, 2)>) -> (d0 : dense, d1 : compressed)
}>

#CSR_SLICE_DYN = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (?, ?, ?), (?, ?, ?) ]
map = (d0 : #sparse_tensor<slice(?, ?, ?)>, d1 : #sparse_tensor<slice(?, ?, ?)>) -> (d0 : dense, d1 : compressed)
}>

#COO = #sparse_tensor.encoding<{
map = (d0, d1) -> (d0 : compressed(nonunique), d1 : singleton)
}>

#COO_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "compressed_nu", "singleton" ],
dimSlices = [ (1, 4, 1), (1, 4, 2) ]
map = (d0 : #sparse_tensor<slice(1, 4, 1)>, d1 : #sparse_tensor<slice(1, 4, 2)>) -> (d0 : compressed(nonunique), d1 : singleton)
}>

#COO_SLICE_DYN = #sparse_tensor.encoding<{
lvlTypes = [ "compressed_nu", "singleton" ],
dimSlices = [ (?, ?, ?), (?, ?, ?) ]
map = (d0 : #sparse_tensor<slice(?, ?, ?)>, d1 : #sparse_tensor<slice(?, ?, ?)>) -> (d0 : compressed(nonunique), d1 : singleton)
}>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,43 @@
}>

#DCSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "compressed", "compressed" ],
dimSlices = [ (0, 4, 1), (0, 8, 1) ]
map = (d0 : #sparse_tensor<slice(0, 4, 1)>, d1 : #sparse_tensor<slice(0, 8, 1)>) -> (d0 : compressed, d1 : compressed)
}>

#CSR = #sparse_tensor.encoding<{
map = (d0, d1) -> (d0 : dense, d1 : compressed)
}>

#CSR_SLICE = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (0, 4, 1), (0, 8, 1) ]
map = (d0 : #sparse_tensor<slice(0, 4, 1)>, d1 : #sparse_tensor<slice(0, 8, 1)>) -> (d0 : dense, d1 : compressed)
}>

#COO = #sparse_tensor.encoding<{
map = (d0, d1) -> (d0 : compressed(nonunique), d1 : singleton)
}>

#CSR_SLICE_1 = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (0, 4, 2), (0, 4, 1) ]
map = (d0 : #sparse_tensor<slice(0, 4, 2)>, d1 : #sparse_tensor<slice(0, 4, 1)>) -> (d0 : dense, d1 : compressed)
}>

#DCSR_SLICE_1 = #sparse_tensor.encoding<{
lvlTypes = [ "compressed", "compressed" ],
dimSlices = [ (0, 4, 2), (1, 4, 1) ]
map = (d0 : #sparse_tensor<slice(0, 4, 2)>, d1 : #sparse_tensor<slice(1, 4, 1)>) -> (d0 : compressed, d1 : compressed)
}>

#COO_SLICE_1 = #sparse_tensor.encoding<{
lvlTypes = [ "compressed_nu", "singleton" ],
dimSlices = [ (0, 4, 2), (0, 4, 1) ]
map = (d0 : #sparse_tensor<slice(0, 4, 2)>, d1 : #sparse_tensor<slice(0, 4, 1)>) -> (d0 : compressed(nonunique), d1 : singleton)
}>

#COO_SLICE_2 = #sparse_tensor.encoding<{
lvlTypes = [ "compressed_nu", "singleton" ],
dimSlices = [ (0, 4, 2), (1, 4, 1) ]
map = (d0 : #sparse_tensor<slice(0, 4, 2)>, d1 : #sparse_tensor<slice(1, 4, 1)>) -> (d0 : compressed(nonunique), d1 : singleton)
}>

#CSR_SLICE_dyn = #sparse_tensor.encoding<{
lvlTypes = [ "dense", "compressed" ],
dimSlices = [ (?, 4, ?), (?, 4, ?) ]
map = (d0 : #sparse_tensor<slice(?, 4, ?)>, d1 : #sparse_tensor<slice(?, 4, ?)>) -> (d0 : dense, d1 : compressed)
}>

#DCSR_SLICE_dyn = #sparse_tensor.encoding<{
lvlTypes = [ "compressed", "compressed" ],
dimSlices = [ (?, 4, ?), (?, 4, ?) ]
map = (d0 : #sparse_tensor<slice(?, 4, ?)>, d1 : #sparse_tensor<slice(?, 4, ?)>) -> (d0 : compressed, d1 : compressed)
}>

module {
Expand Down