109 lines · plain
1//--------------------------------------------------------------------------------------------------2// WHEN CREATING A NEW TEST, PLEASE JUST COPY & PASTE WITHOUT EDITS.3//4// Set-up that's shared across all tests in this directory. In principle, this5// config could be moved to lit.local.cfg. However, there are downstream users that6// do not use these LIT config files. Hence why this is kept inline.7//8// DEFINE: %{sparsifier_opts} = enable-runtime-library=true9// DEFINE: %{sparsifier_opts_sve} = enable-arm-sve=true %{sparsifier_opts}10// DEFINE: %{compile} = mlir-opt %s --sparsifier="%{sparsifier_opts}"11// DEFINE: %{compile_sve} = mlir-opt %s --sparsifier="%{sparsifier_opts_sve}"12// DEFINE: %{run_libs} = -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils13// DEFINE: %{run_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils14// DEFINE: %{run_opts} = -e main -entry-point-result=void15// DEFINE: %{run} = mlir-runner %{run_opts} %{run_libs}16// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs_sve}17//18// DEFINE: %{env} =19//--------------------------------------------------------------------------------------------------20 21// RUN: %{compile} | %{run} | FileCheck %s22//23// Do the same run, but now with direct IR generation.24// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false25// RUN: %{compile} | %{run} | FileCheck %s26//27// Do the same run, but now with direct IR generation and vectorization.28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true29// RUN: %{compile} | %{run} | FileCheck %s30 31#SparseVector = #sparse_tensor.encoding<{map = (d0) -> (d0 : compressed)}>32#DenseVector = #sparse_tensor.encoding<{map = (d0) -> (d0 : dense)}>33 34#trait_vec_op = {35 indexing_maps = [36 affine_map<(i) -> (i)>, // a (in)37 affine_map<(i) -> (i)>, // b (in)38 affine_map<(i) -> (i)> // x (out)39 ],40 iterator_types = ["parallel"]41}42 43module {44 // Creates a dense vector using the minimum values from two input sparse vectors.45 // When there is no overlap, include the present value in the output.46 func.func @vector_min(%arga: tensor<?xbf16, #SparseVector>,47 %argb: tensor<?xbf16, #SparseVector>) -> tensor<?xbf16, #DenseVector> {48 %c = arith.constant 0 : index49 %d = tensor.dim %arga, %c : tensor<?xbf16, #SparseVector>50 %xv = tensor.empty (%d) : tensor<?xbf16, #DenseVector>51 %0 = linalg.generic #trait_vec_op52 ins(%arga, %argb: tensor<?xbf16, #SparseVector>, tensor<?xbf16, #SparseVector>)53 outs(%xv: tensor<?xbf16, #DenseVector>) {54 ^bb(%a: bf16, %b: bf16, %x: bf16):55 %1 = sparse_tensor.binary %a, %b : bf16, bf16 to bf1656 overlap={57 ^bb0(%a0: bf16, %b0: bf16):58 %cmp = arith.cmpf "olt", %a0, %b0 : bf1659 %2 = arith.select %cmp, %a0, %b0: bf1660 sparse_tensor.yield %2 : bf1661 }62 left=identity63 right=identity64 linalg.yield %1 : bf1665 } -> tensor<?xbf16, #DenseVector>66 return %0 : tensor<?xbf16, #DenseVector>67 }68 69 // Driver method to call and verify the kernel.70 func.func @main() {71 %c0 = arith.constant 0 : index72 73 // Setup sparse vectors.74 %v1 = arith.constant sparse<75 [ [0], [3], [11], [17], [20], [21], [28], [29], [31] ],76 [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]77 > : tensor<32xbf16>78 %v2 = arith.constant sparse<79 [ [1], [3], [4], [10], [16], [18], [21], [28], [29], [31] ],80 [11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0 ]81 > : tensor<32xbf16>82 %sv1 = sparse_tensor.convert %v1 : tensor<32xbf16> to tensor<?xbf16, #SparseVector>83 %sv2 = sparse_tensor.convert %v2 : tensor<32xbf16> to tensor<?xbf16, #SparseVector>84 85 // Call the sparse vector kernel.86 %0 = call @vector_min(%sv1, %sv2)87 : (tensor<?xbf16, #SparseVector>,88 tensor<?xbf16, #SparseVector>) -> tensor<?xbf16, #DenseVector>89 90 //91 // Verify the result.92 //93 // CHECK: ---- Sparse Tensor ----94 // CHECK-NEXT: nse = 3295 // CHECK-NEXT: dim = ( 32 )96 // CHECK-NEXT: lvl = ( 32 )97 // CHECK-NEXT: values : ( 1, 11, 0, 2, 13, 0, 0, 0, 0, 0, 14, 3, 0, 0, 0, 0, 15, 4, 16, 0, 5, 6, 0, 0, 0, 0, 0, 0, 7, 8, 0, 9 )98 // CHECK-NEXT: ----99 //100 sparse_tensor.print %0 : tensor<?xbf16, #DenseVector>101 102 // Release the resources.103 bufferization.dealloc_tensor %sv1 : tensor<?xbf16, #SparseVector>104 bufferization.dealloc_tensor %sv2 : tensor<?xbf16, #SparseVector>105 bufferization.dealloc_tensor %0 : tensor<?xbf16, #DenseVector>106 return107 }108}109