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, this 5// config could be moved to lit.local.cfg. However, there are downstream users that 6// do not use these LIT config files. Hence why this is kept inline. 7// 8// DEFINE: %{sparsifier_opts} = enable-runtime-library=true 9// 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_utils 13// DEFINE: %{run_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils 14// DEFINE: %{run_opts} = -e main -entry-point-result=void 15// 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} | %{env} %{run} | FileCheck %s 22// 23// Do the same run, but now with direct IR generation. 24// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false 25// RUN: %{compile} | %{env} %{run} | FileCheck %s 26// 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=true 29// RUN: %{compile} | %{env} %{run} | FileCheck %s 30// 31// Do the same run, but now with direct IR generation and VLA vectorization. 32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{env} %{run_sve} | FileCheck %s %} 33 34 35#COO_2D = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : compressed(nonunique), d1 : singleton), posWidth = 32, crdWidth = 32 }> 36#COO_3D = #sparse_tensor.encoding<{ map = (d0, d1, d2) -> (d0 : compressed(nonunique), d1 : singleton(nonunique), d2 : singleton), posWidth = 32, crdWidth = 32 }> 37 38module { 39 func.func private @printMemref3dF32(%ptr : tensor<?x?x?xf32> {bufferization.access = "read"}) attributes { llvm.emit_c_interface } 40 func.func private @printMemref2dF32(%ptr : tensor<?x?xf32> {bufferization.access = "read"}) attributes { llvm.emit_c_interface } 41 42 func.func @test_sparse_rhs(%arg0: tensor<5x6xf32>, %arg1: tensor<6x2x3xf32, #COO_3D>) -> tensor<?x?x?xf32> { 43 %collapsed = tensor.collapse_shape %arg1 [[0], [1, 2]] : tensor<6x2x3xf32, #COO_3D> into tensor<6x6xf32, #COO_2D> 44 %0 = tensor.empty() : tensor<5x6xf32> 45 %cst = arith.constant 0.000000e+00 : f32 46 %1 = linalg.fill ins(%cst : f32) outs(%0 : tensor<5x6xf32>) -> tensor<5x6xf32> 47 %2 = linalg.matmul ins(%arg0, %collapsed : tensor<5x6xf32>, tensor<6x6xf32, #COO_2D>) outs(%1 : tensor<5x6xf32>) -> tensor<5x6xf32> 48 %expanded = tensor.expand_shape %2 [[0], [1, 2]] output_shape [5,2,3]: tensor<5x6xf32> into tensor<5x2x3xf32> 49 %ret1 = tensor.cast %expanded : tensor<5x2x3xf32> to tensor<?x?x?xf32> 50 51 // Note: tensor.collapse_shape is a metadata-only operation on dense tensors 52 // but requires reallocation on sparse tensors. 53 bufferization.dealloc_tensor %collapsed : tensor<6x6xf32, #COO_2D> 54 55 return %ret1 : tensor<?x?x?xf32> 56 } 57 58 func.func @test_sparse_all(%arg0: tensor<5x6xf32, #COO_2D>, %arg1: tensor<6x2x3xf32, #COO_3D>) -> tensor<?x?x?xf32> { 59 %collapsed = tensor.collapse_shape %arg1 [[0], [1, 2]] : tensor<6x2x3xf32, #COO_3D> into tensor<6x6xf32, #COO_2D> 60 %0 = tensor.empty() : tensor<5x6xf32> 61 %cst = arith.constant 0.000000e+00 : f32 62 %1 = linalg.fill ins(%cst : f32) outs(%0 : tensor<5x6xf32>) -> tensor<5x6xf32> 63 %2 = linalg.matmul ins(%arg0, %collapsed : tensor<5x6xf32, #COO_2D>, tensor<6x6xf32, #COO_2D>) outs(%1 : tensor<5x6xf32>) -> tensor<5x6xf32> 64 %expanded = tensor.expand_shape %2 [[0], [1, 2]] output_shape [5,2,3]: tensor<5x6xf32> into tensor<5x2x3xf32> 65 %ret1 = tensor.cast %expanded : tensor<5x2x3xf32> to tensor<?x?x?xf32> 66 67 // Note: tensor.collapse_shape is a metadata-only operation on dense tensors 68 // but requires reallocation on sparse tensors. 69 bufferization.dealloc_tensor %collapsed : tensor<6x6xf32, #COO_2D> 70 71 return %ret1 : tensor<?x?x?xf32> 72 } 73 74 func.func @test_dense(%arg0: tensor<5x6xf32>, %arg1: tensor<6x2x3xf32>) -> tensor<?x?x?xf32> { 75 %collapsed = tensor.collapse_shape %arg1 [[0], [1, 2]] : tensor<6x2x3xf32> into tensor<6x6xf32> 76 %0 = tensor.empty() : tensor<5x6xf32> 77 %cst = arith.constant 0.000000e+00 : f32 78 %1 = linalg.fill ins(%cst : f32) outs(%0 : tensor<5x6xf32>) -> tensor<5x6xf32> 79 %2 = linalg.matmul ins(%arg0, %collapsed : tensor<5x6xf32>, tensor<6x6xf32>) outs(%1 : tensor<5x6xf32>) -> tensor<5x6xf32> 80 %expanded = tensor.expand_shape %2 [[0], [1, 2]] output_shape [5,2,3]: tensor<5x6xf32> into tensor<5x2x3xf32> 81 %ret1 = tensor.cast %expanded : tensor<5x2x3xf32> to tensor<?x?x?xf32> 82 return %ret1 : tensor<?x?x?xf32> 83 } 84 85 func.func @test_sparse_all_2(%arg0: tensor<5x6xf32, #COO_2D>, %arg1: tensor<2x3x6xf32, #COO_3D>) -> tensor<?x?x?xf32> { 86 // collapse the first two level this time, as this is the level requires coiterations. 87 %collapsed = tensor.collapse_shape %arg1 [[0, 1], [2]] : tensor<2x3x6xf32, #COO_3D> into tensor<6x6xf32, #COO_2D> 88 %0 = tensor.empty() : tensor<5x6xf32> 89 %cst = arith.constant 0.000000e+00 : f32 90 %1 = linalg.fill ins(%cst : f32) outs(%0 : tensor<5x6xf32>) -> tensor<5x6xf32> 91 %2 = linalg.matmul ins(%arg0, %collapsed : tensor<5x6xf32, #COO_2D>, tensor<6x6xf32, #COO_2D>) outs(%1 : tensor<5x6xf32>) -> tensor<5x6xf32> 92 %expanded = tensor.expand_shape %2 [[0], [1, 2]] output_shape [5,2,3]: tensor<5x6xf32> into tensor<5x2x3xf32> 93 %ret1 = tensor.cast %expanded : tensor<5x2x3xf32> to tensor<?x?x?xf32> 94 95 // Note: tensor.collapse_shape is a metadata-only operation on dense tensors 96 // but requires reallocation on sparse tensors. 97 bufferization.dealloc_tensor %collapsed : tensor<6x6xf32, #COO_2D> 98 99 return %ret1 : tensor<?x?x?xf32> 100 } 101 102 103 func.func @main() { 104 // Setup two sparse vectors. 105 %d1 = arith.constant sparse< 106 [ [0, 0], [1, 1], [2, 2], [2, 3], [4, 5] ], 107 [1.0, 2.0, 3.0, 4.0, 5.0] 108 > : tensor<5x6xf32> 109 110 %d2 = arith.constant sparse< 111 [ [0, 0, 0], [1, 1, 1], [2, 1, 1] ], 112 [ 6.0, 7.0, 8.0] 113 > : tensor<6x2x3xf32> 114 %shape = arith.constant dense<[2, 3, 6]> : tensor<3xi32> 115 116 %d3 = tensor.reshape %d2(%shape): (tensor<6x2x3xf32>, tensor<3xi32>) -> tensor<2x3x6xf32> 117 %s1 = sparse_tensor.convert %d1 : tensor<5x6xf32> to tensor<5x6xf32, #COO_2D> 118 %s2 = sparse_tensor.convert %d2 : tensor<6x2x3xf32> to tensor<6x2x3xf32, #COO_3D> 119 %s3 = sparse_tensor.convert %d3 : tensor<2x3x6xf32> to tensor<2x3x6xf32, #COO_3D> 120 121 // CHECK: Memref base@ = {{.*}} rank = 3 offset = 0 sizes = [5, 2, 3] strides = [6, 3, 1] data = 122 // CHECK-NEXT:[ 123 // CHECK-SAME: [ 124 // CHECK-SAME: [6, 0, 0], 125 // CHECK-NEXT: [0, 0, 0]], 126 // CHECK-NEXT: [ 127 // CHECK-SAME: [0, 0, 0], 128 // CHECK-NEXT: [0, 14, 0]], 129 // CHECK-NEXT: [ 130 // CHECK-SAME: [0, 0, 0], 131 // CHECK-NEXT: [0, 24, 0]], 132 // CHECK-NEXT: [ 133 // CHECK-SAME: [0, 0, 0], 134 // CHECK-NEXT: [0, 0, 0]], 135 // CHECK-NEXT: [ 136 // CHECK-SAME: [0, 0, 0], 137 // CHECK-NEXT: [0, 0, 0]]] 138 %do1 = call @test_dense(%d1, %d2) : (tensor<5x6xf32>, tensor<6x2x3xf32>) -> tensor<?x?x?xf32> 139 call @printMemref3dF32(%do1) : (tensor<?x?x?xf32>) -> () 140 141 // Same results. 142 // CHECK-NEXT: Memref base@ = {{.*}} rank = 3 offset = 0 sizes = [5, 2, 3] strides = [6, 3, 1] data = 143 // CHECK-NEXT:[ 144 // CHECK-SAME: [ 145 // CHECK-SAME: [6, 0, 0], 146 // CHECK-NEXT: [0, 0, 0]], 147 // CHECK-NEXT: [ 148 // CHECK-SAME: [0, 0, 0], 149 // CHECK-NEXT: [0, 14, 0]], 150 // CHECK-NEXT: [ 151 // CHECK-SAME: [0, 0, 0], 152 // CHECK-NEXT: [0, 24, 0]], 153 // CHECK-NEXT: [ 154 // CHECK-SAME: [0, 0, 0], 155 // CHECK-NEXT: [0, 0, 0]], 156 // CHECK-NEXT: [ 157 // CHECK-SAME: [0, 0, 0], 158 // CHECK-NEXT: [0, 0, 0]]] 159 %so1 = call @test_sparse_rhs(%d1, %s2): (tensor<5x6xf32>, tensor<6x2x3xf32, #COO_3D>) -> tensor<?x?x?xf32> 160 call @printMemref3dF32(%so1) : (tensor<?x?x?xf32>) -> () 161 162 // Same results. 163 // CHECK-NEXT: Memref base@ = {{.*}} rank = 3 offset = 0 sizes = [5, 2, 3] strides = [6, 3, 1] data = 164 // CHECK-NEXT:[ 165 // CHECK-SAME: [ 166 // CHECK-SAME: [6, 0, 0], 167 // CHECK-NEXT: [0, 0, 0]], 168 // CHECK-NEXT: [ 169 // CHECK-SAME: [0, 0, 0], 170 // CHECK-NEXT: [0, 14, 0]], 171 // CHECK-NEXT: [ 172 // CHECK-SAME: [0, 0, 0], 173 // CHECK-NEXT: [0, 24, 0]], 174 // CHECK-NEXT: [ 175 // CHECK-SAME: [0, 0, 0], 176 // CHECK-NEXT: [0, 0, 0]], 177 // CHECK-NEXT: [ 178 // CHECK-SAME: [0, 0, 0], 179 // CHECK-NEXT: [0, 0, 0]]] 180 %so2 = call @test_sparse_all(%s1, %s2): (tensor<5x6xf32, #COO_2D>, tensor<6x2x3xf32, #COO_3D>) -> tensor<?x?x?xf32> 181 call @printMemref3dF32(%so2) : (tensor<?x?x?xf32>) -> () 182 183 // Same results. 184 // CHECK-NEXT: Memref base@ = {{.*}} rank = 3 offset = 0 sizes = [5, 2, 3] strides = [6, 3, 1] data = 185 // CHECK-NEXT:[ 186 // CHECK-SAME: [ 187 // CHECK-SAME: [6, 0, 0], 188 // CHECK-NEXT: [0, 0, 0]], 189 // CHECK-NEXT: [ 190 // CHECK-SAME: [0, 0, 0], 191 // CHECK-NEXT: [0, 14, 0]], 192 // CHECK-NEXT: [ 193 // CHECK-SAME: [0, 0, 0], 194 // CHECK-NEXT: [0, 24, 0]], 195 // CHECK-NEXT: [ 196 // CHECK-SAME: [0, 0, 0], 197 // CHECK-NEXT: [0, 0, 0]], 198 // CHECK-NEXT: [ 199 // CHECK-SAME: [0, 0, 0], 200 // CHECK-NEXT: [0, 0, 0]]] 201 %so3 = call @test_sparse_all_2(%s1, %s3): (tensor<5x6xf32, #COO_2D>, tensor<2x3x6xf32, #COO_3D>) -> tensor<?x?x?xf32> 202 call @printMemref3dF32(%so2) : (tensor<?x?x?xf32>) -> () 203 204 bufferization.dealloc_tensor %s1 : tensor<5x6xf32, #COO_2D> 205 bufferization.dealloc_tensor %s2 : tensor<6x2x3xf32, #COO_3D> 206 bufferization.dealloc_tensor %s3 : tensor<2x3x6xf32, #COO_3D> 207 bufferization.dealloc_tensor %do1 : tensor<?x?x?xf32> 208 bufferization.dealloc_tensor %so1 : tensor<?x?x?xf32> 209 bufferization.dealloc_tensor %so2 : tensor<?x?x?xf32> 210 bufferization.dealloc_tensor %so3 : tensor<?x?x?xf32> 211 212 return 213 } 214} 215