-
Notifications
You must be signed in to change notification settings - Fork 50
[OpOptimization] Add BatchMatMul benchmark and [OpOptimization] Further optimize BatchMatMulBroadcast and add OpenMP tests #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -227,13 +227,42 @@ $ mkdir build && cd build | |
| $ cmake -G Ninja .. \ | ||
| -DCMAKE_BUILD_TYPE=RELEASE \ | ||
| -DOP_OPTIMIZATION_BENCHMARKS=ON \ | ||
| -DCMAKE_CXX_COMPILER=clang++ \ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently we only need to use clang++ in matmul-benchmark, so the modification here should be withdrawn. |
||
| -DBUDDY_MLIR_BUILD_DIR=/PATH/TO/BUDDY-MLIR/BUILD/ | ||
| $ ninja <your target operation benchmark> | ||
|
|
||
| // Operation benchamrk supported include: | ||
| // - conv2d-nchw-fchw-benchmark | ||
| // - matmul-benchmark | ||
| ``` | ||
| ### matmul-benchmark | ||
| `OpenMP` and `lld` LTO is required in matmul-benchmark. To ensure version compatibility with the project, it's recommended to use the LLVM toolchains built within the `buddy-benchmark`. Follow the steps below: | ||
| - build llvm toolchains with `lld` and `OpenMP`. | ||
| ``` | ||
| $ cd buddy-mlir/llvm/build | ||
| $ cmake -G Ninja ../llvm \ | ||
| -DLLVM_ENABLE_PROJECTS="mlir;clang;lld;openmp" \ | ||
| -DLLVM_TARGETS_TO_BUILD="host;RISCV" \ | ||
| -DLLVM_ENABLE_ASSERTIONS=ON \ | ||
| -DLLVM_ENABLE_RUNTIMES=all \ | ||
| -DOPENMP_ENABLE_LIBOMPTARGET=OFF \ | ||
| -DCMAKE_BUILD_TYPE=RELEASE | ||
| ``` | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a specific construction process is required(in matmul-benchmark), please ensure that the construction process you provide is complete. For example, add the step |
||
| - use the `clang++` in `buddy-mlir/llvm/build/bin`. | ||
| ``` | ||
| $ mkdir build && cd build | ||
| $ cmake -G Ninja .. \ | ||
| -DCMAKE_BUILD_TYPE=RELEASE \ | ||
| -DOP_OPTIMIZATION_BENCHMARKS=ON \ | ||
| -DCMAKE_CXX_COMPILER=/PATH/TO/BUDDY-MLIR/BUILD/bin/clang++ \ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /PATH/TO/BUDDY-MLIR/BUILD/bin/clang++ -> |
||
| -DBUDDY_MLIR_BUILD_DIR=/PATH/TO/BUDDY-MLIR/BUILD/ | ||
| $ ninja matmul-benchmark | ||
| ``` | ||
| - `matmul-benchmark` need to load the `libomp.so` in `buddy-mlir/llvm/build/lib` to execute, here's a temporary way without root. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommand to rephrase the description here like this: To execute
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| ``` | ||
| $ export LD_LIBRARY_PATH=/PATH/TO/BUDDY-MLIR/BUILD/lib/:$LD_LIBRARY_PATH | ||
| ``` | ||
|
|
||
| Run TVM operation optimization benchmark cases. | ||
| - Install TVM ([steps](./thirdparty/README.md#tvm)). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module{ | ||
| func.func @bm_batch_matmul(%a : memref<?x?x?xf32>, %b : memref<?x?x?xf32>, %c : memref<?x?x?xf32>) { | ||
| linalg.batch_matmul | ||
| ins(%a, %b: memref<?x?x?xf32>, memref<?x?x?xf32>) | ||
| outs(%c: memref<?x?x?xf32>) | ||
| return | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // The MLIR prototype of batchmatmul-optimize in buddy-opt. | ||
|
|
||
| #map = affine_map<(d0) -> (d0 ceildiv STEP_PLACEHOLDER)> | ||
| #tail_len_map = affine_map<(d0) -> (d0 mod STEP_PLACEHOLDER)> | ||
| #if_set = affine_set<(d0)[s0] : (s0 - d0 * STEP_PLACEHOLDER >= STEP_PLACEHOLDER)> | ||
| #b_col_idx_tail_map = affine_map<(d0) -> (d0 * STEP_PLACEHOLDER)> | ||
|
|
||
| func.func @batch_matmul_broadcast_STEP_PLACEHOLDER(%a : memref<?x?x?xf32>, %b : memref<?x?x?xf32>, %c : memref<?x?x?xf32>) { | ||
| %c0 = arith.constant 0 : index | ||
| %c1 = arith.constant 1 : index | ||
| %c2 = arith.constant 2 : index | ||
| %step = arith.constant STEP_PLACEHOLDER : index | ||
| %c0_f32 = arith.constant 0.0 : f32 | ||
| %c0_f32_vec = vector.splat %c0_f32 : vector<STEP_PLACEHOLDERxf32> | ||
|
|
||
| %a_row = memref.dim %a, %c1 : memref<?x?x?xf32> | ||
| %a_col = memref.dim %a, %c2 : memref<?x?x?xf32> | ||
| %b_row = memref.dim %b, %c1 : memref<?x?x?xf32> | ||
| %b_col = memref.dim %b, %c2 : memref<?x?x?xf32> | ||
| %batch = memref.dim %a, %c0 : memref<?x?x?xf32> | ||
|
|
||
| %tail_len = affine.apply #tail_len_map(%b_col) | ||
| %mask_vec = vector.create_mask %tail_len : vector<STEP_PLACEHOLDERxi1> | ||
|
|
||
| affine.parallel (%batch_idx) = (0) to (%batch){ // Affine.parallel can be lowered to the omp dialect, which enables batch-level parallelization. | ||
| affine.prefetch %a[%batch_idx, %a_row, %a_col], read, locality<3>, data : memref<?x?x?xf32> // Explicitly prefetch, about 5% faster on X86. | ||
| affine.for %b_row_idx = 0 to %b_row { | ||
| affine.for %b_col_idx = 0 to #map(%b_col) { | ||
| %b_vec = affine.vector_load %b[%batch_idx, %b_row_idx, %b_col_idx * STEP_PLACEHOLDER] : memref<?x?x?xf32>, vector<STEP_PLACEHOLDERxf32> | ||
| %b_col_idx_tail = affine.apply #b_col_idx_tail_map(%b_col_idx) | ||
| affine.for %a_row_idx = 0 to %a_row { | ||
| %a_ele = affine.load %a[%batch_idx, %a_row_idx, %b_row_idx] : memref<?x?x?xf32> | ||
| %a_vec = vector.broadcast %a_ele : f32 to vector<STEP_PLACEHOLDERxf32> | ||
| %c_vec = affine.vector_load %c[%batch_idx, %a_row_idx, %b_col_idx * STEP_PLACEHOLDER] : memref<?x?x?xf32>, vector<STEP_PLACEHOLDERxf32> | ||
| %result_vec = vector.fma %a_vec, %b_vec, %c_vec : vector<STEP_PLACEHOLDERxf32> | ||
| affine.if #if_set(%b_col_idx)[%b_col] { | ||
| affine.vector_store %result_vec, %c[%batch_idx, %a_row_idx, %b_col_idx * STEP_PLACEHOLDER] : memref<?x?x?xf32>, vector<STEP_PLACEHOLDERxf32> | ||
| } else { | ||
| vector.maskedstore %c[%batch_idx, %a_row_idx, %b_col_idx_tail], %mask_vec, %result_vec : memref<?x?x?xf32>, vector<STEP_PLACEHOLDERxi1>, vector<STEP_PLACEHOLDERxf32> | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.