xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_pack_d.mlir (revision eb206e9ea84eff0a0596fed2de8316d924f946d1)
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} | %{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} | %{run} | FileCheck %s
26
27#CCC = #sparse_tensor.encoding<{
28  map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : compressed),
29  posWidth = 64,
30  crdWidth = 32
31}>
32
33#DenseCSR = #sparse_tensor.encoding<{
34  map = (d0, d1, d2) -> (d0 : dense, d1 : dense, d2 : compressed),
35  posWidth = 64,
36  crdWidth = 32
37}>
38
39#CSRDense = #sparse_tensor.encoding<{
40  map = (d0, d1, d2) -> (d0 : dense, d1 : compressed, d2 : dense),
41  posWidth = 64,
42  crdWidth = 32
43}>
44
45//
46// Test assembly operation with CCC, dense-CSR and CSR-dense.
47//
48module {
49  //
50  // Main driver.
51  //
52  func.func @main() {
53    %c0 = arith.constant 0 : index
54    %f0 = arith.constant 0.0 : f32
55
56    //
57    // Setup CCC.
58    //
59
60    %data0 = arith.constant dense<
61       [ 1.0,  2.0,  3.0,  4.0,  5.0,  6.0,  7.0,  8.0 ]> : tensor<8xf32>
62    %pos00 = arith.constant dense<
63       [ 0, 3  ]> : tensor<2xi64>
64    %crd00 = arith.constant dense<
65       [ 0, 2, 3 ]> : tensor<3xi32>
66    %pos01 = arith.constant dense<
67       [ 0, 2, 4, 5  ]> : tensor<4xi64>
68    %crd01 = arith.constant dense<
69       [ 0, 1, 1, 2, 1 ]> : tensor<5xi32>
70    %pos02 = arith.constant dense<
71       [ 0, 2, 4, 5, 7, 8  ]> : tensor<6xi64>
72    %crd02 = arith.constant dense<
73       [ 0, 1, 0, 1, 0, 0, 1, 0  ]> : tensor<8xi32>
74
75    %s0 = sparse_tensor.assemble (%pos00, %crd00, %pos01, %crd01, %pos02, %crd02), %data0 :
76       (tensor<2xi64>, tensor<3xi32>,
77        tensor<4xi64>, tensor<5xi32>,
78        tensor<6xi64>, tensor<8xi32>), tensor<8xf32> to tensor<4x3x2xf32, #CCC>
79
80    //
81    // Setup DenseCSR.
82    //
83
84    %data1 = arith.constant dense<
85       [ 1.0,  2.0,  3.0,  4.0,  5.0,  6.0,  7.0,  8.0,
86         9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 ]> : tensor<16xf32>
87    %pos1 = arith.constant dense<
88       [ 0, 2, 3, 4, 6, 6, 7, 9, 11, 13, 14, 15, 16 ]> : tensor<13xi64>
89    %crd1 = arith.constant dense<
90       [ 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1]> : tensor<16xi32>
91
92    %s1 = sparse_tensor.assemble (%pos1, %crd1), %data1 : (tensor<13xi64>, tensor<16xi32>), tensor<16xf32> to tensor<4x3x2xf32, #DenseCSR>
93
94    //
95    // Setup CSRDense.
96    //
97
98    %data2 = arith.constant dense<
99      [ 1.0,  2.0,  0.0,  3.0,  4.0,  0.0, 5.0, 6.0,  0.0, 7.0,  8.0,
100        9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 0.0, 0.0, 15.0, 0.0, 16.0 ]> : tensor<22xf32>
101    %pos2 = arith.constant dense<
102      [ 0, 3, 5, 8, 11 ]> : tensor<5xi64>
103    %crd2 = arith.constant dense<
104      [ 0, 1, 2, 0, 2, 0, 1, 2, 0, 1, 2 ]> : tensor<11xi32>
105
106    %s2 = sparse_tensor.assemble (%pos2, %crd2), %data2  : (tensor<5xi64>, tensor<11xi32>), tensor<22xf32> to tensor<4x3x2xf32, #CSRDense>
107
108    //
109    // Verify.
110    //
111    // CHECK:      ---- Sparse Tensor ----
112    // CHECK-NEXT: nse = 8
113    // CHECK-NEXT: dim = ( 4, 3, 2 )
114    // CHECK-NEXT: lvl = ( 4, 3, 2 )
115    // CHECK-NEXT: pos[0] : ( 0, 3 )
116    // CHECK-NEXT: crd[0] : ( 0, 2, 3 )
117    // CHECK-NEXT: pos[1] : ( 0, 2, 4, 5 )
118    // CHECK-NEXT: crd[1] : ( 0, 1, 1, 2, 1 )
119    // CHECK-NEXT: pos[2] : ( 0, 2, 4, 5, 7, 8 )
120    // CHECK-NEXT: crd[2] : ( 0, 1, 0, 1, 0, 0, 1, 0 )
121    // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, 6, 7, 8 )
122    // CHECK-NEXT: ----
123    // CHECK:      ---- Sparse Tensor ----
124    // CHECK-NEXT: nse = 16
125    // CHECK-NEXT: dim = ( 4, 3, 2 )
126    // CHECK-NEXT: lvl = ( 4, 3, 2 )
127    // CHECK-NEXT: pos[2] : ( 0, 2, 3, 4, 6, 6, 7, 9, 11, 13, 14, 15, 16 )
128    // CHECK-NEXT: crd[2] : ( 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1 )
129    // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 )
130    // CHECK-NEXT: ----
131    // CHECK:      ---- Sparse Tensor ----
132    // CHECK-NEXT: nse = 22
133    // CHECK-NEXT: dim = ( 4, 3, 2 )
134    // CHECK-NEXT: lvl = ( 4, 3, 2 )
135    // CHECK-NEXT: pos[1] : ( 0, 3, 5, 8, 11 )
136    // CHECK-NEXT: crd[1] : ( 0, 1, 2, 0, 2, 0, 1, 2, 0, 1, 2 )
137    // CHECK-NEXT: values : ( 1, 2, 0, 3, 4, 0, 5, 6, 0, 7, 8, 9, 10, 11, 12, 13, 14, 0, 0, 15, 0, 16 )
138    // CHECK-NEXT: ----
139    //
140    sparse_tensor.print %s0 : tensor<4x3x2xf32, #CCC>
141    sparse_tensor.print %s1 : tensor<4x3x2xf32, #DenseCSR>
142    sparse_tensor.print %s2 : tensor<4x3x2xf32, #CSRDense>
143
144    // TODO: This check is no longer needed once the codegen path uses the
145    // buffer deallocation pass. "dealloc_tensor" turn into a no-op in the
146    // codegen path.
147    %has_runtime = sparse_tensor.has_runtime_library
148    scf.if %has_runtime {
149      // sparse_tensor.assemble copies buffers when running with the runtime
150      // library. Deallocations are not needed when running in codegen mode.
151      bufferization.dealloc_tensor %s0 : tensor<4x3x2xf32, #CCC>
152      bufferization.dealloc_tensor %s1 : tensor<4x3x2xf32, #DenseCSR>
153      bufferization.dealloc_tensor %s2 : tensor<4x3x2xf32, #CSRDense>
154    }
155
156    return
157  }
158}
159