Skip to content

[Codegen][CPU] Select inner_tiled distribution tiles with the mmt4d cost model - #25130

Open
shuangjuliu wants to merge 5 commits into
iree-org:mainfrom
shuangjuliu:inner_tiled_thread
Open

shuangjuliu wants to merge 5 commits into
iree-org:mainfrom
shuangjuliu:inner_tiled_thread

Conversation

@shuangjuliu

@shuangjuliu shuangjuliu commented Oct 27, 2026 •

Copy link
Copy Markdown

setRootConfig for iree_codegen.inner_tiled hardcoded the distribution
tile to 1, so each workgroup computed a single inner tile over the full K
and large matmuls became thousands of tiny workgroups (1530 for
540x516x332 f32 on RVV), losing to the mmt4d ukernel path at 4/8 threads.

Port the getMmt4dLoweringConfig cost model: pick tile counts per
workgroup among the divisors of the static M/N iteration bounds, filtered
by the iree-llvmcpu-matmul-tile-bytes budget over LHS+RHS+ACC bytes and
scored by traversal cost weighted by thread underutilization. Inner tile
extents come from the op's data_tiled_mma_layout kind. Falls back to the
previous tile-of-1 for dynamic bounds, non-MMA kinds, or multi-output ops.
Vector tiling is unchanged, so the unit-bound lowering invariants and
ukernel ordering are preserved.

The 540x516x332 f32 RVV shape now selects [18, 1, 0]: 85 workgroups.

Tests: new select_riscv_inner_tiled_lowering_strategy.mlir pins the model
output and the dynamic-bounds fallback; a large x86 case is added to
select_x86_64_lowering_strategy.mlir. Existing pinned configs unchanged;
full LLVMCPU + ukernel lit suites pass.

Towards #25028 (hardware benchmarking pending).

Assisted-by: Claude Code, Qwen

Signed-off-by: Shuangju.Liu [email protected]

small: 64³ medium: 256³ rect: 81×41×19 large: 540×516×332

dtype shape threads inner_tiled ms ukernel ms ratio (inner_tiled / ukernel) inner_tiled CPU/wall ukernel CPU/wall
f32 small 1 0.148 0.255 🟢 0.580 1.01 1.00
f32 small 4 0.205 0.231 🟢 0.887 2.00 2.23
f32 small 8 0.206 0.234 🟢 0.880 2.70 3.07
f32 medium 1 5.13 6.26 🟢 0.819 1.00 1.00
f32 medium 4 2.21 2.22 🟢 0.995 3.60 3.56
f32 medium 8 1.69 1.59 🔴 1.063 6.21 6.19
f32 rect 1 0.103 0.194 🟢 0.531 1.01 1.01
f32 rect 4 0.182 0.240 🟢 0.758 1.85 2.08
f32 rect 8 0.214 0.224 🟢 0.955 3.13 3.14
f32 large 1 26.20 32.70 🟢 0.801 1.00 1.00
f32 large 4 11.20 8.61 🔴 1.301 3.88 3.81
f32 large 8 9.36 5.99 🔴 1.563 7.55 7.28
f16 small 1 28.00 22.20 🔴 1.261 1.00 1.00
f16 small 4 7.35 5.64 🔴 1.303 3.88 3.92
f16 small 8 3.98 3.07 🔴 1.296 7.24 7.36
f16 medium 1 1714.00 1386.00 🔴 1.237 1.00 1.00
f16 medium 4 429.00 348.00 🔴 1.233 3.99 3.98
f16 medium 8 218.00 177.00 🔴 1.232 7.89 7.84
f16 rect 1 6.57 5.59 🔴 1.175 1.00 1.00
f16 rect 4 1.83 2.01 🟢 0.910 3.74 2.90
f16 rect 8 1.03 1.32 🟢 0.780 6.83 4.98
f16 large 1 9297.00 8031.00 🔴 1.158 1.00 1.00
f16 large 4 2331.00 2339.00 🟢 0.997 3.99 3.39
f16 large 8 1169.00 1179.00 🟢 0.992 7.97 6.74
bf16 small 1 0.548 0.470 🔴 1.166 1.00 1.00
bf16 small 4 0.317 0.279 🔴 1.136 2.66 2.62
bf16 small 8 0.272 0.291 🟢 0.935 3.68 3.35
bf16 medium 1 27.10 29.20 🟢 0.928 1.00 1.00
bf16 medium 4 7.29 7.42 🟢 0.982 3.90 3.88
bf16 medium 8 4.08 4.05 🔴 1.007 7.13 7.21
bf16 rect 1 0.167 0.141 🔴 1.184 1.01 1.00
bf16 rect 4 0.208 0.188 🔴 1.106 2.04 1.82
bf16 rect 8 0.201 0.194 🔴 1.036 2.69 2.65
bf16 large 1 137.00 175.00 🟢 0.783 0.99 1.00
bf16 large 4 36.30 53.50 🟢 0.679 3.97 3.44
bf16 large 8 18.70 28.50 🟢 0.656 7.70 6.49

Legend: 🟢 ratio < 1.0 (inner_tiled faster) | 🔴 ratio ≥ 1.0 (ukernel faster or neutral)

@github-actions

Copy link
Copy Markdown

Hello @shuangjuliu 👋

Thank you for submitting a Pull Request to IREE! It looks like this is your first one. We have one ask, and you can also find some general tips below.


Action required: acknowledge IREE project policies

IREE is a Linux Foundation project. All participants are expected to follow the LF Projects Code of Conduct.

All contributions to IREE must follow our IREE AI Tool Use Policy. In particular:

  • Contributors must fully understand, and vouch for, all submitted changes and the intent behind them.
  • Substantial use of LLM/generative AI tools must be noted in the PR description, e.g. via Assisted-by: tool-name or Co-authored-by: tool-name tool@email trailers.
  • Contributors must write PR descriptions themselves. There must always be a human in the loop: contributors must respond to reviews and questions by themselves.
    If a response includes LLM-assisted segments (e.g. reproducers, LLM agent analysis excerpts), the segment should be clearly marked as "assisted", same as for PR contents.
  • GitHub issues labeled as "Good first issue" are explicitly designated as learning opportunities for newcomers to the project. With exceptions for boilerplate edits, AI tool usage for resolutions to such issues is forbidden.

We kindly ask you to reply to this message and confirm that you understand and accept the cited policies, particularly the AI Tool Use Policy.


General guidance

Our general Contributing guide contains information and links to detailed guides on code quality, testing, commit summaries and our CI system.

A common point for new PRs: if a DCO signing check fails for you, check out the section on Developer Certificate of Origin.
In these cases, it should suffice to amend your commit signature(s) per the guide and force-push the PR branch.

If you have any questions, feel free to leave a comment here, or ask away on IREE Discord.

Thank you,
The IREE Community

@shuangjuliu

Copy link
Copy Markdown
Author

Hello @shuangjuliu 👋

Thank you for submitting a Pull Request to IREE! It looks like this is your first one. We have one ask, and you can also find some general tips below.

Action required: acknowledge IREE project policies

IREE is a Linux Foundation project. All participants are expected to follow the LF Projects Code of Conduct.

All contributions to IREE must follow our IREE AI Tool Use Policy. In particular:

  • Contributors must fully understand, and vouch for, all submitted changes and the intent behind them.
  • Substantial use of LLM/generative AI tools must be noted in the PR description, e.g. via Assisted-by: tool-name or Co-authored-by: tool-name tool@email trailers.
  • Contributors must write PR descriptions themselves. There must always be a human in the loop: contributors must respond to reviews and questions by themselves.
    If a response includes LLM-assisted segments (e.g. reproducers, LLM agent analysis excerpts), the segment should be clearly marked as "assisted", same as for PR contents.
  • GitHub issues labeled as "Good first issue" are explicitly designated as learning opportunities for newcomers to the project. With exceptions for boilerplate edits, AI tool usage for resolutions to such issues is forbidden.

We kindly ask you to reply to this message and confirm that you understand and accept the cited policies, particularly the AI Tool Use Policy.

General guidance

Our general Contributing guide contains information and links to detailed guides on code quality, testing, commit summaries and our CI system.

A common point for new PRs: if a DCO signing check fails for you, check out the section on Developer Certificate of Origin. In these cases, it should suffice to amend your commit signature(s) per the guide and force-push the PR branch.

If you have any questions, feel free to leave a comment here, or ask away on IREE Discord.

Thank you, The IREE Community

I understand and accept the IREE project policies, including the AI Tool Use Policy.

@egebeysel

egebeysel commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

I'm sorry I was OOO so I couldn't have time to look at it, I'll try and take a look ASAP, hopefully tomorrow :)

@egebeysel

egebeysel commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

A quick look at the results: it looks way better! Thanks for this :) If you'd like, you could go further in your exploration and dive a bit into why the remaining diffs could be, e.g. if they are because of the ever-so-slightly different distribution heuristics, instruction-level issues, code bloat in the backend (extra vsetvli instructions being added to places etc.), or due to the memory relayout ops (pack + transpose + expand or unpack + transpose + collapse which turn into map_stores not being vectorized etc.). The remaining delta looks reasonable enough to stem from distribution heuristics only but a deeper investigation would be nice for us to confirm, and maybe tweak the heuristics even further at one point. Not necessarily here in this PR, but we could add that investigation to the perf tracking issue. Regardless, thanks, nice job!

P.S.: Could you fix the DCO in the meanwhile?

@shuangjuliu
shuangjuliu force-pushed the inner_tiled_thread branch 2 times, most recently from 3ddfe1b to 7bd5d55 Compare November 9, 2026 15:44
@shuangjuliu shuangjuliu closed this Nov 9, 2026
@shuangjuliu shuangjuliu reopened this Nov 9, 2026
@shuangjuliu

Copy link
Copy Markdown
Author

Rebase and add DCO.

@egebeysel egebeysel 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 for working on this! Generally LGTM :) You could maybe make the PR description a bit more concise.

Other than that, we could handle dynamic shapes like mmt4d does, i.e. substitute kTypicalDynamicSize = 1024 for dynamic extents instead of bailing out to one tile per workgroup. We would ideally add tests for these as well.

@egebeysel

Copy link
Copy Markdown
Contributor

Should also probably fix the linter error. One more thing, the mmt4d and inner_tiled do share quite a bit of logic, maybe you could raise some into helper functions and reuse them, the respective config selection functions would also be more readable that way.

…e selection

The old hardcoded tile=1 created too many small workgroups and degraded
multi-thread matmul performance. Reuse the mmt4d cost model to select M/N tile
counts, subject to memory budget and thread utilization scoring. Fall back to
tile=1 for dynamic shapes, non-MMA and multi-output ops. Vector tiling unchanged.

540x516x332 f32 RVV now uses [18,1,0] (85 workgroups vs 1530).

Add RVV and x86 strategy tests. Existing configs preserved.

Towards iree-org#25028 (hardware benchmarking pending).

Assisted-by: Claude Code, Qwen

Signed-off-by: Shuangju.Liu <[email protected]>
…ed lit tests

Pin the cost model's distribution picks for the iree-org#24932 shapes (64^3,
256^3, 81x41x19, 540x516x332) with packed operands from real compiles.
The RVV file sweeps --iree-llvmcpu-number-of-threads=8/4/1 (small shapes
consolidate tiles as threads drop; large stays [18,1,0]; dynamic falls
back to [1,1,0]); the x86 file covers the same matrix at 8 threads.

Assisted-by: Claude Code, Qwen, Doubao

Signed-off-by: Shuangju.Liu <[email protected]>
…d empty tiles

Follow-up to cost model to add fallback to tile-of-1 for two unpriceable cases.

1. Scalable/SVE tiles: SVE intrinsics return static base-VL shape but keep dynamic
VL-dependent inner extents. Budgeting from base-VL underestimates footprint, causing
bad workgroup selection. Check operand layouts and fall back on dynamic inner extents.

2. Empty M/N bounds: division by zero in the underutilization term crashed iree-opt.
Empty shapes now use fallback.

Tests:
- AArch64 SVE: add scalable inner_tiled fallback case
- RVV: add batched matmul + empty-M test
- x86: add SMALLTILE variant under 65536 byte budget to lock known tile selections

Towards iree-org#25028.

Assisted-by: Claude Code, Qwen

Signed-off-by: Shuangju.Liu <[email protected]>
…4d and inner_tiled

Extract the duplicated distribution cost model from mmt4d and inner_tiled
into shared helpers: MatmulTileModel, evalMatmulTileCost,
matmulTileFitsBudget, and selectMatmulWorkgroupTile. Both config functions
now fill a MatmulTileModel and call the shared search, shrinking their cost
sections by ~35 and ~65 lines. No tile selection changes.

The empty-M/N division-by-zero guard moves into the shared search, so mmt4d
gains the same fix inner_tiled needed. Also clang-format the file and update
test comments that named the moved functions.

Assisted-by: Claude Code, Deepseek

Signed-off-by: Shuangju.Liu <[email protected]>
Dynamic M/N/K outer bounds now substitute 1024 elements (mmt4d's
kTypicalDynamicSize) instead of falling back to one tile per workgroup;
the constant moves to file scope for both call sites. A dynamic inner tile
(scalable SVE) still falls back.

Tests: pin the substituted picks — [5,1,0] for all-dynamic, plus focused
M/N/K cases ([34,2,0], [11,4,0]/[11,8,0], [1,2,0]). A full-pipeline run
confirms dynamic inner_tiled lowers to llvm.riscv.vfmacc, so the old
fallback was unnecessary.

Assisted-by: Claude Code, Deepseek

Signed-off-by: Shuangju.Liu <[email protected]>
@shuangjuliu

Copy link
Copy Markdown
Author

Updated and added tests.

@AGindinson
AGindinson requested a review from egebeysel November 17, 2026 00:02

@egebeysel egebeysel 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 for updating, LGTM. Could you add a few benchmarking numbers for dynamically compiled matmuls ran with a few different sizes and dtypes? Also, I just assumed that these benchmarks are from a Jupiter 2 board, is that true? Could you add the board name to the description as well?

This branch has not been deployed

No deployments
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.

2 participants