-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples][MLIRSparseTensor] Add example to show how sparse vectoriza…
…tion rewrite ForOp Signed-off-by: Avimitin <[email protected]>
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
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
52 changes: 52 additions & 0 deletions
52
examples/MLIRSparseTensor/sparse-tensor-vectorization.mlir
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,52 @@ | ||
#SparseVector = #sparse_tensor.encoding<{ | ||
dimLevelType = ["compressed"] | ||
}> | ||
|
||
#trait_mul = { | ||
indexing_maps = [ | ||
affine_map<(i) -> (i)>, // a | ||
affine_map<(i) -> (i)>, // b | ||
affine_map<(i) -> (i)> // x (out) | ||
], | ||
iterator_types = ["parallel"], | ||
doc = "x(i) = a(i) * b(i)" | ||
} | ||
|
||
// Example for parallel loop vectorization | ||
func.func @sparse_mul(%arga: tensor<1024xf32, #SparseVector>, | ||
%argb: tensor<1024xf32>, | ||
%argx: tensor<1024xf32>) -> tensor<1024xf32> { | ||
%0 = linalg.generic #trait_mul | ||
ins(%arga, %argb: tensor<1024xf32, #SparseVector>, tensor<1024xf32>) | ||
outs(%argx: tensor<1024xf32>) { | ||
^bb(%a: f32, %b: f32, %x: f32): | ||
%0 = arith.mulf %a, %b : f32 | ||
linalg.yield %0 : f32 | ||
} -> tensor<1024xf32> | ||
return %0 : tensor<1024xf32> | ||
} | ||
|
||
#trait_reduction = { | ||
indexing_maps = [ | ||
affine_map<(i) -> (i)>, // a | ||
affine_map<(i) -> (i)>, // b | ||
affine_map<(i) -> ()> // x (out) | ||
], | ||
iterator_types = ["reduction"], | ||
doc = "x += a(i) * b(i)" | ||
} | ||
|
||
// Example for reduction loop vectorization | ||
func.func @sparse_reduction(%arga: tensor<1024xf32, #SparseVector>, | ||
%argb: tensor<1024xf32>, | ||
%argx: tensor<f32>) -> tensor<f32> { | ||
%0 = linalg.generic #trait_reduction | ||
ins(%arga, %argb: tensor<1024xf32, #SparseVector>, tensor<1024xf32>) | ||
outs(%argx: tensor<f32>) { | ||
^bb(%a: f32, %b: f32, %x: f32): | ||
%0 = arith.mulf %a, %b : f32 | ||
%1 = arith.addf %x, %0 : f32 | ||
linalg.yield %1 : f32 | ||
} -> tensor<f32> | ||
return %0 : tensor<f32> | ||
} |