Repository navigation
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
|
I created a smaller repro here: https://gist.github.com/hanhanW/0a1aafecb0a3539614001007e1f56b0f Run: |
|
@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
}
}
|
|
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 |
|
Thanks a ton @hanhanW !! |
|
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). |
Abbreviated Benchmark Summary@ commit 885f33038283dc89e4d67e4f2ba7a6c24b2a42ec (vs. base 0a92a719d2ed8c1e925a3923ca7eb64f9011d1f5) Data-Tiling Comparison TableClick to show
Regressed Latencies 🚩
Improved Latencies 🎉
[Top 3 out of 8 results showed] Improved Total Dispatch Sizes 🎉
For more information: |
|
Benchmark results report 5%-9% speedups on multiple models, as well as substantial code size shrinks on a few models. |
| return signalPassFailure(); | ||
| } | ||
| MaterializeEncodingTypeConverter typeConverter(materializeEncodingFn); | ||
| bool transposeNarrowN = !isVMVXBackend(targetAttr); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
hanhanW
left a comment
There was a problem hiding this comment.
Nice! LGTM, just one nit. (sorry I forgot to drop the comment)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I see, we can do it in a follow-up in this case.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Heads-up that we'll need to use flow.tensor.dynamic_constant instead after landing #17034
Max191
left a comment
There was a problem hiding this comment.
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.
| #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> |
There was a problem hiding this comment.
Do we need all these includes?
There was a problem hiding this comment.
We need <numeric> for std::iota. Dropped the others.
| std::swap(newShape[m], newShape[n]); | ||
| std::swap(newOriginalShape[m], newOriginalShape[n]); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Great catch! @lialan has that fixed in his rebased PR.
|
Superseded by #17446 . |
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]>
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]>
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]>
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]>
(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]>
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]>
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]>
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]>
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]>
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]>
(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]>
(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]>
(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]>
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]>
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]>
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]>
(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]>
(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]>
…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]>
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:
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(thetinmmt4d), as generally with matrix multiplicationso in
mmt4dtermsAs
packandunpackalready have enough generality to perform these transpositions, we just directly generate the right transposingpackandunpackops. An earlier plan was to generatelinalg.transposeand rely on a later folding pattern, but it turned out to just be simpler to directly generate the already-transposedpack,unpack.A legitimate question was: should this transposition be implemented at
SetEncodinginstead of atMaterializeEncoding? That would have been simpler in some ways, but:linalg.generic, not just named matmul ops.benchmark-extra: x86_64-dt-only, android-cpu-dt-only