Repository navigation
[Encoding] Add getOffsetSizesStrides interface for load/store materialization - #20741
Conversation
c9c5ea4 to
de074b7
Compare
hanhanW
left a comment
There was a problem hiding this comment.
I have a strong concern about passing executable target to the interface method, see the other comment. I think the VMVX ukernel support has been a pain and I can help fix it first. Let me know what you think, thanks!
In general, the old offsets, sizes and strides should be passed as well, but the current materialization patterns assumes whole loads/stores (isLoadOfWholeSource, isStoreToWholeTarget). Imo this could be left to future work/a follow up.
We can decouple them a bit. The whole loads/stores are implementation details in the patterns. The interface provides the transformed sizes/strides/offsets. The patterns make the decision to bail out when it is not a full load/store, and how we define interface methods should not be guided by their implementation details. I don't have a strong opinion, and we can leave them to future work. Just sharing my thought about this comment.
Yeah, that's fair and the reason I didn't do it yet is that I would have to adjust the current implementations of the interface to support a partial load/stores and then I might just update the patterns as well to support it. And my thinking was to do a follow-up as it actually changes the implementation. |
I see your point, and it is fair to me. Thanks for your sharing! |
ce02d76 to
a242296
Compare
I did think more about this today and I think we can already add the old offsets, sizes and strides to the interface and then just return a failure in case of a partial access. I updated the PR for that, but I did leave the old checks around though for the default implementation (https://github.com/iree-org/iree/pull/20741/files#diff-9b275dec6a3f214c6e32c1d9ebd8b75a50ad5d84be23c9db090f5a1e8e667af4R195) as I would otherwise need to enable partial slices there and add tests for it and I would prefer to do that in a follow up. |
hanhanW
left a comment
There was a problem hiding this comment.
Thanks for the awesome work, and it helps me define the encodings that we are inventing!
| FailureOr<SmallVector<OpFoldResult>> getPackedDimsForDispatchTensor( | ||
| OpBuilder &builder, Location loc, | ||
| IREE::TensorExt::DispatchTensorType dispatchTensorType, | ||
| ValueRange dynamicDims) const; |
There was a problem hiding this comment.
I checked a couple of times, and I think it is not used at all?
I'd expect a cleanup in MaterializeInterfaceBindingEncoding, which calls getPackedDynamicDimsForDispatchTensor that uses a local getPackedDimsForDispatchTensor method. Is it possible to remove the local methods now?
EDIT: RE my second question, I think it is not possible at the moment, because sizes != dims. We'll need a different interface method to get rid of it.
There was a problem hiding this comment.
Yes, the idea was to use this method for the remaining use of getPackedDimsForDispatchTensor in MaterializeEncodingPatterns, so removed the local version of that one and getSwizzledShape. This seems perfectly possible, not sure what issue you were expecting with:
EDIT: RE my second question, I think it is not possible at the moment, because sizes != dims
There was a problem hiding this comment.
I probably misread the code previously, but it looks good to me now. Please add a doc to it.
It can be done in a follow-up. IMO, it deserves an interface method like getOffsetsSizesStrides. Maybe we can name it as getMaterializedDynamicDims(). (Any other suggestion is welcome.) We don't have a use case today, because padding only works for static shape. However, I can imagine that different kinds of relayout need something similar to this.
Furthermore, I think I will need it being an interface method for tracing encoding types for host, see the last paragraph in #20450 for more details. I think this is outside the scope of your PR, so let me pick this up when I start looking at the tracing support.
Moving it to the type converter looks okay to me, just wanna point out that we would make it be an interface method in the future. I'll follow up on this after you land the PR.
There was a problem hiding this comment.
I probably misread the code previously, but it looks good to me now. Please add a doc to it.
Done.
Moving it to the type converter looks okay to me, just wanna point out that we would make it be an interface method in the future. I'll follow up on this after you land the PR.
Yes, it doesn't have to stay within the type converter, but for now I think it's an improvement to make the functions reusable with relatively clean interfaces (no need to pass the type converter to it, no need to access/pass the layout attr around as it's already a member in the type converter).
I can see it being useful as an interface as well as it's quite general and it could be implemented for padding. I wouldn't have gone for it as long as there was only a single use for it or if it was only being used within getOffsetsSizesStrides, but if you see a need for tracing support as well that makes sense. One thing I am wondering though is whether we can generally implement getOffsetsSizesStrides with getMaterializedDynamicDims because then we potentially only need the latter as an interface.
There was a problem hiding this comment.
One thing I am wondering though is whether we can generally implement getOffsetsSizesStrides with getMaterializedDynamicDims because then we potentially only need the latter as an interface.
I think they may be different. The getOffsetsSizesStrides is for materializing a slice, and getMaterializedDynamicDims is for materializing the whole tensor. I'd say that getMaterializedDynamicDims is a syntax sugar version of the other one. Having both looks okay to me atm. Let's see after I have tracing prototype support. :)
| ":EncodingInterfacesGen", | ||
| ":EncodingOpsIncGen", | ||
| ":EncodingTypesGen", | ||
| "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect", |
There was a problem hiding this comment.
This is something we want to avoid in terms of dependency. The rule is Util/Encoding/TensorExt should not depend on other IREE dialects. I see why this happens, and I think we probably want to move the interface to Encoding dialect.
I started the work from Codegen and I wanted to have a clear line between codegen and host. This is why I introduced the interface in codegen dialect.
IMO, it is reasonable to move the interface to Encoding because we still have codegen specific implemented in compiler/Codegen/ExternalInterfaces. And now I also feel that the new interface method can belong to LayoutAttrInterface after we move the interface to the encoding dialect.
Let me share more about my thinking about the interfaces here, and I'll also update the issue #20742
- EncodingLayoutResolverAttrInterface is an interface that help resolve verbose encodings into serialized/specialized encodings.
- SerializableEncodingAttrInterface is an interface that describes encoding properties and should have enough information for later transformation.
- LayoutAttrInterface is an interface that provides a set of interface method to materialize serialized encoding to physical operations/types/etc.
With the definition, there are three states for an encoding. (a) Verbose encoding -> (b) serialized encoding -> (c) physical operations and types.
EncodingLayoutResolverAttrInterface helps (a) -> (b).
SerializableEncodingAttrInterface describes the properties for (b).
LayoutAttrInterface helps (b) -> (c).
Does it sound reasonable to you? What do you think? (more eyes are better, and I'd appreciate your feedback!)
(I was a little struggle when I wrote the issue. I could not figure this out until you connect these together. I'm sorry about that and I really appreciate your work!)
@benvanik we might add this dependency temporarily, and we can drop it soon. I've been fighting with better definition for encodings and the work helps a lot. If you have a concern about this temporary dep, either @jtuyls or I can move the interface first. This work is mainly to put padding encoding to a stable state.
There was a problem hiding this comment.
(Reason that I'm not moving the interface now is that I want to have the better definition first. I'm pretty sure that we can drop the dep in the future. If I really fail on it, it can always be Attribute and we drop the dep)
There was a problem hiding this comment.
This is something we want to avoid in terms of dependency. The rule is Util/Encoding/TensorExt should not depend on other IREE dialects. I see why this happens, and I think we probably want to move the interface to Encoding dialect.
Yes, 100%! I was thinking the same when I added it and I was going to propose moving the interface after I had a deeper look. Should have left a note on it.
With the definition, there are three states for an encoding. (a) Verbose encoding -> (b) serialized encoding -> (c) physical operations and types.
EncodingLayoutResolverAttrInterface helps (a) -> (b).
SerializableEncodingAttrInterface describes the properties for (b).
LayoutAttrInterface helps (b) -> (c).
Does it sound reasonable to you? What do you think? (more eyes are better, and I'd appreciate your feedback!)
Yes, I think this makes a lot of sense! There are two 'smells' related to this LayoutAttrInterface parameter in the interface method. One is that it introduces this dependency and the other is that LayoutAttrInterface might be the better home for this method as then LayoutAttrInterface doesn't need to be passed. The above description of the relation between the three interfaces clears up a lot and following that we should move getOffsetsSizesStrides and convertType to that interface. Then it might not be technically necessary to move LayoutAttrInterface to Encoding to avoid the dependency, but I feel like it would still be more logical to have the three interfaces within the same space.
There was a problem hiding this comment.
One is that it introduces this dependency and the other is that LayoutAttrInterface might be the better home for this method as then LayoutAttrInterface doesn't need to be passed.
This was something in my mind, and I forgot to note in the comment. Good catch! :)
Okay, I think we have a solid definition for the interfaces and encodings. What is the execution plan? Do you want to land the PR first, or move the interface to the Encoding dialect first? I don't have a strong opinion because now I can see a path to the finial picture we had discussed.
There was a problem hiding this comment.
Ok, I am currently thinking the following:
- Move
LayoutAttrInterface - Move
convertTypetoLayoutAttrInterface - (This PR) Add
getOffsetsSizesStridesorgetMaterializedDynamicDims(if we wouldn't needgetOffsetsSizesStrides) - Enable partial slices as a follow up (added for completeness)
If there is a need to get in this work earlier, we can start with merging this, but any dependent work might be impacted anyway until 1-3 is done as interface methods would be moved around.
There was a problem hiding this comment.
SGTM, I think we don't have other dependent work. Just in case if you want to land this, since you already made significant improvements in the PR. Thanks for keeping the codebase state clean and better!
a242296 to
be8cb22
Compare
hanhanW
left a comment
There was a problem hiding this comment.
Looks good to me, and I enjoy the discussion in the review! Thanks for making it happen, how we layer encodings is going to be awesome!
Just a couple of nits, and a question about the execution plan. I think it is very close to land.
| ":EncodingInterfacesGen", | ||
| ":EncodingOpsIncGen", | ||
| ":EncodingTypesGen", | ||
| "//compiler/src/iree/compiler/Codegen/Dialect/Codegen/IR:IREECodegenDialect", |
There was a problem hiding this comment.
One is that it introduces this dependency and the other is that LayoutAttrInterface might be the better home for this method as then LayoutAttrInterface doesn't need to be passed.
This was something in my mind, and I forgot to note in the comment. Good catch! :)
Okay, I think we have a solid definition for the interfaces and encodings. What is the execution plan? Do you want to land the PR first, or move the interface to the Encoding dialect first? I don't have a strong opinion because now I can see a path to the finial picture we had discussed.
| FailureOr<SmallVector<OpFoldResult>> getPackedDimsForDispatchTensor( | ||
| OpBuilder &builder, Location loc, | ||
| IREE::TensorExt::DispatchTensorType dispatchTensorType, | ||
| ValueRange dynamicDims) const; |
There was a problem hiding this comment.
I probably misread the code previously, but it looks good to me now. Please add a doc to it.
It can be done in a follow-up. IMO, it deserves an interface method like getOffsetsSizesStrides. Maybe we can name it as getMaterializedDynamicDims(). (Any other suggestion is welcome.) We don't have a use case today, because padding only works for static shape. However, I can imagine that different kinds of relayout need something similar to this.
Furthermore, I think I will need it being an interface method for tracing encoding types for host, see the last paragraph in #20450 for more details. I think this is outside the scope of your PR, so let me pick this up when I start looking at the tracing support.
Moving it to the type converter looks okay to me, just wanna point out that we would make it be an interface method in the future. I'll follow up on this after you land the PR.
be8cb22 to
8983b2a
Compare
As discussed here: #20741 (comment), the `LayoutAttrInterface` belongs in the Encoding and should contain the interface methods for materializing encodings. This PR moves the `LayoutAttrInterface` to the Encoding dialect. Other follow-up PRs will take care of moving `convertType` and `getOffsetsSizesStrides` to it. --------- Signed-off-by: Jorn Tuyls <[email protected]>
The `convertType` interface method, which converts a type into its materialized form, belongs in the Encoding's `LayoutAttrInterface` instead of the `SerializableEncodingAttrInterface` as the former is responsible for materializing encodings, while the latter describes properties of serialized encodings, see #20741 (comment). Signed-off-by: Jorn Tuyls <[email protected]>
…lization Signed-off-by: Jorn Tuyls <[email protected]>
8983b2a to
99bead3
Compare
|
@hanhanW I rebased this on main and moved the |
| FailureOr<SmallVector<OpFoldResult>> getPackedDimsForDispatchTensor( | ||
| OpBuilder &builder, Location loc, | ||
| IREE::TensorExt::DispatchTensorType dispatchTensorType, | ||
| ValueRange dynamicDims) const; |
There was a problem hiding this comment.
One thing I am wondering though is whether we can generally implement getOffsetsSizesStrides with getMaterializedDynamicDims because then we potentially only need the latter as an interface.
I think they may be different. The getOffsetsSizesStrides is for materializing a slice, and getMaterializedDynamicDims is for materializing the whole tensor. I'd say that getMaterializedDynamicDims is a syntax sugar version of the other one. Having both looks okay to me atm. Let's see after I have tracing prototype support. :)
As discussed here: #20741 (comment), the `LayoutAttrInterface` belongs in the Encoding and should contain the interface methods for materializing encodings. This PR moves the `LayoutAttrInterface` to the Encoding dialect. Other follow-up PRs will take care of moving `convertType` and `getOffsetsSizesStrides` to it. --------- Signed-off-by: Jorn Tuyls <[email protected]>
The `convertType` interface method, which converts a type into its materialized form, belongs in the Encoding's `LayoutAttrInterface` instead of the `SerializableEncodingAttrInterface` as the former is responsible for materializing encodings, while the latter describes properties of serialized encodings, see #20741 (comment). Signed-off-by: Jorn Tuyls <[email protected]>
…lization (#20741) This PR adds the `getOffsetSizesStrides` interface method as part of the effort to collapse MaterializeEncodingIntoPadding into MaterializeEncoding: #20160. This should enable the collapsing of the padding materialization patterns for `dispatch.tensor.load` and `dispatch.tensor.store` into the generic one, however, this is intentionally left out of the PR as the MaterializeEncodingIntoPadding pass is being adjusted for dynamic padding. The declaration looks like this: ```cpp /// 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); ``` Two notes on this: - The old offsets, sizes and strides are passed, but internally the implementations check for and only implement whole slices. Support for partial slices if left for future work to avoid changing the functionality (and adding corresponding tests) in this PR. - The VMVX ukernel information is retrieved from the layoutAttr (IREE::Encoding::LayoutAttrInterface). Signed-off-by: Jorn Tuyls <[email protected]>
This PR adds the
getOffsetSizesStridesinterface method as part of the effort to collapse MaterializeEncodingIntoPadding into MaterializeEncoding: #20160.This should enable the collapsing of the padding materialization patterns for
dispatch.tensor.loadanddispatch.tensor.storeinto the generic one, however, this is intentionally left out of the PR as @MaheshRavishankar is looking at adjusting the MaterializeEncodingIntoPadding pass for dynamic padding.The declaration looks like this:
Two notes on this:
In general, the old offsets, sizes and strides should be passed as well, but the current materialization patterns assumes whole loads/stores (The old offsets, sizes and strides are passed, but internally the implementations check for and only implement whole slices. Support for partial slices if left for future work to avoid changing the functionality (and adding corresponding tests) in this PR.isLoadOfWholeSource,isStoreToWholeTarget). Imo this could be left to future work/a follow up.TheThe VMVX ukernel information is retrieved from the layoutAttr (IREE::Codegen::LayoutAttrInterface).targetAttris passed as anAttributeinstead ofIREE::HAL::ExecutableTargetAttrto avoid a circular dependency between the HAL and Encoding dialects. This might need a better solution though longer term, i.e. avoiding the need to pass this attribute as it's only needed to check for a ukernel in the VMVX backend or by removing the HAL dependency on Encoding?