Skip to content

Commit

Permalink
Merge pull request #174 from Avimitin/sparse-tensor-number-of-entries
Browse files Browse the repository at this point in the history
[example][MLIRSparseTensor] add sparse_tensor.number_of_entries
  • Loading branch information
Avimitin committed Jul 6, 2023
2 parents 570d403 + 8dbf87e commit 9589ac1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/MLIRSparseTensor/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ sparse-tensor-dump-run:
--sparse-compiler="enable-runtime-library=true" | \
${MLIR_CPU_RUNNER} -e main -O0 --entry-point-result=void \
--shared-libs=${MLIR_RUNNER_UTILS},${MLIR_C_RUNNER_UTILS}

sparse-tensor-number-of-entries-lower:
@${MLIR_OPT} ./sparse-tensor-number-of-entries.mlir \
--sparse-compiler="enable-runtime-library=false" -o log.mlir
sparse-tensor-number-of-entries-translate:
@${MLIR_OPT} ./sparse-tensor-number-of-entries.mlir \
--sparse-compiler="enable-runtime-library=false" | \
${MLIR_TRANSLATE} --mlir-to-llvmir -o log.ll
sparse-tensor-number-of-entries-run:
@${MLIR_OPT} ./sparse-tensor-number-of-entries.mlir \
--sparse-compiler="enable-runtime-library=false" | \
${MLIR_CPU_RUNNER} -e main -O0 --entry-point-result=void \
--shared-libs=${MLIR_RUNNER_UTILS},${MLIR_C_RUNNER_UTILS}
24 changes: 24 additions & 0 deletions examples/MLIRSparseTensor/sparse-tensor-number-of-entries.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This is an example of operation `sparse_tensor.number_of_entries` in MLIR SparseTensor Dialect.
// The `sparse_tensor.number_of_entries` operation accpet a tensor with sparse attribute, then return the number
// of the non-zero entry in the given sparse tensor.

#SparseMatrix = #sparse_tensor.encoding<{
dimLevelType = ["compressed", "compressed"]
}>

func.func @main() {
%d0 = arith.constant dense<[
[1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 2.0, 0.0],
[0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 3.0]
]> : tensor<4x4xf64>

%s0 = sparse_tensor.convert %d0 : tensor<4x4xf64> to tensor<4x4xf64, #SparseMatrix>
%n0 = sparse_tensor.number_of_entries %s0 : tensor<4x4xf64, #SparseMatrix>

// This should print 3
vector.print %n0 : index

return
}

0 comments on commit 9589ac1

Please sign in to comment.