xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_quantized_matmul.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// 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} | %{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} | %{run_sve} | FileCheck %s %}
33
34#DCSR = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : compressed, d1 : compressed) }>
35
36// An example of a quantized sparse matmul. With the zero offset for the
37// sparse input, the sparsifier generates very efficient code for the
38//      x(i,j) += (ext(a(i,k)) - 2) * ext(b(k,j))
39// operation.
40module {
41
42  func.func @quantized_matmul(%input1: tensor<5x3xi8>,
43                         %input2: tensor<3x6xi8, #DCSR>,
44                         %output: tensor<5x6xi32>) -> tensor<5x6xi32> {
45    %c0 = arith.constant 0 : i32
46    %c2 = arith.constant 2 : i32
47    %0 = linalg.quantized_matmul
48      ins(%input1, %input2, %c2, %c0 : tensor<5x3xi8>, tensor<3x6xi8, #DCSR>, i32, i32)
49      outs(%output : tensor<5x6xi32>) -> tensor<5x6xi32>
50    return %0: tensor<5x6xi32>
51  }
52
53  func.func @main() {
54    %c0 = arith.constant 0 : index
55    %i0 = arith.constant 0 : i32
56
57    %input1 = arith.constant dense<[
58      [  -128,   3,  127 ],
59      [     0,   0,    0 ],
60      [    11,   1,    0 ],
61      [     0,   5,   -1 ],
62      [    13,   0,    3 ]
63    ]> : tensor<5x3xi8>
64
65    %input2 = arith.constant dense<[
66      [  127,   0, -128,    0,   0,   3 ],
67      [    0,   0,    0,    0,   0,   0 ],
68      [    0,   0,    0,  100,  10,   0 ]
69    ]> : tensor<3x6xi8>
70
71    %sparse_input2 = sparse_tensor.convert %input2 : tensor<3x6xi8> to tensor<3x6xi8, #DCSR>
72
73    // Call the kernel.
74    %output = arith.constant dense<0> : tensor<5x6xi32>
75    %0 = call @quantized_matmul(%input1, %sparse_input2, %output)
76       : (tensor<5x3xi8>,
77          tensor<3x6xi8, #DCSR>,
78          tensor<5x6xi32>) -> tensor<5x6xi32>
79
80    //
81    // Verify the output.
82    //
83    // CHECK:    ( ( -16510, 0, 16640, 12500, 1250, -390 ),
84    // CHECK-SAME: ( -254, 0, 256, -200, -20, -6 ),
85    // CHECK-SAME: ( 1143, 0, -1152, -200, -20, 27 ),
86    // CHECK-SAME: ( -254, 0, 256, -300, -30, -6 ),
87    // CHECK-SAME: ( 1397, 0, -1408, 100, 10, 33 ) )
88    //
89    %v = vector.transfer_read %0[%c0, %c0], %i0
90      : tensor<5x6xi32>, vector<5x6xi32>
91    vector.print %v : vector<5x6xi32>
92
93    // Release the resources.
94    bufferization.dealloc_tensor %sparse_input2 : tensor<3x6xi8, #DCSR>
95    bufferization.dealloc_tensor %0 : tensor<5x6xi32>
96
97    return
98  }
99}
100