-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[mlir][sparse][CRunnerUtils] Add shuffle in CRunnerUtils #77124
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
7 commits
Select commit
Hold shift + click to select a range
a1f9b09
[mlir][sparse][CRunnerUtils] Add shuffle and shuffleFree in CRunnerUt…
yinying-lisa-li dfea486
address comments and use memref instead of pointer
yinying-lisa-li a49feea
format
yinying-lisa-li acf7ca2
fix issue for windows
yinying-lisa-li 0c2b978
header
yinying-lisa-li b93ee7e
remove copy
yinying-lisa-li a3c71f7
mref check
yinying-lisa-li 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
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
95 changes: 95 additions & 0 deletions
95
mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_generate.mlir
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//-------------------------------------------------------------------------------------------------- | ||
// WHEN CREATING A NEW TEST, PLEASE JUST COPY & PASTE WITHOUT EDITS. | ||
// | ||
// Set-up that's shared across all tests in this directory. In principle, this | ||
// config could be moved to lit.local.cfg. However, there are downstream users that | ||
// do not use these LIT config files. Hence why this is kept inline. | ||
// | ||
// DEFINE: %{sparsifier_opts} = enable-runtime-library=true | ||
// DEFINE: %{sparsifier_opts_sve} = enable-arm-sve=true %{sparsifier_opts} | ||
// DEFINE: %{compile} = mlir-opt %s --sparsifier="%{sparsifier_opts}" | ||
// DEFINE: %{compile_sve} = mlir-opt %s --sparsifier="%{sparsifier_opts_sve}" | ||
// DEFINE: %{run_libs} = -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils | ||
// DEFINE: %{run_opts} = -e entry -entry-point-result=void | ||
// DEFINE: %{run} = mlir-cpu-runner %{run_opts} %{run_libs} | ||
// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs} | ||
// | ||
// DEFINE: %{env} = | ||
//-------------------------------------------------------------------------------------------------- | ||
|
||
// RUN: %{compile} | %{run} | FileCheck %s | ||
|
||
// | ||
// Integration test that generates a tensor with specified sparsity level. | ||
// | ||
|
||
!Generator = !llvm.ptr | ||
!Array = !llvm.ptr | ||
|
||
#SparseVector = #sparse_tensor.encoding<{ | ||
map = (d0) -> (d0 : compressed) | ||
}> | ||
|
||
module { | ||
func.func private @rtsrand(index) -> (!Generator) | ||
func.func private @rtrand(!Generator, index) -> (index) | ||
func.func private @rtdrand(!Generator) -> () | ||
func.func private @shuffle(memref<?xi64>, !Generator) -> () attributes { llvm.emit_c_interface } | ||
|
||
// | ||
// Main driver. | ||
// | ||
func.func @entry() { | ||
%c0 = arith.constant 0 : index | ||
%c1 = arith.constant 1 : index | ||
%f0 = arith.constant 0.0 : f64 | ||
%c99 = arith.constant 99 : index | ||
%c100 = arith.constant 100 : index | ||
|
||
// Set up input size and sparsity level. | ||
%size = arith.constant 50 : index | ||
%sparsity = arith.constant 90 : index | ||
%zeros = arith.muli %size, %sparsity : index | ||
%nz = arith.floordivsi %zeros, %c100 : index | ||
%nse = arith.subi %size, %nz : index | ||
|
||
// Set up an empty vector. | ||
%empty = tensor.empty(%size) : tensor<?xf64> | ||
%zero_vec = linalg.fill ins(%f0 : f64) outs(%empty : tensor<?xf64>) -> tensor<?xf64> | ||
|
||
// Generate shuffled indices in the range of [0, %size). | ||
%array = memref.alloc (%size) : memref<?xi64> | ||
%g = func.call @rtsrand(%c0) : (index) ->(!Generator) | ||
func.call @shuffle(%array, %g) : (memref<?xi64>, !Generator) -> () | ||
|
||
// Iterate through the number of nse indices to insert values. | ||
%output = scf.for %iv = %c0 to %nse step %c1 iter_args(%iter = %zero_vec) -> tensor<?xf64> { | ||
// Fetch the index to insert value from shuffled index array. | ||
%val = memref.load %array[%iv] : memref<?xi64> | ||
%idx = arith.index_cast %val : i64 to index | ||
// Generate a random number from 1 to 100. | ||
%ri0 = func.call @rtrand(%g, %c99) : (!Generator, index) -> (index) | ||
%ri1 = arith.addi %ri0, %c1 : index | ||
%r0 = arith.index_cast %ri1 : index to i64 | ||
%fr = arith.uitofp %r0 : i64 to f64 | ||
// Insert the random number to current index. | ||
%out = tensor.insert %fr into %iter[%idx] : tensor<?xf64> | ||
scf.yield %out : tensor<?xf64> | ||
} | ||
|
||
%sv = sparse_tensor.convert %output : tensor<?xf64> to tensor<?xf64, #SparseVector> | ||
%n0 = sparse_tensor.number_of_entries %sv : tensor<?xf64, #SparseVector> | ||
|
||
// Print the number of non-zeros for verification. | ||
// | ||
// CHECK: 5 | ||
vector.print %n0 : index | ||
|
||
// Release the resources. | ||
bufferization.dealloc_tensor %sv : tensor<?xf64, #SparseVector> | ||
memref.dealloc %array : memref<?xi64> | ||
func.call @rtdrand(%g) : (!Generator) -> () | ||
|
||
return | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.