Skip to content

Data tiling: transpose narrow-N into narrow-M - #16890

Closed
bjacob wants to merge 1 commit into
iree-org:mainfrom
bjacob:transpose
Closed

bjacob wants to merge 1 commit into
iree-org:mainfrom
bjacob:transpose

Conversation

@bjacob

@bjacob bjacob commented Mar 25, 2024 •

Copy link
Copy Markdown
Collaborator

This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:

  • With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
  • When we get to serious distribution tiling (Proper distribution-tiling for mmt4d #16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in mmt4d (the t in mmt4d), as generally with matrix multiplication

B * Transpose(A) == Transpose( A * Transpose(B) )

so in mmt4d terms

mmt4d(B, A) == Transpose(mmt4d(A, B))

As pack and unpack already have enough generality to perform these transpositions, we just directly generate the right transposing pack and unpack ops. An earlier plan was to generate linalg.transpose and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed pack, unpack.

A legitimate question was: should this transposition be implemented at SetEncoding instead of at MaterializeEncoding? That would have been simpler in some ways, but:

  • The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
  • SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
  • The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
  • Before MaterializeEncoding we would have to handle linalg.generic, not just named matmul ops.

benchmark-extra: x86_64-dt-only, android-cpu-dt-only

@bjacob

This comment was marked as outdated.

@hanhanW

hanhanW commented Mar 25, 2024

Copy link
Copy Markdown
Contributor

I created a smaller repro here: https://gist.github.com/hanhanW/0a1aafecb0a3539614001007e1f56b0f

Run: iree-opt --pass-pipeline="builtin.module(util.func(iree-codegen-cpu-materialize-encoding),canonicalize,cse)" ~/a.mlir

@hanhanW

hanhanW commented Mar 25, 2024

Copy link
Copy Markdown
Contributor

@bjacob I got the bug. You need to transpose the outs from linalg.matmul as well. We packed init value of outs, but it is not transposed. So the types mismatch. Applying the below patch should fix the issue.

diff --git a/compiler/src/iree/compiler/Codegen/Common/MaterializeEncodingIntoPackUnPack.cpp b/compiler/src/iree/compiler/Codegen/Common/MaterializeEncodingIntoPackUnPack.cpp
index ace519d430..eb9eb6e3ee 100644
--- a/compiler/src/iree/compiler/Codegen/Common/MaterializeEncodingIntoPackUnPack.cpp
+++ b/compiler/src/iree/compiler/Codegen/Common/MaterializeEncodingIntoPackUnPack.cpp
@@ -773,7 +773,37 @@ struct SetEncodingOpToPackOpConversion
       rewriter.replaceOp(encodingOp, result);
       return success();
     }
-    rewriter.replaceOp(encodingOp, packOp->getResult());
+    Value result = packOp->getResult();
+
+    auto encoding = getEncodingAttr(encodingOp.getResultType());
+    if (encoding &&
+        encoding.getRole().getValue() ==
+            mlir::iree_compiler::IREE::LinalgExt::EncodingRole::RESULT) {
+      llvm::dbgs() << "lowerSetEncodingToPackOp sees:\n\nencodingOp:\n\n"
+                   << encodingOp << "\n\npackedValue:\n\n"
+                   << result << "\n\n";
+
+      IntegerAttr narrowM = encoding.getMatmulNarrow_M();
+      IntegerAttr narrowN = encoding.getMatmulNarrow_N();
+      bool transpose =
+          narrowN && (!narrowM || narrowM.getInt() > narrowN.getInt());
+      if (transpose) {
+        llvm::dbgs() << "Transpose PackOp From SetEncoding:\n";
+        SmallVector<int64_t> permutation;
+        if (result.getType().cast<RankedTensorType>().getRank() == 4) {
+          // linalg.matmul case
+          permutation = {1, 0, 3, 2};
+        } else if (result.getType().cast<RankedTensorType>().getRank() == 5) {
+          // linalg.batch_matmul case. Leave dimension 0 alone, it's the batch.
+          permutation = {0, 2, 1, 4, 3};
+        } else {
+          assert(false && "rank should be 4 or 5 here");
+        }
+        result = createTranspose(rewriter, result, permutation)->getResult(0);
+      }
+    }
+
+    rewriter.replaceOp(encodingOp, result);
     return success();
   }
 };

Here is the result IR after applying the patch:

#executable_target_xyz = #hal.executable.target<"xyz", "xyz", {cpu_features = "+avx512vnni", target_triple = "x86_64-xyz-xyz"}>
#map = affine_map<()[s0] -> (s0 ceildiv 16)>
#map1 = affine_map<()[s0] -> (s0 ceildiv 2)>
module {
  util.func public @matmul_n4_i8(%arg0: !hal.buffer_view, %arg1: !hal.buffer_view, %arg2: !hal.buffer_view) -> !hal.buffer_view attributes {hal.executable.target = #executable_target_xyz} {
    %c0_i32 = arith.constant 0 : i32
    %c0_i8 = arith.constant 0 : i8
    %0 = hal.buffer_view.dim<%arg0 : !hal.buffer_view>[0] : index
    %1 = hal.buffer_view.dim<%arg0 : !hal.buffer_view>[1] : index
    %2 = hal.tensor.import %arg0 "input0" : !hal.buffer_view -> tensor<?x?xi8>{%0, %1}
    %3 = hal.buffer_view.dim<%arg1 : !hal.buffer_view>[0] : index
    %4 = hal.tensor.import %arg1 "input1" : !hal.buffer_view -> tensor<?x4xi8>{%3}
    %5 = hal.buffer_view.dim<%arg2 : !hal.buffer_view>[0] : index
    %6 = hal.tensor.import %arg2 "input2" : !hal.buffer_view -> tensor<?x4xi32>{%5}
    %7 = affine.apply #map()[%0]
    %8 = affine.apply #map1()[%1]
    %9 = tensor.empty(%7, %8) : tensor<?x?x16x2xi8>
    %pack = tensor.pack %2 padding_value(%c0_i8 : i8) outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [16, 2] into %9 : tensor<?x?xi8> -> tensor<?x?x16x2xi8>
    %10 = affine.apply #map1()[%3]
    %11 = tensor.empty(%10) : tensor<1x?x4x2xi8>
    %pack_0 = tensor.pack %4 padding_value(%c0_i8 : i8) outer_dims_perm = [1, 0] inner_dims_pos = [1, 0] inner_tiles = [4, 2] into %11 : tensor<?x4xi8> -> tensor<1x?x4x2xi8>
    %12 = affine.apply #map()[%5]
    %13 = tensor.empty(%12) : tensor<1x?x4x16xi32>
    // %pack_1 was wrong.
    %pack_1 = tensor.pack %6 padding_value(%c0_i32 : i32) outer_dims_perm = [1, 0] inner_dims_pos = [1, 0] inner_tiles = [4, 16] into %13 : tensor<?x4xi32> -> tensor<1x?x4x16xi32>
    %14 = linalg.mmt4d ins(%pack_0, %pack : tensor<1x?x4x2xi8>, tensor<?x?x16x2xi8>) outs(%pack_1 : tensor<1x?x4x16xi32>) -> tensor<1x?x4x16xi32>
    %15 = tensor.empty(%7) : tensor<?x1x16x4xi32>
    %transposed = linalg.transpose ins(%14 : tensor<1x?x4x16xi32>) outs(%15 : tensor<?x1x16x4xi32>) permutation = [1, 0, 3, 2] 
    %16 = tensor.empty(%5) : tensor<?x4xi32>
    %unpack = tensor.unpack %transposed outer_dims_perm = [0, 1] inner_dims_pos = [0, 1] inner_tiles = [16, 4] into %16 : tensor<?x1x16x4xi32> -> tensor<?x4xi32>
    %17 = hal.tensor.export %unpack "output0" : tensor<?x4xi32>{%5} -> !hal.buffer_view
    util.return %17 : !hal.buffer_view
  }
}

@hanhanW

hanhanW commented Mar 25, 2024

Copy link
Copy Markdown
Contributor

Adding more context about why you're seeing the error: The types from type converter mismatch the type from adpator in matmul lowering. In this case, the driver inserts the unrealized_conversion_cast op. So they can have the same type. I think it should just print out the IR and raise the error about unrealized_conversion_cast. I spent some time about the crash, but I still don't know yet...

@bjacob

bjacob commented Mar 26, 2024

Copy link
Copy Markdown
Collaborator Author

Thanks a ton @hanhanW !!

@bjacob

bjacob commented Apr 6, 2024

Copy link
Copy Markdown
Collaborator Author

At long last, the game of whack-a-mole is over... this finally has all tests simultaneously passing. (I know I claimed something similar 2 weeks ago, but that was only the e2e tests passing, the lit test had regressed, and it turned out that batch_matvec was mishandled and no e2e test covered that).

@bjacob bjacob changed the title WIP: transpose narrow-N into narrow-M Data tiling: transpose narrow-N into narrow-M Apr 6, 2024
@bjacob bjacob added benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks labels Apr 6, 2024
@github-actions

github-actions Bot commented Apr 6, 2024 •

Copy link
Copy Markdown

Abbreviated Benchmark Summary

@ commit 885f33038283dc89e4d67e4f2ba7a6c24b2a42ec (vs. base 0a92a719d2ed8c1e925a3923ca7eb64f9011d1f5)

Data-Tiling Comparison Table

Click to show
Name No-DT (baseline) DT-Only DT-UK
BertForMaskedLMTF(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[30-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 224.062 (1.0X) 133.882 (1.7X) 106.247 (2.1X)
BertLargePTBatch1(linalg) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[30-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 655.773 (1.0X) 271.539 (2.4X) 229.116 (2.9X)
BertLargeTF(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[30-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 680.602 (1.0X) 270.429 (2.5X) 219.665 (3.1X)
DeepLabV3_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 6.939 (1.0X) 9.432 (0.7X) 8.587 (0.8X)
DeepLabV3_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 33.196 (1.0X) 39.024 (0.9X) 33.358 (1.0X)
EfficientNetV2STF(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[15-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 34.647 (1.0X) 36.951 (0.9X) 33.918 (1.0X)
EfficientNetV2STF(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 265.938 (1.0X) 261.342 (1.0X) 233.227 (1.1X)
EfficientNet_int8(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 5.922 (1.0X) 11.169 (0.5X) 5.234 (1.1X)
EfficientNet_int8(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 28.839 (1.0X) 53.638 (0.5X) 15.405 (1.9X)
Falcon7bGptqPT(linalg) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[30-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 20419.102 (1.0X) 9590.683 (2.1X) 4149.860 (4.9X)
Falcon7bInt4GptqPT(linalg) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[30-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 20453.727 (1.0X) 9923.697 (2.1X) 4035.081 (5.1X)
GPT2_117M_TF_1X1XI32(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[15-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 9.030 (1.0X) 8.813 (1.0X) 8.566 (1.1X)
GPT2_117M_TF_1X1XI32(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 70.177 (1.0X) 37.142 (1.9X) 39.075 (1.8X)
GPT2_117M_TF_1X4XI32(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[15-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 10.639 (1.0X) 7.733 (1.4X) 8.036 (1.3X)
GPT2_117M_TF_1X4XI32(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 88.262 (1.0X) 41.365 (2.1X) 40.742 (2.2X)
MiniLML12H384Uncased(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[15-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 12.399 (1.0X) 14.554 (0.9X) 12.880 (1.0X)
MiniLML12H384Uncased(stablehlo) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 79.236 (1.0X) 78.794 (1.0X) 58.394 (1.4X)
MobileBertSquad_fp16(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[15-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 33.977 (1.0X) 63.783 (0.5X) 58.855 (0.6X)
MobileBertSquad_fp16(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 179.484 (1.0X) 247.122 (0.7X) 186.302 (1.0X)
MobileBertSquad_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[15-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 34.082 (1.0X) 64.145 (0.5X) 59.022 (0.6X)
MobileBertSquad_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 178.935 (1.0X) 247.904 (0.7X) 187.967 (1.0X)
MobileBertSquad_int8(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[15-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 61.021 (1.0X) 218.910 (0.3X) 65.609 (0.9X)
MobileBertSquad_int8(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 482.066 (1.0X) 1042.318 (0.5X) 216.534 (2.2X)
MobileNetV1_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 5.210 (1.0X) 5.724 (0.9X) 5.058 (1.0X)
MobileNetV1_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 27.308 (1.0X) 24.394 (1.1X) 19.731 (1.4X)
MobileNetV2_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 3.686 (1.0X) 5.268 (0.7X) 4.898 (0.8X)
MobileNetV2_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 11.757 (1.0X) 15.598 (0.8X) 12.503 (0.9X)
MobileNetV2_int8(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 5.750 (1.0X) 9.926 (0.6X) 5.539 (1.0X)
MobileNetV2_int8(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 21.469 (1.0X) 44.468 (0.5X) 13.986 (1.5X)
MobileNetV3Small_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 2.872 (1.0X) 3.972 (0.7X) 3.303 (0.9X)
MobileNetV3Small_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ c2-standard-60[cpu] 2.777 (1.0X) 3.840 (0.7X) 3.265 (0.9X)
MobileSSD_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 8.333 (1.0X) 10.585 (0.8X) 9.484 (0.9X)
MobileSSD_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 34.662 (1.0X) 39.878 (0.9X) 32.605 (1.1X)
PersonDetect_int8(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 0.777 (1.0X) 1.440 (0.5X) 0.749 (1.0X)
PersonDetect_int8(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ c2-standard-60[cpu] 0.710 (1.0X) 1.366 (0.5X) 0.667 (1.1X)
PoseNet_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 4.128 (1.0X) 6.012 (0.7X) 5.232 (0.8X)
PoseNet_fp32(tflite) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-60[cpu] 18.313 (1.0X) 25.845 (0.7X) 20.942 (0.9X)
matmul_1x256x2048_i8_i4_i32_tile_config_default(linalg) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ c2-standard-60[cpu] 0.065 (1.0X) 0.065 (1.0X) 0.065 (1.0X)
matmul_1x256x2048_i8_i8_i32_tile_config_default(linalg) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ c2-standard-60[cpu] 0.042 (1.0X) 0.226 (0.2X) 0.021 (2.0X)
matmul_256x256x2048_i8_i4_i32_tile_config_default(linalg) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ c2-standard-60[cpu] 7.417 (1.0X) 7.431 (1.0X) 7.404 (1.0X)
matmul_256x256x2048_i8_i8_i32_tile_config_default(linalg) [x86_64-cascadelake-linux_gnu-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ c2-standard-60[cpu] 6.687 (1.0X) 13.282 (0.5X) 1.966 (3.4X)
DeepLabV3_fp32(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 48.723 (1.0X) 79.876 (0.6X) 77.703 (0.6X)
DeepLabV3_fp32(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 51.122 (1.0X) 79.991 (0.6X) 78.019 (0.7X)
DeepLabV3_fp32(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[2-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 34.336 (1.0X) 46.667 (0.7X) 46.376 (0.7X)
GPT2_117M_TF_1X1XI32(stablehlo) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 94.313 (1.0X) 21.855 (4.3X) 21.290 (4.4X)
GPT2_117M_TF_1X1XI32(stablehlo) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 93.822 (1.0X) 22.102 (4.2X) 22.100 (4.2X)
GPT2_117M_TF_1X1XI32(stablehlo) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[2-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 52.786 (1.0X) 22.165 (2.4X) 22.108 (2.4X)
GPT2_117M_TF_1X4XI32(stablehlo) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 126.985 (1.0X) 28.076 (4.5X) 27.910 (4.5X)
GPT2_117M_TF_1X4XI32(stablehlo) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 127.826 (1.0X) 30.082 (4.2X) 29.856 (4.3X)
GPT2_117M_TF_1X4XI32(stablehlo) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[2-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 77.855 (1.0X) 26.501 (2.9X) 26.619 (2.9X)
MobileBertSquad_fp32(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 693.356 (1.0X) 373.937 (1.9X) 369.699 (1.9X)
MobileBertSquad_fp32(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 706.749 (1.0X) 375.824 (1.9X) 370.663 (1.9X)
MobileBertSquad_fp32(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[2-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 402.146 (1.0X) 228.164 (1.8X) 224.521 (1.8X)
MobileBertSquad_int8(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 1062.432 (1.0X) 628.011 (1.7X) 256.568 (4.1X)
MobileBertSquad_int8(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 1061.986 (1.0X) 631.388 (1.7X) 261.095 (4.1X)
MobileBertSquad_int8(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[2-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 556.887 (1.0X) 348.207 (1.6X) 153.024 (3.6X)
Vit_int8(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 2089.044 (1.0X) 1086.519 (1.9X) 312.460 (6.7X)
Vit_int8(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[1-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 2090.200 (1.0X) 1085.563 (1.9X) 310.681 (6.7X)
Vit_int8(tflite) [armv8.2-a-generic-linux_android29-llvm_cpu] local_task(embedded_elf)[2-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 1137.131 (1.0X) 608.876 (1.9X) 189.771 (6.0X)
matmul_1x256x2048_i8_i4_i32_tile_config_default(linalg) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 0.081 (1.0X) 0.069 (1.2X) 0.016 (5.1X)
matmul_1x256x2048_i8_i8_i32_tile_config_default(linalg) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 0.072 (1.0X) 0.064 (1.1X) 0.017 (4.2X)
matmul_256x256x2048_i8_i4_i32_tile_config_default(linalg) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 12.172 (1.0X) 10.073 (1.2X) 1.415 (8.6X)
matmul_256x256x2048_i8_i8_i32_tile_config_default(linalg) [armv8.2-a-generic-linux_android29-llvm_cpu] local_sync(embedded_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 16.528 (1.0X) 6.807 (2.4X) 1.209 (13.7X)

Regressed Latencies 🚩

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
DeepLabV3\_fp32(tflite) [armv8.2-a-generic-linux\_android29-llvm\_cpu][experimental-flags,no-dt] local\_task(embedded\_elf)[2-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 34.336 (vs. 31.738, 8.19%↑) 35.318 2.218

Improved Latencies 🎉

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
matmul\_1x256x2048\_i8\_i4\_i32\_tile\_config\_default(linalg) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][default-flags,dt-uk] local\_sync(embedded\_elf)[full-inference,default-flags] with default @ c2-standard-60[cpu] 0.065 (vs. 0.086, 24.48%↓) 0.065 0.000
GPT2\_117M\_TF\_1X4XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][experimental-flags,no-dt] local\_task(embedded\_elf)[1-thread,full-inference,system-scheduling] with default @ pixel-6-pro[big-cores] 127.826 (vs. 138.910, 7.98%↓) 127.630 0.574
matmul\_1x256x2048\_i8\_i8\_i32\_tile\_config\_default(linalg) [armv8.2-a-generic-linux\_android29-llvm\_cpu][experimental-flags,dt-only] local\_sync(embedded\_elf)[full-inference,default-flags] with default @ pixel-6-pro[big-cores] 0.064 (vs. 0.069, 6.91%↓) 0.065 0.001

[Top 3 out of 8 results showed]

Improved Total Dispatch Sizes 🎉

Benchmark Name Total Dispatch Size (bytes)
PoseNet\_fp32(tflite) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][default-flags,dt-uk,compile-stats] 39760 (vs. 55840, 28.80%↓)
PoseNet\_fp32(tflite) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,dt-only,compile-stats] 78416 (vs. 84064, 6.72%↓)
BertLargeTF(stablehlo) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,dt-only,compile-stats] 72256 (vs. 76272, 5.27%↓)

For more information:

Source Workflow Run

@bjacob

bjacob commented Apr 6, 2024 •

Copy link
Copy Markdown
Collaborator Author

Benchmark results report 5%-9% speedups on multiple models, as well as substantial code size shrinks on a few models.

@bjacob bjacob removed benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks labels Apr 8, 2024
@bjacob
bjacob marked this pull request as ready for review April 8, 2024 14:56
@bjacob
bjacob requested review from dcaballe and hanhanW as code owners April 8, 2024 14:56
@bjacob bjacob added benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks labels Apr 8, 2024
return signalPassFailure();
}
MaterializeEncodingTypeConverter typeConverter(materializeEncodingFn);
bool transposeNarrowN = !isVMVXBackend(targetAttr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some comments about why VMVX is excluded.

EDIT: do we only want this being applied on llvm-cpu? What's the issue on VMVX side, I thought they will navigate to the same kernel if we apply the change unconditionally. It can make logics easier.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that. I was being lazy. Back when I was desperate to get this to finally work, I cut corners by disabling this whole transposition on VMVX to limit the number of edge cases I needed to debug. Now that everything else is working, I tried reenabling that, ran into a bug, debugged it: I had forgotten a transposition in the materialization of dispatch.tensor.load/store ops, that for some reason only manifested itself on VMVX. Fixed. Now it's all much simpler, that transposeNarrowN boolean is gone.

@bjacob bjacob removed benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks labels Apr 11, 2024
@bjacob
bjacob requested review from Max191 and hanhanW April 11, 2024 03:03

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! LGTM, just one nit. (sorry I forgot to drop the comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a test case that has M dynamic and N narrow? You can use flow.tensor.constant to create the input, so M can be dynamic to the compiler. E.g., https://github.com/openxla/iree/blob/336ba1278c410b7dd46c2d01acbaa8ab29fc7b9a/tests/e2e/tensor_ops/pack_dynamic_inner_tiles.mlir#L22-L26

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I had forgotten that from our discussion. And when I added that, I ran into more failures in FlattenMemrefSubspan. Fixing that involves adding missing memref.dim -> expand_shape fold patterns upstream. So it'll be a bit longer until we can finally close this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, we can do it in a follow-up in this case.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must fix it first, because otherwise this issue (that is exposed by transpose-narrow-N) is going to be run into in the wild.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not think that much because we don't have such cases in our benchmark suite.. You're right on this, it should be fixed first. I'm happy to see that we have more coverage. :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two new PRs need to be merged first before we can add the dynamic test cases here and finish merging this:
llvm/llvm-project#88423
#17020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads-up that we'll need to use flow.tensor.dynamic_constant instead after landing #17034

@Max191 Max191 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review. Overall looks good if it is working, but one part looks a little strange to me. I think it's possible there could be some unusual cases that don't show up in any tests or benchmarks that would hit a bug.

Comment on lines +8 to +15
#include "iree/compiler/Dialect/LinalgExt/IR/LinalgExtOps.h"
#include "llvm/Support/Debug.h"
#include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Utils/IndexingUtils.h"
#include "mlir/IR/BuiltinTypes.h"

#include <numeric>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need all these includes?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need <numeric> for std::iota. Dropped the others.

Comment on lines +55 to +56
std::swap(newShape[m], newShape[n]);
std::swap(newOriginalShape[m], newOriginalShape[n]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks strange to me. The contraction dims correspond to the iteration space, but they are used on specific tensors here. I may be misinterpreting what this is doing, but I would think you should map the cDim to the tensor dim like is done here:
https://github.com/openxla/iree/blob/5adae9e950665ce183f231656828ace63d24b28e/compiler/src/iree/compiler/Codegen/Common/EncodingUtils.cpp#L97-L104

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, if for some reason the K iteration dims of a matmul are outer dimensions (like (k, m, n)), then the m and n iteration dims would not line up with the m and n result tensor dims.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! @lialan has that fixed in his rebased PR.

@bjacob

bjacob commented May 27, 2024

Copy link
Copy Markdown
Collaborator Author

Superseded by #17446 .

@bjacob bjacob closed this May 27, 2024
lialan pushed a commit to lialan/iree that referenced this pull request May 27, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
lialan pushed a commit to lialan/iree that referenced this pull request May 27, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
lialan pushed a commit to lialan/iree that referenced this pull request May 28, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
lialan pushed a commit to lialan/iree that referenced this pull request May 28, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
ScottTodd pushed a commit that referenced this pull request May 28, 2024
(This is a rebasing PR for #16890 )

This is a generic idea in the design of matrix multiplication
implementations: the M and N dimensions play symmetrical roles, so there
is this opportunity to halve the problem space by transposition. The
immediate motivation is ukernels: we have chosen to implement narrow
ukernels only for the narrow-M cases, not narrow-N, in preparation for
this. This is the reason why this PR is a 5%-9% e2e speedup on multiple
ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often
caused codegen to fall off of good vectorization cases. This
transposition moves unit or generally smaller static dimensions to the
outer dimensions, which will help with that.
* When we get to serious distribution tiling (#16410), the reduction of
generality will again greatly help.

This transposition is made easier by (and was all along part of the idea
in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally
with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these
transpositions, we just directly generate the right transposing `pack`
and `unpack` ops. An earlier plan was to generate `linalg.transpose` and
rely on a later folding pattern, but it turned out to just be simpler to
directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at
`SetEncoding` instead of at `MaterializeEncoding`? That would have been
simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't
belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do
data-tiling. The transposition would be difficult to revert, and
generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only
helps since at MaterializeEncoding we know we are generating a mmt4d. We
couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`,
not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only

Signed-off-by: Alan Li <[email protected]>
Co-authored-by: Benoit Jacob <[email protected]>
lialan pushed a commit to lialan/iree that referenced this pull request Jun 3, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
lialan pushed a commit to lialan/iree that referenced this pull request Jun 3, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
lialan pushed a commit to lialan/iree that referenced this pull request Jun 3, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
lialan pushed a commit to lialan/iree that referenced this pull request Jun 3, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
lialan added a commit to lialan/iree that referenced this pull request Jun 4, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
gglangg pushed a commit to gglangg/iree that referenced this pull request Jun 4, 2024
(This is a rebasing PR for iree-org#16890 )

This is a generic idea in the design of matrix multiplication
implementations: the M and N dimensions play symmetrical roles, so there
is this opportunity to halve the problem space by transposition. The
immediate motivation is ukernels: we have chosen to implement narrow
ukernels only for the narrow-M cases, not narrow-N, in preparation for
this. This is the reason why this PR is a 5%-9% e2e speedup on multiple
ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often
caused codegen to fall off of good vectorization cases. This
transposition moves unit or generally smaller static dimensions to the
outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of
generality will again greatly help.

This transposition is made easier by (and was all along part of the idea
in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally
with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these
transpositions, we just directly generate the right transposing `pack`
and `unpack` ops. An earlier plan was to generate `linalg.transpose` and
rely on a later folding pattern, but it turned out to just be simpler to
directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at
`SetEncoding` instead of at `MaterializeEncoding`? That would have been
simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't
belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do
data-tiling. The transposition would be difficult to revert, and
generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only
helps since at MaterializeEncoding we know we are generating a mmt4d. We
couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`,
not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only

Signed-off-by: Alan Li <[email protected]>
Co-authored-by: Benoit Jacob <[email protected]>
gglangg pushed a commit to gglangg/iree that referenced this pull request Jun 4, 2024
(This is a rebasing PR for iree-org#16890 )

This is a generic idea in the design of matrix multiplication
implementations: the M and N dimensions play symmetrical roles, so there
is this opportunity to halve the problem space by transposition. The
immediate motivation is ukernels: we have chosen to implement narrow
ukernels only for the narrow-M cases, not narrow-N, in preparation for
this. This is the reason why this PR is a 5%-9% e2e speedup on multiple
ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often
caused codegen to fall off of good vectorization cases. This
transposition moves unit or generally smaller static dimensions to the
outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of
generality will again greatly help.

This transposition is made easier by (and was all along part of the idea
in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally
with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these
transpositions, we just directly generate the right transposing `pack`
and `unpack` ops. An earlier plan was to generate `linalg.transpose` and
rely on a later folding pattern, but it turned out to just be simpler to
directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at
`SetEncoding` instead of at `MaterializeEncoding`? That would have been
simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't
belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do
data-tiling. The transposition would be difficult to revert, and
generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only
helps since at MaterializeEncoding we know we are generating a mmt4d. We
couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`,
not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only

Signed-off-by: Alan Li <[email protected]>
Co-authored-by: Benoit Jacob <[email protected]>
bangtianliu pushed a commit to bangtianliu/iree that referenced this pull request Jun 5, 2024
(This is a rebasing PR for iree-org#16890 )

This is a generic idea in the design of matrix multiplication
implementations: the M and N dimensions play symmetrical roles, so there
is this opportunity to halve the problem space by transposition. The
immediate motivation is ukernels: we have chosen to implement narrow
ukernels only for the narrow-M cases, not narrow-N, in preparation for
this. This is the reason why this PR is a 5%-9% e2e speedup on multiple
ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often
caused codegen to fall off of good vectorization cases. This
transposition moves unit or generally smaller static dimensions to the
outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of
generality will again greatly help.

This transposition is made easier by (and was all along part of the idea
in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally
with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these
transpositions, we just directly generate the right transposing `pack`
and `unpack` ops. An earlier plan was to generate `linalg.transpose` and
rely on a later folding pattern, but it turned out to just be simpler to
directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at
`SetEncoding` instead of at `MaterializeEncoding`? That would have been
simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't
belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do
data-tiling. The transposition would be difficult to revert, and
generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only
helps since at MaterializeEncoding we know we are generating a mmt4d. We
couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`,
not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only

Signed-off-by: Alan Li <[email protected]>
Co-authored-by: Benoit Jacob <[email protected]>
lialan added a commit to lialan/iree that referenced this pull request Jun 5, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
lialan pushed a commit to lialan/iree that referenced this pull request Jun 5, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
lialan pushed a commit to lialan/iree that referenced this pull request Jun 5, 2024
This is a generic idea in the design of matrix multiplication implementations: the M and N dimensions play symmetrical roles, so there is this opportunity to halve the problem space by transposition. The immediate motivation is ukernels: we have chosen to implement narrow ukernels only for the narrow-M cases, not narrow-N, in preparation for this. This is the reason why this PR is a 5%-9% e2e speedup on multiple ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often caused codegen to fall off of good vectorization cases. This transposition moves unit or generally smaller static dimensions to the outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of generality will again greatly help.

This transposition is made easier by (and was all along part of the idea in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these transpositions, we just directly generate the right transposing `pack` and `unpack` ops. An earlier plan was to generate `linalg.transpose` and rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at `SetEncoding` instead of at `MaterializeEncoding`? That would have been simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do data-tiling. The transposition would be difficult to revert, and generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only helps since at MaterializeEncoding we know we are generating a mmt4d. We couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`, not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>
bjacob added a commit that referenced this pull request Jun 6, 2024
(This is a rebasing PR for #16890 )

This is a generic idea in the design of matrix multiplication
implementations: the M and N dimensions play symmetrical roles, so there
is this opportunity to halve the problem space by transposition. The
immediate motivation is ukernels: we have chosen to implement narrow
ukernels only for the narrow-M cases, not narrow-N, in preparation for
this. This is the reason why this PR is a 5%-9% e2e speedup on multiple
ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often
caused codegen to fall off of good vectorization cases. This
transposition moves unit or generally smaller static dimensions to the
outer dimensions, which will help with that.
* When we get to serious distribution tiling (#16410), the reduction of
generality will again greatly help.

This transposition is made easier by (and was all along part of the idea
in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally
with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these
transpositions, we just directly generate the right transposing `pack`
and `unpack` ops. An earlier plan was to generate `linalg.transpose` and
rely on a later folding pattern, but it turned out to just be simpler to
directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at
`SetEncoding` instead of at `MaterializeEncoding`? That would have been
simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't
belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do
data-tiling. The transposition would be difficult to revert, and
generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only
helps since at MaterializeEncoding we know we are generating a mmt4d. We
couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`,
not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>

ci-extra: build_test_all_arm64

Signed-off-by: Alan Li <[email protected]>
Co-authored-by: Benoit Jacob <[email protected]>
LLITCHEV pushed a commit to LLITCHEV/iree that referenced this pull request Jul 30, 2024
(This is a rebasing PR for iree-org#16890 )

This is a generic idea in the design of matrix multiplication
implementations: the M and N dimensions play symmetrical roles, so there
is this opportunity to halve the problem space by transposition. The
immediate motivation is ukernels: we have chosen to implement narrow
ukernels only for the narrow-M cases, not narrow-N, in preparation for
this. This is the reason why this PR is a 5%-9% e2e speedup on multiple
ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often
caused codegen to fall off of good vectorization cases. This
transposition moves unit or generally smaller static dimensions to the
outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of
generality will again greatly help.

This transposition is made easier by (and was all along part of the idea
in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally
with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these
transpositions, we just directly generate the right transposing `pack`
and `unpack` ops. An earlier plan was to generate `linalg.transpose` and
rely on a later folding pattern, but it turned out to just be simpler to
directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at
`SetEncoding` instead of at `MaterializeEncoding`? That would have been
simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't
belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do
data-tiling. The transposition would be difficult to revert, and
generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only
helps since at MaterializeEncoding we know we are generating a mmt4d. We
couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`,
not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only

Signed-off-by: Alan Li <[email protected]>
Co-authored-by: Benoit Jacob <[email protected]>
Signed-off-by: Lubo Litchev <[email protected]>
LLITCHEV pushed a commit to LLITCHEV/iree that referenced this pull request Jul 30, 2024
…g#17545)

(This is a rebasing PR for iree-org#16890 )

This is a generic idea in the design of matrix multiplication
implementations: the M and N dimensions play symmetrical roles, so there
is this opportunity to halve the problem space by transposition. The
immediate motivation is ukernels: we have chosen to implement narrow
ukernels only for the narrow-M cases, not narrow-N, in preparation for
this. This is the reason why this PR is a 5%-9% e2e speedup on multiple
ML models with ukernels (and a > 2x speedup on matvec microbenchmarks).

The idea should be beneficial beyond ukernels though:
* With codegen (outside of ukernels), inner unit dimensions have often
caused codegen to fall off of good vectorization cases. This
transposition moves unit or generally smaller static dimensions to the
outer dimensions, which will help with that.
* When we get to serious distribution tiling (iree-org#16410), the reduction of
generality will again greatly help.

This transposition is made easier by (and was all along part of the idea
in) the RHS-transposition in `mmt4d` (the `t` in `mmt4d`), as generally
with matrix multiplication

```
B * Transpose(A) == Transpose( A * Transpose(B) )
```

so in `mmt4d` terms

```
mmt4d(B, A) == Transpose(mmt4d(A, B))
```

As `pack` and `unpack` already have enough generality to perform these
transpositions, we just directly generate the right transposing `pack`
and `unpack` ops. An earlier plan was to generate `linalg.transpose` and
rely on a later folding pattern, but it turned out to just be simpler to
directly generate the already-transposed `pack`, `unpack`.

A legitimate question was: should this transposition be implemented at
`SetEncoding` instead of at `MaterializeEncoding`? That would have been
simpler in some ways, but:
* The benefit of the transposition depends on the backend, so it doesn't
belong in Flow.
* SetEncoding must be reversible in case the back-end doesn't want to do
data-tiling. The transposition would be difficult to revert, and
generally confusing in settings where it's not wanted.
* The above mmt4d-specific trait simplifying the transposition only
helps since at MaterializeEncoding we know we are generating a mmt4d. We
couldn't so easily rely on that in SetEncoding.
* Before MaterializeEncoding we would have to handle `linalg.generic`,
not just named matmul ops.

Co-authored-by: Benoit Jacob <[email protected]>

benchmark-extra: x86_64-dt-only, android-cpu-dt-only
Signed-off-by: Alan Li <[email protected]>

ci-extra: build_test_all_arm64

Signed-off-by: Alan Li <[email protected]>
Co-authored-by: Benoit Jacob <[email protected]>
Signed-off-by: Lubo Litchev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants