You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Collapse MaterializeEncodingIntoPadding into the generic pass #20160
@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?
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:
We should add convertType to the SerializableEncodingAttrInterface. Because only the serialized encoding attribute knows what the final types are.
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.
IMO, the serialized encoding should be able to generate the ops that bring the global layout to the preferred layout and vice versa.
The tensor.empty materialization pattern could use above interface methods, because it only cares the final type and transformed dynamic dimensions.
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.
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.
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:
The MaterializeEncodingTypeConverter will call this interface method to specialize the type conversion for different encoding attributes. This can look like:
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.
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:
Move LayoutAttrInterface from Codegen to Encoding to avoid a dependency in Encoding on Codegen when introducing the new interface methods above.
Activity
hanhanW commentedon Mar 4, 2025
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.
[Codegen] Teach MaterializeEncodingIntoPadding about resolved layouts.
[Codegen] Teach MaterializeEncodingIntoPadding about resolved layouts.
hanhanW commentedon Mar 27, 2025
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
MaterializeEncodingInfoat all in the padding approach. We also don't need thegetPackedDimsFor*methods exposed to patterns. They are all data-tiling specific.What needs to be fixed in my mind are:
convertTypeto theSerializableEncodingAttrInterface. Because only the serialized encoding attribute knows what the final types are.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.tensor.emptymaterialization pattern could use above interface methods, because it only cares the final type and transformed dynamic dimensions.lowerOpimplementation. 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.
[Codegen] Teach MaterializeEncodingIntoPadding about resolved layouts. (
hanhanW commentedon Apr 15, 2025
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.
[Codegen] Use GPUPadLayoutAttr to resolve layouts. (#20565)
Migrate Flow dependencies in Stream/Codegen to TensorExt (#20564)
[TensorExt][NFC] Remove dependency on Encoding through ExternalModel (#…
jtuyls commentedon May 1, 2025
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
SerializableEncodingAttrInterfacethat 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 theDispatchTensorLoadOpandDispatchTensorStoreOpconvertType
The
SerializableEncodingAttrInterfacewill 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
MaterializePadEncodingTypeConverterand combines all type conversion intoMaterializeEncodingTypeConverter.For example, based on the current implementation in
MaterializeEncodingIntoPaddingforPadEncodingLayoutAttr, this will look like:The
MaterializeEncodingTypeConverterwill call this interface method to specialize the type conversion for different encoding attributes. This can look like:getOffsetsSizesStrides
The implementations for materializing
DispatchTensorLoadOpandDispatchTensorStoreOpforCPUEncodingLayoutAttr/GPUEncodingLayoutAttrandPadEncodingLayoutAttrare quite similar except for the offsets, sizes, strides and dynamic dims the newDispatchTensorLoadOpandDispatchTensorStoreOpare created with. ThegetOffsetsSizesStridesinterface method can compute these differently for every encoding based on the type and dynamic dimensions:This should work for all patterns that convert
DispatchTensorLoadOpandDispatchTensorStoreOpinto a materialized version of the same op. However,PadEncodingLayoutAttrneeds an additionaltensor::InsertSliceOpto be inserted forDispatchTensorStoreOp and an additionaltensor::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:
convertTypeto SerializableEncodingAttrInterface: [Encoding] Add convertType interface to generalize type conversion #20700getOffsetsSizesStrides: [Encoding] Add getOffsetSizesStrides interface for load/store materialization #20741As 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:
convertTypeto LayoutAttrInterfacegetOffsetsSizesStridesorgetMaterializedDynamicDims(if we wouldn't need getOffsetsSizesStrides)17 remaining items
[DT] Implement MaterializeInterfaceBindingEncoding with interface met…
[DT] Collapse MaterializeEncodingIntoPaddingPass into the generic pas…
[DT][NFCI] Implement getOffsetsSizesStrides for GPU padding resolver. (…
[DT][NFC] Collapse MaterializeScaledContractionOp into generic patter…
[DT][NFC] Refactor linalg.fill/generic op lowering to interface imple…
[DT] Implement MaterializeInterfaceBindingEncoding with interface met…
[DT] Collapse MaterializeEncodingIntoPaddingPass into the generic pas…
[Codegen] Teach MaterializeEncodingIntoPadding about resolved layouts. (
[Codegen] Use GPUPadLayoutAttr to resolve layouts. (#20565)
Migrate Flow dependencies in Stream/Codegen to TensorExt (#20564)
[TensorExt][NFC] Remove dependency on Encoding through ExternalModel (#…
[Encoding] Add convertType interface to generalize type conversion (#…
[NFC][Encoding] Move materializeEncodingValueFn to type converter (#2…
[Encoding] Add getOffsetSizesStrides interface for load/store materia…
[Codegen] Simplify tensor load/store padding materialization (#21160)