Skip to content

[Codegen] Generalize iree_gpu.multi_mma to iree_codegen.inner_tiled - #21062

Merged
krzysz00 merged 13 commits into
iree-org:mainfrom
krzysz00:inner-tile-op
Jun 13, 2025
Merged

krzysz00 merged 13 commits into
iree-org:mainfrom
krzysz00:inner-tile-op

Conversation

@krzysz00

@krzysz00 krzysz00 commented Jun 9, 2025 •

Copy link
Copy Markdown
Contributor

In order to implement support for block-scaled MMA and to enable future operations over other "inner tiled" operations (for example, hand-rolled CPU micro-kernels), generalize iree_gpu.multi_mma to iree_codegen.inner_tiled.

This PR is the next stage of the changes begun in #21000

The main differences between iree_gpu.multi_mma and iree_codegen.inner_tiled are that:

  1. inner_tiled takes an arbitrary number of inputs and accumulator outputs/inits, with the INnerTileDescAttrInterface describing the expected number of arguments. The inits are named "outputs" following the linalg.generic convention.
  2. The permutations - when present - are now stored as an array-of-arrays, and not as one array for each argument (since there are no longer fixed "lhs", "rhs", and "acc" arguments).
  3. The operation takes an InnerTileDescAttrInterface implement instead of a MmaInterfaceAttr, though checking for the old interface is still sometimes used to scope patterns down to just multi-mma-like operations.
  4. We use linalg.iterator_type<> instead of iree_gpu.iterator_type<>, since trying to define iree_codegen.iterator_type<> was causing redefinition errors

In terms of transformations, this PR generalizes (and, as a consequence, renames) those transformations that had clear generalizations to arbitrary intrinsics. This means that the configuration logic / lowering to multi-MMA is unaffeced, but a lot of the internal transformations (such as vectorization or tiling) have been reworked to support N inputs.

These changes have not required more than mechanical (changing name + assembly format) changes to any tests and never showed end-to-end correctness issues, so it's likely that this PR causes no change in the generated code.

To provide a motivating example for the generalization

iree_codegen.inner_tiled ins(%lhs, %lhs_scale, %rhs, %rhs_scale) acc(%acc) {
  indexing_maps = [
    affine_map<(i, j, k) -> (i, k)>,
    affine_map<(i, j, k) -> (i, k)>,
    affine_map<(i, j, k) -> (j, k)>,
    affine_map<(i, j, k) -> (j, k)>,
    affine_map<(i, j, k) -> (i, j)>
  ],
  iterator_types = [parallel, parallel, reduction],
  kind = #iree_gpu.scaling_mma_kind<MFMA_16x16x128, lhsType = f4E2M1FN, rhsType = f8E3M3FN, acc_type = f32>
} : tensor<?x?x16x128xf4E2M1FN>,
  tensor<?x?x16x4xf32>, // Note k dimension is a bunch smaller for scales
  tensor<?x?x128x16xf8E4M3FN>,
  tensor<?x?x4x16xf32> into
  tensor<?x?x16x16xf32>

The exact representation of scaling MMAs will be designed out later, but the above example is a demonstration of the overall motivation for this change

Fixes ##20814

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

Here's the first round of comments. Mostly nits, with the main comment being about having only a single accumulator. Nice work so far, this is a pretty huge refactor!

krzysz00 added 2 commits June 11, 2025 17:58
In order to implement support for block-scaled MMA and to enable
future operations over other "inner tiled" operations (for example,
hand-rolled CPU micro-kernels), generalize iree_gpu.multi_mma to
iree_codegen.inner_tiled.

This PR is the next stage of the changes begun in iree-org#21000

The main differences between iree_gpu.multi_mma and
iree_codegen.inner_tiled are that:
1. inner_tiled takes an arbitrary number of inputs, with the
INnerTileDescAttrInterface describing the expected number of
arguments.
2. The permutations - when present - are now stored as an
array-of-arrays, and not as one array for each argument (since there
are no longer fixed "lhs", "rhs", and "acc" arguments).
3. The operation takes an InnerTileDescAttrInterface implement instead
of a MmaInterfaceAttr, though checking for the old interface is still
sometimes used to scope patterns down to just multi-mma-like
operations.
4. We use linalg.iterator_type<> instead of iree_gpu.iterator_type<>,
since trying to define iree_codegen.iterator_type<> was causing
redefinition errors

In terms of transformatios, this PR generalizes (and, as a
consequence, renames) those transformations that had clear
generalizations to arbitrary intrinsics. This means that the
configuration logic / lowering to multi-MMA is unaffeced, but a lot of
the internal transformations (such as vectorization or tiling) have
been reworked to support N inputs.

These changes have not required more than mechanical (changing name +
assembly format) changes to any tests and never showed end-to-end
correctness issues, so it's likely that this PR causes no change in
the generated code.
Comment on lines +120 to +121
kind that is to be used. The second return value is the upper bound
on the size of the mapping kind that'll be passed to the forall, if known.

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.

What do you mean by the size of the mapping kind?

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.

Is the new wording clearer?

@krzysz00
krzysz00 requested a review from Max191 June 12, 2025 19:10

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

Nice! It pretty much looks good to me % one more comment about the getDistributionMappingKind function.

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

Awesome, just a bunch of minor comments. Looks like this was a lot more than I expected, great job wading through it!

@Max191 Max191 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 addressing all the comments, it LTGM!

@krzysz00
krzysz00 merged commit 7e54ffc into iree-org:main Jun 13, 2025
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
…21062)

In order to implement support for block-scaled MMA and to enable future
operations over other "inner tiled" operations (for example, hand-rolled
CPU micro-kernels), generalize iree_gpu.multi_mma to
iree_codegen.inner_tiled.

This PR is the next stage of the changes begun in #21000

The main differences between iree_gpu.multi_mma and
iree_codegen.inner_tiled are that:
1. inner_tiled takes an arbitrary number of inputs and accumulator
outputs/inits, with the INnerTileDescAttrInterface describing the
expected number of arguments. The inits are named "outputs" following
the `linalg.generic` convention.
2. The permutations - when present - are now stored as an
array-of-arrays, and not as one array for each argument (since there are
no longer fixed "lhs", "rhs", and "acc" arguments).
3. The operation takes an InnerTileDescAttrInterface implement instead
of a MmaInterfaceAttr, though checking for the old interface is still
sometimes used to scope patterns down to just multi-mma-like operations.
4. We use linalg.iterator_type<> instead of iree_gpu.iterator_type<>,
since trying to define iree_codegen.iterator_type<> was causing
redefinition errors

In terms of transformations, this PR generalizes (and, as a consequence,
renames) those transformations that had clear generalizations to
arbitrary intrinsics. This means that the configuration logic / lowering
to multi-MMA is unaffeced, but a lot of the internal transformations
(such as vectorization or tiling) have been reworked to support N
inputs.

These changes have not required more than mechanical (changing name +
assembly format) changes to any tests and never showed end-to-end
correctness issues, so it's likely that this PR causes no change in the
generated code.

To provide a motivating example for the generalization
```mlir
iree_codegen.inner_tiled ins(%lhs, %lhs_scale, %rhs, %rhs_scale) acc(%acc) {
  indexing_maps = [
    affine_map<(i, j, k) -> (i, k)>,
    affine_map<(i, j, k) -> (i, k)>,
    affine_map<(i, j, k) -> (j, k)>,
    affine_map<(i, j, k) -> (j, k)>,
    affine_map<(i, j, k) -> (i, j)>
  ],
  iterator_types = [parallel, parallel, reduction],
  kind = #iree_gpu.scaling_mma_kind<MFMA_16x16x128, lhsType = f4E2M1FN, rhsType = f8E3M3FN, acc_type = f32>
} : tensor<?x?x16x128xf4E2M1FN>,
  tensor<?x?x16x4xf32>, // Note k dimension is a bunch smaller for scales
  tensor<?x?x128x16xf8E4M3FN>,
  tensor<?x?x4x16xf32> into
  tensor<?x?x16x16xf32>
```

The exact representation of scaling MMAs will be designed out later, but
the above example is a demonstration of the overall motivation for this
change

Fixes ##20814

---------

Co-authored-by: Max191 <[email protected]>
Co-authored-by: Quinn Dawkins <[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