Skip to content

Commit 97c19a4

Browse files
authored
[mlir][ArmSME][nfc] Add integration test for i8 to i32 matmul (#81607)
Currently marked as XFAIL due to bug in QEMU. See test for details.
1 parent 9552a39 commit 97c19a4

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// RUN: mlir-opt %s \
2+
// RUN: -transform-interpreter -test-transform-dialect-erase-schedule \
3+
// RUN: -one-shot-bufferize="bufferize-function-boundaries" -canonicalize \
4+
// RUN: -arm-sme-vector-legalization -canonicalize -cse \
5+
// RUN: -convert-vector-to-arm-sme -arm-sme-outer-product-fusion \
6+
// RUN: -allocate-arm-sme-tiles -convert-arm-sme-to-scf \
7+
// RUN: -enable-arm-streaming="streaming-mode=streaming-locally za-mode=new-za only-if-required-by-ops" \
8+
// RUN: -convert-vector-to-scf=full-unroll -convert-arm-sme-to-llvm \
9+
// RUN: -test-lower-to-llvm | \
10+
// RUN: %mcr_aarch64_cmd \
11+
// RUN: -e=main -entry-point-result=void \
12+
// RUN: -march=aarch64 -mattr="+sve,+sme" \
13+
// RUN: -shared-libs=%mlir_runner_utils,%mlir_c_runner_utils,%arm_sme_abi_shlib,%mlir_arm_runner_utils | \
14+
// RUN: FileCheck %s
15+
16+
/// This is very similar to the SME multi-tile-matmul.mlir test, except that it
17+
/// tests a mixed i8 to i32 matmul and outer product fusion which fuses 16
18+
/// outer products (four per tile) into four 4-way outer products.
19+
20+
/// NOTE: QEMU gives incorrect result for SME SMOPA 4-way outer product
21+
/// instruction (version <= 8.2.0, latest version at time of writing), see:
22+
/// https://gitlab.com/qemu-project/qemu/-/issues/2083
23+
/// This test is expected to fail until a fixed version of QEMU can be used.
24+
25+
/// FIXME: Remove the 'XFAIL' below once a fixed QEMU version is available
26+
/// (and installed on CI buildbot).
27+
/// XFAIL: *
28+
29+
func.func @matmul_i8_to_i32(%A : tensor<?x?xi8>, %B : tensor<?x?xi8>, %C : tensor<?x?xi32>) {
30+
%res = linalg.matmul ins(%A, %B: tensor<?x?xi8>, tensor<?x?xi8>)
31+
outs(%C: tensor<?x?xi32>) -> tensor<?x?xi32>
32+
%xf = tensor.cast %res : tensor<?x?xi32> to tensor<*xi32>
33+
call @printMemrefI32(%xf) : (tensor<*xi32>) -> ()
34+
return
35+
}
36+
37+
func.func @main() {
38+
/// Set SVL to 128-bit. This ensures this small matmul will use all four
39+
/// 32-bit SME virtual tiles.
40+
%c128 = arith.constant 128 : i32
41+
func.call @setArmSVLBits(%c128) : (i32) -> ()
42+
43+
%c0 = arith.constant 0 : i32
44+
%c7 = arith.constant 7 : index
45+
46+
%A = arith.constant dense<[
47+
[1, 8, 15, 22, 29, 36, 43, 50, 57, 64, 71, 78, 85],
48+
[2, 9, 16, 23, 30, 37, 44, 51, 58, 65, 72, 79, 86],
49+
[3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 73, 80, 87],
50+
[4, 11, 18, 25, 32, 39, 46, 53, 60, 67, 74, 81, 88],
51+
[5, 12, 19, 26, 33, 40, 47, 54, 61, 68, 75, 82, 89],
52+
[6, 13, 20, 27, 34, 41, 48, 55, 62, 69, 76, 83, 90],
53+
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91]
54+
]> : tensor<7x13xi8>
55+
56+
%B_init = tensor.empty() : tensor<13x7xi8>
57+
%B = linalg.transpose ins(%A: tensor<7x13xi8>)
58+
outs(%B_init: tensor<13x7xi8>) permutation = [1, 0]
59+
60+
%A_dyn = tensor.cast %A : tensor<7x13xi8> to tensor<?x?xi8>
61+
%B_dyn = tensor.cast %B : tensor<13x7xi8> to tensor<?x?xi8>
62+
63+
%C_init = bufferization.alloc_tensor(%c7, %c7) : tensor<?x?xi32>
64+
%C = linalg.fill ins(%c0 : i32) outs(%C_init : tensor<?x?xi32>) -> tensor<?x?xi32>
65+
66+
// CHECK: Unranked Memref {{.*}} rank = 2 offset = 0 sizes = [7, 7] strides = [7, 1] data =
67+
// CHECK: [32955, 33514, 34073, 34632, 35191, 35750, 36309]
68+
// CHECK: [33514, 34086, 34658, 35230, 35802, 36374, 36946]
69+
// CHECK: [34073, 34658, 35243, 35828, 36413, 36998, 37583]
70+
// CHECK: [34632, 35230, 35828, 36426, 37024, 37622, 38220]
71+
// CHECK: [35191, 35802, 36413, 37024, 37635, 38246, 38857]
72+
// CHECK: [35750, 36374, 36998, 37622, 38246, 38870, 39494]
73+
// CHECK: [36309, 36946, 37583, 38220, 38857, 39494, 40131]
74+
call @matmul_i8_to_i32(%A_dyn, %B_dyn, %C) : (tensor<?x?xi8>, tensor<?x?xi8>, tensor<?x?xi32>) -> ()
75+
76+
return
77+
}
78+
79+
module attributes {transform.with_named_sequence} {
80+
transform.named_sequence @__transform_main(%module : !transform.any_op {transform.consumed}) {
81+
%matmul = transform.structured.match ops{["linalg.matmul"]} in %module
82+
: (!transform.any_op) -> !transform.any_op
83+
84+
// Step 1: Tile for size [8] x [8] (unrolled by 4), which corresponds to
85+
// (2 x SVLs) x (2 x SVLs), where SVLs is the number of 32-bit elements in a
86+
// vector of SVL bits. This uses all four 32-bit SME virtual tiles.
87+
%tiled_linalg_op, %loop_i, %loop_j, %loop_k = transform.structured.tile_using_for %matmul[[8], [8], 4]
88+
: (!transform.any_op) -> (!transform.any_op, !transform.op<"scf.for">, !transform.op<"scf.for">, !transform.op<"scf.for">)
89+
90+
// Step 2: Vectorize.
91+
transform.structured.vectorize %tiled_linalg_op vector_sizes [[8], [8], 4]
92+
: !transform.any_op
93+
94+
// Step 3: Bufferize ahead of TransferReadDropUnitDimsPattern, which
95+
// currently only supports memrefs.
96+
%bufferize = transform.bufferization.one_shot_bufferize %module
97+
{bufferize_function_boundaries=true} : (!transform.any_op) -> !transform.any_op
98+
99+
%func = transform.structured.match ops{["func.func"]} in %bufferize
100+
: (!transform.any_op) -> !transform.any_op
101+
102+
// Step 4: Lower vector.multi_reduction to vector.contract (+ some helpful patterns).
103+
transform.apply_patterns to %func {
104+
transform.apply_patterns.vector.lower_masked_transfers
105+
transform.apply_patterns.vector.transfer_permutation_patterns
106+
transform.apply_patterns.vector.reduction_to_contract
107+
} : !transform.any_op
108+
109+
// Step 5: Lower vector.contract to vector.outerproduct. Also drop unit
110+
// dims, specifically to prevent vector.transfer_read of vector<[8]x1xi32>,
111+
// which can't be lowered in generic path.
112+
transform.apply_patterns to %func {
113+
transform.apply_patterns.vector.lower_contraction lowering_strategy = "outerproduct"
114+
transform.apply_patterns.vector.lower_masks
115+
transform.apply_patterns.vector.rank_reducing_subview_patterns
116+
} : !transform.any_op
117+
118+
transform.yield
119+
}
120+
}
121+
122+
func.func private @printMemrefI32(%ptr : tensor<*xi32>)
123+
func.func private @setArmSVLBits(%bits : i32)

0 commit comments

Comments
 (0)