Skip to content

[Codegen][GPU] Add a inner tiled op descriptor for scaled MMA - #21141

Merged
krzysz00 merged 2 commits into
mainfrom
users/krzysz00/scaling-mfma-desc
Jun 25, 2025
Merged

krzysz00 merged 2 commits into
mainfrom
users/krzysz00/scaling-mfma-desc

Conversation

@krzysz00

Copy link
Copy Markdown
Contributor

This commit adds a descriptor attribute - #iree_gpu.scaled_mma_layout - for MMAs that takes "scale" operands and tests for its lowering from DistributeInnerTiledToLanes and afterwards.

This descriptor takes four input operands - the lleft-hand side of the matrix, the LHS scales, the right-hand side, and the RHS scales, in that order. The operation has one accumulator.

Scaled MMAs are, like MMA intrinsics, represented by an enum, which litss out the available intrinsics (currenvtly, we only have AMD's MI-350). However, unlike with MMA intrinsics, the input and output types are given in the scaled_mma_layout attribute itself. This is because these scaled MMAs support arbitrary pairs of left- and right-hand side types from {f4E2M1FN, f6E3M2FN, f6E3M2FN, f8E4M3FN, f8E5M2}, so listing out all the combinations would take up a lot of space and produce a large number of nearly-identical cases in the descriotor logic.

This inner tiled operation treats the k values that have distinct scales and the "block" (the B (currently always 32) values that share a common scale) as separate reduction dimensions. This means that the scale operands, in effect, don't have a block dimension. This also means that models where the block size is larger than 32 can be supported by tiling across the scale dimension.

The following work is not yet implemented:

  • ContretizeMmaShapes has not yet been generalized to arbitrary inner tiled ops. This will be needed to correctly lower the 32x32x64 intrinsic, which has an outer dimension on the accumulator.
  • Lowering configuration logic and PackToIntrinsics have not been addressed

Note that this PR does contain a few code changes. One is declaring the vector unrolling logic to be safe for non-MMA inner_tiled ops. The other is a bug fix to basisFromSizesStrides(), which used an incorrect tiebreaking strategy for multiple dimensions that had the same stride (now it's ties broken by the length of the dimension and then its position in the list).

This commit adds a descriptor attribute - #iree_gpu.scaled_mma_layout -
for MMAs that takes "scale" operands and tests for its lowering
from DistributeInnerTiledToLanes and afterwards.

This descriptor takes four input operands - the lleft-hand side of the
matrix, the LHS scales, the right-hand side, and the RHS scales, in
that order. The operation has one accumulator.

Scaled MMAs are, like MMA intrinsics, represented by an enum, which
litss out the available intrinsics (currenvtly, we only have AMD's
MI-350). However, unlike with MMA intrinsics, the input and output
types are given in the scaled_mma_layout attribute itself. This is
because these scaled MMAs support arbitrary pairs of left- and
right-hand side types from {f4E2M1FN, f6E3M2FN, f6E3M2FN, f8E4M3FN,
f8E5M2}, so listing out all the combinations would take up a lot of
space and produce a large number of nearly-identical cases in the
descriotor logic.

This inner tiled operation treats the k values that have distinct
scales and the "block" (the B (currently always 32) values that share
a common scale) as separate reduction dimensions. This means that the
scale operands, in effect, don't have a block dimension. This also
means that models where the block size is larger than 32 can be
supported by tiling across the scale dimension.

The following work is not yet implemented:
- ContretizeMmaShapes has not yet been generalized to arbitrary inner
tiled ops. This will be needed to correctly lower the 32x32x64
intrinsic, which has an outer dimension on the accumulator.
- Lowering configuration logic and PackToIntrinsics have not been
addressed

Note that this PR does contain a few code changes. One is declaring
the vector unrolling logic to be safe for non-MMA inner_tiled ops. The
other is a bug fix to basisFromSizesStrides(), which used an incorrect
tiebreaking strategy for multiple dimensions that had the same
stride (now it's ties broken by the length of the dimension and _then_
its position in the list).
@krzysz00

Copy link
Copy Markdown
Contributor Author

Superscedes #21110 so I can stack on top of it

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

Cool, this looks good. Much easier review than I expected, thanks for all the tests + comments

for (auto [set, map] : llvm::zip_equal(resExprs, maps)) {
set.insert_range(map.getResults());
}
// Note: the below conditions are a best guess and may be too strict.

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.

Just checking isProjectedPermutation should be good enough, I don't think mixing scale dimensions with other operands will break anything (although it would be weird).

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 figured a bit of paranoia never hurt anyone?

@krzysz00
krzysz00 merged commit a46fe4b into main Jun 25, 2025
@krzysz00
krzysz00 deleted the users/krzysz00/scaling-mfma-desc branch June 25, 2025 05:19
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
This commit adds a descriptor attribute - #iree_gpu.scaled_mma_layout -
for MMAs that takes "scale" operands and tests for its lowering from
DistributeInnerTiledToLanes and afterwards.

This descriptor takes four input operands - the lleft-hand side of the
matrix, the LHS scales, the right-hand side, and the RHS scales, in that
order. The operation has one accumulator.

Scaled MMAs are, like MMA intrinsics, represented by an enum, which
litss out the available intrinsics (currenvtly, we only have AMD's
MI-350). However, unlike with MMA intrinsics, the input and output types
are given in the scaled_mma_layout attribute itself. This is because
these scaled MMAs support arbitrary pairs of left- and right-hand side
types from {f4E2M1FN, f6E3M2FN, f6E3M2FN, f8E4M3FN, f8E5M2}, so listing
out all the combinations would take up a lot of space and produce a
large number of nearly-identical cases in the descriotor logic.

This inner tiled operation treats the k values that have distinct scales
and the "block" (the B (currently always 32) values that share a common
scale) as separate reduction dimensions. This means that the scale
operands, in effect, don't have a block dimension. This also means that
models where the block size is larger than 32 can be supported by tiling
across the scale dimension.

The following work is not yet implemented:
- ContretizeMmaShapes has not yet been generalized to arbitrary inner
tiled ops. This will be needed to correctly lower the 32x32x64
intrinsic, which has an outer dimension on the accumulator.
- Lowering configuration logic and PackToIntrinsics have not been
addressed

Note that this PR does contain a few code changes. One is declaring the
vector unrolling logic to be safe for non-MMA inner_tiled ops. The other
is a bug fix to basisFromSizesStrides(), which used an incorrect
tiebreaking strategy for multiple dimensions that had the same stride
(now it's ties broken by the length of the dimension and _then_ its
position in the list).

---------

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.

2 participants