Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir] Add ContractionOpInterface utility functions for vector matrix multiplication #68945

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ def LinalgContractionOpInterface : OpInterface<"ContractionOpInterface"> {
/*methodBody=*/[{
return mlir::isRowMajorBatchMatmul($_op.getIndexingMaps());
}]>,
InterfaceMethod<
/*desc=*/[{
Returns whether the given op has indexing maps that correspond to a
vector-matrix multiplication.
}],
/*retTy=*/"bool",
/*methodName=*/"isVecmat",
/*args=*/(ins),
/*methodBody=*/[{
return mlir::isVecmat($_op.getIndexingMaps());
}]>,
InterfaceMethod<
/*desc=*/[{
Returns whether the given op has indexing maps that correspond to a
matrix-vector multiplication.
}],
/*retTy=*/"bool",
/*methodName=*/"isMatvec",
/*args=*/(ins),
/*methodBody=*/[{
return mlir::isMatvec($_op.getIndexingMaps());
}]>,
InterfaceMethod<
/*desc=*/[{
Returns whether the given op has indexing maps that correspond to a
batched matrix-vector multiplication.
}],
/*retTy=*/"bool",
/*methodName=*/"isBatchMatvec",
/*args=*/(ins),
/*methodBody=*/[{
return mlir::isBatchMatvec($_op.getIndexingMaps());
}]>,
];
}

Expand Down
18 changes: 18 additions & 0 deletions mlir/include/mlir/Dialect/Utils/StructuredOpsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ bool isColumnMajorMatmul(ArrayAttr indexingMaps);
/// the reduction.
bool isRowMajorBatchMatmul(ArrayAttr indexingMaps);

/// Tests whether the given maps describe a vector matrix multiplication. The
/// test is permutation-invariant. Note that this only checks the affine maps
/// from an operation, so does not perform any checks on the math being
/// performed within the reduction.
bool isVecmat(ArrayAttr indexingMaps);

/// Tests whether the given maps describe a matrix vector multiplication. The
/// test is permutation-invariant. Note that this only checks the affine maps
/// from an operation, so does not perform any checks on the math being
/// performed within the reduction.
bool isMatvec(ArrayAttr indexingMaps);

/// Tests whether the given maps describe a batch matrix vector multiplication.
/// The test is permutation-invariant. Note that this only checks the affine
/// maps from an operation, so does not perform any checks on the math being
/// performed within the reduction.
bool isBatchMatvec(ArrayAttr indexingMaps);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: rename to VecMatMul/MatVecMul, VectorMatrixMult/MatrixVectorMult or similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My I kept them as vecmat/matvec since that's what the Linalg op is called. I agree that your version is clearer, but I'm not sure if it's better to stay consistent. Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, fair enough. Thanks!


/// Return positions in `iteratorTypes` that match `iteratorTypeName`.
inline void findPositionsOfType(ArrayRef<utils::IteratorType> iteratorTypes,
utils::IteratorType iteratorTypeName,
Expand Down
91 changes: 82 additions & 9 deletions mlir/lib/Dialect/Utils/StructuredOpsUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ bool mlir::isRowMajorMatmul(ArrayAttr indexingMaps) {
if (indexingMaps.size() != 3)
return false;

auto map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
auto map1 = cast<AffineMapAttr>(indexingMaps[1]).getValue();
auto map2 = cast<AffineMapAttr>(indexingMaps[2]).getValue();
AffineMap map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
AffineMap map1 = cast<AffineMapAttr>(indexingMaps[1]).getValue();
AffineMap map2 = cast<AffineMapAttr>(indexingMaps[2]).getValue();

if (map0.getNumResults() != 2 || map1.getNumResults() != 2 ||
map2.getNumResults() != 2 || map0.getNumInputs() != 3 ||
Expand All @@ -47,9 +47,9 @@ bool mlir::isColumnMajorMatmul(ArrayAttr indexingMaps) {
if (indexingMaps.size() != 3)
return false;

auto map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
auto map1 = cast<AffineMapAttr>(indexingMaps[1]).getValue();
auto map2 = cast<AffineMapAttr>(indexingMaps[2]).getValue();
AffineMap map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
AffineMap map1 = cast<AffineMapAttr>(indexingMaps[1]).getValue();
AffineMap map2 = cast<AffineMapAttr>(indexingMaps[2]).getValue();

if (map0.getNumResults() != 2 || map1.getNumResults() != 2 ||
map2.getNumResults() != 2 || map0.getNumInputs() != 3 ||
Expand All @@ -73,9 +73,9 @@ bool mlir::isRowMajorBatchMatmul(ArrayAttr indexingMaps) {
if (indexingMaps.size() != 3)
return false;

auto map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
auto map1 = cast<AffineMapAttr>(indexingMaps[1]).getValue();
auto map2 = cast<AffineMapAttr>(indexingMaps[2]).getValue();
AffineMap map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
AffineMap map1 = cast<AffineMapAttr>(indexingMaps[1]).getValue();
AffineMap map2 = cast<AffineMapAttr>(indexingMaps[2]).getValue();

if (map0.getNumResults() != 3 || map1.getNumResults() != 3 ||
map2.getNumResults() != 3 || map0.getNumInputs() != 4 ||
Expand All @@ -96,6 +96,79 @@ bool mlir::isRowMajorBatchMatmul(ArrayAttr indexingMaps) {
return indexingMaps == maps;
}

bool mlir::isVecmat(ArrayAttr indexingMaps) {
if (indexingMaps.size() != 3)
return false;
AffineMap map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
AffineMap map1 = cast<AffineMapAttr>(indexingMaps[1]).getValue();
AffineMap map2 = cast<AffineMapAttr>(indexingMaps[2]).getValue();

if (map0.getNumResults() != 1 || map1.getNumResults() != 2 ||
map2.getNumResults() != 1 || map0.getNumInputs() != 2 ||
map1.getNumInputs() != 2 || map2.getNumInputs() != 2) {
return false;
}

// Extract dimensions for K * KxN -> N
AffineExpr k = map0.getResult(0);
AffineExpr n = map2.getResult(0);
auto *context = indexingMaps.getContext();
auto mapA = AffineMapAttr::get(AffineMap::get(2, 0, {k}, context));
auto mapB = AffineMapAttr::get(AffineMap::get(2, 0, {k, n}, context));
auto mapC = AffineMapAttr::get(AffineMap::get(2, 0, {n}, context));
auto maps = ArrayAttr::get(context, {mapA, mapB, mapC});
return indexingMaps == maps;
}

bool mlir::isMatvec(ArrayAttr indexingMaps) {
if (indexingMaps.size() != 3)
return false;
AffineMap map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
AffineMap map1 = cast<AffineMapAttr>(indexingMaps[1]).getValue();
AffineMap map2 = cast<AffineMapAttr>(indexingMaps[2]).getValue();

if (map0.getNumResults() != 2 || map1.getNumResults() != 1 ||
map2.getNumResults() != 1 || map0.getNumInputs() != 2 ||
map1.getNumInputs() != 2 || map2.getNumInputs() != 2) {
return false;
}

// Extract dimensions for N*K * K -> N
AffineExpr k = map1.getResult(0);
AffineExpr n = map2.getResult(0);
auto *context = indexingMaps.getContext();
auto mapA = AffineMapAttr::get(AffineMap::get(2, 0, {n, k}, context));
auto mapB = AffineMapAttr::get(AffineMap::get(2, 0, {k}, context));
auto mapC = AffineMapAttr::get(AffineMap::get(2, 0, {n}, context));
auto maps = ArrayAttr::get(context, {mapA, mapB, mapC});
return indexingMaps == maps;
}

bool mlir::isBatchMatvec(ArrayAttr indexingMaps) {
if (indexingMaps.size() != 3)
return false;
AffineMap map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
AffineMap map1 = cast<AffineMapAttr>(indexingMaps[1]).getValue();
AffineMap map2 = cast<AffineMapAttr>(indexingMaps[2]).getValue();

if (map0.getNumResults() != 3 || map1.getNumResults() != 2 ||
map2.getNumResults() != 2 || map0.getNumInputs() != 3 ||
map1.getNumInputs() != 3 || map2.getNumInputs() != 3) {
return false;
}

// Extract dimensions for B*N*K * B*K -> B*N
AffineExpr b = map0.getResult(0);
AffineExpr k = map1.getResult(1);
AffineExpr n = map2.getResult(1);
auto *context = indexingMaps.getContext();
auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {b, n, k}, context));
auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {b, k}, context));
auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {b, n}, context));
auto maps = ArrayAttr::get(context, {mapA, mapB, mapC});
return indexingMaps == maps;
}

Operation *mlir::clone(OpBuilder &b, Operation *op, TypeRange newResultTypes,
ValueRange newOperands) {
IRMapping bvm;
Expand Down
130 changes: 130 additions & 0 deletions mlir/unittests/Dialect/Utils/StructuredOpsUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,134 @@ TEST(isRowMajorBatchMatmul, FirstInputSwapped) {
EXPECT_THAT(maps, Not(Truly(isRowMajorBatchMatmul)));
}

TEST(isVecmat, Simple) {
MLIRContext context;

AffineExpr k, n;
bindDims(&context, k, n);
auto mapA = AffineMapAttr::get(AffineMap::get(2, 0, {k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(2, 0, {k, n}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(2, 0, {n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Truly(isVecmat));
}

TEST(isVecmat, BindingSwapped) {
MLIRContext context;

AffineExpr k, n;
bindDims(&context, n, k); // bind in different order
auto mapA = AffineMapAttr::get(AffineMap::get(2, 0, {k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(2, 0, {k, n}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(2, 0, {n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Truly(isVecmat));
}

TEST(isVecmat, WrongDimOrderMatrix) {
MLIRContext context;

AffineExpr k, n;
bindDims(&context, k, n);
auto mapA = AffineMapAttr::get(AffineMap::get(2, 0, {k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(2, 0, {n, k}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(2, 0, {n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Not(Truly(isVecmat)));
}

TEST(isMatvec, Simple) {
MLIRContext context;

AffineExpr k, n;
bindDims(&context, k, n);
auto mapA = AffineMapAttr::get(AffineMap::get(2, 0, {n, k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(2, 0, {k}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(2, 0, {n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Truly(isMatvec));
}

TEST(isMatvec, BindingSwapped) {
MLIRContext context;

AffineExpr k, n;
bindDims(&context, n, k); // bind in different order
auto mapA = AffineMapAttr::get(AffineMap::get(2, 0, {n, k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(2, 0, {k}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(2, 0, {n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Truly(isMatvec));
}

TEST(isMatvec, WrongDimOrderMatrix) {
MLIRContext context;

AffineExpr k, n;
bindDims(&context, k, n);
auto mapA = AffineMapAttr::get(AffineMap::get(2, 0, {k, n}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(2, 0, {k}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(2, 0, {n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Not(Truly(isMatvec)));
}

TEST(isBatchMatvec, Simple) {
MLIRContext context;

AffineExpr batch, k, n;
bindDims(&context, batch, k, n);
auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {batch, n, k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {batch, k}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {batch, n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Truly(isBatchMatvec));
}

TEST(isBatchMatvec, BindingSwapped) {
MLIRContext context;

AffineExpr batch, k, n;
bindDims(&context, batch, n, k); // bind in different order
auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {batch, n, k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {batch, k}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {batch, n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Truly(isBatchMatvec));
}

TEST(isBatchMatvec, Matmul) {
MLIRContext context;

AffineExpr m, n, k;
bindDims(&context, m, n, k);
auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {m, k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {k, n}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {m, n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Not(Truly(isBatchMatvec)));
}

TEST(isBatchMatvec, WrongDimOrderMatrix) {
MLIRContext context;

AffineExpr batch, k, n;
bindDims(&context, batch, k, n);
auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {batch, k, n}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {batch, k}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {batch, n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Not(Truly(isBatchMatvec)));
}

} // namespace