Skip to content

Collapse MaterializeEncodingIntoPadding into the generic pass #20160

Closed

Description

@hanhanW

@kuhar has been developing the GPU padding approach with the data-tiling flow. We branched a MaterializeEncodingIntoPadding pass that cooks the padding logic separately.

Now we observe that the divergence is about how we lower flow.dispatch.tensor load/store ops, see patterns for details. I think we should be able to collapse the patterns if we move pad/pack/swizzle logic to attribute interface methods. Because the whole point is about getting correct shape type and dispatch the index/offset/strides.

@pashu123 can you pick this up and expand the subtasks in the issue?

Activity

  1. added
    codegen
    on Mar 4, 2025
  2. hanhanW commented on Mar 4, 2025

    @hanhanW
    ContributorAuthor

    An additional work is that we need to get the encoding resolver attribute when it shows up in the configuration. This is missing in the generic pass.

  3. hanhanW commented on Mar 27, 2025

    @hanhanW
    ContributorAuthor

    There are several divergences between MaterializeEncoding pass and MaterializeEncodingIntoPadding pass. There are too many data-tiling specific logic exposed to patterns and type converter. E.g., we do not care MaterializeEncodingInfo at all in the padding approach. We also don't need the getPackedDimsFor* methods exposed to patterns. They are all data-tiling specific.

    What needs to be fixed in my mind are:

    1. We should add convertType to the SerializableEncodingAttrInterface. Because only the serialized encoding attribute knows what the final types are.
    2. We need an interface method (e.g., getOffsetsSizesStrides(), which takes the dynamic dimension sizes, to generate the ranges for load/store. I recently learned that the binding types and the final compute types are different on padding approach. [Codegen] Teach MaterializeEncodingIntoPadding about resolved layouts. #20393 is an example about why we need the interface method.
    3. IMO, the serialized encoding should be able to generate the ops that bring the global layout to the preferred layout and vice versa.
    4. The tensor.empty materialization pattern could use above interface methods, because it only cares the final type and transformed dynamic dimensions.
    5. The other computation op lowering (e.g., elementwise ops) should also be moved to lowerOp implementation. Or we need to add an interface method to transform indexing maps, like those dynamic dimension sizes.

    Where should the interface methods be introduced is not clear to me at the moment. I need to think more about the difference between encoding types and encoding resolver. I've been thinking it today, and now I think encoding resolver is a superset of encoding types. Because the encoding resolver can be attached to the encoding types (like EncodingAttr), and they are also used to serialize the encoding types. I'll think more about it. I think the above points are what we're looking for in the work.

  4. hanhanW commented on Apr 15, 2025

    @hanhanW
    ContributorAuthor

    The other computation op lowering (e.g., elementwise ops) should also be moved to lowerOp implementation. Or we need to add an interface method to transform indexing maps, like those dynamic dimension sizes.

    I'm looking at supporting multiple reduction dimensions cases for padding approach, and now I realize that this is a right direction to go again because we'll need to collapse reduction dimensions and then pad the collapsed dimension. I'll try to create an EPIC issue for the multi-reduction support.

    EDIT: multi-reduction is no longer needed because we will solve the problem in a different approach.

  5. jtuyls commented on May 1, 2025

    @jtuyls
    Contributor

    RFC: Collapse MaterializeEncodingIntoPadding into MaterializeEncoding

    Adding more detailed information for the above.

    Rationale

    The MaterializeEncoding pass should ideally be be generic to support different encoding types with minimal changes and without custom transformations/passes. The current encodings encountered perform some kind of reshaping, packing, swizzling and or padding. There is a lot of common logic for materializing these and it seems very doable to accommodate all existing variations and potential future versions as long as they perform some combination of these operations.

    Proposal

    Add two methods to the SerializableEncodingAttrInterface that encoding attributes should implement to specialize the materialization logic:

    • convertType: Given a type, this returns the materialized type for the encoding attribute.
    • getOffsetsSizesStrides: Given the dynamic dimension sizes, this generates the offsets, sizes and strides for the DispatchTensorLoadOp and DispatchTensorStoreOp

    convertType

    The SerializableEncodingAttrInterface will have a new interface method:

    Type convertype(type type);

    Different serializable encoding attributes will provide their own implementation that returns the materialized type. This avoids the need for MaterializePadEncodingTypeConverter and combines all type conversion into MaterializeEncodingTypeConverter.

    For example, based on the current implementation in MaterializeEncodingIntoPadding for PadEncodingLayoutAttr, this will look like:

    Type PadEncodingLayoutAttr::convertType(Type type) const {
      return TypeSwitch<mlir::Type, mlir::Type>(type)
          .Case<RankedTensorType>(
              [&](auto concreteType) { return concreteType.dropEncoding(); })
          .Case<IREE::TensorExt::DispatchTensorType>([&](auto concreteType) {
            auto rankedTensorType =
                dyn_cast<RankedTensorType>(concreteType.getBoundType());
            if (!rankedTensorType) {
              return concreteType;
            }
            rankedTensorType = getPaddedType(*this, rankedTensorType);
            return IREE::TensorExt::DispatchTensorType::get(
                concreteType.getAccess(), rankedTensorType);
          })
          .Default([&](auto concreteType) { return concreteType; });
    }

    The MaterializeEncodingTypeConverter will call this interface method to specialize the type conversion for different encoding attributes. This can look like:

    MaterializeEncodingTypeConverter::MaterializeEncodingTypeConverter(
        IREE::Codegen::LayoutAttrInterface layoutAttr)
        : layoutAttr(layoutAttr) {
      addConversion([](IntegerType intType) { return intType; });
      addConversion([](IndexType indexType) { return indexType; });
      addConversion([](FloatType floatType) { return floatType; });
      addConversion([](MemRefType memrefType) { return memrefType; });
      addConversion([=](RankedTensorType type) {
        SerializableEncodingAttrInterface serializableEncodingAttr =
            getSerializableEncodingAttr(getLayoutAttr(), type);
        if (serializableEncodingAttr) {
          return cast<RankedTensorType>(serializableEncodingAttr.convertType(type));
        }
        return type.dropEncoding();
      });
      addConversion([&](IREE::TensorExt::DispatchTensorType dispatchTensorType) {
        auto boundType =
            dyn_cast<RankedTensorType>(dispatchTensorType.getBoundType());
        if (!boundType || !boundType.getEncoding()) {
          return dispatchTensorType;
        }
        SerializableEncodingAttrInterface serializableEncodingAttr =
            getSerializableEncodingAttr(getLayoutAttr(), boundType);
        if (serializableEncodingAttr) {
          return cast<IREE::TensorExt::DispatchTensorType>(
              serializableEncodingAttr.convertType(dispatchTensorType));
        }
        Type convertedBoundType = convertType(boundType);
        return IREE::TensorExt::DispatchTensorType::get(
            dispatchTensorType.getAccess(), convertedBoundType);
      });
    }

    getOffsetsSizesStrides

    The implementations for materializing DispatchTensorLoadOp and DispatchTensorStoreOp for CPUEncodingLayoutAttr/GPUEncodingLayoutAttr and PadEncodingLayoutAttr are quite similar except for the offsets, sizes, strides and dynamic dims the new DispatchTensorLoadOp and DispatchTensorStoreOp are created with. The getOffsetsSizesStrides interface method can compute these differently for every encoding based on the type and dynamic dimensions:

    /// Returns success if materialized `newOffsets`, `newSizes` and
    /// `newStrides` can be calculated and set for the slice specified by
    /// `offsets`, `sizes` and `strides` on the dispatch tensor `type` with
    /// potential `dynamicDims` sizes.
    LogicalResult
    getOffsetsSizesStrides(OpBuilder &builder, Location loc,
                             IREE::TensorExt::DispatchTensorType type,
                             IREE::Codegen::LayoutAttrInterface layoutAttr,
                             ValueRange dynamicDims,
                             ArrayRef<OpFoldResult> offsets,
                             ArrayRef<OpFoldResult> sizes,
                             ArrayRef<OpFoldResult> strides,
                             SmallVectorImpl<OpFoldResult> &newOffsets,
                             SmallVectorImpl<OpFoldResult> &newSizes,
                             SmallVectorImpl<OpFoldResult> &newStrides);

    This should work for all patterns that convert DispatchTensorLoadOp and DispatchTensorStoreOp into a materialized version of the same op. However, PadEncodingLayoutAttr needs an additional tensor::InsertSliceOp to be inserted for DispatchTensorStoreOp and an additional tensor::ExtractSliceOpto be inserted forDispatchTensorLoadOp`. I think though that this can be derived by comparing the unmaterialized type with the materialized type and inserting slicing in case of padding. This seems quite general to potential future encodings as long as they perform some combination of reshaping, packing, swizzling and/or padding.

    Execution Plan

    Original plan:

    As we discussed and found out in #20741 (comment), the above methods really belong to the LayoutAttrInterface instead of SerializableEncodingAttrInterface as the former is responsible for materialization. Therefore, the plan has been adjusted to accomplish that as well:

  6. 17 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions