|
| 1 | +// RUN: mlir-opt %s -transform-interpreter | FileCheck %s |
| 2 | + |
| 3 | +func.func @mmt4d_to_fma(%A: tensor<16x16x8x1xf32>, %B: tensor<16x16x8x1xf32>, %C_in: tensor<16x16x8x8xf32>) -> tensor<16x16x8x8xf32> { |
| 4 | + %res = linalg.mmt4d |
| 5 | + ins(%A, %B: tensor<16x16x8x1xf32>, tensor<16x16x8x1xf32>) |
| 6 | + outs(%C_in: tensor<16x16x8x8xf32>) |
| 7 | + -> tensor<16x16x8x8xf32> |
| 8 | + return %res : tensor<16x16x8x8xf32> |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +// CHECK-LABEL: @mmt4d_to_fma |
| 13 | +// CHECK-COUNT-8: vector.fma |
| 14 | + |
| 15 | +module attributes {transform.with_named_sequence} { |
| 16 | + transform.named_sequence @__transform_main(%module: !transform.any_op {transform.readonly}) { |
| 17 | + %func = transform.structured.match ops{["func.func"]} in %module : (!transform.any_op) -> !transform.op<"func.func"> |
| 18 | + |
| 19 | + %mmt4d = transform.structured.match ops{["linalg.mmt4d"]} in %func |
| 20 | + |
| 21 | + // Step 1: Tile |
| 22 | + : (!transform.op<"func.func">) -> !transform.any_op |
| 23 | + // Tile parallel dims |
| 24 | + %tiled_linalg_op_p, %loops:4 = transform.structured.tile_using_for %mmt4d[1, 1, 0, 8, 8, 0] |
| 25 | + : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op) |
| 26 | + // Tile reduction dims |
| 27 | + %tiled_linalg_op_r, %loops2:2 = transform.structured.tile_using_for %tiled_linalg_op_p[0, 0, 1, 0, 0, 1] |
| 28 | + : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op) |
| 29 | + |
| 30 | + // Step 2: Vectorize |
| 31 | + transform.structured.vectorize %tiled_linalg_op_r : !transform.any_op |
| 32 | + |
| 33 | + // Step 3: Simplify |
| 34 | + // vector.multi_reduction --> vector.contract |
| 35 | + // Generates a 6-dim vector.contract with the dim matching the original MMT4D Op |
| 36 | + // and with the following split into parallel and reduction dims: |
| 37 | + // * parallel, parallel, reduction, parallel, parallel, reduction |
| 38 | + transform.apply_patterns to %func { |
| 39 | + transform.apply_patterns.vector.reduction_to_contract |
| 40 | + // Reduce the rank of xfer ops. This transforms vector.contract to be |
| 41 | + // more matmul-like and to enable the lowering to outer product Ops. |
| 42 | + transform.apply_patterns.vector.transfer_permutation_patterns |
| 43 | + } : !transform.op<"func.func"> |
| 44 | + |
| 45 | + // Hoisting and LICM - not strictly required |
| 46 | + %func_h = transform.structured.hoist_redundant_vector_transfers %func |
| 47 | + : (!transform.op<"func.func">) -> !transform.op<"func.func"> |
| 48 | + %all_loops = transform.structured.match interface{LoopLikeInterface} in %func_h |
| 49 | + : (!transform.op<"func.func">) -> !transform.any_op |
| 50 | + transform.apply_licm to %all_loops : !transform.any_op |
| 51 | + transform.loop.hoist_loop_invariant_subsets %all_loops : !transform.any_op |
| 52 | + |
| 53 | + // Simplify the 6-dim vector.contract into a 3-dim matmul-like |
| 54 | + // vector.contract with the following split into parallel and reduction |
| 55 | + // dims: |
| 56 | + // * parallel, parallel, reduction |
| 57 | + transform.apply_patterns to %func_h { |
| 58 | + transform.apply_patterns.vector.reduction_to_contract |
| 59 | + transform.apply_patterns.vector.cast_away_vector_leading_one_dim |
| 60 | + transform.apply_patterns.canonicalization |
| 61 | + } : !transform.op<"func.func"> |
| 62 | + |
| 63 | + // Step 4: Lower vector.contract to vector.fma via vector.outerproduct |
| 64 | + transform.apply_patterns to %func_h { |
| 65 | + transform.apply_patterns.vector.lower_contraction lowering_strategy = "outerproduct" |
| 66 | + transform.apply_patterns.vector.lower_outerproduct |
| 67 | + } : !transform.op<"func.func"> |
| 68 | + transform.yield |
| 69 | + } |
| 70 | +} |
0 commit comments