Skip to content

Commit

Permalink
[midend][tests] Add batch level parallelism outer product BatchMatMul…
Browse files Browse the repository at this point in the history
… optimization pass and tests.
  • Loading branch information
EllisLambda authored and xlinsist committed Sep 9, 2023
1 parent da0c057 commit a8f1653
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/MLIRLinalg/linalg-batch-matmul.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: buddy-opt -batchmatmul-optimize -verify-diagnostics -expand-strided-metadata -lower-affine -convert-vector-to-llvm -finalize-memref-to-llvm -convert-scf-to-cf -convert-linalg-to-llvm -llvm-request-c-wrappers -convert-func-to-llvm -reconcile-unrealized-casts %s \
// RUN: | mlir-cpu-runner -O0 -e buddy_batchmatmul_f32 \
// RUN: -shared-libs=%mlir_runner_utils_dir/libmlir_runner_utils%shlibext,%mlir_runner_utils_dir/libmlir_c_runner_utils%shlibext \
// RUN: | FileCheck %s

memref.global "private" @A : memref<2x2x3xf32> = dense<[[[9., 4., 6.],[2., 4., 0.]],[[6., 3., 3.],[0., 4., 7.]]]>
memref.global "private" @B : memref<2x3x4xf32> = dense<[[[1., 3., 8., 0.],[1., 8., 8., 7.], [6., 9., 7., 9.]],[[3., 8., 6., 8.],[2., 7., 0., 6.],[0., 4., 0., 4.]]]>
memref.global "private" @C : memref<2x2x4xf32> = dense<[[[ 49., 113., 146., 82.],[ 6., 38., 48., 28.]],[[ 24., 81., 36., 78.],[ 8., 56., 0., 52.]]]>

func.func private @printMemrefF32(memref<*xf32>) attributes { llvm.emit_c_interface }

func.func @buddy_batchmatmul_f32() -> f32{
%a = memref.get_global @A : memref<2x2x3xf32>
%b = memref.get_global @B : memref<2x3x4xf32>
%c = memref.get_global @C : memref<2x2x4xf32>

linalg.batch_matmul
ins(%a, %b: memref<2x2x3xf32>, memref<2x3x4xf32>)
outs(%c: memref<2x2x4xf32>)
%printed_c = memref.cast %c : memref<2x2x4xf32> to memref<*xf32>
call @printMemrefF32(%printed_c) : (memref<*xf32>) -> ()
// CHECK: {{Unranked Memref base@ = 0x[0-9A-Fa-f]{1,} rank = 3 offset = 0 sizes = \[2, 2, 4\] strides = \[8, 4, 1\] data =}}
// CHECK{LITERAL}: [[[98, 226, 292, 164],
// CHECK{LITERAL}: [12, 76, 96, 56]],
// CHECK{LITERAL}: [[48, 162, 72, 156],
// CHECK{LITERAL}: [16, 112, 0, 104]]]
%zero = arith.constant 0.0 :f32
return %zero :f32
}
55 changes: 55 additions & 0 deletions examples/MLIRLinalg/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,61 @@ linalg-matmul-optimize-run:
${MLIR_CPU_RUNNER} ${OPT_FLAG} -e main -entry-point-result=void \
-shared-libs=${MLIR_RUNNER_UTILS} -shared-libs=${MLIR_C_RUNNER_UTILS}

linalg-batch-matmul-optimize-run:
@${BUDDY_OPT} linalg-matmul.mlir ${MLIR_OPT_OPTIONS} \
-batchmatmul-optimize="step-placeholder=64" \
-convert-linalg-to-loops \
-expand-strided-metadata \
-lower-affine \
-convert-scf-to-cf \
-convert-vector-to-llvm \
-finalize-memref-to-llvm \
-convert-arith-to-llvm \
-convert-func-to-llvm \
-reconcile-unrealized-casts | \
${MLIR_CPU_RUNNER} ${OPT_FLAG} -e main -entry-point-result=void \
-shared-libs=${MLIR_RUNNER_UTILS} -shared-libs=${MLIR_C_RUNNER_UTILS}

linalg-batch-matmul-lower:
@${MLIR_OPT} linalg-batch-matmul.mlir ${MLIR_OPT_OPTIONS} \
-convert-linalg-to-loops -lower-affine -convert-scf-to-cf \
-convert-vector-to-llvm -finalize-memref-to-llvm -convert-arith-to-llvm \
-convert-func-to-llvm -reconcile-unrealized-casts \
-o ./log.mlir

linalg-batch-matmul-translate:
@${MLIR_OPT} linalg-batch-matmul.mlir ${MLIR_OPT_OPTIONS} \
-convert-linalg-to-loops -lower-affine -convert-scf-to-cf \
-convert-vector-to-llvm -finalize-memref-to-llvm -convert-arith-to-llvm \
-convert-func-to-llvm -reconcile-unrealized-casts | \
${MLIR_TRANSLATE} --mlir-to-llvmir -o log.ll

linalg-batch-matmul-run:
@${MLIR_OPT} linalg-batch-matmul.mlir ${MLIR_OPT_OPTIONS} \
-convert-linalg-to-loops -lower-affine -convert-scf-to-cf \
-convert-vector-to-llvm -finalize-memref-to-llvm -convert-arith-to-llvm \
-convert-func-to-llvm -reconcile-unrealized-casts | \
${MLIR_CPU_RUNNER} ${OPT_FLAG} -e main -entry-point-result=void -shared-libs=${MLIR_RUNNER_UTILS} -shared-libs=${MLIR_C_RUNNER_UTILS}

linalg-batch-matmul-optimize-lower:
@${BUDDY_OPT} linalg-batch-matmul.mlir ${MLIR_OPT_OPTIONS} \
-batchmatmul-optimize="step-placeholder=64" \
-o ./log.mlir

linalg-batch-matmul-optimize-translate:
@${BUDDY_OPT} linalg-batch-matmul.mlir ${MLIR_OPT_OPTIONS} \
-batchmatmul-optimize="step-placeholder=64" \
-convert-linalg-to-loops \
-expand-strided-metadata \
-lower-affine \
-convert-scf-to-cf \
-convert-vector-to-llvm \
-finalize-memref-to-llvm \
-convert-arith-to-llvm \
-convert-func-to-llvm \
-reconcile-unrealized-casts | \
${MLIR_TRANSLATE} --mlir-to-llvmir -o log.ll

linalg-conv2d_nchw_fchw-lower:
@${MLIR_OPT} ./linalg-conv2d_nchw_fchw.mlir \
-convert-linalg-to-loops -o ./log.mlir
Expand Down
Loading

0 comments on commit a8f1653

Please sign in to comment.