-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[BACKEND]Fix DotOperand(Ampere) LinearLayoutConversion #5038
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,17 @@ SmallVector<StringAttr> standardOutDimNames(MLIRContext *ctx, int rank) { | |
| return ret; | ||
| } | ||
|
|
||
| // TODO Have order be a mandatory argument of standardOutDimNames. | ||
| SmallVector<StringAttr> permuteDimNames(const SmallVector<StringAttr> &names, | ||
| const SmallVector<unsigned> &order) { | ||
| assert(names.size() == order.size()); | ||
| SmallVector<StringAttr> ret; | ||
| for (unsigned i : order) { | ||
| ret.push_back(names[i]); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| void assertIsRegisterLayout(const LinearLayout &layout) { | ||
| assert(layout.getNumInDims() > 0); | ||
| MLIRContext *ctx = layout.getInDimNames().begin()->getContext(); | ||
|
|
@@ -281,15 +292,19 @@ LinearLayout ampereMmaToLinearLayout(ArrayRef<int64_t> shape, | |
|
|
||
| MLIRContext *ctx = mma.getContext(); | ||
| SmallVector<StringAttr> dimNames = standardOutDimNames(ctx, rank); | ||
| auto orderedDimNames = permuteDimNames(dimNames, getOrder(mma)); | ||
| // By using `reverse(dimNames)` below, we set the order to be row-major | ||
| assert(getOrder(mma) == getMatrixOrder(rank, /*rowMajor=*/true)); | ||
|
|
||
| LinearLayout ctaLayout( | ||
| {{S("register"), {{1, 0}, {0, 8}}}, | ||
| {S("lane"), {{2, 0}, {4, 0}, {0, 1}, {0, 2}, {0, 4}}}}, | ||
| llvm::to_vector(llvm::reverse(ArrayRef(dimNames).take_back(2)))); | ||
|
|
||
| ctaLayout *= identityND( | ||
| S("warp"), mma.getWarpsPerCTA(), | ||
| llvm::to_vector(llvm::reverse(llvm::seq<unsigned>(rank))), dimNames); | ||
| ArrayRef(orderedDimNames).take_front(2)); | ||
| assert(getWarpOrder(mma) == getMatrixOrder(rank, /*rowMajor=*/true)); | ||
| // FIXME(Lezcano). identityND should not have an `order` param as it's | ||
| // redundant with the order of the out dims. | ||
| ctaLayout *= | ||
| identityND(S("warp"), mma.getWarpsPerCTA(), mma.getWarpOrder(), dimNames); | ||
|
|
||
| return combineCtaCgaWithShape(ctaLayout, mma.getCTALayout(), shape); | ||
| } | ||
|
|
@@ -322,10 +337,14 @@ LinearLayout hopperMmaToLinearLayout(ArrayRef<int64_t> shape, | |
| ctaLayout *= LinearLayout::identity1D(n / ctaLayout.getOutDimSize(S("dim1")), | ||
| S("register"), S("dim1")); | ||
|
|
||
| // Expand the `warp` dimension according to warpsPerCTA. | ||
| // | ||
| // It's weird that this is order [0,1] when MMAv2's warpsPerCTA is [1,0], but | ||
| // this really does seem to be correct. | ||
| // The order given by choosing (`dim1`, `dim0`) is [1, 0], that is, N-major. | ||
| // Since the warpOrder needs to be M-major, we need to transpose the out | ||
| // dimensions AND transpose the order | ||
| // FIXME(Lezcano). identityND should not have an `order` param as it's | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree |
||
| // redundant. The order is already given by the order of the | ||
| // out dims, and if it has an order, it shouldn't change the | ||
| // order of the out dims. | ||
| assert(getWarpOrder(mma) == SmallVector<unsigned>({0, 1})); | ||
| ctaLayout *= identityND(S("warp"), mma.getWarpsPerCTA(), /*order=*/{0, 1}, | ||
| {S("dim0"), S("dim1")}) | ||
| .transposeOuts(llvm::to_vector(ctaLayout.getOutDimNames())); | ||
|
|
@@ -843,18 +862,24 @@ SliceEncodingAttr::toLinearLayout(ArrayRef<int64_t> shape) const { | |
|
|
||
| LinearLayout ampereDotToLinearLayout(ArrayRef<int64_t> shape, | ||
| DotOperandEncodingAttr dot) { | ||
| // TODO,BE. Implement ampereMMA in terms of this one | ||
| // Note that, even though MMAv2 looks similar to this layout, they are just | ||
| // the same at a register and lane level. The warps treatment is different! | ||
| int rank = shape.size(); | ||
| auto mma = cast<NvidiaMmaEncodingAttr>(dot.getParent()); | ||
| int kWidth = dot.getKWidth(); | ||
| bool isA = dot.getOpIdx() == 0; | ||
|
|
||
| assert(mma.isAmpere()); | ||
| assert((rank == 2 && mma.getInstrShape() == ArrayRef<unsigned>({16, 8})) || | ||
| (rank == 3 && mma.getInstrShape() == ArrayRef<unsigned>({1, 16, 8}))); | ||
| assert(mma.isAmpere()); | ||
|
|
||
| MLIRContext *ctx = mma.getContext(); | ||
| SmallVector<StringAttr> dimNames = standardOutDimNames(ctx, rank); | ||
| // A and B have kMajor order | ||
| assert(getOrder(dot) == | ||
| getOrderForDotOperand(dot.getOpIdx(), rank, /*kMajor=*/true)); | ||
|
|
||
| auto kMajorDims = | ||
| permuteDimNames(standardOutDimNames(ctx, rank), getOrder(dot)); | ||
|
|
||
| // Implement A. For B transpose in the end | ||
| std::vector<std::vector<int32_t>> registers; | ||
|
|
@@ -881,24 +906,51 @@ LinearLayout ampereDotToLinearLayout(ArrayRef<int64_t> shape, | |
| } | ||
| registers.push_back({i, 0}); | ||
|
|
||
| if (!isA) { | ||
| for (auto &r : registers) { | ||
| std::swap(r[0], r[1]); | ||
| LinearLayout ctaLayout({{S("register"), registers}, {S("lane"), lanes}}, | ||
| ArrayRef(kMajorDims).take_front(2)); | ||
|
|
||
| // Let warpsPerCTAMma = {2, 2}, then | ||
| // warpsPerCTA = {2, 1} for opA and warpsPerCTA = {1, 2} for opB | ||
| // assume warpOrder = {0, 1} | ||
| // Assume that C is tiled by 2x2 tiles. Since warpOrder={1, 0}, we have that | ||
| // the C is owned as per the following layout: | ||
| // C: 0 | 1 | ||
| // - | - | ||
| // 2 | 3 | ||
| // In order to be able to compute C, we need the following warp tiling of | ||
| // A and B: | ||
| // A: 0 1 | 0 1 B: 0 2 | 1 3 | ||
| // - - | - - - - | - - | ||
| // 2 3 | 2 3 0 2 | 1 3 | ||
| // In particular, for A and B we need to broadcast along K | ||
|
|
||
| assert(mma.getWarpOrder() == getMatrixOrder(rank, /*rowMajor=*/true)); | ||
| auto warpsPerCTAMma = mma.getWarpsPerCTA(); | ||
| std::vector<std::vector<int32_t>> warps; | ||
| if (isA) { | ||
| for (int i = 1; i < warpsPerCTAMma[1]; i *= 2) { | ||
| warps.push_back({0, 0}); | ||
| } | ||
| for (int i = 1; i < warpsPerCTAMma[0]; i *= 2) { | ||
| warps.push_back({0, i}); | ||
| } | ||
| } else { | ||
| for (int i = 1; i < warpsPerCTAMma[1]; i *= 2) { | ||
| warps.push_back({0, i}); | ||
| } | ||
| for (auto &l : lanes) { | ||
| std::swap(l[0], l[1]); | ||
| for (int i = 1; i < warpsPerCTAMma[0]; i *= 2) { | ||
| warps.push_back({0, 0}); | ||
| } | ||
| } | ||
| if (rank == 3) { | ||
| for (auto &w : warps) { | ||
| w.push_back(0); | ||
| } | ||
| } | ||
|
|
||
| LinearLayout ctaLayout( | ||
| {{S("register"), registers}, {S("lane"), lanes}}, | ||
| llvm::to_vector(llvm::reverse(ArrayRef(dimNames).take_back(2)))); | ||
|
|
||
| auto order = dot.getCTAOrder(); | ||
| assert(order[0] == rank - 1 && order[1] == rank - 2); | ||
| ctaLayout *= identityND(S("warp"), dot.getWarpsPerCTA(), order, dimNames); | ||
| ctaLayout *= LinearLayout({{S("warp"), warps}}, kMajorDims); | ||
|
|
||
| return combineCtaCgaWithShape(ctaLayout, mma.getCTALayout(), shape); | ||
| return combineCtaCgaWithShape(ctaLayout, getCTALayout(dot), shape); | ||
| } | ||
|
|
||
| std::optional<LinearLayout> | ||
|
|
@@ -907,7 +959,7 @@ DotOperandEncodingAttr::toLinearLayout(ArrayRef<int64_t> shape) const { | |
| if (auto mfmaLayout = llvm::dyn_cast<AMDMfmaEncodingAttr>(parent)) { | ||
| return mfmaDotToLinearLayout(*this, shape); | ||
| } else if (auto mma = mlir::dyn_cast<NvidiaMmaEncodingAttr>(parent)) { | ||
| if (mma.getVersionMajor() == 2 && mma.getVersionMinor() == 0) { | ||
| if (mma.isAmpere()) { | ||
| return ampereDotToLinearLayout(shape, *this); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused about the name "matrix" when rank > 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We consider it as a batched matrix of shape
[*, m, n], where * are zero or more dimensions (see the comment below).