Skip to content

[DT] Next steps for data tiling fusion improvements #18513

Open

Description

@Max191

Overview

This issue is a tracker for the next steps of enabling the data tiling fusion improvements, which are currently behind the --iree-dispatch-creation-experimental-data-tiling flag. Once the steps here are complete, there should be fairly reasonable support for general grouped quantized matmul operations through data tiling.

The main steps are as follows:

  1. Allow SetEncoding to set encodings on contraction ops that have greater than one batch, M, N, or K dimension.
  2. Support multi batch/M/N/K contractions in MaterializeEncoding.
  3. Make a lowering path for new data tiled multi batch/M/N/K contractions.
  4. Add a new matching pattern for UKernels to lower new data tiled multi batch/M/N/K to UKernel ops.

1. SetEncoding

This change should be very simple. SetEncoding should not behave any differently with multiple batch, M, N, or K dimensions. We just need to remove the restriction in the pass for single batch/M/N/K dimensions.

2. MaterializeEncoding

This is a more involved step, since materializing a multi batch/M/N/K dimension contraction cannot create an MMT4D op. Instead, we need to create a linalg.generic op with the appropriate iterator_types and indexing_maps. There are useful functions for checking if generic ops are contractions, and getting the appropriate dimension mapping to batch, M, N, and K dimensions here:

https://github.com/llvm/llvm-project/blob/030c6da7af826b641db005be925b20f956c3a6bb/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.h#L50-L71

Here is an example of how this is used to find the narrow M and N sizes of a matmul:

linalg::ContractionDimensions cDims =
linalg::inferContractionDims(linalgOp).value();
auto map = linalgOp.getIndexingMapsArray().back();
auto getOutputSizeAtDimPos = [&](unsigned dimPos) -> int64_t {
return outType.getDimSize(
map.getResultPosition(getAffineDimExpr(dimPos, linalgOp->getContext()))
.value());
};
// M or N can be empty instead of having an explicit dim size of 1 for matvec
// and vecmat, so set to 1 if empty.
int64_t M = cDims.m.empty() ? 1 : getOutputSizeAtDimPos(cDims.m[0]);
int64_t N = cDims.n.empty() ? 1 : getOutputSizeAtDimPos(cDims.n[0]);

I have written some IR for what I would expect the final transformation to look like below. Note that the only materialization pattern that should be needed is the one for the final contraction operation. The patterns for set_encoding, unset_encoding, and dequant ops should already be supported. I only included them in this example so it is clear where this all comes from.

module {
  func.func @grouped_quantized_matmul(%lhs: tensor<1024x32x128xf32>, %rhs_quant: tensor<11008x32x128xi4>,
                                      %scales: tensor<11008x32xf32>, %zps: tensor<11008x32xf32>) -> tensor<1024x11008xf32> {
    %cst = arith.constant 0.000000e+00 : f32
    %dq_init = tensor.empty() : tensor<11008x32x128xf32>
    %rhs = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>,
                         affine_map<(d0, d1, d2) -> (d0, d1)>,
                         affine_map<(d0, d1, d2) -> (d0, d1)>,
                         affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]}
        ins(%rhs_quant, %scales, %zps : tensor<11008x32x128xi4>, tensor<11008x32xf32>, tensor<11008x32xf32>) outs(%dq_init : tensor<11008x32x128xf32>) {
    ^bb0(%in: i4, %in_0: f32, %in_1: f32, %out: f32):
      %5 = arith.extui %in : i4 to i32
      %6 = arith.uitofp %5 : i32 to f32
      %7 = arith.subf %6, %in_1 : f32
      %8 = arith.mulf %7, %in_0 : f32
      linalg.yield %8 : f32
    } -> tensor<11008x32x128xf32>
    %empty = tensor.empty() : tensor<1024x11008xf32>
    %mm_init = linalg.fill ins(%cst : f32) outs(%empty : tensor<1024x11008xf32>) -> tensor<1024x11008xf32>
    %mm = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d2, d3)>,
                         affine_map<(d0, d1, d2, d3) -> (d1, d2, d3)>,
                         affine_map<(d0, d1, d2, d3) -> (d0, d1)>],
        iterator_types = ["parallel", "parallel", "reduction", "reduction"]}
        ins(%lhs, %rhs : tensor<1024x32x128xf32>, tensor<11008x32x128xf32>) outs(%mm_init : tensor<1024x11008xf32>) {
    ^bb0(%in: f32, %in_0: f32, %out: f32):
      %5 = arith.mulf %in, %in_0 : f32
      %6 = arith.addf %5, %out : f32
      linalg.yield %6 : f32
    } -> tensor<1024x11008xf32>
    return %mm : tensor<1024x11008xf32>
  }

  // ======================================================================== //
  // ======= @grouped_quantized_matmul after materializing encodings: ======= //
  // ======================================================================== //

  func.func @packed_grouped_quantized_matmul(%lhs: tensor<1024x32x128xf32>, %rhs_quant: tensor<11008x32x128xi4>,
                                      %scales: tensor<11008x32xf32>, %zps: tensor<11008x32xf32>) -> tensor<1024x11008xf32> {
    %cst = arith.constant 0.000000e+00 : f32
    %cst_i4 = arith.constant 0 : i4
    // LHS pack ( [M x K0 x K1] -> [M x K0 x K1 x m x k1] )
    %pack_lhs_init = tensor.empty() : tensor<256x32x8x4x16xf32>
    %pack_lhs = tensor.pack %lhs padding_value(%cst : f32)
                                 outer_dims_perm = [0, 1, 2]
                                 inner_dims_pos = [0, 2]
                                 inner_tiles = [4, 16] into %pack_lhs_init
        : tensor<1024x32x128xf32> -> tensor<256x32x8x4x16xf32>
    // Scales pack ( [N x K0] -> [N x K0 x n] )
    %pack_scales_init = tensor.empty() : tensor<1376x32x8xf32>
    %pack_scales = tensor.pack %scales padding_value(%cst : f32)
                                 outer_dims_perm = [0, 1]
                                 inner_dims_pos = [0]
                                 inner_tiles = [8] into %pack_scales_init
        : tensor<11008x32xf32> -> tensor<1376x32x8xf32>
    // Zero points pack ( [N x K0] -> [N x K0 x n] )
    %pack_zps_init = tensor.empty() : tensor<1376x32x8xf32>
    %pack_zps = tensor.pack %zps padding_value(%cst : f32)
                                 outer_dims_perm = [0, 1]
                                 inner_dims_pos = [0]
                                 inner_tiles = [8] into %pack_zps_init
        : tensor<11008x32xf32> -> tensor<1376x32x8xf32>
    // RHS quant pack ( [N x K0 x K1] -> [N x K0 x K1 x n x k1] )
    %pack_rhs_init = tensor.empty() : tensor<1376x32x8x8x16xi4>
    %pack_rhs_quant = tensor.pack %rhs_quant padding_value(%cst_i4 : i4)
                                 outer_dims_perm = [0, 1, 2]
                                 inner_dims_pos = [0, 2]
                                 inner_tiles = [8, 16] into %pack_rhs_init
        : tensor<11008x32x128xi4> -> tensor<1376x32x8x8x16xi4>
    %dq_init = tensor.empty() : tensor<1376x32x8x8x16xf32>
    %pack_rhs = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3, d4)>,
                         affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d3)>,
                         affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d3)>,
                         affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3, d4)>],
        iterator_types = ["parallel", "parallel", "parallel", "parallel", "parallel"]}
        ins(%pack_rhs_quant, %pack_scales, %pack_zps : tensor<1376x32x8x8x16xi4>, tensor<1376x32x8xf32>, tensor<1376x32x8xf32>)
        outs(%dq_init : tensor<1376x32x8x8x16xf32>) {
    ^bb0(%in: i4, %in_0: f32, %in_1: f32, %out: f32):
      %5 = arith.extui %in : i4 to i32
      %6 = arith.uitofp %5 : i32 to f32
      %7 = arith.subf %6, %in_1 : f32
      %8 = arith.mulf %7, %in_0 : f32
      linalg.yield %8 : f32
    } -> tensor<1376x32x8x8x16xf32>
    // Matmul init ( [M x N x m x n] )
    %empty = tensor.empty() : tensor<256x1376x4x8xf32>
    %mm_init = linalg.fill ins(%cst : f32) outs(%empty : tensor<256x1376x4x8xf32>) -> tensor<256x1376x4x8xf32>
    %mm = linalg.generic {
        indexing_maps = [affine_map<(M, N, K0, K1, m, n, k1) -> (M, K0, K1, m, k1)>,
                         affine_map<(M, N, K0, K1, m, n, k1) -> (N, K0, K1, n, k1)>,
                         affine_map<(M, N, K0, K1, m, n, k1) -> (M, N, m, n)>],
        iterator_types = ["parallel", "parallel", "reduction", "reduction", "parallel", "parallel", "reduction"]}
        ins(%pack_lhs, %pack_rhs : tensor<256x32x8x4x16xf32>, tensor<1376x32x8x8x16xf32>) outs(%mm_init : tensor<256x1376x4x8xf32>) {
    ^bb0(%in: f32, %in_0: f32, %out: f32):
      %5 = arith.mulf %in, %in_0 : f32
      %6 = arith.addf %5, %out : f32
      linalg.yield %6 : f32
    } -> tensor<256x1376x4x8xf32>
    %unpack_init = tensor.empty() : tensor<1024x11008xf32>
    %unpack = tensor.unpack %mm outer_dims_perm = [0, 1]
                                inner_dims_pos = [0, 1]
                                inner_tiles = [4, 8] into %unpack_init
        : tensor<256x1376x4x8xf32> -> tensor<1024x11008xf32>
    return %unpack : tensor<1024x11008xf32>
  }
}

3. Lowering Path

The next thing we need is a lowering path for the resulting data tiled matmul:

%mm = linalg.generic {
    indexing_maps = [affine_map<(M, N, K0, K1, m, n, k1) -> (M, K0, K1, m, k1)>,
                     affine_map<(M, N, K0, K1, m, n, k1) -> (N, K0, K1, n, k1)>,
                     affine_map<(M, N, K0, K1, m, n, k1) -> (M, N, m, n)>],
    iterator_types = ["parallel", "parallel", "reduction", "reduction", "parallel", "parallel", "reduction"]}
    ins(%pack_lhs, %pack_rhs : tensor<256x32x8x4x16xf32>, tensor<1376x32x8x8x16xf32>) outs(%mm_init : tensor<256x1376x4x8xf32>) {
^bb0(%in: f32, %in_0: f32, %out: f32):
  %5 = arith.mulf %in, %in_0 : f32
  %6 = arith.addf %5, %out : f32
  linalg.yield %6 : f32
} -> tensor<256x1376x4x8xf32>

This primarily means setting an appropriate lowering configuration for the op, but there will likely be issues to solve in whatever lowering pipeline this goes down. There are a few changes to be made for lowering configurations:

  1. Right now, generic op contractions will get a default GenericOp configuration:
    /// Sets the lowering configuration for a generic op to use
    /// CPUDoubleTilingExpert pipeline.
    static LogicalResult
    setRootConfig(mlir::FunctionOpInterface entryPointFn,
    linalg::GenericOp genericOp, const LinalgOpInfo &linalgOpInfo,
    const TargetMLTransformInfo &targetMLTransInfo) {

    Even when the contraction is a generic op, it should be handled as a contraction op. This means we need some new logic to handle linalg ops for which linalg::isaContractionOpInterface returns true. There is something similar done in GPU configurations:
    //====---------------------------------------------------------------------===//
    // Contraction Pipeline Configuration
    //====---------------------------------------------------------------------===//
    static LogicalResult setContractConfig(IREE::GPU::TargetAttr target,
    mlir::FunctionOpInterface entryPoint,
    linalg::LinalgOp op) {
    if (!linalg::isaContractionOpInterface(op) || op.getNumParallelLoops() < 2) {
    return failure();
    }

    In general, I think contraction ops with multiple M, N or K dimensions should have all outer M/N/K dimensions tiled to 1 as an initial strategy.
  2. Once there is some logic in place to set configurations based on contraction dimensions, we should refactor the existing matmul-like op config logic to use the new logic. This is more of a code clean-up task, so everything is in one place.
  3. The pipeline to use will be the new TileRootFuseConsumerProducer pipeline. There may be some issues along the way to solve as more dispatches are tested on the new pipeline.

Lower to UKernels

UKernels are rooted on MMT4D ops right now, and we don't want to lose the benefits of ukernels when we materialized generic ops instead of mmt4d. The CPULowerToUKernels pass converts a set of named operations into UKernel ops based on some matching patterns called here:

FailureOr<IREE::Codegen::UKernelOpInterface> ukernelOp =
matchDAGForUKernel(rewriter, op, skipIntermediateRoundings);

A new matchDAGForUKernel matcher will be needed for linalg.generic ops. The new matcher will need to check the shapes of the generic op inputs against the supported ukernel shapes, since it is not guaranteed that an arbitrary generic op will match the shape of a ukernel. Would be good to get @bjacob's input on this, since matching against all possible ukernel shapes seems a little cumbersome.

EDIT: See Benoit's comment below. There should be no need to match against specific shapes for now. Worst case will be bad performance, but that can be something to look into later.

Another detail is that we will most likely want to do some unit dim folding after tiling to get rid of outer unit dimensions on the contractions. This will make matching ukernels much more simple.

Final Comments

The sequence of steps in the overview are listed in some order, but that is not a strict dependency order. Particularly, steps 2. and 3. could be done in either order, since I have provided some example IR after materialization. Doing 3. first could provide some lessons for the particular form we want as a result of 2. in general. Although 3. is dependent on 2. for e2e enablement, they could be worked on in parallel.

Activity

  1. bjacob commented on Sep 12, 2024

    @bjacob
    Collaborator

    Regarding matching generic to mmt4d ukernels:

    This can be viewed as 2 separate steps:

    1. Match linalg.generic to linalg.mmt4d. This is the new code that needs to be written. It can be a separate pass or pattern, independent of ukernels.
    2. From there, the existing CPULowerToUkernels pass can run, unmodified.

    So all you need to do here is that step 1.

    There is a caveat about what "support" means. The mmt4d ukernel supports arbitrary linalg.mmt4d ops shapes, for the supported combinations of element types, in the sense that the lowering always succeeds and there is always an actual ukernel to handle any case. The caveat is about performance: that wide support is achieved thanks to a slow fallback (which is slower than any generic codegen), so there is a performance cliff here, that one can silently fall off of.

    The worst-case scenario then is that implementing the linalg.generic to linalg.mmt4d rewrite causes a workload to hit the slow ukernel path. However, that problem preexists with workloads that use linalg.mmt4d ops. The reason why we haven't prioritized solving it is a combination of: it's not been such a big deal (because we have added ukernel fast paths as needed) and it's not trivial to fix because this requires enlarging the contact surface between ukernels and the compiler. However, if we are willing to do that, then the solution is a simple enough PR: #16880 . GitHub shows an outdated diff base for that PR, but most of that diff is already in main as #16879 was merged: we have already structured the ukernels code to have a declarative table enumerating fast code paths, so what is left to do in that PR is a simple matter of exposing that to the compiler.

  2. Max191 commented on Sep 12, 2024

    @Max191
    ContributorAuthor

    There is a caveat about what "support" means. The mmt4d ukernel supports arbitrary linalg.mmt4d ops shapes, for the supported combinations of element types, in the sense that the lowering always succeeds and there is always an actual ukernel to handle any case. The caveat is about performance: that wide support is achieved thanks to a slow fallback (which is slower than any generic codegen), so there is a performance cliff here, that one can silently fall off of.

    I see, thanks for the context. I think it makes sense to start by unconditionally converting linalg.generic to mmt4d. It would be best to complicate the current next steps as little as we can. We can leave it as a potential TODO in case we hit any future performance pitfalls in the wild.

  3. noumanamir-10xe commented on Nov 13, 2025

    @noumanamir-10xe
    Contributor

    @bjacob @Max191 I am working on lowering a grouped quantized matmul to ukernel in a downstream project that uses IREE. In my case, I have the activations (lhs) dynamically quantized too. The MLIR is given below:

    !activation_type = tensor<1x?x64x32xi8>
    !activation_scale_type = tensor<1x?x64x1xf16>
    !weight_type = tensor<8192x64x32xi8>
    !weight_scale_type = tensor<8192x64x1xf16>
    !result_type = tensor<1x?x8192xf16>
    
    #map = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d3, d4)>
    #map1 = affine_map<(d0, d1, d2, d3, d4) -> (d2, d3, d4)>
    #map2 = affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3)>
    #map3 = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
    #map4 = affine_map<(d0, d1, d2, d3) -> (d0, d1, d3, 0)>
    #map5 = affine_map<(d0, d1, d2, d3) -> (d2, d3, 0)>
    #map6 = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2)>
    #map7 = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
    module {
      util.func public @q8_0(%arg0: !activation_type, %arg1: !activation_scale_type, %arg2: !weight_type, %arg3: !weight_scale_type) -> !result_type {
        %c0 = arith.constant 0 : index
        %c1 = arith.constant 1 : index
        %c2 = arith.constant 2 : index
        %c3 = arith.constant 3 : index
        %dim = tensor.dim %arg0, %c0 : !activation_type
        %dim_0 = tensor.dim %arg0, %c1 : !activation_type
        %dim_1 = tensor.dim %arg0, %c2 : !activation_type
        %dim_2 = tensor.dim %arg0, %c3 : !activation_type
        %dim_3 = tensor.dim %arg2, %c0 : !weight_type
        %0 = tensor.empty(%dim, %dim_0, %dim_3, %dim_1) : tensor<?x?x?x?xi32>
        %c0_i32 = arith.constant 0 : i32
        z%1 = linalg.fill ins(%c0_i32 : i32) outs(%0 : tensor<?x?x?x?xi32>) -> tensor<?x?x?x?xi32>
        %2 = linalg.generic {indexing_maps = [#map, #map1, #map2], iterator_types = ["parallel", "parallel", "parallel", "parallel", "reduction"]} ins(%arg0, %arg2 : !activation_type, !weight_type) outs(%1 : tensor<?x?x?x?xi32>) {
        ^bb0(%in: i8, %in_4: i8, %out: i32):
          %8 = arith.extsi %in : i8 to i32
          %9 = arith.extsi %in_4 : i8 to i32
          %10 = arith.muli %8, %9 : i32
          %11 = arith.addi %out, %10 : i32
          linalg.yield %11 : i32
        } -> tensor<?x?x?x?xi32>
        %3 = tensor.empty(%dim, %dim_0, %dim_3) : tensor<?x?x?xf32>
        %cst = arith.constant 0.000000e+00 : f32
        %4 = linalg.fill ins(%cst : f32) outs(%3 : tensor<?x?x?xf32>) -> tensor<?x?x?xf32>
        %5 = linalg.generic {indexing_maps = [#map3, #map4, #map5, #map6], iterator_types = ["parallel", "parallel", "parallel", "reduction"]} ins(%2, %arg1, %arg3 : tensor<?x?x?x?xi32>, !activation_scale_type, !weight_scale_type) outs(%4 : tensor<?x?x?xf32>) {
        ^bb0(%in: i32, %in_4: f16, %in_5: f16, %out: f32):
          %8 = arith.mulf %in_4, %in_5 : f16
          %9 = arith.extf %8 : f16 to f32
          %10 = arith.sitofp %in : i32 to f32
          %11 = arith.mulf %9, %10 : f32
          %12 = arith.addf %out, %11 : f32
          linalg.yield %12 : f32
        } -> tensor<?x?x?xf32>
        %6 = tensor.empty(%dim, %dim_0, %dim_3) : tensor<?x?x?xf16>
        %7 = linalg.generic {indexing_maps = [#map7, #map7], iterator_types = ["parallel", "parallel", "parallel"]} ins(%5 : tensor<?x?x?xf32>) outs(%6 : tensor<?x?x?xf16>) {
        ^bb0(%in: f32, %out: f16):
          %8 = arith.truncf %in : f32 to f16
          linalg.yield %8 : f16
        } -> tensor<?x?x?xf16>
        %cast = tensor.cast %7 : tensor<?x?x?xf16> to !result_type
        util.return %cast : !result_type
      }
    }

    In this case, the two generics will end up in separate dispatches. The first one ends up becoming linalg.mmt4d and gets lowered to ukernel. The second one lowers to reduction codegen. My goal is to lower both of these to a single ukernel so that I can have the mutiplication by scales right after I do a single block's reduction and avoid the materialization of the intermediate tensor as shown in the above MLIR. The structure I would have is similar to described above by @Max191 , pack of lhs, pack of lhs_scales, pack of rhs, and pack of rhs_scales. My question is that in order to avoid two separate dispatches what should I do? Or is there a better way to tackle this problem.

  4. bjacob commented on Nov 17, 2025

    @bjacob
    Collaborator

    Discussed on the phone. Summary of your options:

    1. Feel free to go ahead and write your own microkernel matching the fused generic, particularly as it sounded like you did NOT need that microkernel to live upstream. I can't guarantee that we would take such an ad-hoc ukernel in the IREE tree.
    2. Another near-term approach is to just ensure that the two generics go into the same dispatch, but don't try to fuse them into a single generic. Let the two generics go to their own microkernel each. The producer microkernel would still store its output-tile to memory, and then the consumer microkernel would reload that tile from memory. Then late-IR optimizations should perform store-to-load forwarding --- as long as this is all within the same dispatch, and that intermediate tile size is static-shape and small enough for loops to be unrolled. This could give you the same codegen as a custom fused-kernel, without having to write one.
    3. In the longer-term, we need to carry over to the CPU side recent codegen/ukernel improvements from the GPU side. Multiple things would help here. MLIR ukernels would help by reconciling fusions and ukernels, removing the need for a custom ukernel for the fusion. But the thing that might have the biggest impact would be to carry over DataTiledMMAAttr to the CPU side, as discussed on Discord today, as that could remove the need for ukernels altogether: https://discord.com/channels/689900678990135345/1254843174111678555/1440011047380586506
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions