Skip to content

[AMD] Fix kwidth printing and parsing - #3676

Merged
antiagainst merged 10 commits into
triton-lang:mainfrom
binarman:fix_kwidth_printing
May 17, 2024
Merged

antiagainst merged 10 commits into
triton-lang:mainfrom
binarman:fix_kwidth_printing

Conversation

@binarman

@binarman binarman commented Apr 16, 2024 •

Copy link
Copy Markdown
Contributor

This PR fixes:

  • parsing and printing of kWidth attribute for MFMA and WMMA layouts

@binarman
binarman requested a review from ptillet as a code owner April 16, 2024 18:14
@binarman

Copy link
Copy Markdown
Contributor Author

FYI @oplavsic

auto mmaParent = parent.dyn_cast<NvidiaMmaEncodingAttr>();
printer << "<{"
<< "opIdx = " << getOpIdx() << ", parent = " << getParent();
if (mmaParent && mmaParent.isAmpere())

@binarman binarman Apr 16, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have a question related to Nvidia.

This part mismatches parsing:

  • Parse for Ampere and Hopper
  • Print only for Ampere

Can anyone clarify, which behavior is correct?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm 60% sure that printing for Hopper is correct, but also in the 40% case that it's incorrect then printing it will make it obvious to someone else that it shouldn't be there. :) Thanks for noticing this.

@jlebar jlebar Apr 16, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also this whole thing seems unnecessarily confusing. It seems to me that we shouldn't need any of this custom parsing logic. Instead:

  • Make kWidth an optional attribute with a default of 0 or -1.
  • Use a regular MLIR parser and printer, no need for the custom C++ at all. It will not print kWidth when it's 0, because that's the default.
  • Add a verifier that checks that kWidth is 0 for those encodings that don't support kWidth. Also check that the kWidth is explicitly equal to AMDWmmaEncodingAttr::getMNKDimPerWMMAInstr()[2] for AMDWmma.

Not your sin, I guess you don't need to change it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can try to rework this with default value approach without custom parsing.
I suspect this will change formatting, but we can experiment and see if this make sense

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is OK to change formatting. You'll have to fix the tests, but I've done it, it's not too bad.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Turned out declarative format can express same formatting 🙂

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yup. Great to see this getting cleaned up. Thanks @binarman!

@jlebar jlebar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting changes because I think this needs a unit test.

Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Outdated
<< "opIdx = " << getOpIdx() << ", parent = " << getParent();
if (mmaParent && mmaParent.isAmpere())
bool isAmdWithKWidth =
parent.isa<AMDMfmaEncodingAttr>() || parent.isa<AMDWmmaEncodingAttr>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems like a similar inconsistency to Ampere/Hopper. Here we're printing kWidth if AMDWmma, but if AMDWmma we ignore any parsed value and unconditionally do kWidth = AMDWmmaEncodingAttr::getMNKDimPerWMMAInstr()[2];

auto mmaParent = parent.dyn_cast<NvidiaMmaEncodingAttr>();
printer << "<{"
<< "opIdx = " << getOpIdx() << ", parent = " << getParent();
if (mmaParent && mmaParent.isAmpere())

@jlebar jlebar Apr 16, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also this whole thing seems unnecessarily confusing. It seems to me that we shouldn't need any of this custom parsing logic. Instead:

  • Make kWidth an optional attribute with a default of 0 or -1.
  • Use a regular MLIR parser and printer, no need for the custom C++ at all. It will not print kWidth when it's 0, because that's the default.
  • Add a verifier that checks that kWidth is 0 for those encodings that don't support kWidth. Also check that the kWidth is explicitly equal to AMDWmmaEncodingAttr::getMNKDimPerWMMAInstr()[2] for AMDWmma.

Not your sin, I guess you don't need to change it.

Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Outdated
Attribute _kWidth = attrs.get("kWidth");
if (_kWidth) {
if (!mmaParent || mmaParent.isVolta()) {
bool isAmdWithKWidth =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Write a unit test?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1

Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
Comment thread python/tutorials/06-fused-attention.py Resolved
Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Outdated
Attribute _kWidth = attrs.get("kWidth");
if (_kWidth) {
if (!mmaParent || mmaParent.isVolta()) {
bool isAmdWithKWidth =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1

@antiagainst

antiagainst commented Apr 16, 2024 •

Copy link
Copy Markdown
Member

Also, @binarman, please make sure all tests pass before marking pull requests as ready for review. As addressing bots happy needs changing the impl. (You can ping me if you need to trigger CI runs.)

@binarman

binarman commented May 1, 2024

Copy link
Copy Markdown
Contributor Author

@antiagainst @jlebar
I've updated this PR, can you take a look?

@jlebar
jlebar requested a review from antiagainst May 1, 2024 20:50
@alefimov-amd
alefimov-amd force-pushed the fix_kwidth_printing branch 2 times, most recently from 794e451 to dff9246 Compare May 9, 2024 14:50

@antiagainst antiagainst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cool, looks better. I still have a few comments. Major one is actuall the tests.

Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
auto mmaParent = parent.dyn_cast<NvidiaMmaEncodingAttr>();
printer << "<{"
<< "opIdx = " << getOpIdx() << ", parent = " << getParent();
if (mmaParent && mmaParent.isAmpere())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yup. Great to see this getting cleaned up. Thanks @binarman!

@antiagainst
antiagainst marked this pull request as draft May 14, 2024 21:05
@antiagainst

Copy link
Copy Markdown
Member

(converting to draft as we said internally. will mark it open after approval from me)

binarman added 4 commits May 15, 2024 15:17
This PR fixes:
- parsing and printing of kWidth attribute for MFMA and WMMA layouts
- comment in 06-fused-attention.p tutorial
@binarman
binarman force-pushed the fix_kwidth_printing branch from dff9246 to 09680cf Compare May 15, 2024 21:21

@antiagainst antiagainst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome, thanks for bearing with my nitpicking! :)

@antiagainst
antiagainst marked this pull request as ready for review May 17, 2024 00:00

@jlebar jlebar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

Comment thread lib/Dialect/TritonGPU/IR/Dialect.cpp Resolved
@antiagainst
antiagainst enabled auto-merge (squash) May 17, 2024 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants