Skip to content

Add isBatchVecmat utilities for linalg.batch_vecmat #70284

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

Merged
merged 1 commit into from
Oct 26, 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
11 changes: 11 additions & 0 deletions mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ def LinalgContractionOpInterface : OpInterface<"ContractionOpInterface"> {
return mlir::isVecmat($_op.getIndexingMaps());
}]>,
InterfaceMethod<
/*desc=*/[{
Returns whether the given op has indexing maps that correspond to a
batched vector-matrix multiplication.
}],
/*retTy=*/"bool",
/*methodName=*/"isBatchVecmat",
/*args=*/(ins),
/*methodBody=*/[{
return mlir::isBatchVecmat($_op.getIndexingMaps());
}]>,
InterfaceMethod<
/*desc=*/[{
Returns whether the given op has indexing maps that correspond to a
matrix-vector multiplication.
Expand Down
6 changes: 6 additions & 0 deletions mlir/include/mlir/Dialect/Utils/StructuredOpsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ bool isRowMajorBatchMatmul(ArrayAttr indexingMaps);
/// performed within the reduction.
bool isVecmat(ArrayAttr indexingMaps);

/// Tests whether the given maps describe a batch 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 isBatchVecmat(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
Expand Down
25 changes: 25 additions & 0 deletions mlir/lib/Dialect/Utils/StructuredOpsUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,31 @@ bool mlir::isVecmat(ArrayAttr indexingMaps) {
return indexingMaps == maps;
}

bool mlir::isBatchVecmat(ArrayAttr indexingMaps) {
if (indexingMaps.size() != 3)
return false;
AffineMap map0 = cast<AffineMapAttr>(indexingMaps[0]).getValue();
Copy link
Contributor

Choose a reason for hiding this comment

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

you could use the infer utils which I find nicer to work with:

if (maps == infer({{m, k}, {k, n}, {m, n}})) {
your call :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i see. nicer indeed, but i was copy and pasting other instances in this file. The move to infer looks like it would be a nice improvement, but should be done for the whole file togeteher, so, in a separate PR.

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

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

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

bool mlir::isMatvec(ArrayAttr indexingMaps) {
if (indexingMaps.size() != 3)
return false;
Expand Down
52 changes: 52 additions & 0 deletions mlir/unittests/Dialect/Utils/StructuredOpsUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,56 @@ TEST(isBatchMatvec, WrongDimOrderMatrix) {
EXPECT_THAT(maps, Not(Truly(isBatchMatvec)));
}

TEST(isBatchVecmat, Simple) {
MLIRContext context;

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

EXPECT_THAT(maps, Truly(isBatchVecmat));
}

TEST(isBatchVecmat, 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, k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {batch, k, n}, &context));
auto mapC = AffineMapAttr::get(AffineMap::get(3, 0, {batch, n}, &context));
auto maps = ArrayAttr::get(&context, {mapA, mapB, mapC});

EXPECT_THAT(maps, Truly(isBatchVecmat));
}

TEST(isBatchVecmat, 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(isBatchVecmat)));
}

TEST(isBatchVecmat, WrongDimOrderMatrix) {
MLIRContext context;

AffineExpr batch, k, n;
bindDims(&context, batch, k, n);
auto mapA = AffineMapAttr::get(AffineMap::get(3, 0, {batch, k}, &context));
auto mapB = AffineMapAttr::get(AffineMap::get(3, 0, {batch, n, 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(isBatchVecmat)));
}

} // namespace