Skip to content

[Codegen][GPU] Sort intrinsic according to k alignment - Step 1 of 2- Track MmaInterfaceAttr via field instead of index - #21103

Merged
jerryyin merged 3 commits into
mainfrom
users/zyin/refactor-create-gpu-intrinsic-type
Jun 18, 2025
Merged

jerryyin merged 3 commits into
mainfrom
users/zyin/refactor-create-gpu-intrinsic-type

Conversation

@jerryyin

@jerryyin jerryyin commented Jun 16, 2025 •

Copy link
Copy Markdown
Member

This PR is NFC. This PR simplified the intrinsic derive process via:

  • Created a dedicated GPUIntrinsicType to differentiate from a GPUMatmulShapeType
    • This newly created GPUIntrinsicType inherits from GPUMatmulShapeType
    • It keep track of the MmaInterfaceAttr directly in a field
  • Removed the need to track the intrinsic separately through a index like intrinsicIndex
    • The separately maintained index used reference only to the position of intrinsic, making it error prone. To sort and prioritize the intrinsic will have to keep track of the intrinsic before and after sorting.
    • Keeping track of intrinsic instead of index makes tracking much easier
  • Replaced the index field from GPUMMASchedule to MmaInterfaceAttr for the same reasoning

Sort intrinsic according to K alignment is dependent on this change to land. I break the NFC aspect of the feature into this PR so it is easier to do code reviews. For the convolution configurations that are not k-aligned by 16x16x16 intrinsic (but aligned with 32x32x8), this together with next PR will bring 5-10x perf lift.

@qedawkins
qedawkins requested a review from krzysz00 June 16, 2025 17:14
@jerryyin jerryyin changed the title Sort intrinsic according to k alignment - Step 1 of 2- Track MmaInterfaceAttr via field instead of index [Codegen][GPU] Sort intrinsic according to k alignment - Step 1 of 2- Track MmaInterfaceAttr via field instead of index Jun 16, 2025
@jerryyin
jerryyin force-pushed the users/zyin/refactor-create-gpu-intrinsic-type branch from 3c6158d to 750081f Compare June 16, 2025 17:30

@krzysz00 krzysz00 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.

Lgtm

@kuhar kuhar 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.

LGTM % nit. Maybe wait for @qedawkins to stamp too before landing.

@qedawkins qedawkins 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.

I'm worried about how painful it is to follow this code, but that's eternally been the case with heuristic code. Looks like an improvement to me.

@jerryyin
jerryyin force-pushed the users/zyin/refactor-create-gpu-intrinsic-type branch 3 times, most recently from bf1ea95 to 866c222 Compare June 18, 2025 14:26
@jerryyin

Copy link
Copy Markdown
Member Author

@qedawkins I hear you! The heuristic code has been a bit spaghetti like and looks like this is a common infra supporting a wide range of different pipelines. I tried hard to narrow the feature into a small scope (i.e. adding an argument with default parameter) and only to find that makes the situation worse. This PR ended to become the minimal refactor that hopefully makes this common infra slightly cleaner and more generic.

@jerryyin
jerryyin merged commit f1a20c3 into main Jun 18, 2025
@jerryyin
jerryyin deleted the users/zyin/refactor-create-gpu-intrinsic-type branch June 18, 2025 15:51
jerryyin added a commit that referenced this pull request Jun 20, 2025
…- Creating intrinsic sort routine (#21128)

This is a follow-up of #21103.

This intrinsic sorting routine will sort the MMA intrinsics by following
precedence rules:
1) K-alignment. We prefer intrinsics that can evenly divide the K
dimension of the problem.
2) M/N-alignment. We prefer intrinsics that can evenly divide the M and
N dimensions of the problem.
  3) Intrinsic with larger gemm size.
  4) Intrinsic with larger K size.

Scope of the impact is igemm and matmul problems. Other pipelines can
choose to adopt this if the sorting turns out to be useful.

### Motivation:

convolution configuration with 
> BOO_CACHE_ON=0 python boo_driver.py convbfp16 -n 16 -c 40 -H 192 -W
128 -k 40 -y 3 -x 3 -p 1 -q 1 -u 2 -v 2 -l 1 -j 1 -m conv -g 1 -F 1 -t 1
--in_layout NHWC --out_layout NHWC --fil_layout NHWC --iter 50

Since the gemmK size is 360, the best intrinsic is 32x32x8xbf16, not
16x16x16xbf16, as the former will end in aligned K. Before this PR:
`362.06 us`. After this PR: `44.74 us`. Note that due to limitation of
the tuner, this combination won't be covered by typical tuning run with
2k trials. So it is essential we bridge the gap through heuristic first
before tuner is more capable of searching through different heuristics.

---------

Signed-off-by: jerryyin <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
… Track MmaInterfaceAttr via field instead of index (#21103)

This PR is NFC. This PR simplified the intrinsic derive process via:
- Created a dedicated `GPUIntrinsicType` to differentiate from a
`GPUMatmulShapeType`
- This newly created `GPUIntrinsicType` inherits from
`GPUMatmulShapeType`
   - It keep track of the `MmaInterfaceAttr` directly in a field
- Removed the need to track the intrinsic separately through a index
like `intrinsicIndex`
- The separately maintained index used reference only to the position of
intrinsic, making it error prone. To sort and prioritize the intrinsic
will have to keep track of the intrinsic before and after sorting.
- Keeping track of intrinsic instead of index makes tracking much easier
- Replaced the index field from `GPUMMASchedule` to `MmaInterfaceAttr`
for the same reasoning

Sort intrinsic according to K alignment is dependent on this change to
land. I break the NFC aspect of the feature into this PR so it is easier
to do code reviews. For the convolution configurations that are not
k-aligned by 16x16x16 intrinsic (but aligned with 32x32x8), this
together with next PR will bring 5-10x perf lift.

---------

Signed-off-by: jerryyin <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
…- Creating intrinsic sort routine (#21128)

This is a follow-up of #21103.

This intrinsic sorting routine will sort the MMA intrinsics by following
precedence rules:
1) K-alignment. We prefer intrinsics that can evenly divide the K
dimension of the problem.
2) M/N-alignment. We prefer intrinsics that can evenly divide the M and
N dimensions of the problem.
  3) Intrinsic with larger gemm size.
  4) Intrinsic with larger K size.

Scope of the impact is igemm and matmul problems. Other pipelines can
choose to adopt this if the sorting turns out to be useful.

### Motivation:

convolution configuration with 
> BOO_CACHE_ON=0 python boo_driver.py convbfp16 -n 16 -c 40 -H 192 -W
128 -k 40 -y 3 -x 3 -p 1 -q 1 -u 2 -v 2 -l 1 -j 1 -m conv -g 1 -F 1 -t 1
--in_layout NHWC --out_layout NHWC --fil_layout NHWC --iter 50

Since the gemmK size is 360, the best intrinsic is 32x32x8xbf16, not
16x16x16xbf16, as the former will end in aligned K. Before this PR:
`362.06 us`. After this PR: `44.74 us`. Note that due to limitation of
the tuner, this combination won't be covered by typical tuning run with
2k trials. So it is essential we bridge the gap through heuristic first
before tuner is more capable of searching through different heuristics.

---------

Signed-off-by: jerryyin <[email protected]>
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.

4 participants