[mlir] [MLIR][Linalg] Introduce transpose/broadcast semantic to linalg.batch… (PR #130944)
Andrzej Warzyński
llvmlistbot at llvm.org
Fri Mar 21 05:41:11 PDT 2025
================
@@ -1054,6 +1054,137 @@ def BatchMatmulOp : LinalgStructuredBase_Op<"batch_matmul", !listconcat([AttrSiz
}
+//===----------------------------------------------------------------------===//
+// Op definition for BatchReduceMatmulOp
+//===----------------------------------------------------------------------===//
+
+def BatchReduceMatmulOp : LinalgStructuredBase_Op<"batch_reduce_matmul", [
+ AttrSizedOperandSegments,
+ LinalgContractionOpInterface]> {
+
+ let summary = [{Performs a batch-reduce matrix multiplication of two 3D inputs.
+The partial multiplication results are reduced into a 2D output.}];
+ let description = [{
+ Numeric casting is performed on the operands to the inner multiply, promoting
+ them to the same data type as the accumulator/output.
+
+ Broadcast and Transpose semantics can be appiled by specifying the explicit attribute
+ 'indexing_maps' as shown below. This is a list attribute, so must include maps for all
+ arguments if specified.
+
+ Example Transpose:
+ ```
+ linalg.batch_reduce_matmul indexing_maps = [
+ affine_map<(d0, d1, d2, d3) -> (d0, d3, d1)>, // transpose
+ affine_map<(d0, d1, d2, d3) -> (d0, d3, d2)>,
+ affine_map<(d0, d1, d2, d3) -> (d1, d2)>
+ ]
+ ins(%arg0, %arg1 : memref<2x5x3xf32>,memref<2x5x7xf32>)
+ outs(%arg2: memref<3x7xf32>)
----------------
banach-space wrote:
This indentation is unnecessarily deep. Could we follow the "golden" standard of `linalg.generic`, please?
* https://mlir.llvm.org/docs/Dialects/Linalg/#linalggeneric-linalggenericop
`linalg.contract` is also more consistently formatted (and looks very similar to `linalg.batch_reduce_matmul`:
* https://mlir.llvm.org/docs/Dialects/Linalg/#linalgcontract-linalgcontractop
So, this should look like:
```mlir
linalg.batch_reduce_matmul
indexing_maps = [ affine_map<(d0, d1, d2, d3) -> (d0, d3, d1)>, // transpose
affine_map<(d0, d1, d2, d3) -> (d0, d3, d2)>,
affine_map<(d0, d1, d2, d3) -> (d1, d2)>]
ins(%arg0, %arg1 : memref<2x5x3xf32>,memref<2x5x7xf32>)
outs(%arg2: memref<3x7xf32>)
```
Very importantly, this file should use consistent formatting. I appreciate that that's currently not the case:
* https://github.com/llvm/llvm-project/blob/d928a671b84afb9c2ad64353694537a198f04651/mlir/include/mlir/Dialect/Linalg/IR/LinalgStructuredOps.td#L857-L864 vs
* https://github.com/llvm/llvm-project/blob/d928a671b84afb9c2ad64353694537a198f04651/mlir/include/mlir/Dialect/Linalg/IR/LinalgStructuredOps.td#L714-L721
My ask is that we begind converging towards uniform style starting with this PR. To me, `linalg.generic` and `linalg.contract` are good examples to follow.
https://github.com/llvm/llvm-project/pull/130944
More information about the Mlir-commits
mailing list