1// RUN: mlir-opt %s \ 2// RUN: --sparsification --sparse-tensor-conversion \ 3// RUN: --convert-linalg-to-loops --convert-vector-to-scf --convert-scf-to-std \ 4// RUN: --func-bufferize --tensor-constant-bufferize --tensor-bufferize \ 5// RUN: --std-bufferize --finalizing-bufferize \ 6// RUN: --convert-vector-to-llvm --convert-std-to-llvm | \ 7// RUN: TENSOR0="%mlir_integration_test_dir/data/test.tns" \ 8// RUN: mlir-cpu-runner \ 9// RUN: -e entry -entry-point-result=void \ 10// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 11// RUN: FileCheck %s 12 13!Filename = type !llvm.ptr<i8> 14 15#SparseTensor = #sparse_tensor.encoding<{ 16 dimLevelType = [ "compressed", "compressed", "compressed", "compressed", 17 "compressed", "compressed", "compressed", "compressed" ], 18 // Note that any dimOrdering permutation should give the same results 19 // since, even though it impacts the sparse storage scheme layout, 20 // it should not change the semantics. 21 dimOrdering = affine_map<(i,j,k,l,m,n,o,p) -> (p,o,j,k,i,l,m,n)> 22}> 23 24#trait_flatten = { 25 indexing_maps = [ 26 affine_map<(i,j,k,l,m,n,o,p) -> (i,j,k,l,m,n,o,p)>, // A 27 affine_map<(i,j,k,l,m,n,o,p) -> (i,j)> // X (out) 28 ], 29 iterator_types = [ "parallel", "parallel", "reduction", "reduction", 30 "reduction", "reduction", "reduction", "reduction" ], 31 doc = "X(i,j) += A(i,j,k,l,m,n,o,p)" 32} 33 34// 35// Integration test that lowers a kernel annotated as sparse to 36// actual sparse code, initializes a matching sparse storage scheme 37// from file, and runs the resulting code with the JIT compiler. 38// 39module { 40 // 41 // A kernel that flattens a rank 8 tensor into a dense matrix. 42 // 43 func @kernel_flatten(%arga: tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>, 44 %argx: tensor<7x3xf64>) -> tensor<7x3xf64> { 45 %0 = linalg.generic #trait_flatten 46 ins(%arga: tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>) 47 outs(%argx: tensor<7x3xf64>) { 48 ^bb(%a: f64, %x: f64): 49 %0 = addf %x, %a : f64 50 linalg.yield %0 : f64 51 } -> tensor<7x3xf64> 52 return %0 : tensor<7x3xf64> 53 } 54 55 func private @getTensorFilename(index) -> (!Filename) 56 57 // 58 // Main driver that reads tensor from file and calls the sparse kernel. 59 // 60 func @entry() { 61 %d0 = constant 0.0 : f64 62 %c0 = constant 0 : index 63 %c1 = constant 1 : index 64 %c3 = constant 3 : index 65 %c7 = constant 7 : index 66 67 // Setup matrix memory that is initialized to zero. 68 %xdata = memref.alloc() : memref<7x3xf64> 69 scf.for %i = %c0 to %c7 step %c1 { 70 scf.for %j = %c0 to %c3 step %c1 { 71 memref.store %d0, %xdata[%i, %j] : memref<7x3xf64> 72 } 73 } 74 %x = memref.tensor_load %xdata : memref<7x3xf64> 75 76 // Read the sparse tensor from file, construct sparse storage. 77 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename) 78 %a = sparse_tensor.new %fileName : !llvm.ptr<i8> to tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor> 79 80 // Call the kernel. 81 %0 = call @kernel_flatten(%a, %x) 82 : (tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>, tensor<7x3xf64>) -> tensor<7x3xf64> 83 84 // Print the result for verification. 85 // 86 // CHECK: ( 6.25, 0, 0 ) 87 // CHECK: ( 4.224, 6.21, 0 ) 88 // CHECK: ( 0, 0, 15.455 ) 89 // CHECK: ( 0, 0, 0 ) 90 // CHECK: ( 0, 0, 0 ) 91 // CHECK: ( 0, 0, 0 ) 92 // CHECK: ( 7, 0, 0 ) 93 // 94 %r = memref.buffer_cast %0 : memref<7x3xf64> 95 scf.for %i = %c0 to %c7 step %c1 { 96 %v = vector.transfer_read %r[%i, %c0], %d0: memref<7x3xf64>, vector<3xf64> 97 vector.print %v : vector<3xf64> 98 } 99 100 // Release the resources. 101 memref.dealloc %xdata : memref<7x3xf64> 102 103 return 104 } 105} 106