Skip to content

Commit 80e61e3

Browse files
authored
Remove redundant linalg.matmul_signed (#98615)
`linalg.matmul` already has an attribute for casts, defaults to signed but allowed unsigned, so the operation `linalg.matmul_unsigned` is redundant. The generalization test has an example on how to lower to unsigned matmul in linalg. This is the first PR in a list of many that will simplify the linalg operations by using similar attributes. Ref: https://discourse.llvm.org/t/rfc-transpose-attribute-for-linalg-matmul-operations/80092
1 parent a5cf99d commit 80e61e3

File tree

3 files changed

+9
-92
lines changed

3 files changed

+9
-92
lines changed

mlir/include/mlir/Dialect/Linalg/IR/LinalgNamedStructuredOps.yaml

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,74 +1137,6 @@ structured_op: !LinalgStructuredOpConfig
11371137
- !ScalarExpression
11381138
scalar_arg: B
11391139
--- !LinalgOpConfig
1140-
metadata: !LinalgOpMetadata
1141-
name: matmul_unsigned
1142-
cpp_class_name: MatmulUnsignedOp
1143-
doc: |-
1144-
Performs an unsigned matrix multiplication of two 2D inputs.
1145-
1146-
Numeric casting is performed on the operands to the inner multiply, promoting
1147-
them to the same data type as the accumulator/output.
1148-
implements:
1149-
- LinalgContractionOpInterface
1150-
structured_op: !LinalgStructuredOpConfig
1151-
args:
1152-
- !LinalgOperandDefConfig
1153-
name: A
1154-
kind: input_tensor
1155-
type_var: T1
1156-
shape_map: affine_map<()[s0, s1, s2] -> (s0, s1)>
1157-
- !LinalgOperandDefConfig
1158-
name: B
1159-
kind: input_tensor
1160-
type_var: T2
1161-
shape_map: affine_map<()[s0, s1, s2] -> (s1, s2)>
1162-
- !LinalgOperandDefConfig
1163-
name: C
1164-
kind: output_tensor
1165-
type_var: U
1166-
shape_map: affine_map<()[s0, s1, s2] -> (s0, s2)>
1167-
indexing_maps: !LinalgIndexingMapsConfig
1168-
static_indexing_maps:
1169-
- affine_map<(d0, d1, d2)[s0, s1, s2] -> (d0, d2)>
1170-
- affine_map<(d0, d1, d2)[s0, s1, s2] -> (d2, d1)>
1171-
- affine_map<(d0, d1, d2)[s0, s1, s2] -> (d0, d1)>
1172-
iterator_types:
1173-
- parallel
1174-
- parallel
1175-
- reduction
1176-
assignments:
1177-
- !ScalarAssign
1178-
arg: C
1179-
value: !ScalarExpression
1180-
scalar_fn:
1181-
kind: binary
1182-
fn_name: add
1183-
operands:
1184-
- !ScalarExpression
1185-
scalar_arg: C
1186-
- !ScalarExpression
1187-
scalar_fn:
1188-
kind: binary
1189-
fn_name: mul
1190-
operands:
1191-
- !ScalarExpression
1192-
scalar_fn:
1193-
kind: type
1194-
fn_name: cast_unsigned
1195-
type_var: U
1196-
operands:
1197-
- !ScalarExpression
1198-
scalar_arg: A
1199-
- !ScalarExpression
1200-
scalar_fn:
1201-
kind: type
1202-
fn_name: cast_unsigned
1203-
type_var: U
1204-
operands:
1205-
- !ScalarExpression
1206-
scalar_arg: B
1207-
--- !LinalgOpConfig
12081140
metadata: !LinalgOpMetadata
12091141
name: quantized_matmul
12101142
cpp_class_name: QuantizedMatmulOp

mlir/python/mlir/dialects/linalg/opdsl/ops/core_named_ops.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -388,24 +388,6 @@ def matmul(
388388
C[D.m, D.n] += cast(U, A[D.m, D.k]) * cast(U, B[D.k, D.n])
389389

390390

391-
@linalg_structured_op
392-
def matmul_unsigned(
393-
A=TensorDef(T1, S.M, S.K),
394-
B=TensorDef(T2, S.K, S.N),
395-
C=TensorDef(U, S.M, S.N, output=True),
396-
):
397-
"""Performs an unsigned matrix multiplication of two 2D inputs.
398-
399-
Numeric casting is performed on the operands to the inner multiply, promoting
400-
them to the same data type as the accumulator/output.
401-
"""
402-
domain(D.m, D.n, D.k)
403-
implements(ContractionOpInterface)
404-
C[D.m, D.n] += TypeFn.cast_unsigned(U, A[D.m, D.k]) * TypeFn.cast_unsigned(
405-
U, B[D.k, D.n]
406-
)
407-
408-
409391
@linalg_structured_op
410392
def quantized_matmul(
411393
A=TensorDef(T1, S.M, S.K),

mlir/test/Dialect/Linalg/generalize-named-polymorphic-ops.mlir

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ func.func @generalize_matmul_tensor_f16f64i32(%A : tensor<16x8xf16>, %B: tensor<
7979
// -----
8080

8181
func.func @generalize_matmul_unsigned_tensor_i16i64i32(%A : tensor<16x8xi16>, %B: tensor<8x32xi64>, %C: tensor<16x32xi32>) -> tensor<16x32xi32> {
82-
%0 = linalg.matmul_unsigned ins(%A, %B: tensor<16x8xi16>, tensor<8x32xi64>)
83-
outs(%C: tensor<16x32xi32>) -> tensor<16x32xi32>
82+
%0 = linalg.matmul { cast = #linalg.type_fn<cast_unsigned> }
83+
ins(%A, %B: tensor<16x8xi16>, tensor<8x32xi64>)
84+
outs(%C: tensor<16x32xi32>) -> tensor<16x32xi32>
8485
return %0: tensor<16x32xi32>
8586
}
8687

@@ -92,8 +93,9 @@ func.func @generalize_matmul_unsigned_tensor_i16i64i32(%A : tensor<16x8xi16>, %B
9293
// -----
9394

9495
func.func @generalize_matmul_unsigned_tensor_i16i64f32(%A : tensor<16x8xi16>, %B: tensor<8x32xi64>, %C: tensor<16x32xf32>) -> tensor<16x32xf32> {
95-
%0 = linalg.matmul_unsigned ins(%A, %B: tensor<16x8xi16>, tensor<8x32xi64>)
96-
outs(%C: tensor<16x32xf32>) -> tensor<16x32xf32>
96+
%0 = linalg.matmul { cast = #linalg.type_fn<cast_unsigned> }
97+
ins(%A, %B: tensor<16x8xi16>, tensor<8x32xi64>)
98+
outs(%C: tensor<16x32xf32>) -> tensor<16x32xf32>
9799
return %0: tensor<16x32xf32>
98100
}
99101

@@ -105,8 +107,9 @@ func.func @generalize_matmul_unsigned_tensor_i16i64f32(%A : tensor<16x8xi16>, %B
105107
// -----
106108

107109
func.func @generalize_matmul_unsigned_tensor_f16f64i32(%A : tensor<16x8xf16>, %B: tensor<8x32xf64>, %C: tensor<16x32xi32>) -> tensor<16x32xi32> {
108-
%0 = linalg.matmul_unsigned ins(%A, %B: tensor<16x8xf16>, tensor<8x32xf64>)
109-
outs(%C: tensor<16x32xi32>) -> tensor<16x32xi32>
110+
%0 = linalg.matmul { cast = #linalg.type_fn<cast_unsigned> }
111+
ins(%A, %B: tensor<16x8xf16>, tensor<8x32xf64>)
112+
outs(%C: tensor<16x32xi32>) -> tensor<16x32xi32>
110113
return %0: tensor<16x32xi32>
111114
}
112115

0 commit comments

Comments
 (0)