[LinalgExt] Add group_matmul and group_mmt4d ops (#24909) # What & why This PR introduces a new `group_matmul` operation, to be the IREE equivalent of Torch's [grouped_mm](https://docs.pytorch.org/docs/main/generated/torch.nn.functional.grouped_mm.html). It's purpose is to enable efficient inference of Mixture-of-Experts models. The op is superficially similar to `linalg.matmul` and `linalg.batch_matmul`, but cannot be expressed as a pure `linalg.generic` without masking inactive elements, so I believe it belongs here in LinalgExt. The description in LinalgExtOps.td gives more info. # How 1) We add the standard matmul encodings to LHS, RHS and result of our op in SetEncoding.cpp 2) We define an auxillary `group_mmt4d` op - the grouped equivalent of `mmt4d` 3) In MaterializeEncodingPatterns.cpp, we convert `group_matmul` to `group_mmt4d` 4) We implement the tiling interface for `group_mmt4d` 5) After workgroup tiling, we add a `DecomposeGroupMmt4DPass`, which turns `group_mmt4d` into `mmt4d` + some extra ops a) if all input rows of a tile belong to the same expert, we just slice + `mmt4d` b) otherwise, we loop over experts; for each of them we mask out rows that don't belong to it # Performance Preliminary benchmarks have shown speed close to a matmul of comparable shape. This is expected since: - decomposition of `group_mmt4d` happens after workgroup tiling, so the op turns into a single dispatch - there is no control flow on the host side - the number of tiles containing a boundary between experts is limited: (5a) is much more common than (5b) - only the weights of the active experts need are accessed - no wasted memory bandwidth # Known shortcomings / future work - only LLVMCPU backend is supported currently - dynamic tile sizes (RVV & SVE) not implemented - for compilation to succeed, you must have enabled: - data tiling - distribution tiling (on by default) - LHS and RHS must be floats - integer operands could result in an identity layout (not implemented) - the attribute placed on `group_mmt4d` to support the narrow-N transpose is an ugly hack - this will no longer be needed once we can lower to `inner_tiled` instead - both ops implemented here could possibly be unified into some kind of `group_contract` - by analogy to [linalg.contract](https://mlir.llvm.org/docs/Dialects/Linalg/#linalgcontract-linalgcontractop) - instead of passing in expert offsets, perhaps LHS should just be a proper [ragged tensor](https://iree.dev/reference/mlir-dialects/TensorExt/#iree_tensor_extcast_to_ragged_shape-tensorextcasttoraggedshapeop)? - this feature is still work-in-progress IIUC - gather/scatter should be fused with this op Assisted-by: Codex Signed-off-by: Maksymilian B. Knust <knust@roofline.ai>
IREE (Intermediate Representation Execution Environment, pronounced as “eerie”) is an MLIR-based end-to-end compiler and runtime that lowers Machine Learning (ML) models to a unified IR that scales up to meet the needs of the datacenter and down to satisfy the constraints and special considerations of mobile and edge deployments.
See our website for project details, user guides, and instructions on building from source.
Releases notes are published on GitHub releases.
| Package | Release status |
|---|---|
| GitHub release (stable) | |
| GitHub release (nightly) | |
iree-base-compiler | |
iree-base-runtime |
For more details on the release process, see https://iree.dev/developers/general/release-management/.
| Operating system | Build status |
|---|---|
| Linux | |
| macOS |
For the full list of workflows see https://iree.dev/developers/general/github-actions/.
See our website for more information.
Community meeting recordings: IREE YouTube channel
| Date | Title | Recording | Slides |
|---|---|---|---|
| 2025-06-10 | Data-Tiling in IREE: Achieving High Performance Through Compiler Design (AsiaLLVM) | recording | slides |
| 2025-05-17 | Introduction to GPU architecture and IREE's GPU CodeGen Pipeline | recording | slides |
| 2025-02-12 | The Long Tail of AI: SPIR-V in IREE and MLIR (Vulkanised) | recording | slides |
| 2024-10-01 | Unveiling the Inner Workings of IREE: An MLIR-Based Compiler for Diverse Hardware | recording | |
| 2021-06-09 | IREE Runtime Design Tech Talk | recording | slides |
| 2020-08-20 | IREE CodeGen (MLIR Open Design Meeting) | recording | slides |
| 2020-03-18 | Interactive HAL IR Walkthrough | recording | |
| 2020-01-31 | End-to-end MLIR Workflow in IREE (MLIR Open Design Meeting) | recording | slides |
IREE is licensed under the terms of the Apache 2.0 License with LLVM Exceptions. See LICENSE for more information.